]> git.sesse.net Git - nageru/commitdiff
Factor out a convert_audio_to_fp32() function.
authorSteinar H. Gunderson <steinar+nageru@gunderson.no>
Sun, 11 Aug 2019 08:50:50 +0000 (10:50 +0200)
committerSteinar H. Gunderson <steinar+nageru@gunderson.no>
Sun, 11 Aug 2019 08:53:32 +0000 (10:53 +0200)
nageru/audio_mixer.cpp
nageru/audio_mixer.h

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);
index de6ff5414032a7f414d82b7195156c2921400a7c..9dc5c89d9702666bd8f936e5096604196aead143 100644 (file)
@@ -41,6 +41,11 @@ struct AudioFormat;
 // Assumes little-endian and chunky, signed PCM throughout.
 std::vector<int32_t> convert_audio_to_fixed32(const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, unsigned num_destination_channels);
 
+// Similar, except converts ot floating-point instead, and 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);
+
 enum EQBand {
        EQ_BAND_BASS = 0,
        EQ_BAND_MID,