cubeb_sink: Support variable sample_rate and num_channels.
This commit is contained in:
		| @@ -13,14 +13,24 @@ namespace AudioCore { | |||||||
|  |  | ||||||
| class SinkStreamImpl final : public SinkStream { | class SinkStreamImpl final : public SinkStream { | ||||||
| public: | public: | ||||||
|     SinkStreamImpl(cubeb* ctx, cubeb_devid output_device, const std::string& name) : ctx{ctx} { |     SinkStreamImpl(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device, | ||||||
|         cubeb_stream_params params; |                    const std::string& name) | ||||||
|         params.rate = 48000; |         : ctx{ctx}, num_channels{num_channels_} { | ||||||
|         params.channels = GetNumChannels(); |  | ||||||
|         params.format = CUBEB_SAMPLE_S16NE; |  | ||||||
|         params.layout = CUBEB_LAYOUT_STEREO; |  | ||||||
|  |  | ||||||
|         u32 minimum_latency = 0; |         if (num_channels == 6) { | ||||||
|  |             // 6-channel audio does not seem to work with cubeb + SDL, so we downsample this to 2 | ||||||
|  |             // channel for now | ||||||
|  |             is_6_channel = true; | ||||||
|  |             num_channels = 2; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         cubeb_stream_params params{}; | ||||||
|  |         params.rate = sample_rate; | ||||||
|  |         params.channels = num_channels; | ||||||
|  |         params.format = CUBEB_SAMPLE_S16NE; | ||||||
|  |         params.layout = num_channels == 1 ? CUBEB_LAYOUT_MONO : CUBEB_LAYOUT_STEREO; | ||||||
|  |  | ||||||
|  |         u32 minimum_latency{}; | ||||||
|         if (cubeb_get_min_latency(ctx, ¶ms, &minimum_latency) != CUBEB_OK) { |         if (cubeb_get_min_latency(ctx, ¶ms, &minimum_latency) != CUBEB_OK) { | ||||||
|             LOG_CRITICAL(Audio_Sink, "Error getting minimum latency"); |             LOG_CRITICAL(Audio_Sink, "Error getting minimum latency"); | ||||||
|         } |         } | ||||||
| @@ -58,11 +68,7 @@ public: | |||||||
|  |  | ||||||
|         queue.reserve(queue.size() + sample_count * GetNumChannels()); |         queue.reserve(queue.size() + sample_count * GetNumChannels()); | ||||||
|  |  | ||||||
|         if (num_channels == 2) { |         if (is_6_channel) { | ||||||
|             // Copy as-is |  | ||||||
|             std::copy(samples, samples + sample_count * GetNumChannels(), |  | ||||||
|                       std::back_inserter(queue)); |  | ||||||
|         } else if (num_channels == 6) { |  | ||||||
|             // Downsample 6 channels to 2 |             // Downsample 6 channels to 2 | ||||||
|             const size_t sample_count_copy_size = sample_count * num_channels * 2; |             const size_t sample_count_copy_size = sample_count * num_channels * 2; | ||||||
|             queue.reserve(sample_count_copy_size); |             queue.reserve(sample_count_copy_size); | ||||||
| @@ -71,13 +77,14 @@ public: | |||||||
|                 queue.push_back(samples[i + 1]); |                 queue.push_back(samples[i + 1]); | ||||||
|             } |             } | ||||||
|         } else { |         } else { | ||||||
|             ASSERT_MSG(false, "Unimplemented"); |             // Copy as-is | ||||||
|  |             std::copy(samples, samples + sample_count * GetNumChannels(), | ||||||
|  |                       std::back_inserter(queue)); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     u32 GetNumChannels() const { |     u32 GetNumChannels() const { | ||||||
|         // Only support 2-channel stereo output for now |         return num_channels; | ||||||
|         return 2; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
| private: | private: | ||||||
| @@ -85,6 +92,8 @@ private: | |||||||
|  |  | ||||||
|     cubeb* ctx{}; |     cubeb* ctx{}; | ||||||
|     cubeb_stream* stream_backend{}; |     cubeb_stream* stream_backend{}; | ||||||
|  |     u32 num_channels{}; | ||||||
|  |     bool is_6_channel{}; | ||||||
|  |  | ||||||
|     std::vector<s16> queue; |     std::vector<s16> queue; | ||||||
|  |  | ||||||
| @@ -131,7 +140,8 @@ CubebSink::~CubebSink() { | |||||||
|  |  | ||||||
| SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels, | SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels, | ||||||
|                                          const std::string& name) { |                                          const std::string& name) { | ||||||
|     sink_streams.push_back(std::make_unique<SinkStreamImpl>(ctx, output_device, name)); |     sink_streams.push_back( | ||||||
|  |         std::make_unique<SinkStreamImpl>(ctx, sample_rate, num_channels, output_device, name)); | ||||||
|     return *sink_streams.back(); |     return *sink_streams.back(); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user