]> git.sesse.net Git - nageru/commitdiff
Add a final makeup gain, trying to set the level straight at +0 LU (more or less...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 12 Jan 2016 00:00:58 +0000 (01:00 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 12 Jan 2016 00:00:58 +0000 (01:00 +0100)
mainwindow.cpp
mainwindow.h
mixer.cpp
mixer.h
ui_mainwindow.ui

index 8cd0abdb2a9412dbc0fb7915341712be445ad7ce..01d9de143569ac57a45d17f9065bff2b41239cea 100644 (file)
@@ -114,6 +114,10 @@ void MainWindow::mixer_created(Mixer *mixer)
        connect(ui->gainstaging_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
                global_mixer->set_gain_staging_auto(state == Qt::Checked);
        });
+       connect(ui->makeup_gain_knob, &QAbstractSlider::sliderMoved, this, &MainWindow::final_makeup_gain_knob_changed);
+       connect(ui->makeup_gain_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
+               global_mixer->set_final_makeup_gain_auto(state == Qt::Checked);
+       });
 
        connect(ui->limiter_threshold_knob, &QDial::valueChanged, this, &MainWindow::limiter_threshold_knob_changed);
        connect(ui->compressor_threshold_knob, &QDial::valueChanged, this, &MainWindow::compressor_threshold_knob_changed);
@@ -124,7 +128,7 @@ void MainWindow::mixer_created(Mixer *mixer)
                global_mixer->set_compressor_enabled(state == Qt::Checked);
        });
        connect(ui->reset_meters_button, &QPushButton::clicked, this, &MainWindow::reset_meters_button_clicked);
-       mixer->set_audio_level_callback(bind(&MainWindow::audio_level_callback, this, _1, _2, _3, _4, _5, _6));
+       mixer->set_audio_level_callback(bind(&MainWindow::audio_level_callback, this, _1, _2, _3, _4, _5, _6, _7));
 }
 
 void MainWindow::mixer_shutting_down()
@@ -156,6 +160,16 @@ void MainWindow::gain_staging_knob_changed(int value)
        // The label will be updated by the audio level callback.
 }
 
+void MainWindow::final_makeup_gain_knob_changed(int value)
+{
+       ui->makeup_gain_auto_checkbox->setCheckState(Qt::Unchecked);
+
+       float gain_db = value * 0.1f;
+       global_mixer->set_final_makeup_gain_db(gain_db);
+
+       // The label will be updated by the audio level callback.
+}
+
 void MainWindow::cutoff_knob_changed(int value)
 {
        float octaves = value * 0.1f;
@@ -194,7 +208,7 @@ void MainWindow::reset_meters_button_clicked()
        ui->peak_display->setStyleSheet("");
 }
 
-void MainWindow::audio_level_callback(float level_lufs, float peak_db, float global_level_lufs, float range_low_lufs, float range_high_lufs, float gain_staging_db)
+void MainWindow::audio_level_callback(float level_lufs, float peak_db, float global_level_lufs, float range_low_lufs, float range_high_lufs, float gain_staging_db, float final_makeup_gain_db)
 {
        timeval now;
        gettimeofday(&now, nullptr);
@@ -224,6 +238,10 @@ void MainWindow::audio_level_callback(float level_lufs, float peak_db, float glo
                ui->gainstaging_knob->setValue(lrintf(gain_staging_db * 10.0f));
                snprintf(buf, sizeof(buf), "%+.1f dB", gain_staging_db);
                ui->gainstaging_db_display->setText(buf);
+
+               ui->makeup_gain_knob->setValue(lrintf(final_makeup_gain_db * 10.0f));
+               snprintf(buf, sizeof(buf), "%+.1f dB", final_makeup_gain_db);
+               ui->makeup_gain_db_display->setText(buf);
        });
 }
 
index 07bea73a58a0ea2a91c1626e8fd737e13e14c7e0..7a14b6e9b1593f83c9baa8a2ce131f153a4660f9 100644 (file)
@@ -40,6 +40,7 @@ public slots:
        void set_transition_names(std::vector<std::string> transition_names);
        void update_channel_name(Mixer::Output output);
        void gain_staging_knob_changed(int value);
+       void final_makeup_gain_knob_changed(int value);
        void cutoff_knob_changed(int value);
        void limiter_threshold_knob_changed(int value);
        void compressor_threshold_knob_changed(int value);
@@ -51,7 +52,7 @@ private:
        void set_white_balance(int channel_number, int x, int y);
 
        // Called from the mixer.
-       void audio_level_callback(float level_lufs, float peak_db, float global_level_lufs, float range_low_lufs, float range_high_lufs, float auto_gain_staging_db);
+       void audio_level_callback(float level_lufs, float peak_db, float global_level_lufs, float range_low_lufs, float range_high_lufs, float gain_staging_db, float final_makeup_gain_db);
        timeval last_audio_level_callback{0, 0};
 
        Ui::MainWindow *ui;
index 72022026f771056e0211ccac874e69c8ee0ed22a..f3ff4c385c1d46b4f3b03dc5832a6ba816a8587a 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
@@ -534,7 +534,7 @@ void Mixer::thread_func()
 
                        audio_level_callback(loudness_s, 20.0 * log10(peak),
                                             loudness_i, loudness_range_low, loudness_range_high,
-                                            gain_staging_db);
+                                            gain_staging_db, 20.0 * log10(final_makeup_gain));
                }
 
                for (unsigned card_index = 1; card_index < num_cards; ++card_index) {
@@ -716,7 +716,7 @@ void Mixer::process_audio_one_frame(int64_t frame_pts_int, int num_samples)
        // 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.
        {
-               unique_lock<mutex> lock(level_compressor_mutex);
+               unique_lock<mutex> lock(compressor_mutex);
                if (level_compressor_enabled) {
                        float threshold = 0.01f;   // -40 dBFS.
                        float ratio = 20.0f;
@@ -782,6 +782,46 @@ void Mixer::process_audio_one_frame(int64_t frame_pts_int, int num_samples)
                peak = max<float>(peak, find_peak(interpolated_samples_out.data(), out_stereo_samples * 2));
        }
 
+       // At this point, we are most likely close to +0 LU, 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
+       // offset shouldn't change much (only if the type of audio changes
+       // significantly). Thus, we shoot for updating this value basically
+       // “whenever we process buffers”, since the R128 calculation isn't exactly
+       // something we get out per-sample.
+       //
+       // Note that there's a feedback loop here, so we choose a very slow filter
+       // (half-time of 100 seconds).
+       double target_loudness_factor, alpha;
+       {
+               unique_lock<mutex> lock(compressor_mutex);
+               double loudness_lu = r128.loudness_M() - ref_level_lufs;
+               double current_makeup_lu = 20.0f * log10(final_makeup_gain);
+               target_loudness_factor = pow(10.0f, -loudness_lu / 20.0f);
+
+               // If we're outside +/- 5 LU uncorrected, 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) {
+                       alpha = 0.0;
+               } else {
+                       // Formula adapted from
+                       // https://en.wikipedia.org/wiki/Low-pass_filter#Simple_infinite_impulse_response_filter.
+                       const double half_time_s = 100.0;
+                       const double fc_mul_2pi_delta_t = 1.0 / (half_time_s * OUTPUT_FREQUENCY);
+                       alpha = fc_mul_2pi_delta_t / (fc_mul_2pi_delta_t + 1.0);
+               }
+
+               double m = final_makeup_gain;
+               for (size_t i = 0; i < samples_out.size(); i += 2) {
+                       samples_out[i + 0] *= m;
+                       samples_out[i + 1] *= m;
+                       m += (target_loudness_factor - m) * alpha;
+               }
+               final_makeup_gain = m;
+       }
+
        // Find R128 levels.
        vector<float> left, right;
        deinterleave_samples(samples_out, &left, &right);
diff --git a/mixer.h b/mixer.h
index f50eb58db5560f620b2fcdb1cc9097f4b6c5d1cb..ffae67a6e398687640c155ae12d8dd81c33f11e0 100644 (file)
--- a/mixer.h
+++ b/mixer.h
@@ -103,7 +103,7 @@ public:
 
        typedef std::function<void(float level_lufs, float peak_db,
                                   float global_level_lufs, float range_low_lufs, float range_high_lufs,
-                                  float gain_staging_db)> audio_level_callback_t;
+                                  float gain_staging_db, float final_makeup_gain_db)> audio_level_callback_t;
        void set_audio_level_callback(audio_level_callback_t callback)
        {
                audio_level_callback = callback;
@@ -171,17 +171,30 @@ public:
 
        void set_gain_staging_db(float gain_db)
        {
-               std::unique_lock<std::mutex> lock(level_compressor_mutex);
+               std::unique_lock<std::mutex> lock(compressor_mutex);
                level_compressor_enabled = false;
                gain_staging_db = gain_db;
        }
 
        void set_gain_staging_auto(bool enabled)
        {
-               std::unique_lock<std::mutex> lock(level_compressor_mutex);
+               std::unique_lock<std::mutex> lock(compressor_mutex);
                level_compressor_enabled = enabled;
        }
 
+       void set_final_makeup_gain_db(float gain_db)
+       {
+               std::unique_lock<std::mutex> lock(compressor_mutex);
+               final_makeup_gain_auto = false;
+               final_makeup_gain = pow(10.0f, gain_db / 20.0f);
+       }
+
+       void set_final_makeup_gain_auto(bool enabled)
+       {
+               std::unique_lock<std::mutex> lock(compressor_mutex);
+               final_makeup_gain_auto = enabled;
+       }
+
        void schedule_cut()
        {
                should_cut = true;
@@ -283,12 +296,12 @@ private:
        std::atomic<float> locut_cutoff_hz;
 
        // First compressor; takes us up to about -12 dBFS.
-       std::mutex level_compressor_mutex;
        StereoCompressor level_compressor;  // Under compressor_mutex. Used to set/override gain_staging_db if <level_compressor_enabled>.
        float gain_staging_db = 0.0f;  // Under compressor_mutex.
        bool level_compressor_enabled = true;  // Under compressor_mutex.
 
-       static constexpr float ref_level_dbfs = -14.0f;
+       static constexpr float ref_level_dbfs = -14.0f;  // Chosen so that we end up around 0 LU in practice.
+       static constexpr float ref_level_lufs = -23.0f;  // 0 LU, more or less by definition.
 
        StereoCompressor limiter;
        std::atomic<float> limiter_threshold_dbfs{ref_level_dbfs + 4.0f};   // 4 dB.
@@ -297,6 +310,9 @@ private:
        std::atomic<float> compressor_threshold_dbfs{ref_level_dbfs - 12.0f};  // -12 dB.
        std::atomic<bool> compressor_enabled{true};
 
+       double final_makeup_gain = 1.0;  // Under compressor_mutex. Read/write by the user. Note: Not in dB, we want the numeric precision so that we can change it slowly.
+       bool final_makeup_gain_auto = true;  // Under compressor_mutex.
+
        std::unique_ptr<ALSAOutput> alsa;
 
        struct AudioTask {
index bd10511d8ce800429dd7fd8a528b17eaefca9368..21cb7decfbf76df5efa8a452b081b778f8151a9d 100644 (file)
        </layout>
       </item>
       <item>
-       <layout class="QGridLayout" name="audiostrip">
+       <layout class="QGridLayout" name="audiostrip" columnstretch="0,0,0,0,0,0">
         <property name="bottomMargin">
          <number>0</number>
         </property>
           </property>
          </widget>
         </item>
-        <item row="1" column="4">
+        <item row="1" column="5">
          <spacer name="horizontalSpacer_2">
           <property name="orientation">
            <enum>Qt::Horizontal</enum>
           </property>
          </widget>
         </item>
+        <item row="3" column="3">
+         <widget class="QCheckBox" name="limiter_enabled">
+          <property name="text">
+           <string>Enabled</string>
+          </property>
+          <property name="checked">
+           <bool>true</bool>
+          </property>
+         </widget>
+        </item>
         <item row="0" column="0">
          <widget class="QLabel" name="locut_cutoff_label">
           <property name="text">
           </property>
          </widget>
         </item>
-        <item row="3" column="3">
-         <widget class="QCheckBox" name="limiter_enabled">
-          <property name="text">
-           <string>Enabled</string>
-          </property>
-          <property name="checked">
-           <bool>true</bool>
-          </property>
-         </widget>
-        </item>
         <item row="1" column="3">
          <widget class="QDial" name="limiter_threshold_knob">
           <property name="minimumSize">
           </property>
          </widget>
         </item>
+        <item row="0" column="2">
+         <widget class="QLabel" name="compressor_threshold_label">
+          <property name="text">
+           <string>Compr. threshold</string>
+          </property>
+         </widget>
+        </item>
         <item row="2" column="3">
          <widget class="QLabel" name="limiter_threshold_db_display">
           <property name="text">
           </property>
          </widget>
         </item>
-        <item row="0" column="2">
-         <widget class="QLabel" name="compressor_threshold_label">
+        <item row="3" column="2">
+         <widget class="QCheckBox" name="compressor_enabled">
           <property name="text">
-           <string>Compr. threshold</string>
+           <string>Enabled</string>
+          </property>
+          <property name="checked">
+           <bool>true</bool>
           </property>
          </widget>
         </item>
           </property>
          </widget>
         </item>
-        <item row="3" column="2">
-         <widget class="QCheckBox" name="compressor_enabled">
+        <item row="0" column="4">
+         <widget class="QLabel" name="makeup_gain_label">
           <property name="text">
-           <string>Enabled</string>
+           <string>Makeup gain</string>
+          </property>
+          <property name="alignment">
+           <set>Qt::AlignCenter</set>
+          </property>
+         </widget>
+        </item>
+        <item row="1" column="4">
+         <widget class="QDial" name="makeup_gain_knob">
+          <property name="minimumSize">
+           <size>
+            <width>64</width>
+            <height>64</height>
+           </size>
+          </property>
+          <property name="maximumSize">
+           <size>
+            <width>16777215</width>
+            <height>64</height>
+           </size>
+          </property>
+          <property name="minimum">
+           <number>-300</number>
+          </property>
+          <property name="maximum">
+           <number>300</number>
+          </property>
+          <property name="notchTarget">
+           <double>60.000000000000000</double>
+          </property>
+          <property name="notchesVisible">
+           <bool>true</bool>
+          </property>
+         </widget>
+        </item>
+        <item row="2" column="4">
+         <widget class="QLabel" name="makeup_gain_db_display">
+          <property name="text">
+           <string>-0.0 dB</string>
+          </property>
+          <property name="alignment">
+           <set>Qt::AlignCenter</set>
+          </property>
+         </widget>
+        </item>
+        <item row="3" column="4">
+         <widget class="QCheckBox" name="makeup_gain_auto_checkbox">
+          <property name="text">
+           <string>Auto</string>
           </property>
           <property name="checked">
            <bool>true</bool>