diff options
Diffstat (limited to 'src/audio_core/cubeb_sink.cpp')
| -rw-r--r-- | src/audio_core/cubeb_sink.cpp | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/src/audio_core/cubeb_sink.cpp b/src/audio_core/cubeb_sink.cpp index 34ae5b062..1501ef1f4 100644 --- a/src/audio_core/cubeb_sink.cpp +++ b/src/audio_core/cubeb_sink.cpp | |||
| @@ -13,20 +13,30 @@ namespace AudioCore { | |||
| 13 | 13 | ||
| 14 | class SinkStreamImpl final : public SinkStream { | 14 | class SinkStreamImpl final : public SinkStream { |
| 15 | public: | 15 | public: |
| 16 | SinkStreamImpl(cubeb* ctx, cubeb_devid output_device) : ctx{ctx} { | 16 | SinkStreamImpl(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device, |
| 17 | cubeb_stream_params params; | 17 | const std::string& name) |
| 18 | params.rate = 48000; | 18 | : ctx{ctx}, num_channels{num_channels_} { |
| 19 | params.channels = GetNumChannels(); | 19 | |
| 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 | |||
| 27 | cubeb_stream_params params{}; | ||
| 28 | params.rate = sample_rate; | ||
| 29 | params.channels = num_channels; | ||
| 20 | params.format = CUBEB_SAMPLE_S16NE; | 30 | params.format = CUBEB_SAMPLE_S16NE; |
| 21 | params.layout = CUBEB_LAYOUT_STEREO; | 31 | params.layout = num_channels == 1 ? CUBEB_LAYOUT_MONO : CUBEB_LAYOUT_STEREO; |
| 22 | 32 | ||
| 23 | u32 minimum_latency = 0; | 33 | u32 minimum_latency{}; |
| 24 | if (cubeb_get_min_latency(ctx, ¶ms, &minimum_latency) != CUBEB_OK) { | 34 | if (cubeb_get_min_latency(ctx, ¶ms, &minimum_latency) != CUBEB_OK) { |
| 25 | LOG_CRITICAL(Audio_Sink, "Error getting minimum latency"); | 35 | LOG_CRITICAL(Audio_Sink, "Error getting minimum latency"); |
| 26 | } | 36 | } |
| 27 | 37 | ||
| 28 | if (cubeb_stream_init(ctx, &stream_backend, "yuzu Audio Output", nullptr, nullptr, | 38 | if (cubeb_stream_init(ctx, &stream_backend, name.c_str(), nullptr, nullptr, output_device, |
| 29 | output_device, ¶ms, std::max(512u, minimum_latency), | 39 | ¶ms, std::max(512u, minimum_latency), |
| 30 | &SinkStreamImpl::DataCallback, &SinkStreamImpl::StateCallback, | 40 | &SinkStreamImpl::DataCallback, &SinkStreamImpl::StateCallback, |
| 31 | this) != CUBEB_OK) { | 41 | this) != CUBEB_OK) { |
| 32 | LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream"); | 42 | LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream"); |
| @@ -51,33 +61,29 @@ public: | |||
| 51 | cubeb_stream_destroy(stream_backend); | 61 | cubeb_stream_destroy(stream_backend); |
| 52 | } | 62 | } |
| 53 | 63 | ||
| 54 | void EnqueueSamples(u32 num_channels, const s16* samples, size_t sample_count) override { | 64 | void EnqueueSamples(u32 num_channels, const std::vector<s16>& samples) override { |
| 55 | if (!ctx) { | 65 | if (!ctx) { |
| 56 | return; | 66 | return; |
| 57 | } | 67 | } |
| 58 | 68 | ||
| 59 | queue.reserve(queue.size() + sample_count * GetNumChannels()); | 69 | queue.reserve(queue.size() + samples.size() * GetNumChannels()); |
| 60 | 70 | ||
| 61 | if (num_channels == 2) { | 71 | if (is_6_channel) { |
| 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 | 72 | // Downsample 6 channels to 2 |
| 67 | const size_t sample_count_copy_size = sample_count * num_channels * 2; | 73 | const size_t sample_count_copy_size = samples.size() * 2; |
| 68 | queue.reserve(sample_count_copy_size); | 74 | queue.reserve(sample_count_copy_size); |
| 69 | for (size_t i = 0; i < sample_count * num_channels; i += num_channels) { | 75 | for (size_t i = 0; i < samples.size(); i += num_channels) { |
| 70 | queue.push_back(samples[i]); | 76 | queue.push_back(samples[i]); |
| 71 | queue.push_back(samples[i + 1]); | 77 | queue.push_back(samples[i + 1]); |
| 72 | } | 78 | } |
| 73 | } else { | 79 | } else { |
| 74 | ASSERT_MSG(false, "Unimplemented"); | 80 | // Copy as-is |
| 81 | std::copy(samples.begin(), samples.end(), std::back_inserter(queue)); | ||
| 75 | } | 82 | } |
| 76 | } | 83 | } |
| 77 | 84 | ||
| 78 | u32 GetNumChannels() const { | 85 | u32 GetNumChannels() const { |
| 79 | // Only support 2-channel stereo output for now | 86 | return num_channels; |
| 80 | return 2; | ||
| 81 | } | 87 | } |
| 82 | 88 | ||
| 83 | private: | 89 | private: |
| @@ -85,6 +91,8 @@ private: | |||
| 85 | 91 | ||
| 86 | cubeb* ctx{}; | 92 | cubeb* ctx{}; |
| 87 | cubeb_stream* stream_backend{}; | 93 | cubeb_stream* stream_backend{}; |
| 94 | u32 num_channels{}; | ||
| 95 | bool is_6_channel{}; | ||
| 88 | 96 | ||
| 89 | std::vector<s16> queue; | 97 | std::vector<s16> queue; |
| 90 | 98 | ||
| @@ -129,8 +137,10 @@ CubebSink::~CubebSink() { | |||
| 129 | cubeb_destroy(ctx); | 137 | cubeb_destroy(ctx); |
| 130 | } | 138 | } |
| 131 | 139 | ||
| 132 | SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels) { | 140 | SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels, |
| 133 | sink_streams.push_back(std::make_unique<SinkStreamImpl>(ctx, output_device)); | 141 | const std::string& name) { |
| 142 | sink_streams.push_back( | ||
| 143 | std::make_unique<SinkStreamImpl>(ctx, sample_rate, num_channels, output_device, name)); | ||
| 134 | return *sink_streams.back(); | 144 | return *sink_streams.back(); |
| 135 | } | 145 | } |
| 136 | 146 | ||