]> git.sesse.net Git - nageru/blobdiff - audio_mixer.cpp
Write 1.4.0 changelog.
[nageru] / audio_mixer.cpp
index 506e78d50ede72820e08ba1acfecb6d6dbad8411..e4d4cff4b86222e41d6b0d8181b8d519a1f1b6a1 100644 (file)
@@ -1,19 +1,25 @@
 #include "audio_mixer.h"
 
 #include <assert.h>
-#include <endian.h>
 #include <bmusb/bmusb.h>
-#include <stdio.h>
 #include <endian.h>
-#include <cmath>
-#include <limits>
-#ifdef __SSE__
+#include <math.h>
+#ifdef __SSE2__
 #include <immintrin.h>
 #endif
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <algorithm>
+#include <chrono>
+#include <cmath>
+#include <cstddef>
+#include <limits>
+#include <utility>
 
 #include "db.h"
 #include "flags.h"
-#include "mixer.h"
 #include "state.pb.h"
 #include "timebase.h"
 
@@ -319,6 +325,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 +342,7 @@ AudioMixer::BusSettings AudioMixer::get_bus_settings(unsigned bus_index) const
        lock_guard<timed_mutex> 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,11 +358,13 @@ void AudioMixer::set_bus_settings(unsigned bus_index, const AudioMixer::BusSetti
 {
        lock_guard<timed_mutex> 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];
        }
        gain_staging_db[bus_index] = settings.gain_staging_db;
+       last_gain_staging_db[bus_index] = gain_staging_db[bus_index];
        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;
@@ -402,7 +412,7 @@ void AudioMixer::find_sample_src_from_device(const map<DeviceSpec, vector<float>
 void AudioMixer::fill_audio_bus(const map<DeviceSpec, vector<float>> &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);
@@ -438,6 +448,31 @@ vector<DeviceSpec> AudioMixer::get_active_devices() const
        return ret;
 }
 
+namespace {
+
+void apply_gain(float db, float last_db, vector<float> *samples)
+{
+       if (fabs(db - last_db) < 1e-3) {
+               // Constant over this frame.
+               const float gain = from_db(db);
+               for (size_t i = 0; i < samples->size(); ++i) {
+                       (*samples)[i] *= gain;
+               }
+       } else {
+               // We need to do a fade.
+               unsigned num_samples = samples->size() / 2;
+               float gain = from_db(last_db);
+               const float gain_inc = pow(from_db(db - last_db), 1.0 / num_samples);
+               for (size_t i = 0; i < num_samples; ++i) {
+                       (*samples)[i * 2 + 0] *= gain;
+                       (*samples)[i * 2 + 1] *= gain;
+                       gain *= gain_inc;
+               }
+       }
+}
+
+}  // namespace
+
 vector<float> AudioMixer::get_output(double pts, unsigned num_samples, ResamplingQueue::RateAdjustmentPolicy rate_adjustment_policy)
 {
        map<DeviceSpec, vector<float>> samples_card;
@@ -486,11 +521,11 @@ vector<float> AudioMixer::get_output(double pts, unsigned num_samples, Resamplin
                                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[bus_index]);
-                               for (size_t i = 0; i < samples_bus.size(); ++i) {
-                                       samples_bus[i] *= g;
-                               }
+                               float db = gain_staging_db[bus_index];
+                               float last_db = last_gain_staging_db[bus_index];
+                               apply_gain(db, last_db, &samples_bus);
                        }
+                       last_gain_staging_db[bus_index] = gain_staging_db[bus_index];
 
 #if 0
                        printf("level=%f (%+5.2f dBFS) attenuation=%f (%+5.2f dB) end_result=%+5.2f dB\n",
@@ -548,13 +583,12 @@ vector<float> 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
@@ -642,22 +676,7 @@ void AudioMixer::apply_eq(unsigned bus_index, vector<float> *samples_bus)
        assert(samples_bus->size() % 2 == 0);
        const unsigned num_samples = samples_bus->size() / 2;
 
-       if (fabs(mid_db - last_mid_db) < 1e-3) {
-               // Constant over this frame.
-               const float gain = from_db(mid_db);
-               for (size_t i = 0; i < samples_bus->size(); ++i) {
-                       (*samples_bus)[i] *= gain;
-               }
-       } else {
-               // We need to do a fade.
-               float gain = from_db(last_mid_db);
-               const float gain_inc = pow(from_db(mid_db - last_mid_db), 1.0 / num_samples);
-               for (size_t i = 0; i < num_samples; ++i) {
-                       (*samples_bus)[i * 2 + 0] *= gain;
-                       (*samples_bus)[i * 2 + 1] *= gain;
-                       gain *= gain_inc;
-               }
-       }
+       apply_gain(mid_db, last_mid_db, samples_bus);
 
        apply_filter_fade(&eq[bus_index][EQ_BAND_BASS], samples_bus->data(), num_samples, bass_freq_hz, bass_db - mid_db, last_bass_db - last_mid_db);
        apply_filter_fade(&eq[bus_index][EQ_BAND_TREBLE], samples_bus->data(), num_samples, treble_freq_hz, treble_db - mid_db, last_treble_db - last_mid_db);
@@ -672,13 +691,14 @@ void AudioMixer::add_bus_to_master(unsigned bus_index, const vector<float> &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<float>(last_fader_volume_db[bus_index], -90.0f));
-               float volume = from_db(max<float>(fader_volume_db[bus_index], -90.0f));
+               float volume = from_db(max<float>(new_volume_db, -90.0f));
 
                float volume_inc = pow(volume / old_volume, 1.0 / num_samples);
                volume = old_volume;
@@ -695,8 +715,8 @@ void AudioMixer::add_bus_to_master(unsigned bus_index, const vector<float> &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;
@@ -710,13 +730,13 @@ void AudioMixer::add_bus_to_master(unsigned bus_index, const vector<float> &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<float> &left, const vector<float> &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
@@ -976,6 +996,12 @@ InputMapping AudioMixer::get_input_mapping() const
        return input_mapping;
 }
 
+unsigned AudioMixer::num_buses() const
+{
+       lock_guard<timed_mutex> lock(audio_mutex);
+       return input_mapping.buses.size();
+}
+
 void AudioMixer::reset_peak(unsigned bus_index)
 {
        lock_guard<timed_mutex> lock(audio_mutex);