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);
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()
// 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;
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);
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);
});
}
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);
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;
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) {
// 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;
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);
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;
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;
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.
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 {
</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>