X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=audio_mixer.cpp;h=3b40955ef1e43b6604fd2d212e3da1e2c225ff3a;hb=a564f192f808841ad8dfa9a4aa6c8db3335bd6fd;hp=769dabfa74a0831cb6678a52f15d245d2c4ca6f2;hpb=d9babea9e8b67a7ccbfa931c16b2c7ca38b84c5a;p=nageru diff --git a/audio_mixer.cpp b/audio_mixer.cpp index 769dabf..3b40955 100644 --- a/audio_mixer.cpp +++ b/audio_mixer.cpp @@ -6,9 +6,13 @@ #include #include #include +#ifdef __SSE__ +#include +#endif #include "db.h" #include "flags.h" +#include "mixer.h" #include "timebase.h" using namespace bmusb; @@ -77,7 +81,9 @@ void convert_fixed32_to_fp32(float *dst, size_t out_channel, size_t out_num_chan } } -float find_peak(const float *samples, size_t num_samples) +float find_peak_plain(const float *samples, size_t num_samples) __attribute__((unused)); + +float find_peak_plain(const float *samples, size_t num_samples) { float m = fabs(samples[0]); for (size_t i = 1; i < num_samples; ++i) { @@ -86,6 +92,54 @@ float find_peak(const float *samples, size_t num_samples) return m; } +#ifdef __SSE__ +static inline float horizontal_max(__m128 m) +{ + __m128 tmp = _mm_shuffle_ps(m, m, _MM_SHUFFLE(1, 0, 3, 2)); + m = _mm_max_ps(m, tmp); + tmp = _mm_shuffle_ps(m, m, _MM_SHUFFLE(2, 3, 0, 1)); + m = _mm_max_ps(m, tmp); + return _mm_cvtss_f32(m); +} + +float find_peak(const float *samples, size_t num_samples) +{ + const __m128 abs_mask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffffu)); + __m128 m = _mm_setzero_ps(); + for (size_t i = 0; i < (num_samples & ~3); i += 4) { + __m128 x = _mm_loadu_ps(samples + i); + x = _mm_and_ps(x, abs_mask); + m = _mm_max_ps(m, x); + } + float result = horizontal_max(m); + + for (size_t i = (num_samples & ~3); i < num_samples; ++i) { + result = max(result, fabs(samples[i])); + } + +#if 0 + // Self-test. We should be bit-exact the same. + float reference_result = find_peak_plain(samples, num_samples); + if (result != reference_result) { + fprintf(stderr, "Error: Peak is %f [%f %f %f %f]; should be %f.\n", + result, + _mm_cvtss_f32(_mm_shuffle_ps(m, m, _MM_SHUFFLE(0, 0, 0, 0))), + _mm_cvtss_f32(_mm_shuffle_ps(m, m, _MM_SHUFFLE(1, 1, 1, 1))), + _mm_cvtss_f32(_mm_shuffle_ps(m, m, _MM_SHUFFLE(2, 2, 2, 2))), + _mm_cvtss_f32(_mm_shuffle_ps(m, m, _MM_SHUFFLE(3, 3, 3, 3))), + reference_result); + abort(); + } +#endif + return result; +} +#else +float find_peak(const float *samples, size_t num_samples) +{ + return find_peak_plain(samples, num_samples); +} +#endif + void deinterleave_samples(const vector &in, vector *out_l, vector *out_r) { size_t num_samples = in.size() / 2; @@ -105,17 +159,25 @@ void deinterleave_samples(const vector &in, vector *out_l, vector< AudioMixer::AudioMixer(unsigned num_cards) : num_cards(num_cards), - level_compressor(OUTPUT_FREQUENCY), limiter(OUTPUT_FREQUENCY), - compressor(OUTPUT_FREQUENCY), correlation(OUTPUT_FREQUENCY) { - locut.init(FILTER_HPF, 2); - - set_locut_enabled(global_flags.locut_enabled); - set_gain_staging_db(global_flags.initial_gain_staging_db); - set_gain_staging_auto(global_flags.gain_staging_auto); - set_compressor_enabled(global_flags.compressor_enabled); + 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; + 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); + + gain_staging_db[bus_index] = global_flags.initial_gain_staging_db; + 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_limiter_enabled(global_flags.limiter_enabled); set_final_makeup_gain_auto(global_flags.final_makeup_gain_auto); @@ -131,8 +193,7 @@ AudioMixer::AudioMixer(unsigned num_cards) new_input_mapping.buses.push_back(input); set_input_mapping(new_input_mapping); - // Look for ALSA cards. - available_alsa_cards = ALSAInput::enumerate_devices(); + alsa_pool.init(); r128.init(2, OUTPUT_FREQUENCY); r128.integr_start(); @@ -142,17 +203,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); @@ -173,25 +223,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); @@ -209,8 +240,7 @@ bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned assert(num_channels > 0); // Convert the audio to fp32. - vector audio; - audio.resize(num_samples * num_channels); + unique_ptr 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) { @@ -218,13 +248,13 @@ bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned assert(num_samples == 0); break; case 16: - convert_fixed16_to_fp32(&audio[0], channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples); + 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[0], channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples); + 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[0], channel_index, num_channels, data, *channel_it, audio_format.num_channels, num_samples); + 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); @@ -234,7 +264,7 @@ bool AudioMixer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned // Now add it. int64_t local_pts = device->next_local_pts; - device->resampling_queue->add_input_samples(local_pts / double(TIMEBASE), audio.data(), num_samples); + device->resampling_queue->add_input_samples(local_pts / double(TIMEBASE), audio.get(), num_samples); device->next_local_pts = local_pts + frame_length; return true; } @@ -266,6 +296,22 @@ 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::AudioDevice *AudioMixer::find_audio_device(DeviceSpec device) { switch (device.type) { @@ -326,6 +372,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; @@ -334,14 +398,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], @@ -350,84 +412,65 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin } } - // TODO: Move lo-cut etc. into each bus. vector samples_out, left, right; samples_out.resize(num_samples * 2); 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]); + apply_eq(bus_index, &samples_bus); - // TODO: We should measure post-fader. - deinterleave_samples(samples_bus, &left, &right); - measure_bus_levels(bus_index, left, right); - - float volume = from_db(fader_volume_db[bus_index]); - if (bus_index == 0) { - for (unsigned i = 0; i < num_samples * 2; ++i) { - samples_out[i] = samples_bus[i] * volume; - } - } else { - for (unsigned i = 0; i < num_samples * 2; ++i) { - samples_out[i] += samples_bus[i] * volume; - } - } - } - - // 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) { - locut.render(samples_out.data(), samples_out.size() / 2, locut_cutoff_hz * 2.0 * M_PI / OUTPUT_FREQUENCY, 0.5f); - } - - { - lock_guard lock(compressor_mutex); - - // Apply a level compressor to get the general level right. - // Basically, if it's over about -40 dBFS, we squeeze it down to that level - // (or more precisely, near it, since we don't use infinite ratio), - // then apply a makeup gain to get it to -14 dBFS. -14 dBFS is, of course, - // entirely arbitrary, but from practical tests with speech, it seems to - // put ut around -23 LUFS, so it's a reasonable starting point for later use. { - if (level_compressor_enabled) { + lock_guard lock(compressor_mutex); + + // Apply a level compressor to get the general level right. + // Basically, if it's over about -40 dBFS, we squeeze it down to that level + // (or more precisely, near it, since we don't use infinite ratio), + // then apply a makeup gain to get it to -14 dBFS. -14 dBFS is, of course, + // entirely arbitrary, but from practical tests with speech, it seems to + // put ut around -23 LUFS, so it's a reasonable starting point for later use. + if (level_compressor_enabled[bus_index]) { float threshold = 0.01f; // -40 dBFS. float ratio = 20.0f; float attack_time = 0.5f; float release_time = 20.0f; float makeup_gain = from_db(ref_level_dbfs - (-40.0f)); // +26 dB. - level_compressor.process(samples_out.data(), samples_out.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain); - gain_staging_db = to_db(level_compressor.get_attenuation() * makeup_gain); + level_compressor[bus_index]->process(samples_bus.data(), samples_bus.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain); + gain_staging_db[bus_index] = to_db(level_compressor[bus_index]->get_attenuation() * makeup_gain); } else { // Just apply the gain we already had. - float g = from_db(gain_staging_db); - for (size_t i = 0; i < samples_out.size(); ++i) { - samples_out[i] *= g; + float g = from_db(gain_staging_db[bus_index]); + for (size_t i = 0; i < samples_bus.size(); ++i) { + samples_bus[i] *= g; } } - } - #if 0 - printf("level=%f (%+5.2f dBFS) attenuation=%f (%+5.2f dB) end_result=%+5.2f dB\n", - level_compressor.get_level(), to_db(level_compressor.get_level()), - level_compressor.get_attenuation(), to_db(level_compressor.get_attenuation()), - to_db(level_compressor.get_level() * level_compressor.get_attenuation() * makeup_gain)); - #endif - - // float limiter_att, compressor_att; - - // The real compressor. - if (compressor_enabled) { - float threshold = from_db(compressor_threshold_dbfs); - float ratio = 20.0f; - float attack_time = 0.005f; - float release_time = 0.040f; - float makeup_gain = 2.0f; // +6 dB. - compressor.process(samples_out.data(), samples_out.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain); - // compressor_att = compressor.get_attenuation(); +#if 0 + printf("level=%f (%+5.2f dBFS) attenuation=%f (%+5.2f dB) end_result=%+5.2f dB\n", + level_compressor.get_level(), to_db(level_compressor.get_level()), + level_compressor.get_attenuation(), to_db(level_compressor.get_attenuation()), + to_db(level_compressor.get_level() * level_compressor.get_attenuation() * makeup_gain)); +#endif + + // The real compressor. + if (compressor_enabled[bus_index]) { + float threshold = from_db(compressor_threshold_dbfs[bus_index]); + float ratio = 20.0f; + float attack_time = 0.005f; + float release_time = 0.040f; + float makeup_gain = 2.0f; // +6 dB. + compressor[bus_index]->process(samples_bus.data(), samples_bus.size() / 2, threshold, ratio, attack_time, release_time, makeup_gain); + // compressor_att = compressor.get_attenuation(); + } } + add_bus_to_master(bus_index, samples_bus, &samples_out); + deinterleave_samples(samples_bus, &left, &right); + measure_bus_levels(bus_index, left, right); + } + + { + lock_guard lock(compressor_mutex); + // Finally a limiter at -4 dB (so, -10 dBFS) to take out the worst peaks only. // Note that since ratio is not infinite, we could go slightly higher than this. if (limiter_enabled) { @@ -443,7 +486,8 @@ vector AudioMixer::get_output(double pts, unsigned num_samples, Resamplin // printf("limiter=%+5.1f compressor=%+5.1f\n", to_db(limiter_att), to_db(compressor_att)); } - // At this point, we are most likely close to +0 LU, but all of our + // At this point, we are most likely close to +0 LU (at least if the + // faders sum to 0 dB and the compressors are on), but all of our // measurements have been on raw sample values, not R128 values. // So we have a final makeup gain to get us to +0 LU; the gain // adjustments required should be relatively small, and also, the @@ -488,12 +532,122 @@ 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()); + 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) { + // 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_inc = pow(volume / old_volume, 1.0 / num_samples); + volume = old_volume; + if (bus_index == 0) { + for (unsigned i = 0; i < num_samples; ++i) { + (*samples_out)[i * 2 + 0] = samples_bus[i * 2 + 0] * volume; + (*samples_out)[i * 2 + 1] = samples_bus[i * 2 + 1] * volume; + volume *= volume_inc; + } + } else { + for (unsigned i = 0; i < num_samples; ++i) { + (*samples_out)[i * 2 + 0] += samples_bus[i * 2 + 0] * volume; + (*samples_out)[i * 2 + 1] += samples_bus[i * 2 + 1] * volume; + volume *= volume_inc; + } + } + } else { + float volume = from_db(fader_volume_db[bus_index]); + if (bus_index == 0) { + for (unsigned i = 0; i < num_samples; ++i) { + (*samples_out)[i * 2 + 0] = samples_bus[i * 2 + 0] * volume; + (*samples_out)[i * 2 + 1] = samples_bus[i * 2 + 1] * volume; + } + } else { + for (unsigned i = 0; i < num_samples; ++i) { + (*samples_out)[i * 2 + 0] += samples_bus[i * 2 + 0] * volume; + (*samples_out)[i * 2 + 1] += samples_bus[i * 2 + 1] * volume; + } + } + } + + last_fader_volume_db[bus_index] = fader_volume_db[bus_index]; +} + void AudioMixer::measure_bus_levels(unsigned bus_index, const vector &left, const vector &right) { - const float *ptrs[] = { left.data(), right.data() }; - { - lock_guard lock(audio_measure_mutex); - bus_r128[bus_index]->process(left.size(), const_cast(ptrs)); + assert(left.size() == right.size()); + const float volume = 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 + }; + for (unsigned channel = 0; channel < 2; ++channel) { + // Compute the current value, including hold and falloff. + // The constants are borrowed from zita-mu1 by Fons Adriaensen. + static constexpr float hold_sec = 0.5f; + static constexpr float falloff_db_sec = 15.0f; // dB/sec falloff after hold. + float current_peak; + PeakHistory &history = peak_history[bus_index][channel]; + history.historic_peak = max(history.historic_peak, peak_levels[channel]); + if (history.age_seconds < hold_sec) { + current_peak = history.last_peak; + } else { + current_peak = history.last_peak * from_db(-falloff_db_sec * (history.age_seconds - hold_sec)); + } + + // See if we have a new peak to replace the old (possibly falling) one. + if (peak_levels[channel] > current_peak) { + history.last_peak = peak_levels[channel]; + history.age_seconds = 0.0f; // Not 100% correct, but more than good enough given our frame sizes. + current_peak = peak_levels[channel]; + } else { + history.age_seconds += float(left.size()) / OUTPUT_FREQUENCY; + } + history.current_level = peak_levels[channel]; + history.current_peak = current_peak; } } @@ -553,53 +707,64 @@ void AudioMixer::send_audio_level_callback() double loudness_range_low = r128.range_min(); double loudness_range_high = r128.range_max(); - vector bus_loudness; - bus_loudness.resize(input_mapping.buses.size()); - for (unsigned bus_index = 0; bus_index < bus_r128.size(); ++bus_index) { - bus_loudness[bus_index] = bus_r128[bus_index]->loudness_S(); + vector bus_levels; + bus_levels.resize(input_mapping.buses.size()); + { + lock_guard lock(compressor_mutex); + for (unsigned bus_index = 0; bus_index < bus_levels.size(); ++bus_index) { + bus_levels[bus_index].current_level_dbfs[0] = to_db(peak_history[bus_index][0].current_level); + bus_levels[bus_index].current_level_dbfs[1] = to_db(peak_history[bus_index][1].current_level); + bus_levels[bus_index].peak_level_dbfs[0] = to_db(peak_history[bus_index][0].current_peak); + bus_levels[bus_index].peak_level_dbfs[1] = to_db(peak_history[bus_index][1].current_peak); + bus_levels[bus_index].historic_peak_dbfs = to_db( + max(peak_history[bus_index][0].historic_peak, + peak_history[bus_index][1].historic_peak)); + bus_levels[bus_index].gain_staging_db = gain_staging_db[bus_index]; + if (compressor_enabled[bus_index]) { + bus_levels[bus_index].compressor_attenuation_db = -to_db(compressor[bus_index]->get_attenuation()); + } else { + bus_levels[bus_index].compressor_attenuation_db = 0.0; + } + } } - audio_level_callback(loudness_s, to_db(peak), bus_loudness, + audio_level_callback(loudness_s, to_db(peak), bus_levels, loudness_i, loudness_range_low, loudness_range_high, - gain_staging_db, to_db(final_makeup_gain), 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; 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::set_input_mapping(const InputMapping &new_input_mapping) @@ -619,26 +784,26 @@ 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); } } - - { - lock_guard lock(audio_measure_mutex); - bus_r128.resize(new_input_mapping.buses.size()); - for (unsigned bus_index = 0; bus_index < bus_r128.size(); ++bus_index) { - if (bus_r128[bus_index] == nullptr) { - bus_r128[bus_index].reset(new Ebu_r128_proc); - } - bus_r128[bus_index]->init(2, OUTPUT_FREQUENCY); + 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); } } @@ -650,3 +815,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;