X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=audio_mixer.cpp;h=7da5b0bea3d84db6853ce5616b795382a5db12b9;hb=1836dccf699779d9092a75755cec96cea1734a2a;hp=e4eb495953baaa49cd487f2b5e16960857d88efb;hpb=e65bea1cd01cba642bbff2a7f79977196b2a2b42;p=nageru diff --git a/audio_mixer.cpp b/audio_mixer.cpp index e4eb495..7da5b0b 100644 --- a/audio_mixer.cpp +++ b/audio_mixer.cpp @@ -1,24 +1,32 @@ #include "audio_mixer.h" #include -#include #include -#include #include -#include -#include -#ifdef __SSE__ +#include +#ifdef __SSE2__ #include #endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "db.h" #include "flags.h" -#include "mixer.h" +#include "metrics.h" #include "state.pb.h" #include "timebase.h" using namespace bmusb; using namespace std; +using namespace std::chrono; using namespace std::placeholders; namespace { @@ -164,8 +172,6 @@ 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); eq[bus_index][EQ_BAND_BASS].init(FILTER_LOW_SHELF, 1); @@ -178,9 +184,19 @@ AudioMixer::AudioMixer(unsigned num_cards) } set_limiter_enabled(global_flags.limiter_enabled); set_final_makeup_gain_auto(global_flags.final_makeup_gain_auto); + + r128.init(2, OUTPUT_FREQUENCY); + r128.integr_start(); + + // hlen=16 is pretty low quality, but we use quite a bit of CPU otherwise, + // and there's a limit to how important the peak meter is. + peak_resampler.setup(OUTPUT_FREQUENCY, OUTPUT_FREQUENCY * 4, /*num_channels=*/2, /*hlen=*/16, /*frel=*/1.0); + + global_audio_mixer = this; alsa_pool.init(); if (!global_flags.input_mapping_filename.empty()) { + // 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(), @@ -198,12 +214,13 @@ AudioMixer::AudioMixer(unsigned num_cards) } } - r128.init(2, OUTPUT_FREQUENCY); - r128.integr_start(); - - // hlen=16 is pretty low quality, but we use quite a bit of CPU otherwise, - // and there's a limit to how important the peak meter is. - peak_resampler.setup(OUTPUT_FREQUENCY, OUTPUT_FREQUENCY * 4, /*num_channels=*/2, /*hlen=*/16, /*frel=*/1.0); + global_metrics.add("audio_loudness_short_lufs", &metric_audio_loudness_short_lufs, Metrics::TYPE_GAUGE); + global_metrics.add("audio_loudness_integrated_lufs", &metric_audio_loudness_integrated_lufs, Metrics::TYPE_GAUGE); + global_metrics.add("audio_loudness_range_low_lufs", &metric_audio_loudness_range_low_lufs, Metrics::TYPE_GAUGE); + global_metrics.add("audio_loudness_range_high_lufs", &metric_audio_loudness_range_high_lufs, Metrics::TYPE_GAUGE); + global_metrics.add("audio_peak_dbfs", &metric_audio_peak_dbfs, Metrics::TYPE_GAUGE); + global_metrics.add("audio_final_makeup_gain_db", &metric_audio_final_makeup_gain_db, Metrics::TYPE_GAUGE); + global_metrics.add("audio_correlation", &metric_audio_correlation, Metrics::TYPE_GAUGE); } void AudioMixer::reset_resampler(DeviceSpec device_spec) @@ -221,12 +238,13 @@ void AudioMixer::reset_resampler_mutex_held(DeviceSpec device_spec) } else { // TODO: ResamplingQueue should probably take the full device spec. // (It's only used for console output, though.) - device->resampling_queue.reset(new ResamplingQueue(device_spec.index, device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size())); + device->resampling_queue.reset(new ResamplingQueue( + device_spec.index, device->capture_frequency, OUTPUT_FREQUENCY, device->interesting_channels.size(), + global_flags.audio_queue_length_ms * 0.001)); } - device->next_local_pts = 0; } -bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, int64_t frame_length) +bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, int64_t frame_length, steady_clock::time_point frame_time) { AudioDevice *device = find_audio_device(device_spec); @@ -265,10 +283,14 @@ bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned } } + // If we changed frequency since last frame, we'll need to reset the resampler. + if (audio_format.sample_rate != device->capture_frequency) { + device->capture_frequency = audio_format.sample_rate; + reset_resampler_mutex_held(device_spec); + } + // Now add it. - int64_t local_pts = device->next_local_pts; - device->resampling_queue->add_input_samples(local_pts / double(TIMEBASE), audio.get(), num_samples); - device->next_local_pts = local_pts + frame_length; + device->resampling_queue->add_input_samples(frame_time, audio.get(), num_samples, ResamplingQueue::ADJUST_RATE); return true; } @@ -290,11 +312,7 @@ bool AudioMixer::add_silence(DeviceSpec device_spec, unsigned samples_per_frame, vector silence(samples_per_frame * num_channels, 0.0f); for (unsigned i = 0; i < num_frames; ++i) { - device->resampling_queue->add_input_samples(device->next_local_pts / double(TIMEBASE), silence.data(), samples_per_frame); - // Note that if the format changed in the meantime, we have - // no way of detecting that; we just have to assume the frame length - // is always the same. - device->next_local_pts += frame_length; + device->resampling_queue->add_input_samples(steady_clock::now(), silence.data(), samples_per_frame, ResamplingQueue::DO_NOT_ADJUST_RATE); } return true; } @@ -319,6 +337,7 @@ AudioMixer::BusSettings AudioMixer::get_default_bus_settings() { BusSettings settings; settings.fader_volume_db = 0.0f; + settings.muted = false; 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; @@ -335,6 +354,7 @@ 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.muted = mute[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]; @@ -350,6 +370,7 @@ void AudioMixer::set_bus_settings(unsigned bus_index, const AudioMixer::BusSetti { lock_guard lock(audio_mutex); fader_volume_db[bus_index] = settings.fader_volume_db; + mute[bus_index] = settings.muted; 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]; @@ -403,7 +424,7 @@ void AudioMixer::find_sample_src_from_device(const map void AudioMixer::fill_audio_bus(const map> &samples_card, const InputMapping::Bus &bus, unsigned num_samples, float *output) { if (bus.device.type == InputSourceType::SILENCE) { - memset(output, 0, num_samples * sizeof(*output)); + memset(output, 0, num_samples * 2 * sizeof(*output)); } else { assert(bus.device.type == InputSourceType::CAPTURE_CARD || bus.device.type == InputSourceType::ALSA_INPUT); @@ -464,7 +485,7 @@ void apply_gain(float db, float last_db, vector *samples) } // namespace -vector AudioMixer::get_output(double pts, unsigned num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy) +vector AudioMixer::get_output(steady_clock::time_point ts, unsigned num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy) { map> samples_card; vector samples_bus; @@ -479,7 +500,7 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin memset(&samples_card[device_spec][0], 0, samples_card[device_spec].size() * sizeof(float)); } else { device->resampling_queue->get_output_samples( - pts, + ts, &samples_card[device_spec][0], num_samples, rate_adjustment_policy); @@ -574,13 +595,12 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin // (half-time of 30 seconds). double target_loudness_factor, alpha; double loudness_lu = r128.loudness_M() - ref_level_lufs; - double current_makeup_lu = to_db(final_makeup_gain); target_loudness_factor = final_makeup_gain * from_db(-loudness_lu); - // If we're outside +/- 5 LU uncorrected, we don't count it as + // If we're outside +/- 5 LU (after correction), we don't count it as // a normal signal (probably silence) and don't change the // correction factor; just apply what we already have. - if (fabs(loudness_lu - current_makeup_lu) >= 5.0 || !final_makeup_gain_auto) { + if (fabs(loudness_lu) >= 5.0 || !final_makeup_gain_auto) { alpha = 0.0; } else { // Formula adapted from @@ -683,13 +703,14 @@ void AudioMixer::add_bus_to_master(unsigned bus_index, const vector &samp assert(samples_bus.size() == samples_out->size()); assert(samples_bus.size() % 2 == 0); unsigned num_samples = samples_bus.size() / 2; - if (fabs(fader_volume_db[bus_index] - last_fader_volume_db[bus_index]) > 1e-3) { + const float new_volume_db = mute[bus_index] ? -90.0f : fader_volume_db[bus_index].load(); + if (fabs(new_volume_db - last_fader_volume_db[bus_index]) > 1e-3) { // The volume has changed; do a fade over the course of this frame. // (We might have some numerical issues here, but it seems to sound OK.) // For the purpose of fading here, the silence floor is set to -90 dB // (the fader only goes to -84). float old_volume = from_db(max(last_fader_volume_db[bus_index], -90.0f)); - float volume = from_db(max(fader_volume_db[bus_index], -90.0f)); + float volume = from_db(max(new_volume_db, -90.0f)); float volume_inc = pow(volume / old_volume, 1.0 / num_samples); volume = old_volume; @@ -706,8 +727,8 @@ void AudioMixer::add_bus_to_master(unsigned bus_index, const vector &samp volume *= volume_inc; } } - } else { - float volume = from_db(fader_volume_db[bus_index]); + } else if (new_volume_db > -90.0f) { + float volume = from_db(new_volume_db); if (bus_index == 0) { for (unsigned i = 0; i < num_samples; ++i) { (*samples_out)[i * 2 + 0] = samples_bus[i * 2 + 0] * volume; @@ -721,13 +742,13 @@ void AudioMixer::add_bus_to_master(unsigned bus_index, const vector &samp } } - last_fader_volume_db[bus_index] = fader_volume_db[bus_index]; + last_fader_volume_db[bus_index] = new_volume_db; } void AudioMixer::measure_bus_levels(unsigned bus_index, const vector &left, const vector &right) { assert(left.size() == right.size()); - const float volume = from_db(fader_volume_db[bus_index]); + const float volume = mute[bus_index] ? 0.0f : from_db(fader_volume_db[bus_index]); const float peak_levels[2] = { find_peak(left.data(), left.size()) * volume, find_peak(right.data(), right.size()) * volume @@ -815,6 +836,14 @@ void AudioMixer::send_audio_level_callback() double loudness_range_low = r128.range_min(); double loudness_range_high = r128.range_max(); + metric_audio_loudness_short_lufs = loudness_s; + metric_audio_loudness_integrated_lufs = loudness_i; + metric_audio_loudness_range_low_lufs = loudness_range_low; + metric_audio_loudness_range_high_lufs = loudness_range_high; + metric_audio_peak_dbfs = to_db(peak); + metric_audio_final_makeup_gain_db = to_db(final_makeup_gain); + metric_audio_correlation = correlation.get_correlation(); + vector bus_levels; bus_levels.resize(input_mapping.buses.size()); { @@ -987,6 +1016,12 @@ InputMapping AudioMixer::get_input_mapping() const return input_mapping; } +unsigned AudioMixer::num_buses() const +{ + lock_guard lock(audio_mutex); + return input_mapping.buses.size(); +} + void AudioMixer::reset_peak(unsigned bus_index) { lock_guard lock(audio_mutex);