diff options
| author | 2022-07-16 23:48:45 +0100 | |
|---|---|---|
| committer | 2022-07-22 01:11:32 +0100 | |
| commit | 458da8a94877677f086f06cdeecf959ec4283a33 (patch) | |
| tree | 583166d77602ad90a0d552f37de8729ad80fd6c1 /src/audio_core/cubeb_sink.cpp | |
| parent | Merge pull request #8598 from Link4565/recv-dontwait (diff) | |
| download | yuzu-458da8a94877677f086f06cdeecf959ec4283a33.tar.gz yuzu-458da8a94877677f086f06cdeecf959ec4283a33.tar.xz yuzu-458da8a94877677f086f06cdeecf959ec4283a33.zip | |
Project Andio
Diffstat (limited to 'src/audio_core/cubeb_sink.cpp')
| -rw-r--r-- | src/audio_core/cubeb_sink.cpp | 249 |
1 files changed, 0 insertions, 249 deletions
diff --git a/src/audio_core/cubeb_sink.cpp b/src/audio_core/cubeb_sink.cpp deleted file mode 100644 index e48c1ee8e..000000000 --- a/src/audio_core/cubeb_sink.cpp +++ /dev/null | |||
| @@ -1,249 +0,0 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <algorithm> | ||
| 5 | #include <atomic> | ||
| 6 | #include <cstring> | ||
| 7 | #include "audio_core/cubeb_sink.h" | ||
| 8 | #include "audio_core/stream.h" | ||
| 9 | #include "common/assert.h" | ||
| 10 | #include "common/logging/log.h" | ||
| 11 | #include "common/ring_buffer.h" | ||
| 12 | #include "common/settings.h" | ||
| 13 | |||
| 14 | #ifdef _WIN32 | ||
| 15 | #include <objbase.h> | ||
| 16 | #endif | ||
| 17 | |||
| 18 | namespace AudioCore { | ||
| 19 | |||
| 20 | class CubebSinkStream final : public SinkStream { | ||
| 21 | public: | ||
| 22 | CubebSinkStream(cubeb* ctx_, u32 sample_rate, u32 num_channels_, cubeb_devid output_device, | ||
| 23 | const std::string& name) | ||
| 24 | : ctx{ctx_}, num_channels{std::min(num_channels_, 6u)} { | ||
| 25 | |||
| 26 | cubeb_stream_params params{}; | ||
| 27 | params.rate = sample_rate; | ||
| 28 | params.channels = num_channels; | ||
| 29 | params.format = CUBEB_SAMPLE_S16NE; | ||
| 30 | params.prefs = CUBEB_STREAM_PREF_PERSIST; | ||
| 31 | switch (num_channels) { | ||
| 32 | case 1: | ||
| 33 | params.layout = CUBEB_LAYOUT_MONO; | ||
| 34 | break; | ||
| 35 | case 2: | ||
| 36 | params.layout = CUBEB_LAYOUT_STEREO; | ||
| 37 | break; | ||
| 38 | case 6: | ||
| 39 | params.layout = CUBEB_LAYOUT_3F2_LFE; | ||
| 40 | break; | ||
| 41 | } | ||
| 42 | |||
| 43 | u32 minimum_latency{}; | ||
| 44 | if (cubeb_get_min_latency(ctx, ¶ms, &minimum_latency) != CUBEB_OK) { | ||
| 45 | LOG_CRITICAL(Audio_Sink, "Error getting minimum latency"); | ||
| 46 | } | ||
| 47 | |||
| 48 | if (cubeb_stream_init(ctx, &stream_backend, name.c_str(), nullptr, nullptr, output_device, | ||
| 49 | ¶ms, std::max(512u, minimum_latency), | ||
| 50 | &CubebSinkStream::DataCallback, &CubebSinkStream::StateCallback, | ||
| 51 | this) != CUBEB_OK) { | ||
| 52 | LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream"); | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | if (cubeb_stream_start(stream_backend) != CUBEB_OK) { | ||
| 57 | LOG_CRITICAL(Audio_Sink, "Error starting cubeb stream"); | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | ~CubebSinkStream() override { | ||
| 63 | if (!ctx) { | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | if (cubeb_stream_stop(stream_backend) != CUBEB_OK) { | ||
| 68 | LOG_CRITICAL(Audio_Sink, "Error stopping cubeb stream"); | ||
| 69 | } | ||
| 70 | |||
| 71 | cubeb_stream_destroy(stream_backend); | ||
| 72 | } | ||
| 73 | |||
| 74 | void EnqueueSamples(u32 source_num_channels, const std::vector<s16>& samples) override { | ||
| 75 | if (source_num_channels > num_channels) { | ||
| 76 | // Downsample 6 channels to 2 | ||
| 77 | ASSERT_MSG(source_num_channels == 6, "Channel count must be 6"); | ||
| 78 | |||
| 79 | std::vector<s16> buf; | ||
| 80 | buf.reserve(samples.size() * num_channels / source_num_channels); | ||
| 81 | for (std::size_t i = 0; i < samples.size(); i += source_num_channels) { | ||
| 82 | // Downmixing implementation taken from the ATSC standard | ||
| 83 | const s16 left{samples[i + 0]}; | ||
| 84 | const s16 right{samples[i + 1]}; | ||
| 85 | const s16 center{samples[i + 2]}; | ||
| 86 | const s16 surround_left{samples[i + 4]}; | ||
| 87 | const s16 surround_right{samples[i + 5]}; | ||
| 88 | // Not used in the ATSC reference implementation | ||
| 89 | [[maybe_unused]] const s16 low_frequency_effects{samples[i + 3]}; | ||
| 90 | |||
| 91 | constexpr s32 clev{707}; // center mixing level coefficient | ||
| 92 | constexpr s32 slev{707}; // surround mixing level coefficient | ||
| 93 | |||
| 94 | buf.push_back(static_cast<s16>(left + (clev * center / 1000) + | ||
| 95 | (slev * surround_left / 1000))); | ||
| 96 | buf.push_back(static_cast<s16>(right + (clev * center / 1000) + | ||
| 97 | (slev * surround_right / 1000))); | ||
| 98 | } | ||
| 99 | queue.Push(buf); | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | queue.Push(samples); | ||
| 104 | } | ||
| 105 | |||
| 106 | std::size_t SamplesInQueue(u32 channel_count) const override { | ||
| 107 | if (!ctx) | ||
| 108 | return 0; | ||
| 109 | |||
| 110 | return queue.Size() / channel_count; | ||
| 111 | } | ||
| 112 | |||
| 113 | void Flush() override { | ||
| 114 | should_flush = true; | ||
| 115 | } | ||
| 116 | |||
| 117 | u32 GetNumChannels() const { | ||
| 118 | return num_channels; | ||
| 119 | } | ||
| 120 | |||
| 121 | private: | ||
| 122 | std::vector<std::string> device_list; | ||
| 123 | |||
| 124 | cubeb* ctx{}; | ||
| 125 | cubeb_stream* stream_backend{}; | ||
| 126 | u32 num_channels{}; | ||
| 127 | |||
| 128 | Common::RingBuffer<s16, 0x10000> queue; | ||
| 129 | std::array<s16, 2> last_frame{}; | ||
| 130 | std::atomic<bool> should_flush{}; | ||
| 131 | |||
| 132 | static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, | ||
| 133 | void* output_buffer, long num_frames); | ||
| 134 | static void StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state); | ||
| 135 | }; | ||
| 136 | |||
| 137 | CubebSink::CubebSink(std::string_view target_device_name) { | ||
| 138 | // Cubeb requires COM to be initialized on the thread calling cubeb_init on Windows | ||
| 139 | #ifdef _WIN32 | ||
| 140 | com_init_result = CoInitializeEx(nullptr, COINIT_MULTITHREADED); | ||
| 141 | #endif | ||
| 142 | |||
| 143 | if (cubeb_init(&ctx, "yuzu", nullptr) != CUBEB_OK) { | ||
| 144 | LOG_CRITICAL(Audio_Sink, "cubeb_init failed"); | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | if (target_device_name != auto_device_name && !target_device_name.empty()) { | ||
| 149 | cubeb_device_collection collection; | ||
| 150 | if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) != CUBEB_OK) { | ||
| 151 | LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported"); | ||
| 152 | } else { | ||
| 153 | const auto collection_end{collection.device + collection.count}; | ||
| 154 | const auto device{ | ||
| 155 | std::find_if(collection.device, collection_end, [&](const cubeb_device_info& info) { | ||
| 156 | return info.friendly_name != nullptr && | ||
| 157 | target_device_name == info.friendly_name; | ||
| 158 | })}; | ||
| 159 | if (device != collection_end) { | ||
| 160 | output_device = device->devid; | ||
| 161 | } | ||
| 162 | cubeb_device_collection_destroy(ctx, &collection); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | CubebSink::~CubebSink() { | ||
| 168 | if (!ctx) { | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | for (auto& sink_stream : sink_streams) { | ||
| 173 | sink_stream.reset(); | ||
| 174 | } | ||
| 175 | |||
| 176 | cubeb_destroy(ctx); | ||
| 177 | |||
| 178 | #ifdef _WIN32 | ||
| 179 | if (SUCCEEDED(com_init_result)) { | ||
| 180 | CoUninitialize(); | ||
| 181 | } | ||
| 182 | #endif | ||
| 183 | } | ||
| 184 | |||
| 185 | SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels, | ||
| 186 | const std::string& name) { | ||
| 187 | sink_streams.push_back( | ||
| 188 | std::make_unique<CubebSinkStream>(ctx, sample_rate, num_channels, output_device, name)); | ||
| 189 | return *sink_streams.back(); | ||
| 190 | } | ||
| 191 | |||
| 192 | long CubebSinkStream::DataCallback([[maybe_unused]] cubeb_stream* stream, void* user_data, | ||
| 193 | [[maybe_unused]] const void* input_buffer, void* output_buffer, | ||
| 194 | long num_frames) { | ||
| 195 | auto* impl = static_cast<CubebSinkStream*>(user_data); | ||
| 196 | auto* buffer = static_cast<u8*>(output_buffer); | ||
| 197 | |||
| 198 | if (!impl) { | ||
| 199 | return {}; | ||
| 200 | } | ||
| 201 | |||
| 202 | const std::size_t num_channels = impl->GetNumChannels(); | ||
| 203 | const std::size_t samples_to_write = num_channels * num_frames; | ||
| 204 | const std::size_t samples_written = impl->queue.Pop(buffer, samples_to_write); | ||
| 205 | |||
| 206 | if (samples_written >= num_channels) { | ||
| 207 | std::memcpy(&impl->last_frame[0], buffer + (samples_written - num_channels) * sizeof(s16), | ||
| 208 | num_channels * sizeof(s16)); | ||
| 209 | } | ||
| 210 | |||
| 211 | // Fill the rest of the frames with last_frame | ||
| 212 | for (std::size_t i = samples_written; i < samples_to_write; i += num_channels) { | ||
| 213 | std::memcpy(buffer + i * sizeof(s16), &impl->last_frame[0], num_channels * sizeof(s16)); | ||
| 214 | } | ||
| 215 | |||
| 216 | return num_frames; | ||
| 217 | } | ||
| 218 | |||
| 219 | void CubebSinkStream::StateCallback([[maybe_unused]] cubeb_stream* stream, | ||
| 220 | [[maybe_unused]] void* user_data, | ||
| 221 | [[maybe_unused]] cubeb_state state) {} | ||
| 222 | |||
| 223 | std::vector<std::string> ListCubebSinkDevices() { | ||
| 224 | std::vector<std::string> device_list; | ||
| 225 | cubeb* ctx; | ||
| 226 | |||
| 227 | if (cubeb_init(&ctx, "yuzu Device Enumerator", nullptr) != CUBEB_OK) { | ||
| 228 | LOG_CRITICAL(Audio_Sink, "cubeb_init failed"); | ||
| 229 | return {}; | ||
| 230 | } | ||
| 231 | |||
| 232 | cubeb_device_collection collection; | ||
| 233 | if (cubeb_enumerate_devices(ctx, CUBEB_DEVICE_TYPE_OUTPUT, &collection) != CUBEB_OK) { | ||
| 234 | LOG_WARNING(Audio_Sink, "Audio output device enumeration not supported"); | ||
| 235 | } else { | ||
| 236 | for (std::size_t i = 0; i < collection.count; i++) { | ||
| 237 | const cubeb_device_info& device = collection.device[i]; | ||
| 238 | if (device.friendly_name) { | ||
| 239 | device_list.emplace_back(device.friendly_name); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | cubeb_device_collection_destroy(ctx, &collection); | ||
| 243 | } | ||
| 244 | |||
| 245 | cubeb_destroy(ctx); | ||
| 246 | return device_list; | ||
| 247 | } | ||
| 248 | |||
| 249 | } // namespace AudioCore | ||