From: Steinar H. Gunderson Date: Tue, 12 Jan 2016 00:00:58 +0000 (+0100) Subject: Add a final makeup gain, trying to set the level straight at +0 LU (more or less... X-Git-Tag: 1.0.0~21 X-Git-Url: https://git.sesse.net/?p=nageru;a=commitdiff_plain;h=8fe6a683cb5bc9f04555c8cb9257f33c4d356ded Add a final makeup gain, trying to set the level straight at +0 LU (more or less; we cannot fully replicate the gating). --- diff --git a/mainwindow.cpp b/mainwindow.cpp index 8cd0abd..01d9de1 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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); }); } diff --git a/mainwindow.h b/mainwindow.h index 07bea73..7a14b6e 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -40,6 +40,7 @@ public slots: void set_transition_names(std::vector 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; diff --git a/mixer.cpp b/mixer.cpp index 7202202..f3ff4c3 100644 --- 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 lock(level_compressor_mutex); + unique_lock 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(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 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 left, right; deinterleave_samples(samples_out, &left, &right); diff --git a/mixer.h b/mixer.h index f50eb58..ffae67a 100644 --- a/mixer.h +++ b/mixer.h @@ -103,7 +103,7 @@ public: typedef std::function 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 lock(level_compressor_mutex); + std::unique_lock lock(compressor_mutex); level_compressor_enabled = false; gain_staging_db = gain_db; } void set_gain_staging_auto(bool enabled) { - std::unique_lock lock(level_compressor_mutex); + std::unique_lock lock(compressor_mutex); level_compressor_enabled = enabled; } + void set_final_makeup_gain_db(float gain_db) + { + std::unique_lock 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 lock(compressor_mutex); + final_makeup_gain_auto = enabled; + } + void schedule_cut() { should_cut = true; @@ -283,12 +296,12 @@ private: std::atomic 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 . 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 limiter_threshold_dbfs{ref_level_dbfs + 4.0f}; // 4 dB. @@ -297,6 +310,9 @@ private: std::atomic compressor_threshold_dbfs{ref_level_dbfs - 12.0f}; // -12 dB. std::atomic 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 alsa; struct AudioTask { diff --git a/ui_mainwindow.ui b/ui_mainwindow.ui index bd10511..21cb7de 100644 --- a/ui_mainwindow.ui +++ b/ui_mainwindow.ui @@ -426,7 +426,7 @@ - + 0 @@ -478,7 +478,7 @@ - + Qt::Horizontal @@ -536,6 +536,16 @@ + + + + Enabled + + + true + + + @@ -553,16 +563,6 @@ - - - - Enabled - - - true - - - @@ -604,6 +604,13 @@ + + + + Compr. threshold + + + @@ -645,10 +652,13 @@ - - + + - Compr. threshold + Enabled + + + true @@ -662,10 +672,58 @@ - - + + - Enabled + Makeup gain + + + Qt::AlignCenter + + + + + + + + 64 + 64 + + + + + 16777215 + 64 + + + + -300 + + + 300 + + + 60.000000000000000 + + + true + + + + + + + -0.0 dB + + + Qt::AlignCenter + + + + + + + Auto true