diff options
| author | 2018-09-08 16:49:04 +0100 | |
|---|---|---|
| committer | 2018-09-08 18:56:38 +0100 | |
| commit | 1aa195a9c0416c986c8224d9dc66d9d5e45401a0 (patch) | |
| tree | 4cd60ccf28c9ab8aeaddce0eb16be3a831ba9196 /src/audio_core/cubeb_sink.cpp | |
| parent | audio_core: Add audio stretcher (diff) | |
| download | yuzu-1aa195a9c0416c986c8224d9dc66d9d5e45401a0.tar.gz yuzu-1aa195a9c0416c986c8224d9dc66d9d5e45401a0.tar.xz yuzu-1aa195a9c0416c986c8224d9dc66d9d5e45401a0.zip | |
cubeb_sink: Perform audio stretching
Diffstat (limited to 'src/audio_core/cubeb_sink.cpp')
| -rw-r--r-- | src/audio_core/cubeb_sink.cpp | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/src/audio_core/cubeb_sink.cpp b/src/audio_core/cubeb_sink.cpp index 3c129122f..7982306b3 100644 --- a/src/audio_core/cubeb_sink.cpp +++ b/src/audio_core/cubeb_sink.cpp | |||
| @@ -6,8 +6,10 @@ | |||
| 6 | #include <cstring> | 6 | #include <cstring> |
| 7 | #include "audio_core/cubeb_sink.h" | 7 | #include "audio_core/cubeb_sink.h" |
| 8 | #include "audio_core/stream.h" | 8 | #include "audio_core/stream.h" |
| 9 | #include "audio_core/time_stretch.h" | ||
| 9 | #include "common/logging/log.h" | 10 | #include "common/logging/log.h" |
| 10 | #include "common/ring_buffer.h" | 11 | #include "common/ring_buffer.h" |
| 12 | #include "core/settings.h" | ||
| 11 | 13 | ||
| 12 | namespace AudioCore { | 14 | namespace AudioCore { |
| 13 | 15 | ||
| @@ -15,14 +17,8 @@ class CubebSinkStream final : public SinkStream { | |||
| 15 | public: | 17 | public: |
| 16 | CubebSinkStream(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device, | 18 | CubebSinkStream(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device, |
| 17 | const std::string& name) | 19 | const std::string& name) |
| 18 | : ctx{ctx}, num_channels{num_channels_} { | 20 | : ctx{ctx}, is_6_channel{num_channels_ == 6}, num_channels{std::min(num_channels_, 2u)}, |
| 19 | 21 | time_stretch{sample_rate, num_channels} { | |
| 20 | if (num_channels == 6) { | ||
| 21 | // 6-channel audio does not seem to work with cubeb + SDL, so we downsample this to 2 | ||
| 22 | // channel for now | ||
| 23 | is_6_channel = true; | ||
| 24 | num_channels = 2; | ||
| 25 | } | ||
| 26 | 22 | ||
| 27 | cubeb_stream_params params{}; | 23 | cubeb_stream_params params{}; |
| 28 | params.rate = sample_rate; | 24 | params.rate = sample_rate; |
| @@ -89,10 +85,6 @@ public: | |||
| 89 | return num_channels; | 85 | return num_channels; |
| 90 | } | 86 | } |
| 91 | 87 | ||
| 92 | u32 GetNumChannelsInQueue() const { | ||
| 93 | return num_channels == 1 ? 1 : 2; | ||
| 94 | } | ||
| 95 | |||
| 96 | private: | 88 | private: |
| 97 | std::vector<std::string> device_list; | 89 | std::vector<std::string> device_list; |
| 98 | 90 | ||
| @@ -103,6 +95,7 @@ private: | |||
| 103 | 95 | ||
| 104 | Common::RingBuffer<s16, 0x10000> queue; | 96 | Common::RingBuffer<s16, 0x10000> queue; |
| 105 | std::array<s16, 2> last_frame; | 97 | std::array<s16, 2> last_frame; |
| 98 | TimeStretcher time_stretch; | ||
| 106 | 99 | ||
| 107 | static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, | 100 | static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, |
| 108 | void* output_buffer, long num_frames); | 101 | void* output_buffer, long num_frames); |
| @@ -153,7 +146,7 @@ SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels, | |||
| 153 | } | 146 | } |
| 154 | 147 | ||
| 155 | long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, | 148 | long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, |
| 156 | void* output_buffer, long num_frames) { | 149 | void* output_buffer, long num_frames) { |
| 157 | CubebSinkStream* impl = static_cast<CubebSinkStream*>(user_data); | 150 | CubebSinkStream* impl = static_cast<CubebSinkStream*>(user_data); |
| 158 | u8* buffer = reinterpret_cast<u8*>(output_buffer); | 151 | u8* buffer = reinterpret_cast<u8*>(output_buffer); |
| 159 | 152 | ||
| @@ -161,9 +154,19 @@ long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const | |||
| 161 | return {}; | 154 | return {}; |
| 162 | } | 155 | } |
| 163 | 156 | ||
| 164 | const size_t num_channels = impl->GetNumChannelsInQueue(); | 157 | const size_t num_channels = impl->GetNumChannels(); |
| 165 | const size_t max_samples_to_write = num_channels * num_frames; | 158 | const size_t samples_to_write = num_channels * num_frames; |
| 166 | const size_t samples_written = impl->queue.Pop(buffer, max_samples_to_write); | 159 | size_t samples_written; |
| 160 | |||
| 161 | if (Settings::values.enable_audio_stretching) { | ||
| 162 | const std::vector<s16> in{impl->queue.Pop()}; | ||
| 163 | const size_t num_in{in.size() / num_channels}; | ||
| 164 | s16* const out{reinterpret_cast<s16*>(buffer)}; | ||
| 165 | const size_t out_frames = impl->time_stretch.Process(in.data(), num_in, out, num_frames); | ||
| 166 | samples_written = out_frames * num_channels; | ||
| 167 | } else { | ||
| 168 | samples_written = impl->queue.Pop(buffer, samples_to_write); | ||
| 169 | } | ||
| 167 | 170 | ||
| 168 | if (samples_written >= num_channels) { | 171 | if (samples_written >= num_channels) { |
| 169 | std::memcpy(&impl->last_frame[0], buffer + (samples_written - num_channels) * sizeof(s16), | 172 | std::memcpy(&impl->last_frame[0], buffer + (samples_written - num_channels) * sizeof(s16), |
| @@ -171,7 +174,7 @@ long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const | |||
| 171 | } | 174 | } |
| 172 | 175 | ||
| 173 | // Fill the rest of the frames with last_frame | 176 | // Fill the rest of the frames with last_frame |
| 174 | for (size_t i = samples_written; i < max_samples_to_write; i += num_channels) { | 177 | for (size_t i = samples_written; i < samples_to_write; i += num_channels) { |
| 175 | std::memcpy(buffer + i * sizeof(s16), &impl->last_frame[0], num_channels * sizeof(s16)); | 178 | std::memcpy(buffer + i * sizeof(s16), &impl->last_frame[0], num_channels * sizeof(s16)); |
| 176 | } | 179 | } |
| 177 | 180 | ||