diff options
Diffstat (limited to 'src/audio_core/cubeb_sink.cpp')
| -rw-r--r-- | src/audio_core/cubeb_sink.cpp | 114 |
1 files changed, 65 insertions, 49 deletions
diff --git a/src/audio_core/cubeb_sink.cpp b/src/audio_core/cubeb_sink.cpp index 5a1177d0c..79155a7a0 100644 --- a/src/audio_core/cubeb_sink.cpp +++ b/src/audio_core/cubeb_sink.cpp | |||
| @@ -3,27 +3,23 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | 5 | #include <algorithm> |
| 6 | #include <atomic> | ||
| 6 | #include <cstring> | 7 | #include <cstring> |
| 7 | #include <mutex> | ||
| 8 | |||
| 9 | #include "audio_core/cubeb_sink.h" | 8 | #include "audio_core/cubeb_sink.h" |
| 10 | #include "audio_core/stream.h" | 9 | #include "audio_core/stream.h" |
| 10 | #include "audio_core/time_stretch.h" | ||
| 11 | #include "common/logging/log.h" | 11 | #include "common/logging/log.h" |
| 12 | #include "common/ring_buffer.h" | ||
| 13 | #include "core/settings.h" | ||
| 12 | 14 | ||
| 13 | namespace AudioCore { | 15 | namespace AudioCore { |
| 14 | 16 | ||
| 15 | class SinkStreamImpl final : public SinkStream { | 17 | class CubebSinkStream final : public SinkStream { |
| 16 | public: | 18 | public: |
| 17 | SinkStreamImpl(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device, | 19 | CubebSinkStream(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device, |
| 18 | const std::string& name) | 20 | const std::string& name) |
| 19 | : ctx{ctx}, num_channels{num_channels_} { | 21 | : ctx{ctx}, num_channels{std::min(num_channels_, 2u)}, time_stretch{sample_rate, |
| 20 | 22 | num_channels} { | |
| 21 | if (num_channels == 6) { | ||
| 22 | // 6-channel audio does not seem to work with cubeb + SDL, so we downsample this to 2 | ||
| 23 | // channel for now | ||
| 24 | is_6_channel = true; | ||
| 25 | num_channels = 2; | ||
| 26 | } | ||
| 27 | 23 | ||
| 28 | cubeb_stream_params params{}; | 24 | cubeb_stream_params params{}; |
| 29 | params.rate = sample_rate; | 25 | params.rate = sample_rate; |
| @@ -38,7 +34,7 @@ public: | |||
| 38 | 34 | ||
| 39 | if (cubeb_stream_init(ctx, &stream_backend, name.c_str(), nullptr, nullptr, output_device, | 35 | if (cubeb_stream_init(ctx, &stream_backend, name.c_str(), nullptr, nullptr, output_device, |
| 40 | ¶ms, std::max(512u, minimum_latency), | 36 | ¶ms, std::max(512u, minimum_latency), |
| 41 | &SinkStreamImpl::DataCallback, &SinkStreamImpl::StateCallback, | 37 | &CubebSinkStream::DataCallback, &CubebSinkStream::StateCallback, |
| 42 | this) != CUBEB_OK) { | 38 | this) != CUBEB_OK) { |
| 43 | LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream"); | 39 | LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream"); |
| 44 | return; | 40 | return; |
| @@ -50,7 +46,7 @@ public: | |||
| 50 | } | 46 | } |
| 51 | } | 47 | } |
| 52 | 48 | ||
| 53 | ~SinkStreamImpl() { | 49 | ~CubebSinkStream() { |
| 54 | if (!ctx) { | 50 | if (!ctx) { |
| 55 | return; | 51 | return; |
| 56 | } | 52 | } |
| @@ -62,27 +58,32 @@ public: | |||
| 62 | cubeb_stream_destroy(stream_backend); | 58 | cubeb_stream_destroy(stream_backend); |
| 63 | } | 59 | } |
| 64 | 60 | ||
| 65 | void EnqueueSamples(u32 num_channels, const std::vector<s16>& samples) override { | 61 | void EnqueueSamples(u32 source_num_channels, const std::vector<s16>& samples) override { |
| 66 | if (!ctx) { | 62 | if (source_num_channels > num_channels) { |
| 63 | // Downsample 6 channels to 2 | ||
| 64 | std::vector<s16> buf; | ||
| 65 | buf.reserve(samples.size() * num_channels / source_num_channels); | ||
| 66 | for (size_t i = 0; i < samples.size(); i += source_num_channels) { | ||
| 67 | for (size_t ch = 0; ch < num_channels; ch++) { | ||
| 68 | buf.push_back(samples[i + ch]); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | queue.Push(buf); | ||
| 67 | return; | 72 | return; |
| 68 | } | 73 | } |
| 69 | 74 | ||
| 70 | std::lock_guard lock{queue_mutex}; | 75 | queue.Push(samples); |
| 76 | } | ||
| 71 | 77 | ||
| 72 | queue.reserve(queue.size() + samples.size() * GetNumChannels()); | 78 | size_t SamplesInQueue(u32 num_channels) const override { |
| 79 | if (!ctx) | ||
| 80 | return 0; | ||
| 73 | 81 | ||
| 74 | if (is_6_channel) { | 82 | return queue.Size() / num_channels; |
| 75 | // Downsample 6 channels to 2 | 83 | } |
| 76 | const size_t sample_count_copy_size = samples.size() * 2; | 84 | |
| 77 | queue.reserve(sample_count_copy_size); | 85 | void Flush() override { |
| 78 | for (size_t i = 0; i < samples.size(); i += num_channels) { | 86 | should_flush = true; |
| 79 | queue.push_back(samples[i]); | ||
| 80 | queue.push_back(samples[i + 1]); | ||
| 81 | } | ||
| 82 | } else { | ||
| 83 | // Copy as-is | ||
| 84 | std::copy(samples.begin(), samples.end(), std::back_inserter(queue)); | ||
| 85 | } | ||
| 86 | } | 87 | } |
| 87 | 88 | ||
| 88 | u32 GetNumChannels() const { | 89 | u32 GetNumChannels() const { |
| @@ -95,10 +96,11 @@ private: | |||
| 95 | cubeb* ctx{}; | 96 | cubeb* ctx{}; |
| 96 | cubeb_stream* stream_backend{}; | 97 | cubeb_stream* stream_backend{}; |
| 97 | u32 num_channels{}; | 98 | u32 num_channels{}; |
| 98 | bool is_6_channel{}; | ||
| 99 | 99 | ||
| 100 | std::mutex queue_mutex; | 100 | Common::RingBuffer<s16, 0x10000> queue; |
| 101 | std::vector<s16> queue; | 101 | std::array<s16, 2> last_frame; |
| 102 | std::atomic<bool> should_flush{}; | ||
| 103 | TimeStretcher time_stretch; | ||
| 102 | 104 | ||
| 103 | static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, | 105 | static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, |
| 104 | void* output_buffer, long num_frames); | 106 | void* output_buffer, long num_frames); |
| @@ -144,38 +146,52 @@ CubebSink::~CubebSink() { | |||
| 144 | SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels, | 146 | SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels, |
| 145 | const std::string& name) { | 147 | const std::string& name) { |
| 146 | sink_streams.push_back( | 148 | sink_streams.push_back( |
| 147 | std::make_unique<SinkStreamImpl>(ctx, sample_rate, num_channels, output_device, name)); | 149 | std::make_unique<CubebSinkStream>(ctx, sample_rate, num_channels, output_device, name)); |
| 148 | return *sink_streams.back(); | 150 | return *sink_streams.back(); |
| 149 | } | 151 | } |
| 150 | 152 | ||
| 151 | long SinkStreamImpl::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, | 153 | long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, |
| 152 | void* output_buffer, long num_frames) { | 154 | void* output_buffer, long num_frames) { |
| 153 | SinkStreamImpl* impl = static_cast<SinkStreamImpl*>(user_data); | 155 | CubebSinkStream* impl = static_cast<CubebSinkStream*>(user_data); |
| 154 | u8* buffer = reinterpret_cast<u8*>(output_buffer); | 156 | u8* buffer = reinterpret_cast<u8*>(output_buffer); |
| 155 | 157 | ||
| 156 | if (!impl) { | 158 | if (!impl) { |
| 157 | return {}; | 159 | return {}; |
| 158 | } | 160 | } |
| 159 | 161 | ||
| 160 | std::lock_guard lock{impl->queue_mutex}; | 162 | const size_t num_channels = impl->GetNumChannels(); |
| 163 | const size_t samples_to_write = num_channels * num_frames; | ||
| 164 | size_t samples_written; | ||
| 165 | |||
| 166 | if (Settings::values.enable_audio_stretching) { | ||
| 167 | const std::vector<s16> in{impl->queue.Pop()}; | ||
| 168 | const size_t num_in{in.size() / num_channels}; | ||
| 169 | s16* const out{reinterpret_cast<s16*>(buffer)}; | ||
| 170 | const size_t out_frames = impl->time_stretch.Process(in.data(), num_in, out, num_frames); | ||
| 171 | samples_written = out_frames * num_channels; | ||
| 161 | 172 | ||
| 162 | const size_t frames_to_write{ | 173 | if (impl->should_flush) { |
| 163 | std::min(impl->queue.size() / impl->GetNumChannels(), static_cast<size_t>(num_frames))}; | 174 | impl->time_stretch.Flush(); |
| 175 | impl->should_flush = false; | ||
| 176 | } | ||
| 177 | } else { | ||
| 178 | samples_written = impl->queue.Pop(buffer, samples_to_write); | ||
| 179 | } | ||
| 164 | 180 | ||
| 165 | memcpy(buffer, impl->queue.data(), frames_to_write * sizeof(s16) * impl->GetNumChannels()); | 181 | if (samples_written >= num_channels) { |
| 166 | impl->queue.erase(impl->queue.begin(), | 182 | std::memcpy(&impl->last_frame[0], buffer + (samples_written - num_channels) * sizeof(s16), |
| 167 | impl->queue.begin() + frames_to_write * impl->GetNumChannels()); | 183 | num_channels * sizeof(s16)); |
| 184 | } | ||
| 168 | 185 | ||
| 169 | if (frames_to_write < num_frames) { | 186 | // Fill the rest of the frames with last_frame |
| 170 | // Fill the rest of the frames with silence | 187 | for (size_t i = samples_written; i < samples_to_write; i += num_channels) { |
| 171 | memset(buffer + frames_to_write * sizeof(s16) * impl->GetNumChannels(), 0, | 188 | std::memcpy(buffer + i * sizeof(s16), &impl->last_frame[0], num_channels * sizeof(s16)); |
| 172 | (num_frames - frames_to_write) * sizeof(s16) * impl->GetNumChannels()); | ||
| 173 | } | 189 | } |
| 174 | 190 | ||
| 175 | return num_frames; | 191 | return num_frames; |
| 176 | } | 192 | } |
| 177 | 193 | ||
| 178 | void SinkStreamImpl::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {} | 194 | void CubebSinkStream::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {} |
| 179 | 195 | ||
| 180 | std::vector<std::string> ListCubebSinkDevices() { | 196 | std::vector<std::string> ListCubebSinkDevices() { |
| 181 | std::vector<std::string> device_list; | 197 | std::vector<std::string> device_list; |