1 #include "mainwindow.h"
11 #include <QInputDialog>
12 #include <QKeySequence>
15 #include <QPushButton>
16 #include <QResizeEvent>
21 #include "aboutdialog.h"
22 #include "disk_space_estimator.h"
25 #include "input_mapping_dialog.h"
28 #include "post_to_main_thread.h"
29 #include "ui_audio_miniview.h"
30 #include "ui_audio_expanded_view.h"
31 #include "ui_display.h"
32 #include "ui_mainwindow.h"
38 using namespace std::chrono;
39 using namespace std::placeholders;
41 Q_DECLARE_METATYPE(std::string);
42 Q_DECLARE_METATYPE(std::vector<std::string>);
44 MainWindow *global_mainwindow = nullptr;
48 void schedule_cut_signal(int ignored)
50 global_mixer->schedule_cut();
53 void quit_signal(int ignored)
55 global_mainwindow->close();
58 void slave_knob(QDial *master, QDial *slave)
60 QWidget::connect(master, &QDial::valueChanged, [slave](int value){
61 slave->blockSignals(true);
62 slave->setValue(value);
63 slave->blockSignals(false);
65 QWidget::connect(slave, &QDial::valueChanged, [master](int value){
66 master->setValue(value);
70 void slave_checkbox(QCheckBox *master, QCheckBox *slave)
72 QWidget::connect(master, &QCheckBox::stateChanged, [slave](int state){
73 slave->blockSignals(true);
74 slave->setCheckState(Qt::CheckState(state));
75 slave->blockSignals(false);
77 QWidget::connect(slave, &QCheckBox::stateChanged, [master](int state){
78 master->setCheckState(Qt::CheckState(state));
82 void slave_fader(NonLinearFader *master, NonLinearFader *slave)
84 QWidget::connect(master, &NonLinearFader::dbValueChanged, [slave](double value) {
85 slave->blockSignals(true);
86 slave->setDbValue(value);
87 slave->blockSignals(false);
89 QWidget::connect(slave, &NonLinearFader::dbValueChanged, [master](double value){
90 master->setDbValue(value);
94 constexpr unsigned DB_NO_FLAGS = 0x0;
95 constexpr unsigned DB_WITH_SIGN = 0x1;
96 constexpr unsigned DB_BARE = 0x2;
98 string format_db(double db, unsigned flags)
101 if (flags & DB_WITH_SIGN) {
104 snprintf(buf, sizeof(buf), "%+.1f", db);
106 } else if (db < 0.0) {
109 // Should never happen, really.
115 snprintf(buf, sizeof(buf), "%.1f", db);
117 } else if (db < 0.0) {
120 // Should never happen, really.
124 if (!(flags & DB_BARE)) {
130 void set_peak_label(QLabel *peak_label, float peak_db)
132 peak_label->setText(QString::fromStdString(format_db(peak_db, DB_BARE)));
134 // -0.1 dBFS is EBU peak limit. We use it consistently, even for the bus meters
135 // (which don't calculate interpolate peak, and in general don't follow EBU recommendations).
136 if (peak_db > -0.1f) {
137 peak_label->setStyleSheet("QLabel { background-color: red; color: white; }");
139 peak_label->setStyleSheet("");
145 MainWindow::MainWindow()
146 : ui(new Ui::MainWindow)
148 global_mainwindow = this;
151 global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
152 disk_free_label = new QLabel(this);
153 disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
154 ui->menuBar->setCornerWidget(disk_free_label);
156 ui->me_live->set_output(Mixer::OUTPUT_LIVE);
157 ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
160 connect(ui->cut_action, &QAction::triggered, this, &MainWindow::cut_triggered);
161 connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
162 connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered);
163 connect(ui->input_mapping_action, &QAction::triggered, this, &MainWindow::input_mapping_triggered);
165 if (global_flags.x264_video_to_http) {
166 connect(ui->x264_bitrate_action, &QAction::triggered, this, &MainWindow::x264_bitrate_triggered);
168 ui->x264_bitrate_action->setEnabled(false);
171 // Hook up the transition buttons. (Keyboard shortcuts are set in set_transition_names().)
172 // TODO: Make them dynamic.
173 connect(ui->transition_btn1, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 0));
174 connect(ui->transition_btn2, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 1));
175 connect(ui->transition_btn3, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 2));
178 transition_btn1 = ui->transition_btn1;
179 transition_btn2 = ui->transition_btn2;
180 transition_btn3 = ui->transition_btn3;
181 qRegisterMetaType<string>("std::string");
182 qRegisterMetaType<vector<string>>("std::vector<std::string>");
183 connect(ui->me_live, &GLWidget::transition_names_updated, this, &MainWindow::set_transition_names);
184 qRegisterMetaType<Mixer::Output>("Mixer::Output");
186 // Hook up the prev/next buttons on the audio views.
187 connect(ui->compact_prev_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 1));
188 connect(ui->compact_next_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 1));
189 connect(ui->full_prev_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 0));
190 connect(ui->full_next_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 0));
192 last_audio_level_callback = steady_clock::now() - seconds(1);
195 void MainWindow::resizeEvent(QResizeEvent* event)
197 QMainWindow::resizeEvent(event);
199 // Ask for a relayout, but only after the event loop is done doing relayout
200 // on everything else.
201 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
204 void MainWindow::mixer_created(Mixer *mixer)
206 // Make the previews.
207 unsigned num_previews = mixer->get_num_channels();
209 for (unsigned i = 0; i < num_previews; ++i) {
210 Mixer::Output output = Mixer::Output(Mixer::OUTPUT_INPUT0 + i);
212 QWidget *preview = new QWidget(this);
213 Ui::Display *ui_display = new Ui::Display;
214 ui_display->setupUi(preview);
215 ui_display->label->setText(mixer->get_channel_name(output).c_str());
216 ui_display->display->set_output(output);
217 ui->preview_displays->insertWidget(previews.size(), preview, 1);
218 previews.push_back(ui_display);
220 // Hook up the click.
221 connect(ui_display->display, &GLWidget::clicked, bind(&MainWindow::channel_clicked, this, i));
223 // Let the theme update the text whenever the resolution or color changed.
224 connect(ui_display->display, &GLWidget::name_updated, this, &MainWindow::update_channel_name);
225 connect(ui_display->display, &GLWidget::color_updated, this, &MainWindow::update_channel_color);
227 // Hook up the keyboard key.
228 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
229 connect(shortcut, &QShortcut::activated, bind(&MainWindow::channel_clicked, this, i));
231 // Hook up the white balance button (irrelevant if invisible).
232 ui_display->wb_button->setVisible(mixer->get_supports_set_wb(output));
233 connect(ui_display->wb_button, &QPushButton::clicked, bind(&MainWindow::wb_button_clicked, this, i));
236 setup_audio_miniview();
237 setup_audio_expanded_view();
238 global_audio_mixer->set_state_changed_callback(bind(&MainWindow::audio_state_changed, this));
240 slave_knob(ui->locut_cutoff_knob, ui->locut_cutoff_knob_2);
241 slave_knob(ui->limiter_threshold_knob, ui->limiter_threshold_knob_2);
242 slave_knob(ui->makeup_gain_knob, ui->makeup_gain_knob_2);
243 slave_checkbox(ui->makeup_gain_auto_checkbox, ui->makeup_gain_auto_checkbox_2);
244 slave_checkbox(ui->limiter_enabled, ui->limiter_enabled_2);
246 // TODO: Fetch all of the values these for completeness,
247 // not just the enable knobs implied by flags.
249 // TODO: Reenable for simple audio.
250 ui->locut_enabled->setChecked(global_audio_mixer->get_locut_enabled(0));
251 connect(ui->locut_enabled, &QCheckBox::stateChanged, [this](int state){
252 global_audio_mixer->set_locut_enabled(0, state == Qt::Checked);
254 ui->gainstaging_knob->setValue(global_audio_mixer->get_gain_staging_db());
255 ui->gainstaging_auto_checkbox->setChecked(global_audio_mixer->get_gain_staging_auto());
256 ui->compressor_enabled->setChecked(global_audio_mixer->get_compressor_enabled());
257 connect(ui->gainstaging_knob, &QAbstractSlider::valueChanged, this, &MainWindow::gain_staging_knob_changed);
258 connect(ui->gainstaging_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
259 global_audio_mixer->set_gain_staging_auto(state == Qt::Checked);
261 ui->compressor_threshold_db_display->setText(
262 QString::fromStdString(format_db(mixer->get_audio_mixer()->get_compressor_threshold_dbfs(), DB_WITH_SIGN)));
263 ui->compressor_threshold_db_display->setText(buf);
264 connect(ui->compressor_threshold_knob, &QDial::valueChanged, this, &MainWindow::compressor_threshold_knob_changed);
265 connect(ui->compressor_enabled, &QCheckBox::stateChanged, [this](int state){
266 global_audio_mixer->set_compressor_enabled(state == Qt::Checked);
269 ui->locut_enabled->setVisible(false);
270 ui->gainstaging_label->setVisible(false);
271 ui->gainstaging_knob->setVisible(false);
272 ui->gainstaging_db_display->setVisible(false);
273 ui->gainstaging_auto_checkbox->setVisible(false);
274 ui->compressor_threshold_label->setVisible(false);
275 ui->compressor_threshold_knob->setVisible(false);
276 ui->compressor_threshold_db_display->setVisible(false);
277 ui->compressor_enabled->setVisible(false);
279 ui->limiter_enabled->setChecked(global_audio_mixer->get_limiter_enabled());
280 ui->makeup_gain_auto_checkbox->setChecked(global_audio_mixer->get_final_makeup_gain_auto());
282 QString limiter_threshold_label(
283 QString::fromStdString(format_db(mixer->get_audio_mixer()->get_limiter_threshold_dbfs(), DB_WITH_SIGN)));
284 ui->limiter_threshold_db_display->setText(limiter_threshold_label);
285 ui->limiter_threshold_db_display_2->setText(limiter_threshold_label);
287 connect(ui->locut_cutoff_knob, &QDial::valueChanged, this, &MainWindow::cutoff_knob_changed);
288 cutoff_knob_changed(ui->locut_cutoff_knob->value());
290 connect(ui->makeup_gain_knob, &QAbstractSlider::valueChanged, this, &MainWindow::final_makeup_gain_knob_changed);
291 connect(ui->makeup_gain_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
292 global_audio_mixer->set_final_makeup_gain_auto(state == Qt::Checked);
295 connect(ui->limiter_threshold_knob, &QDial::valueChanged, this, &MainWindow::limiter_threshold_knob_changed);
296 connect(ui->limiter_enabled, &QCheckBox::stateChanged, [this](int state){
297 global_audio_mixer->set_limiter_enabled(state == Qt::Checked);
299 connect(ui->reset_meters_button, &QPushButton::clicked, this, &MainWindow::reset_meters_button_clicked);
300 mixer->get_audio_mixer()->set_audio_level_callback(bind(&MainWindow::audio_level_callback, this, _1, _2, _3, _4, _5, _6, _7, _8));
302 struct sigaction act;
303 memset(&act, 0, sizeof(act));
304 act.sa_handler = schedule_cut_signal;
305 act.sa_flags = SA_RESTART;
306 sigaction(SIGHUP, &act, nullptr);
308 // Mostly for debugging. Don't override SIGINT, that's so evil if
309 // shutdown isn't instant.
310 memset(&act, 0, sizeof(act));
311 act.sa_handler = quit_signal;
312 act.sa_flags = SA_RESTART;
313 sigaction(SIGUSR1, &act, nullptr);
316 void MainWindow::setup_audio_miniview()
318 // Remove any existing channels.
319 for (QLayoutItem *item; (item = ui->faders->takeAt(0)) != nullptr; ) {
320 delete item->widget();
323 audio_miniviews.clear();
325 // Set up brand new ones from the input mapping.
326 InputMapping mapping = global_audio_mixer->get_input_mapping();
327 audio_miniviews.resize(mapping.buses.size());
328 for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
329 QWidget *channel = new QWidget(this);
330 Ui::AudioMiniView *ui_audio_miniview = new Ui::AudioMiniView;
331 ui_audio_miniview->setupUi(channel);
332 ui_audio_miniview->bus_desc_label->setFullText(
333 QString::fromStdString(mapping.buses[bus_index].name));
334 audio_miniviews[bus_index] = ui_audio_miniview;
336 // Set up the peak meter.
337 VUMeter *peak_meter = ui_audio_miniview->peak_meter;
338 peak_meter->set_min_level(-30.0f);
339 peak_meter->set_max_level(0.0f);
340 peak_meter->set_ref_level(0.0f);
342 // TODO: Set the fader position.
343 ui->faders->addWidget(channel);
345 connect(ui_audio_miniview->fader, &NonLinearFader::dbValueChanged,
346 bind(&MainWindow::mini_fader_changed, this, bus_index, _1));
347 connect(ui_audio_miniview->peak_display_label, &ClickableLabel::clicked,
349 global_audio_mixer->reset_peak(bus_index);
354 void MainWindow::setup_audio_expanded_view()
356 // Remove any existing channels.
357 for (QLayoutItem *item; (item = ui->buses->takeAt(0)) != nullptr; ) {
358 delete item->widget();
361 audio_expanded_views.clear();
363 // Set up brand new ones from the input mapping.
364 InputMapping mapping = global_audio_mixer->get_input_mapping();
365 audio_expanded_views.resize(mapping.buses.size());
366 for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
367 QWidget *channel = new QWidget(this);
368 Ui::AudioExpandedView *ui_audio_expanded_view = new Ui::AudioExpandedView;
369 ui_audio_expanded_view->setupUi(channel);
370 ui_audio_expanded_view->bus_desc_label->setFullText(
371 QString::fromStdString(mapping.buses[bus_index].name));
372 audio_expanded_views[bus_index] = ui_audio_expanded_view;
373 update_eq_label(bus_index, EQ_BAND_TREBLE, global_audio_mixer->get_eq(bus_index, EQ_BAND_TREBLE));
374 update_eq_label(bus_index, EQ_BAND_MID, global_audio_mixer->get_eq(bus_index, EQ_BAND_MID));
375 update_eq_label(bus_index, EQ_BAND_BASS, global_audio_mixer->get_eq(bus_index, EQ_BAND_BASS));
376 // TODO: Set the fader position.
377 ui->buses->addWidget(channel);
379 ui_audio_expanded_view->locut_enabled->setChecked(global_audio_mixer->get_locut_enabled(bus_index));
380 connect(ui_audio_expanded_view->locut_enabled, &QCheckBox::stateChanged, [this, bus_index](int state){
381 global_audio_mixer->set_locut_enabled(bus_index, state == Qt::Checked);
384 connect(ui_audio_expanded_view->treble_knob, &QDial::valueChanged,
385 bind(&MainWindow::eq_knob_changed, this, bus_index, EQ_BAND_TREBLE, _1));
386 connect(ui_audio_expanded_view->mid_knob, &QDial::valueChanged,
387 bind(&MainWindow::eq_knob_changed, this, bus_index, EQ_BAND_MID, _1));
388 connect(ui_audio_expanded_view->bass_knob, &QDial::valueChanged,
389 bind(&MainWindow::eq_knob_changed, this, bus_index, EQ_BAND_BASS, _1));
391 ui_audio_expanded_view->gainstaging_knob->setValue(global_audio_mixer->get_gain_staging_db(bus_index));
392 ui_audio_expanded_view->gainstaging_auto_checkbox->setChecked(global_audio_mixer->get_gain_staging_auto(bus_index));
393 ui_audio_expanded_view->compressor_enabled->setChecked(global_audio_mixer->get_compressor_enabled(bus_index));
395 connect(ui_audio_expanded_view->gainstaging_knob, &QAbstractSlider::valueChanged, bind(&MainWindow::gain_staging_knob_changed, this, bus_index, _1));
396 connect(ui_audio_expanded_view->gainstaging_auto_checkbox, &QCheckBox::stateChanged, [this, bus_index](int state){
397 global_audio_mixer->set_gain_staging_auto(bus_index, state == Qt::Checked);
400 connect(ui_audio_expanded_view->compressor_threshold_knob, &QDial::valueChanged, bind(&MainWindow::compressor_threshold_knob_changed, this, bus_index, _1));
401 connect(ui_audio_expanded_view->compressor_enabled, &QCheckBox::stateChanged, [this, bus_index](int state){
402 global_audio_mixer->set_compressor_enabled(bus_index, state == Qt::Checked);
405 slave_fader(audio_miniviews[bus_index]->fader, ui_audio_expanded_view->fader);
407 // Set up the peak meter.
408 VUMeter *peak_meter = ui_audio_expanded_view->peak_meter;
409 peak_meter->set_min_level(-30.0f);
410 peak_meter->set_max_level(0.0f);
411 peak_meter->set_ref_level(0.0f);
413 connect(ui_audio_expanded_view->peak_display_label, &ClickableLabel::clicked,
415 global_audio_mixer->reset_peak(bus_index);
418 // Set up the compression attenuation meter.
419 VUMeter *reduction_meter = ui_audio_expanded_view->reduction_meter;
420 reduction_meter->set_min_level(0.0f);
421 reduction_meter->set_max_level(10.0f);
422 reduction_meter->set_ref_level(0.0f);
423 reduction_meter->set_flip(true);
426 update_cutoff_labels(global_audio_mixer->get_locut_cutoff());
429 void MainWindow::mixer_shutting_down()
431 ui->me_live->clean_context();
432 ui->me_preview->clean_context();
433 for (Ui::Display *display : previews) {
434 display->display->clean_context();
438 void MainWindow::cut_triggered()
440 global_mixer->schedule_cut();
443 void MainWindow::x264_bitrate_triggered()
446 int new_bitrate = QInputDialog::getInt(this, "Change x264 bitrate", "Choose new bitrate for x264 HTTP output (from 100–100,000 kbit/sec):", global_flags.x264_bitrate, /*min=*/100, /*max=*/100000, /*step=*/100, &ok);
447 if (ok && new_bitrate >= 100 && new_bitrate <= 100000) {
448 global_flags.x264_bitrate = new_bitrate;
449 global_mixer->change_x264_bitrate(new_bitrate);
453 void MainWindow::exit_triggered()
458 void MainWindow::about_triggered()
460 AboutDialog().exec();
463 void MainWindow::input_mapping_triggered()
465 if (InputMappingDialog().exec() == QDialog::Accepted) {
466 setup_audio_miniview();
467 setup_audio_expanded_view();
471 void MainWindow::gain_staging_knob_changed(unsigned bus_index, int value)
473 if (bus_index == 0) {
474 ui->gainstaging_auto_checkbox->setCheckState(Qt::Unchecked);
476 if (bus_index < audio_expanded_views.size()) {
477 audio_expanded_views[bus_index]->gainstaging_auto_checkbox->setCheckState(Qt::Unchecked);
480 float gain_db = value * 0.1f;
481 global_audio_mixer->set_gain_staging_db(bus_index, gain_db);
483 // The label will be updated by the audio level callback.
486 void MainWindow::final_makeup_gain_knob_changed(int value)
488 ui->makeup_gain_auto_checkbox->setCheckState(Qt::Unchecked);
490 float gain_db = value * 0.1f;
491 global_audio_mixer->set_final_makeup_gain_db(gain_db);
493 // The label will be updated by the audio level callback.
496 void MainWindow::cutoff_knob_changed(int value)
498 float octaves = value * 0.1f;
499 float cutoff_hz = 20.0 * pow(2.0, octaves);
500 global_audio_mixer->set_locut_cutoff(cutoff_hz);
501 update_cutoff_labels(cutoff_hz);
504 void MainWindow::update_cutoff_labels(float cutoff_hz)
507 snprintf(buf, sizeof(buf), "%ld Hz", lrintf(cutoff_hz));
508 ui->locut_cutoff_display->setText(buf);
509 ui->locut_cutoff_display_2->setText(buf);
511 for (unsigned bus_index = 0; bus_index < audio_expanded_views.size(); ++bus_index) {
512 audio_expanded_views[bus_index]->locut_enabled->setText(
513 QString("Lo-cut: ") + buf);
517 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
520 if (estimated_seconds_left < 60.0) {
521 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
522 } else if (estimated_seconds_left < 1800.0) { // Less than half an hour: Xm Ys (red).
523 int s = lrintf(estimated_seconds_left);
526 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
527 } else if (estimated_seconds_left < 3600.0) { // Less than an hour: Xm.
528 int m = lrintf(estimated_seconds_left / 60.0);
529 snprintf(time_str, sizeof(time_str), "%dm", m);
530 } else if (estimated_seconds_left < 36000.0) { // Less than ten hours: Xh Ym.
531 int m = lrintf(estimated_seconds_left / 60.0);
534 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
535 } else { // More than ten hours: Xh.
536 int h = lrintf(estimated_seconds_left / 3600.0);
537 snprintf(time_str, sizeof(time_str), "%dh", h);
540 snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
542 std::string label = buf;
544 post_to_main_thread([this, label]{
545 disk_free_label->setText(QString::fromStdString(label));
546 ui->menuBar->setCornerWidget(disk_free_label); // Need to set this again for the sizing to get right.
550 void MainWindow::eq_knob_changed(unsigned bus_index, EQBand band, int value)
552 float gain_db = value * 0.1f;
553 global_audio_mixer->set_eq(bus_index, band, gain_db);
555 update_eq_label(bus_index, band, gain_db);
558 void MainWindow::update_eq_label(unsigned bus_index, EQBand band, float gain_db)
560 Ui::AudioExpandedView *view = audio_expanded_views[bus_index];
561 string db_string = format_db(gain_db, DB_WITH_SIGN);
564 view->treble_label->setText(QString::fromStdString("Treble: " + db_string));
567 view->mid_label->setText(QString::fromStdString("Mid: " + db_string));
570 view->bass_label->setText(QString::fromStdString("Bass: " + db_string));
577 void MainWindow::limiter_threshold_knob_changed(int value)
579 float threshold_dbfs = value * 0.1f;
580 global_audio_mixer->set_limiter_threshold_dbfs(threshold_dbfs);
581 ui->limiter_threshold_db_display->setText(
582 QString::fromStdString(format_db(threshold_dbfs, DB_WITH_SIGN)));
583 ui->limiter_threshold_db_display_2->setText(
584 QString::fromStdString(format_db(threshold_dbfs, DB_WITH_SIGN)));
587 void MainWindow::compressor_threshold_knob_changed(unsigned bus_index, int value)
589 float threshold_dbfs = value * 0.1f;
590 global_audio_mixer->set_compressor_threshold_dbfs(bus_index, threshold_dbfs);
592 QString label(QString::fromStdString(format_db(threshold_dbfs, DB_WITH_SIGN)));
593 if (bus_index == 0) {
594 ui->compressor_threshold_db_display->setText(label);
596 if (bus_index < audio_expanded_views.size()) {
597 audio_expanded_views[bus_index]->compressor_threshold_db_display->setText(label);
601 void MainWindow::mini_fader_changed(int bus, double volume_db)
603 QString label(QString::fromStdString(format_db(volume_db, DB_WITH_SIGN)));
604 audio_miniviews[bus]->fader_label->setText(label);
605 audio_expanded_views[bus]->fader_label->setText(label);
607 global_audio_mixer->set_fader_volume(bus, volume_db);
610 void MainWindow::reset_meters_button_clicked()
612 global_audio_mixer->reset_meters();
613 ui->peak_display->setText(QString::fromStdString(format_db(-HUGE_VAL, DB_WITH_SIGN | DB_BARE)));
614 ui->peak_display->setStyleSheet("");
617 void MainWindow::audio_level_callback(float level_lufs, float peak_db, vector<AudioMixer::BusLevel> bus_levels,
618 float global_level_lufs,
619 float range_low_lufs, float range_high_lufs,
620 float final_makeup_gain_db,
623 steady_clock::time_point now = steady_clock::now();
625 // The meters are somewhat inefficient to update. Only update them
626 // every 100 ms or so (we get updates every 5–20 ms). Note that this
627 // means that the digital peak meters are ever so slightly too low
628 // (each update won't be a faithful representation of the highest peak
629 // since the previous update, since there are frames we won't draw),
630 // but the _peak_ of the peak meters will be correct (it's tracked in
631 // AudioMixer, not here), and that's much more important.
632 double last_update_age = duration<double>(now - last_audio_level_callback).count();
633 if (last_update_age < 0.100) {
636 last_audio_level_callback = now;
638 post_to_main_thread([=]() {
639 ui->vu_meter->set_level(level_lufs);
640 for (unsigned bus_index = 0; bus_index < bus_levels.size(); ++bus_index) {
641 if (bus_index < audio_miniviews.size()) {
642 const AudioMixer::BusLevel &level = bus_levels[bus_index];
643 Ui::AudioMiniView *miniview = audio_miniviews[bus_index];
644 miniview->peak_meter->set_level(
645 level.current_level_dbfs[0], level.current_level_dbfs[1]);
646 miniview->peak_meter->set_peak(
647 level.peak_level_dbfs[0], level.peak_level_dbfs[1]);
648 set_peak_label(miniview->peak_display_label, level.historic_peak_dbfs);
650 Ui::AudioExpandedView *view = audio_expanded_views[bus_index];
651 view->peak_meter->set_level(
652 level.current_level_dbfs[0], level.current_level_dbfs[1]);
653 view->peak_meter->set_peak(
654 level.peak_level_dbfs[0], level.peak_level_dbfs[1]);
655 view->reduction_meter->set_level(level.compressor_attenuation_db);
656 view->gainstaging_knob->blockSignals(true);
657 view->gainstaging_knob->setValue(lrintf(level.gain_staging_db * 10.0f));
658 view->gainstaging_knob->blockSignals(false);
659 view->gainstaging_db_display->setText(
661 QString::fromStdString(format_db(level.gain_staging_db, DB_WITH_SIGN)));
662 set_peak_label(view->peak_display_label, level.historic_peak_dbfs);
665 ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
666 ui->correlation_meter->set_correlation(correlation);
668 ui->peak_display->setText(QString::fromStdString(format_db(peak_db, DB_BARE)));
669 set_peak_label(ui->peak_display, peak_db);
671 // NOTE: Will be invisible when using multitrack audio.
672 ui->gainstaging_knob->blockSignals(true);
673 ui->gainstaging_knob->setValue(lrintf(bus_levels[0].gain_staging_db * 10.0f));
674 ui->gainstaging_knob->blockSignals(false);
675 ui->gainstaging_db_display->setText(
676 QString::fromStdString(format_db(bus_levels[0].gain_staging_db, DB_WITH_SIGN)));
678 ui->makeup_gain_knob->blockSignals(true);
679 ui->makeup_gain_knob->setValue(lrintf(final_makeup_gain_db * 10.0f));
680 ui->makeup_gain_knob->blockSignals(false);
681 ui->makeup_gain_db_display->setText(
682 QString::fromStdString(format_db(final_makeup_gain_db, DB_WITH_SIGN)));
683 ui->makeup_gain_db_display_2->setText(
684 QString::fromStdString(format_db(final_makeup_gain_db, DB_WITH_SIGN)));
688 void MainWindow::relayout()
690 int height = ui->vertical_layout->geometry().height();
692 double remaining_height = height;
694 // Allocate the height; the most important part is to keep the main displays
695 // at 16:9 if at all possible.
696 double me_width = ui->me_preview->width();
697 double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height() + ui->preview_vertical_layout->spacing();
699 // TODO: Scale the widths when we need to do this.
700 if (me_height / double(height) > 0.8) {
701 me_height = height * 0.8;
703 remaining_height -= me_height + ui->vertical_layout->spacing();
705 // Space between the M/E displays and the audio strip.
706 remaining_height -= ui->vertical_layout->spacing();
708 // The label above the audio strip.
709 double compact_label_height = ui->compact_label->geometry().height() +
710 ui->compact_audio_layout->spacing();
711 remaining_height -= compact_label_height;
713 // The previews will be constrained by the remaining height, and the width.
714 double preview_label_height = previews[0]->title_bar->geometry().height() +
715 previews[0]->main_vertical_layout->spacing();
716 int preview_total_width = ui->preview_displays->geometry().width() - (previews.size() - 1) * ui->preview_displays->spacing();
717 double preview_height = min(remaining_height - preview_label_height, (preview_total_width / double(previews.size())) * 9.0 / 16.0);
718 remaining_height -= preview_height + preview_label_height + ui->vertical_layout->spacing();
720 ui->vertical_layout->setStretch(0, lrintf(me_height));
721 ui->vertical_layout->setStretch(1,
722 lrintf(compact_label_height) +
723 lrintf(remaining_height) +
724 lrintf(preview_height + preview_label_height)); // Audio strip and previews together.
726 ui->compact_audio_layout->setStretch(0, lrintf(compact_label_height));
727 ui->compact_audio_layout->setStretch(1, lrintf(remaining_height)); // Audio strip.
728 ui->compact_audio_layout->setStretch(2, lrintf(preview_height + preview_label_height));
730 // Set the widths for the previews.
731 double preview_width = preview_height * 16.0 / 9.0;
732 for (unsigned i = 0; i < previews.size(); ++i) {
733 ui->preview_displays->setStretch(i, lrintf(preview_width));
736 // The preview horizontal spacer.
737 double remaining_preview_width = preview_total_width - previews.size() * preview_width;
738 ui->preview_displays->setStretch(previews.size(), lrintf(remaining_preview_width));
741 void MainWindow::set_transition_names(vector<string> transition_names)
743 if (transition_names.size() < 1 || transition_names[0].empty()) {
744 transition_btn1->setText(QString(""));
746 transition_btn1->setText(QString::fromStdString(transition_names[0] + " (J)"));
747 ui->transition_btn1->setShortcut(QKeySequence("J"));
749 if (transition_names.size() < 2 || transition_names[1].empty()) {
750 transition_btn2->setText(QString(""));
752 transition_btn2->setText(QString::fromStdString(transition_names[1] + " (K)"));
753 ui->transition_btn2->setShortcut(QKeySequence("K"));
755 if (transition_names.size() < 3 || transition_names[2].empty()) {
756 transition_btn3->setText(QString(""));
758 transition_btn3->setText(QString::fromStdString(transition_names[2] + " (L)"));
759 ui->transition_btn3->setShortcut(QKeySequence("L"));
763 void MainWindow::update_channel_name(Mixer::Output output, const string &name)
765 if (output >= Mixer::OUTPUT_INPUT0) {
766 unsigned channel = output - Mixer::OUTPUT_INPUT0;
767 previews[channel]->label->setText(name.c_str());
771 void MainWindow::update_channel_color(Mixer::Output output, const string &color)
773 if (output >= Mixer::OUTPUT_INPUT0) {
774 unsigned channel = output - Mixer::OUTPUT_INPUT0;
775 previews[channel]->frame->setStyleSheet(QString::fromStdString("background-color:" + color));
779 void MainWindow::transition_clicked(int transition_number)
781 global_mixer->transition_clicked(transition_number);
784 void MainWindow::channel_clicked(int channel_number)
786 if (current_wb_pick_display == channel_number) {
787 // The picking was already done from eventFilter(), since we don't get
788 // the mouse pointer here.
790 global_mixer->channel_clicked(channel_number);
794 void MainWindow::wb_button_clicked(int channel_number)
796 current_wb_pick_display = channel_number;
797 QApplication::setOverrideCursor(Qt::CrossCursor);
800 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
802 if (current_wb_pick_display != -1 &&
803 event->type() == QEvent::MouseButtonRelease &&
804 watched->isWidgetType()) {
805 QApplication::restoreOverrideCursor();
806 if (watched == previews[current_wb_pick_display]->display) {
807 const QMouseEvent *mouse_event = (QMouseEvent *)event;
808 set_white_balance(current_wb_pick_display, mouse_event->x(), mouse_event->y());
810 // The user clicked on something else, give up.
811 // (The click goes through, which might not be ideal, but, yes.)
812 current_wb_pick_display = -1;
820 double srgb_to_linear(double x)
825 return pow((x + 0.055) / 1.055, 2.4);
831 void MainWindow::set_white_balance(int channel_number, int x, int y)
833 // Set the white balance to neutral for the grab. It's probably going to
834 // flicker a bit, but hopefully this display is not live anyway.
835 global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, 0.5, 0.5, 0.5);
836 previews[channel_number]->display->updateGL();
837 QRgb reference_color = previews[channel_number]->display->grabFrameBuffer().pixel(x, y);
839 double r = srgb_to_linear(qRed(reference_color) / 255.0);
840 double g = srgb_to_linear(qGreen(reference_color) / 255.0);
841 double b = srgb_to_linear(qBlue(reference_color) / 255.0);
842 global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, r, g, b);
843 previews[channel_number]->display->updateGL();
846 void MainWindow::audio_state_changed()
848 post_to_main_thread([this]{
849 InputMapping mapping = global_audio_mixer->get_input_mapping();
850 for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
851 const InputMapping::Bus &bus = mapping.buses[bus_index];
853 if (bus.device.type == InputSourceType::ALSA_INPUT) {
854 ALSAPool::Device::State state = global_audio_mixer->get_alsa_card_state(bus.device.index);
855 if (state == ALSAPool::Device::State::STARTING) {
857 } else if (state == ALSAPool::Device::State::DEAD) {
862 audio_miniviews[bus_index]->bus_desc_label->setFullText(
863 QString::fromStdString(bus.name + suffix));
864 audio_expanded_views[bus_index]->bus_desc_label->setFullText(
865 QString::fromStdString(bus.name + suffix));