diff options
| author | 2018-07-28 13:44:50 -0400 | |
|---|---|---|
| committer | 2018-07-30 21:45:24 -0400 | |
| commit | f437c11caf2c1afc0b7d0fdb808be10d7b1adfcf (patch) | |
| tree | e5224d2e6b57c5ac0aec46377d6bfae9072ab820 /src/audio_core/cubeb_sink.cpp | |
| parent | audio_core: Add interfaces for Sink and SinkStream. (diff) | |
| download | yuzu-f437c11caf2c1afc0b7d0fdb808be10d7b1adfcf.tar.gz yuzu-f437c11caf2c1afc0b7d0fdb808be10d7b1adfcf.tar.xz yuzu-f437c11caf2c1afc0b7d0fdb808be10d7b1adfcf.zip | |
audio_core: Implement Sink and SinkStream interfaces with cubeb.
Diffstat (limited to 'src/audio_core/cubeb_sink.cpp')
| -rw-r--r-- | src/audio_core/cubeb_sink.cpp | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/src/audio_core/cubeb_sink.cpp b/src/audio_core/cubeb_sink.cpp new file mode 100644 index 000000000..34ae5b062 --- /dev/null +++ b/src/audio_core/cubeb_sink.cpp | |||
| @@ -0,0 +1,190 @@ | |||
| 1 | // Copyright 2018 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstring> | ||
| 7 | |||
| 8 | #include "audio_core/cubeb_sink.h" | ||
| 9 | #include "audio_core/stream.h" | ||
| 10 | #include "common/logging/log.h" | ||
| 11 | |||
| 12 | namespace AudioCore { | ||
| 13 | |||
| 14 | class SinkStreamImpl final : public SinkStream { | ||
| 15 | public: | ||
| 16 | SinkStreamImpl(cubeb* ctx, cubeb_devid output_device) : ctx{ctx} { | ||
| 17 | cubeb_stream_params params; | ||
| 18 | params.rate = 48000; | ||
| 19 | params.channels = GetNumChannels(); | ||
| 20 | params.format = CUBEB_SAMPLE_S16NE; | ||
| 21 | params.layout = CUBEB_LAYOUT_STEREO; | ||
| 22 | |||
| 23 | u32 minimum_latency = 0; | ||
| 24 | if (cubeb_get_min_latency(ctx, ¶ms, &minimum_latency) != CUBEB_OK) { | ||
| 25 | LOG_CRITICAL(Audio_Sink, "Error getting minimum latency"); | ||
| 26 | } | ||
| 27 | |||
| 28 | if (cubeb_stream_init(ctx, &stream_backend, "yuzu Audio Output", nullptr, nullptr, | ||
| 29 | output_device, ¶ms, std::max(512u, minimum_latency), | ||
| 30 | &SinkStreamImpl::DataCallback, &SinkStreamImpl::StateCallback, | ||
| 31 | this) != CUBEB_OK) { | ||
| 32 | LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream"); | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | |||
| 36 | if (cubeb_stream_start(stream_backend) != CUBEB_OK) { | ||
| 37 | LOG_CRITICAL(Audio_Sink, "Error starting cubeb stream"); | ||
| 38 | return; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | ~SinkStreamImpl() { | ||
| 43 | if (!ctx) { | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | if (cubeb_stream_stop(stream_backend) != CUBEB_OK) { | ||
| 48 | LOG_CRITICAL(Audio_Sink, "Error stopping cubeb stream"); | ||
| 49 | } | ||
| 50 | |||
| 51 | cubeb_stream_destroy(stream_backend); | ||
| 52 | } | ||
| 53 | |||
| 54 | void EnqueueSamples(u32 num_channels, const s16* samples, size_t sample_count) override { | ||
| 55 | if (!ctx) { | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | |||
| 59 | queue.reserve(queue.size() + sample_count * GetNumChannels()); | ||
| 60 | |||
| 61 | if (num_channels == 2) { | ||
| 62 | // Copy as-is | ||
| 63 | std::copy(samples, samples + sample_count * GetNumChannels(), | ||
| 64 | std::back_inserter(queue)); | ||
| 65 | } else if (num_channels == 6) { | ||
| 66 | // Downsample 6 channels to 2 | ||
| 67 | const size_t sample_count_copy_size = sample_count * num_channels * 2; | ||
| 68 | queue.reserve(sample_count_copy_size); | ||
| 69 | for (size_t i = 0; i < sample_count * num_channels; i += num_channels) { | ||
| 70 | queue.push_back(samples[i]); | ||
| 71 | queue.push_back(samples[i + 1]); | ||
| 72 | } | ||
| 73 | } else { | ||
| 74 | ASSERT_MSG(false, "Unimplemented"); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | u32 GetNumChannels() const { | ||
| 79 | // Only support 2-channel stereo output for now | ||
| 80 | return 2; | ||
| 81 | } | ||
| 82 | |||
| 83 | private: | ||
| 84 | std::vector<std::string> device_list; | ||
| 85 | |||
| 86 | cubeb* ctx{}; | ||
| 87 | cubeb_stream* stream_backend{}; | ||
| 88 | |||
| 89 | std::vector<s16> queue; | ||
| 90 | |||
| 91 | static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, | ||
| 92 | void* output_buffer, long num_frames); | ||
| 93 | static void StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state); | ||
| 94 | }; | ||
| 95 | |||
| 96 | CubebSink::CubebSink(std::string target_device_name) { | ||
| 97 | if (cubeb_init(&ctx, "yuzu", nullptr) != CUBEB_OK) { | ||
| 98 | LOG_CRITICAL(Audio_Sink, "cubeb_init failed"); | ||
| 99 | return; | ||
| 100 | } | ||
| 101 | |||
| 102 | if (target_device_name != auto_device_name && !target_device_name.empty()) { | ||
| 103 | cubeb_device_collection collection; | ||
| 104 | if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) != CUBEB_OK) { | ||
| 105 | LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported"); | ||
| 106 | } else { | ||
| 107 | const auto collection_end{collection.device + collection.count}; | ||
| 108 | const auto device{std::find_if(collection.device, collection_end, | ||
| 109 | [&](const cubeb_device_info& device) { | ||
| 110 | return target_device_name == device.friendly_name; | ||
| 111 | })}; | ||
| 112 | if (device != collection_end) { | ||
| 113 | output_device = device->devid; | ||
| 114 | } | ||
| 115 | cubeb_device_collection_destroy(ctx, &collection); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | CubebSink::~CubebSink() { | ||
| 121 | if (!ctx) { | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | for (auto& sink_stream : sink_streams) { | ||
| 126 | sink_stream.reset(); | ||
| 127 | } | ||
| 128 | |||
| 129 | cubeb_destroy(ctx); | ||
| 130 | } | ||
| 131 | |||
| 132 | SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels) { | ||
| 133 | sink_streams.push_back(std::make_unique<SinkStreamImpl>(ctx, output_device)); | ||
| 134 | return *sink_streams.back(); | ||
| 135 | } | ||
| 136 | |||
| 137 | long SinkStreamImpl::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, | ||
| 138 | void* output_buffer, long num_frames) { | ||
| 139 | SinkStreamImpl* impl = static_cast<SinkStreamImpl*>(user_data); | ||
| 140 | u8* buffer = reinterpret_cast<u8*>(output_buffer); | ||
| 141 | |||
| 142 | if (!impl) { | ||
| 143 | return {}; | ||
| 144 | } | ||
| 145 | |||
| 146 | const size_t frames_to_write{ | ||
| 147 | std::min(impl->queue.size() / impl->GetNumChannels(), static_cast<size_t>(num_frames))}; | ||
| 148 | |||
| 149 | memcpy(buffer, impl->queue.data(), frames_to_write * sizeof(s16) * impl->GetNumChannels()); | ||
| 150 | impl->queue.erase(impl->queue.begin(), | ||
| 151 | impl->queue.begin() + frames_to_write * impl->GetNumChannels()); | ||
| 152 | |||
| 153 | if (frames_to_write < num_frames) { | ||
| 154 | // Fill the rest of the frames with silence | ||
| 155 | memset(buffer + frames_to_write * sizeof(s16) * impl->GetNumChannels(), 0, | ||
| 156 | (num_frames - frames_to_write) * sizeof(s16) * impl->GetNumChannels()); | ||
| 157 | } | ||
| 158 | |||
| 159 | return num_frames; | ||
| 160 | } | ||
| 161 | |||
| 162 | void SinkStreamImpl::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {} | ||
| 163 | |||
| 164 | std::vector<std::string> ListCubebSinkDevices() { | ||
| 165 | std::vector<std::string> device_list; | ||
| 166 | cubeb* ctx; | ||
| 167 | |||
| 168 | if (cubeb_init(&ctx, "Citra Device Enumerator", nullptr) != CUBEB_OK) { | ||
| 169 | LOG_CRITICAL(Audio_Sink, "cubeb_init failed"); | ||
| 170 | return {}; | ||
| 171 | } | ||
| 172 | |||
| 173 | cubeb_device_collection collection; | ||
| 174 | if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) != CUBEB_OK) { | ||
| 175 | LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported"); | ||
| 176 | } else { | ||
| 177 | for (size_t i = 0; i < collection.count; i++) { | ||
| 178 | const cubeb_device_info& device = collection.device[i]; | ||
| 179 | if (device.friendly_name) { | ||
| 180 | device_list.emplace_back(device.friendly_name); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | cubeb_device_collection_destroy(ctx, &collection); | ||
| 184 | } | ||
| 185 | |||
| 186 | cubeb_destroy(ctx); | ||
| 187 | return device_list; | ||
| 188 | } | ||
| 189 | |||
| 190 | } // namespace AudioCore | ||