X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=audio_mixer.cpp;h=197b4f34605a1b27818c4fb45a40fd715387a83b;hb=26e1ec466d4730b6abc0e20201d704cfdf41a6eb;hp=18a669f0eb8bcfe399b09cbf305d8718617b6203;hpb=2140fc18714d6e9ac2b0aca51d9b7ff986245c08;p=nageru diff --git a/audio_mixer.cpp b/audio_mixer.cpp index 18a669f..197b4f3 100644 --- a/audio_mixer.cpp +++ b/audio_mixer.cpp @@ -12,6 +12,8 @@ #include "db.h" #include "flags.h" +#include "mixer.h" +#include "state.pb.h" #include "timebase.h" using namespace bmusb; @@ -161,33 +163,43 @@ AudioMixer::AudioMixer(unsigned num_cards) limiter(OUTPUT_FREQUENCY), correlation(OUTPUT_FREQUENCY) { + global_audio_mixer = this; + for (unsigned bus_index = 0; bus_index < MAX_BUSES; ++bus_index) { locut[bus_index].init(FILTER_HPF, 2); - locut_enabled[bus_index] = global_flags.locut_enabled; - gain_staging_db[bus_index] = global_flags.initial_gain_staging_db; + eq[bus_index][EQ_BAND_BASS].init(FILTER_LOW_SHELF, 1); + // Note: EQ_BAND_MID isn't used (see comments in apply_eq()). + eq[bus_index][EQ_BAND_TREBLE].init(FILTER_HIGH_SHELF, 1); compressor[bus_index].reset(new StereoCompressor(OUTPUT_FREQUENCY)); - compressor_threshold_dbfs[bus_index] = ref_level_dbfs - 12.0f; // -12 dB. - compressor_enabled[bus_index] = global_flags.compressor_enabled; level_compressor[bus_index].reset(new StereoCompressor(OUTPUT_FREQUENCY)); - level_compressor_enabled[bus_index] = global_flags.gain_staging_auto; + + set_bus_settings(bus_index, get_default_bus_settings()); } set_limiter_enabled(global_flags.limiter_enabled); set_final_makeup_gain_auto(global_flags.final_makeup_gain_auto); - - // Generate a very simple, default input mapping. - InputMapping::Bus input; - input.name = "Main"; - input.device.type = InputSourceType::CAPTURE_CARD; - input.device.index = 0; - input.source_channel[0] = 0; - input.source_channel[1] = 1; + alsa_pool.init(); InputMapping new_input_mapping; - new_input_mapping.buses.push_back(input); - set_input_mapping(new_input_mapping); + if (!global_flags.input_mapping_filename.empty()) { + if (!load_input_mapping_from_file(get_devices(), + global_flags.input_mapping_filename, + &new_input_mapping)) { + fprintf(stderr, "Failed to load input mapping from '%s', exiting.\n", + global_flags.input_mapping_filename.c_str()); + exit(1); + } + } else { + // Generate a very simple, default input mapping. + InputMapping::Bus input; + input.name = "Main"; + input.device.type = InputSourceType::CAPTURE_CARD; + input.device.index = 0; + input.source_channel[0] = 0; + input.source_channel[1] = 1; - // Look for ALSA cards. - available_alsa_cards = ALSAInput::enumerate_devices(); + new_input_mapping.buses.push_back(input); + } + set_input_mapping(new_input_mapping); r128.init(2, OUTPUT_FREQUENCY); r128.integr_start(); @@ -197,17 +209,6 @@ AudioMixer::AudioMixer(unsigned num_cards) peak_resampler.setup(OUTPUT_FREQUENCY, OUTPUT_FREQUENCY * 4, /*num_channels=*/2, /*hlen=*/16, /*frel=*/1.0); } -AudioMixer::~AudioMixer() -{ - for (unsigned card_index = 0; card_index < available_alsa_cards.size(); ++card_index) { - const AudioDevice &device = alsa_inputs[card_index]; - if (device.alsa_device != nullptr) { - device.alsa_device->stop_capture_thread(); - } - } -} - - void AudioMixer::reset_resampler(DeviceSpec device_spec) { lock_guard lock(audio_mutex); @@ -228,25 +229,6 @@ void AudioMixer::reset_resampler_mutex_held(DeviceSpec device_spec) device->next_local_pts = 0; } -void AudioMixer::reset_alsa_mutex_held(DeviceSpec device_spec) -{ - assert(device_spec.type == InputSourceType::ALSA_INPUT); - unsigned card_index = device_spec.index; - AudioDevice *device = find_audio_device(device_spec); - - if (device->alsa_device != nullptr) { - device->alsa_device->stop_capture_thread(); - } - if (device->interesting_channels.empty()) { - device->alsa_device.reset(); - } else { - const ALSAInput::Device &alsa_dev = available_alsa_cards[card_index]; - device->alsa_device.reset(new ALSAInput(alsa_dev.address.c_str(), OUTPUT_FREQUENCY, alsa_dev.num_channels, bind(&AudioMixer::add_audio, this, device_spec, _1, _2, _3, _4))); - device->capture_frequency = device->alsa_device->get_sample_rate(); - device->alsa_device->start_capture_thread(); - } -} - bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, int64_t frame_length) { AudioDevice *device = find_audio_device(device_spec); @@ -320,6 +302,67 @@ bool AudioMixer::add_silence(DeviceSpec device_spec, unsigned samples_per_frame, return true; } +bool AudioMixer::silence_card(DeviceSpec device_spec, bool silence) +{ + AudioDevice *device = find_audio_device(device_spec); + + unique_lock lock(audio_mutex, defer_lock); + if (!lock.try_lock_for(chrono::milliseconds(10))) { + return false; + } + + if (device->silenced && !silence) { + reset_resampler_mutex_held(device_spec); + } + device->silenced = silence; + return true; +} + +AudioMixer::BusSettings AudioMixer::get_default_bus_settings() +{ + BusSettings settings; + settings.fader_volume_db = 0.0f; + settings.locut_enabled = global_flags.locut_enabled; + for (unsigned band_index = 0; band_index < NUM_EQ_BANDS; ++band_index) { + settings.eq_level_db[band_index] = 0.0f; + } + settings.gain_staging_db = global_flags.initial_gain_staging_db; + settings.level_compressor_enabled = global_flags.gain_staging_auto; + settings.compressor_threshold_dbfs = ref_level_dbfs - 12.0f; // -12 dB. + settings.compressor_enabled = global_flags.compressor_enabled; + return settings; +} + +AudioMixer::BusSettings AudioMixer::get_bus_settings(unsigned bus_index) const +{ + lock_guard lock(audio_mutex); + BusSettings settings; + settings.fader_volume_db = fader_volume_db[bus_index]; + settings.locut_enabled = locut_enabled[bus_index]; + for (unsigned band_index = 0; band_index < NUM_EQ_BANDS; ++band_index) { + settings.eq_level_db[band_index] = eq_level_db[bus_index][band_index]; + } + settings.gain_staging_db = gain_staging_db[bus_index]; + settings.level_compressor_enabled = level_compressor_enabled[bus_index]; + settings.compressor_threshold_dbfs = compressor_threshold_dbfs[bus_index]; + settings.compressor_enabled = compressor_enabled[bus_index]; + return settings; +} + +void AudioMixer::set_bus_settings(unsigned bus_index, const AudioMixer::BusSettings &settings) +{ + lock_guard lock(audio_mutex); + fader_volume_db[bus_index] = settings.fader_volume_db; + locut_enabled[bus_index] = settings.locut_enabled; + for (unsigned band_index = 0; band_index < NUM_EQ_BANDS; ++band_index) { + eq_level_db[bus_index][band_index] = settings.eq_level_db[band_index]; + } + gain_staging_db[bus_index] = settings.gain_staging_db; + level_compressor_enabled[bus_index] = settings.level_compressor_enabled; + compressor_threshold_dbfs[bus_index] = settings.compressor_threshold_dbfs; + compressor_enabled[bus_index] = settings.compressor_enabled; +} + AudioMixer::AudioDevice *AudioMixer::find_audio_device(DeviceSpec device) { switch (device.type) { @@ -380,6 +423,24 @@ void AudioMixer::fill_audio_bus(const map> &samples_ca } } +vector AudioMixer::get_active_devices() const +{ + vector ret; + for (unsigned card_index = 0; card_index < MAX_VIDEO_CARDS; ++card_index) { + const DeviceSpec device_spec{InputSourceType::CAPTURE_CARD, card_index}; + if (!find_audio_device(device_spec)->interesting_channels.empty()) { + ret.push_back(device_spec); + } + } + for (unsigned card_index = 0; card_index < MAX_ALSA_CARDS; ++card_index) { + const DeviceSpec device_spec{InputSourceType::ALSA_INPUT, card_index}; + if (!find_audio_device(device_spec)->interesting_channels.empty()) { + ret.push_back(device_spec); + } + } + return ret; +} + vector AudioMixer::get_output(double pts, unsigned num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy) { map> samples_card; @@ -388,14 +449,12 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin lock_guard lock(audio_mutex); // Pick out all the interesting channels from all the cards. - // TODO: If the card has been hotswapped, the number of channels - // might have changed; if so, we need to do some sort of remapping - // to silence. - for (const auto &spec_and_info : get_devices_mutex_held()) { - const DeviceSpec &device_spec = spec_and_info.first; + for (const DeviceSpec &device_spec : get_active_devices()) { AudioDevice *device = find_audio_device(device_spec); - if (!device->interesting_channels.empty()) { - samples_card[device_spec].resize(num_samples * device->interesting_channels.size()); + samples_card[device_spec].resize(num_samples * device->interesting_channels.size()); + if (device->silenced) { + memset(&samples_card[device_spec][0], 0, samples_card[device_spec].size() * sizeof(float)); + } else { device->resampling_queue->get_output_samples( pts, &samples_card[device_spec][0], @@ -409,14 +468,7 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin samples_bus.resize(num_samples * 2); for (unsigned bus_index = 0; bus_index < input_mapping.buses.size(); ++bus_index) { fill_audio_bus(samples_card, input_mapping.buses[bus_index], num_samples, &samples_bus[0]); - - // Cut away everything under 120 Hz (or whatever the cutoff is); - // we don't need it for voice, and it will reduce headroom - // and confuse the compressor. (In particular, any hums at 50 or 60 Hz - // should be dampened.) - if (locut_enabled[bus_index]) { - locut[bus_index].render(samples_bus.data(), samples_bus.size() / 2, locut_cutoff_hz * 2.0 * M_PI / OUTPUT_FREQUENCY, 0.5f); - } + apply_eq(bus_index, &samples_bus); { lock_guard lock(compressor_mutex); @@ -531,6 +583,44 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin return samples_out; } +void AudioMixer::apply_eq(unsigned bus_index, vector *samples_bus) +{ + constexpr float bass_freq_hz = 200.0f; + constexpr float treble_freq_hz = 4700.0f; + + // Cut away everything under 120 Hz (or whatever the cutoff is); + // we don't need it for voice, and it will reduce headroom + // and confuse the compressor. (In particular, any hums at 50 or 60 Hz + // should be dampened.) + if (locut_enabled[bus_index]) { + locut[bus_index].render(samples_bus->data(), samples_bus->size() / 2, locut_cutoff_hz * 2.0 * M_PI / OUTPUT_FREQUENCY, 0.5f); + } + + // Apply the rest of the EQ. Since we only have a simple three-band EQ, + // we can implement it with two shelf filters. We use a simple gain to + // set the mid-level filter, and then offset the low and high bands + // from that if we need to. (We could perhaps have folded the gain into + // the next part, but it's so cheap that the trouble isn't worth it.) + if (fabs(eq_level_db[bus_index][EQ_BAND_MID]) > 0.01f) { + float g = from_db(eq_level_db[bus_index][EQ_BAND_MID]); + for (size_t i = 0; i < samples_bus->size(); ++i) { + (*samples_bus)[i] *= g; + } + } + + float bass_adj_db = eq_level_db[bus_index][EQ_BAND_BASS] - eq_level_db[bus_index][EQ_BAND_MID]; + if (fabs(bass_adj_db) > 0.01f) { + eq[bus_index][EQ_BAND_BASS].render(samples_bus->data(), samples_bus->size() / 2, + bass_freq_hz * 2.0 * M_PI / OUTPUT_FREQUENCY, 0.5f, bass_adj_db / 40.0f); + } + + float treble_adj_db = eq_level_db[bus_index][EQ_BAND_TREBLE] - eq_level_db[bus_index][EQ_BAND_MID]; + if (fabs(treble_adj_db) > 0.01f) { + eq[bus_index][EQ_BAND_TREBLE].render(samples_bus->data(), samples_bus->size() / 2, + treble_freq_hz * 2.0 * M_PI / OUTPUT_FREQUENCY, 0.5f, treble_adj_db / 40.0f); + } +} + void AudioMixer::add_bus_to_master(unsigned bus_index, const vector &samples_bus, vector *samples_out) { assert(samples_bus.size() == samples_out->size()); @@ -695,40 +785,58 @@ void AudioMixer::send_audio_level_callback() correlation.get_correlation()); } -map AudioMixer::get_devices() const +map AudioMixer::get_devices() { lock_guard lock(audio_mutex); - return get_devices_mutex_held(); -} -map AudioMixer::get_devices_mutex_held() const -{ map devices; for (unsigned card_index = 0; card_index < num_cards; ++card_index) { const DeviceSpec spec{ InputSourceType::CAPTURE_CARD, card_index }; const AudioDevice *device = &video_cards[card_index]; DeviceInfo info; - info.name = device->name; - info.num_channels = 8; // FIXME: This is wrong for fake cards. + info.display_name = device->display_name; + info.num_channels = 8; devices.insert(make_pair(spec, info)); } - for (unsigned card_index = 0; card_index < available_alsa_cards.size(); ++card_index) { + vector available_alsa_devices = alsa_pool.get_devices(); + for (unsigned card_index = 0; card_index < available_alsa_devices.size(); ++card_index) { const DeviceSpec spec{ InputSourceType::ALSA_INPUT, card_index }; - const ALSAInput::Device &device = available_alsa_cards[card_index]; + const ALSAPool::Device &device = available_alsa_devices[card_index]; DeviceInfo info; - info.name = device.name + " (" + device.info + ")"; + info.display_name = device.display_name(); info.num_channels = device.num_channels; + info.alsa_name = device.name; + info.alsa_info = device.info; + info.alsa_address = device.address; devices.insert(make_pair(spec, info)); } return devices; } -void AudioMixer::set_name(DeviceSpec device_spec, const string &name) +void AudioMixer::set_display_name(DeviceSpec device_spec, const string &name) { AudioDevice *device = find_audio_device(device_spec); lock_guard lock(audio_mutex); - device->name = name; + device->display_name = name; +} + +void AudioMixer::serialize_device(DeviceSpec device_spec, DeviceSpecProto *device_spec_proto) +{ + lock_guard lock(audio_mutex); + switch (device_spec.type) { + case InputSourceType::SILENCE: + device_spec_proto->set_type(DeviceSpecProto::SILENCE); + break; + case InputSourceType::CAPTURE_CARD: + device_spec_proto->set_type(DeviceSpecProto::CAPTURE_CARD); + device_spec_proto->set_index(device_spec.index); + device_spec_proto->set_display_name(video_cards[device_spec.index].display_name); + break; + case InputSourceType::ALSA_INPUT: + alsa_pool.serialize_device(device_spec.index, device_spec_proto); + break; + } } void AudioMixer::set_input_mapping(const InputMapping &new_input_mapping) @@ -748,14 +856,25 @@ void AudioMixer::set_input_mapping(const InputMapping &new_input_mapping) } // Reset resamplers for all cards that don't have the exact same state as before. - for (const auto &spec_and_info : get_devices_mutex_held()) { - const DeviceSpec &device_spec = spec_and_info.first; + 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->interesting_channels = interesting_channels[device_spec]; - if (device_spec.type == InputSourceType::ALSA_INPUT) { - reset_alsa_mutex_held(device_spec); - } + reset_resampler_mutex_held(device_spec); + } + } + 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); + 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->interesting_channels = interesting_channels[device_spec]; + alsa_pool.reset_device(device_spec.index); reset_resampler_mutex_held(device_spec); } } @@ -768,3 +887,18 @@ InputMapping AudioMixer::get_input_mapping() const lock_guard lock(audio_mutex); return input_mapping; } + +void AudioMixer::reset_peak(unsigned bus_index) +{ + lock_guard lock(audio_mutex); + for (unsigned channel = 0; channel < 2; ++channel) { + PeakHistory &history = peak_history[bus_index][channel]; + history.current_level = 0.0f; + history.historic_peak = 0.0f; + history.current_peak = 0.0f; + history.last_peak = 0.0f; + history.age_seconds = 0.0f; + } +} + +AudioMixer *global_audio_mixer = nullptr;