]> git.sesse.net Git - nageru/blobdiff - nageru/audio_mixer.cpp
Factor out a convert_audio_to_fp32() function.
[nageru] / nageru / audio_mixer.cpp
index a095c177d6265467922f2bc58c4033c5da9cd71e..35e0a23ee2da2b5b4e361838eae0d5aeeb765246 100644 (file)
@@ -330,23 +330,7 @@ bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned
        unique_ptr<float[]> audio(new float[num_samples * num_channels]);
        unsigned channel_index = 0;
        for (auto channel_it = device->interesting_channels.cbegin(); channel_it != device->interesting_channels.end(); ++channel_it, ++channel_index) {
-               switch (audio_format.bits_per_sample) {
-               case 0:
-                       assert(num_samples == 0);
-                       break;
-               case 16:
-                       convert_fixed16_to_fp32(audio.get(), channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples);
-                       break;
-               case 24:
-                       convert_fixed24_to_fp32(audio.get(), channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples);
-                       break;
-               case 32:
-                       convert_fixed32_to_fp32(audio.get(), channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples);
-                       break;
-               default:
-                       fprintf(stderr, "Cannot handle audio with %u bits per sample\n", audio_format.bits_per_sample);
-                       assert(false);
-               }
+               convert_audio_to_fp32(audio.get(), channel_index, num_channels, data, *channel_it, audio_format, num_samples);
        }
 
        // If we changed frequency since last frame, we'll need to reset the resampler.
@@ -360,6 +344,7 @@ bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned
        return true;
 }
 
+// Converts all channels.
 vector<int32_t> convert_audio_to_fixed32(const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, unsigned num_channels)
 {
        vector<int32_t> audio;
@@ -392,6 +377,30 @@ vector<int32_t> convert_audio_to_fixed32(const uint8_t *data, unsigned num_sampl
        return audio;
 }
 
+// Converts only one channel.
+void convert_audio_to_fp32(float *dst, size_t out_channel, size_t out_num_channels,
+                           const uint8_t *src, size_t in_channel, bmusb::AudioFormat in_audio_format,
+                           size_t num_samples)
+{
+       switch (in_audio_format.bits_per_sample) {
+       case 0:
+               assert(num_samples == 0);
+               break;
+       case 16:
+               convert_fixed16_to_fp32(dst, out_channel, out_num_channels, src, in_channel, in_audio_format.num_channels, num_samples);
+               break;
+       case 24:
+               convert_fixed24_to_fp32(dst, out_channel, out_num_channels, src, in_channel, in_audio_format.num_channels, num_samples);
+               break;
+       case 32:
+               convert_fixed32_to_fp32(dst, out_channel, out_num_channels, src, in_channel, in_audio_format.num_channels, num_samples);
+               break;
+       default:
+               fprintf(stderr, "Cannot handle audio with %u bits per sample\n", in_audio_format.bits_per_sample);
+               assert(false);
+       }
+}
+
 bool AudioMixer::add_silence(DeviceSpec device_spec, unsigned samples_per_frame, unsigned num_frames)
 {
        AudioDevice *device = find_audio_device(device_spec);