]> git.sesse.net Git - nageru/blobdiff - nageru/audio_mixer.cpp
Make it possible to adjust the delay without resetting the resampler.
[nageru] / nageru / audio_mixer.cpp
index a095c177d6265467922f2bc58c4033c5da9cd71e..07d10dad3650a9ca037263d661b4d5deca4adbf2 100644 (file)
@@ -19,6 +19,7 @@
 #include <utility>
 
 #include "decibel.h"
+#include "delay_analyzer.h"
 #include "flags.h"
 #include "shared/metrics.h"
 #include "state.pb.h"
@@ -227,6 +228,14 @@ void deinterleave_samples(const vector<float> &in, vector<float> *out_l, vector<
        }
 }
 
+double get_delay_seconds(double extra_delay_ms)
+{
+       // Make sure we never get negative delay. Even 1 ms is probably way less than we
+       // could ever hope to actually have; this is just a failsafe.
+       double delay_ms = max(global_flags.audio_queue_length_ms + extra_delay_ms, 1.0);
+       return delay_ms * 0.001;
+}
+
 }  // namespace
 
 AudioMixer::AudioMixer(unsigned num_capture_cards, unsigned num_ffmpeg_inputs)
@@ -263,7 +272,7 @@ AudioMixer::AudioMixer(unsigned num_capture_cards, unsigned num_ffmpeg_inputs)
                // Must happen after ALSAPool is initialized, as it needs to know the card list.
                current_mapping_mode = MappingMode::MULTICHANNEL;
                InputMapping new_input_mapping;
-               if (!load_input_mapping_from_file(get_devices(),
+               if (!load_input_mapping_from_file(get_devices(HOLD_ALSA_DEVICES),
                                                  global_flags.input_mapping_filename,
                                                  &new_input_mapping)) {
                        fprintf(stderr, "Failed to load input mapping from '%s', exiting.\n",
@@ -300,18 +309,18 @@ void AudioMixer::reset_resampler_mutex_held(DeviceSpec device_spec)
        if (device->interesting_channels.empty()) {
                device->resampling_queue.reset();
        } else {
-               // Make sure we never get negative delay. Even 1 ms is probably way less than we
-               // could ever hope to actually have; this is just a failsafe.
-               double delay_ms = max(global_flags.audio_queue_length_ms + device->extra_delay_ms, 1.0);
-
                device->resampling_queue.reset(new ResamplingQueue(
                        device_spec, device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size(),
-                       delay_ms * 0.001));
+                       get_delay_seconds(device->extra_delay_ms)));
        }
 }
 
 bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, steady_clock::time_point frame_time)
 {
+       if (delay_analyzer != nullptr && delay_analyzer->is_grabbing()) {
+               delay_analyzer->add_audio(device_spec, data, num_samples, audio_format, frame_time);
+       }
+
        AudioDevice *device = find_audio_device(device_spec);
 
        unique_lock<timed_mutex> lock(audio_mutex, defer_lock);
@@ -330,23 +339,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 +353,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 +386,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);
@@ -1021,7 +1039,7 @@ void AudioMixer::send_audio_level_callback()
                correlation.get_correlation());
 }
 
-map<DeviceSpec, DeviceInfo> AudioMixer::get_devices()
+map<DeviceSpec, DeviceInfo> AudioMixer::get_devices(HoldDevices hold_devices)
 {
        lock_guard<timed_mutex> lock(audio_mutex);
 
@@ -1034,7 +1052,7 @@ map<DeviceSpec, DeviceInfo> AudioMixer::get_devices()
                info.num_channels = 8;
                devices.insert(make_pair(spec, info));
        }
-       vector<ALSAPool::Device> available_alsa_devices = alsa_pool.get_devices();
+       vector<ALSAPool::Device> available_alsa_devices = alsa_pool.get_devices(hold_devices);
        for (unsigned card_index = 0; card_index < available_alsa_devices.size(); ++card_index) {
                const DeviceSpec spec{ InputSourceType::ALSA_INPUT, card_index };
                const ALSAPool::Device &device = available_alsa_devices[card_index];
@@ -1104,6 +1122,8 @@ void AudioMixer::set_simple_input(unsigned card_index)
 
        new_input_mapping.buses.push_back(input);
 
+       // NOTE: Delay is implicitly at 0.0 ms, since none has been set in the mapping.
+
        lock_guard<timed_mutex> lock(audio_mutex);
        current_mapping_mode = MappingMode::SIMPLE;
        set_input_mapping_lock_held(new_input_mapping);
@@ -1221,37 +1241,49 @@ void AudioMixer::set_input_mapping_lock_held(const InputMapping &new_input_mappi
        for (unsigned card_index = 0; card_index < MAX_VIDEO_CARDS; ++card_index) {
                const DeviceSpec device_spec{InputSourceType::CAPTURE_CARD, card_index};
                AudioDevice *device = find_audio_device(device_spec);
-               if (device->interesting_channels != interesting_channels[device_spec] ||
-                   device->extra_delay_ms != new_extra_delay_ms[device_spec]) {
+               double extra_delay_ms = new_extra_delay_ms[device_spec];
+               if (device->interesting_channels != interesting_channels[device_spec]) {
                        device->interesting_channels = interesting_channels[device_spec];
-                       device->extra_delay_ms = new_extra_delay_ms[device_spec];
+                       device->extra_delay_ms = extra_delay_ms;
                        reset_resampler_mutex_held(device_spec);
+               } else if (device->extra_delay_ms != extra_delay_ms &&
+                          device->resampling_queue != nullptr) {
+                       device->extra_delay_ms = extra_delay_ms;
+                       device->resampling_queue->change_expected_delay(get_delay_seconds(extra_delay_ms));
                }
        }
        for (unsigned card_index = 0; card_index < MAX_ALSA_CARDS; ++card_index) {
                const DeviceSpec device_spec{InputSourceType::ALSA_INPUT, card_index};
                AudioDevice *device = find_audio_device(device_spec);
+               double extra_delay_ms = new_extra_delay_ms[device_spec];
                if (interesting_channels[device_spec].empty()) {
                        alsa_pool.release_device(card_index);
                } else {
                        alsa_pool.hold_device(card_index);
                }
-               if (device->interesting_channels != interesting_channels[device_spec] ||
-                   device->extra_delay_ms != new_extra_delay_ms[device_spec]) {
+               if (device->interesting_channels != interesting_channels[device_spec]) {
                        device->interesting_channels = interesting_channels[device_spec];
-                       device->extra_delay_ms = new_extra_delay_ms[device_spec];
+                       device->extra_delay_ms = extra_delay_ms;
                        alsa_pool.reset_device(device_spec.index);
                        reset_resampler_mutex_held(device_spec);
+               } else if (device->extra_delay_ms != extra_delay_ms &&
+                          device->resampling_queue != nullptr) {
+                       device->extra_delay_ms = extra_delay_ms;
+                       device->resampling_queue->change_expected_delay(get_delay_seconds(extra_delay_ms));
                }
        }
        for (unsigned card_index = 0; card_index < num_ffmpeg_inputs; ++card_index) {
                const DeviceSpec device_spec{InputSourceType::FFMPEG_VIDEO_INPUT, card_index};
                AudioDevice *device = find_audio_device(device_spec);
-               if (device->interesting_channels != interesting_channels[device_spec] ||
-                   device->extra_delay_ms != new_extra_delay_ms[device_spec]) {
+               double extra_delay_ms = new_extra_delay_ms[device_spec];
+               if (device->interesting_channels != interesting_channels[device_spec]) {
                        device->interesting_channels = interesting_channels[device_spec];
-                       device->extra_delay_ms = new_extra_delay_ms[device_spec];
+                       device->extra_delay_ms = extra_delay_ms;
                        reset_resampler_mutex_held(device_spec);
+               } else if (device->extra_delay_ms != extra_delay_ms &&
+                          device->resampling_queue != nullptr) {
+                       device->extra_delay_ms = extra_delay_ms;
+                       device->resampling_queue->change_expected_delay(get_delay_seconds(extra_delay_ms));
                }
        }