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_display.h"
31 #include "ui_mainwindow.h"
37 using namespace std::chrono;
38 using namespace std::placeholders;
40 Q_DECLARE_METATYPE(std::string);
41 Q_DECLARE_METATYPE(std::vector<std::string>);
43 MainWindow *global_mainwindow = nullptr;
47 void schedule_cut_signal(int ignored)
49 global_mixer->schedule_cut();
52 void quit_signal(int ignored)
54 global_mainwindow->close();
57 constexpr unsigned DB_NO_FLAGS = 0x0;
58 constexpr unsigned DB_WITH_SIGN = 0x1;
59 constexpr unsigned DB_BARE = 0x2;
61 string format_db(double db, unsigned flags)
64 if (flags & DB_WITH_SIGN) {
67 snprintf(buf, sizeof(buf), "%+.1f", db);
69 } else if (db < 0.0) {
72 // Should never happen, really.
78 snprintf(buf, sizeof(buf), "%.1f", db);
80 } else if (db < 0.0) {
83 // Should never happen, really.
87 if (!(flags & DB_BARE)) {
95 MainWindow::MainWindow()
96 : ui(new Ui::MainWindow)
98 global_mainwindow = this;
101 global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
102 disk_free_label = new QLabel(this);
103 disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
104 ui->menuBar->setCornerWidget(disk_free_label);
106 ui->me_live->set_output(Mixer::OUTPUT_LIVE);
107 ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
110 connect(ui->cut_action, &QAction::triggered, this, &MainWindow::cut_triggered);
111 connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
112 connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered);
113 connect(ui->input_mapping_action, &QAction::triggered, this, &MainWindow::input_mapping_triggered);
115 if (global_flags.x264_video_to_http) {
116 connect(ui->x264_bitrate_action, &QAction::triggered, this, &MainWindow::x264_bitrate_triggered);
118 ui->x264_bitrate_action->setEnabled(false);
121 // Hook up the transition buttons. (Keyboard shortcuts are set in set_transition_names().)
122 // TODO: Make them dynamic.
123 connect(ui->transition_btn1, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 0));
124 connect(ui->transition_btn2, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 1));
125 connect(ui->transition_btn3, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 2));
128 transition_btn1 = ui->transition_btn1;
129 transition_btn2 = ui->transition_btn2;
130 transition_btn3 = ui->transition_btn3;
131 qRegisterMetaType<string>("std::string");
132 qRegisterMetaType<vector<string>>("std::vector<std::string>");
133 connect(ui->me_live, &GLWidget::transition_names_updated, this, &MainWindow::set_transition_names);
134 qRegisterMetaType<Mixer::Output>("Mixer::Output");
136 // Hook up the prev/next buttons on the audio views.
137 connect(ui->compact_prev_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 1));
138 connect(ui->compact_next_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 1));
139 connect(ui->full_prev_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 0));
140 connect(ui->full_next_page, &QAbstractButton::clicked, bind(&QStackedWidget::setCurrentIndex, ui->audio_views, 0));
142 last_audio_level_callback = steady_clock::now() - seconds(1);
145 void MainWindow::resizeEvent(QResizeEvent* event)
147 QMainWindow::resizeEvent(event);
149 // Ask for a relayout, but only after the event loop is done doing relayout
150 // on everything else.
151 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
154 void MainWindow::mixer_created(Mixer *mixer)
156 // Make the previews.
157 unsigned num_previews = mixer->get_num_channels();
159 for (unsigned i = 0; i < num_previews; ++i) {
160 Mixer::Output output = Mixer::Output(Mixer::OUTPUT_INPUT0 + i);
162 QWidget *preview = new QWidget(this);
163 Ui::Display *ui_display = new Ui::Display;
164 ui_display->setupUi(preview);
165 ui_display->label->setText(mixer->get_channel_name(output).c_str());
166 ui_display->display->set_output(output);
167 ui->preview_displays->insertWidget(previews.size(), preview, 1);
168 previews.push_back(ui_display);
170 // Hook up the click.
171 connect(ui_display->display, &GLWidget::clicked, bind(&MainWindow::channel_clicked, this, i));
173 // Let the theme update the text whenever the resolution or color changed.
174 connect(ui_display->display, &GLWidget::name_updated, this, &MainWindow::update_channel_name);
175 connect(ui_display->display, &GLWidget::color_updated, this, &MainWindow::update_channel_color);
177 // Hook up the keyboard key.
178 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
179 connect(shortcut, &QShortcut::activated, bind(&MainWindow::channel_clicked, this, i));
181 // Hook up the white balance button (irrelevant if invisible).
182 ui_display->wb_button->setVisible(mixer->get_supports_set_wb(output));
183 connect(ui_display->wb_button, &QPushButton::clicked, bind(&MainWindow::wb_button_clicked, this, i));
186 setup_audio_miniview();
188 // TODO: Fetch all of the values these for completeness,
189 // not just the enable knobs implied by flags.
190 ui->locut_enabled->setChecked(global_mixer->get_audio_mixer()->get_locut_enabled());
191 ui->gainstaging_knob->setValue(global_mixer->get_audio_mixer()->get_gain_staging_db());
192 ui->gainstaging_auto_checkbox->setChecked(global_mixer->get_audio_mixer()->get_gain_staging_auto());
193 ui->compressor_enabled->setChecked(global_mixer->get_audio_mixer()->get_compressor_enabled());
194 ui->limiter_enabled->setChecked(global_mixer->get_audio_mixer()->get_limiter_enabled());
195 ui->makeup_gain_auto_checkbox->setChecked(global_mixer->get_audio_mixer()->get_final_makeup_gain_auto());
197 ui->limiter_threshold_db_display->setText(
198 QString::fromStdString(format_db(mixer->get_audio_mixer()->get_limiter_threshold_dbfs(), DB_WITH_SIGN)));
199 ui->compressor_threshold_db_display->setText(
200 QString::fromStdString(format_db(mixer->get_audio_mixer()->get_compressor_threshold_dbfs(), DB_WITH_SIGN)));
202 connect(ui->locut_cutoff_knob, &QDial::valueChanged, this, &MainWindow::cutoff_knob_changed);
203 cutoff_knob_changed(ui->locut_cutoff_knob->value());
204 connect(ui->locut_enabled, &QCheckBox::stateChanged, [this](int state){
205 global_mixer->get_audio_mixer()->set_locut_enabled(state == Qt::Checked);
208 connect(ui->gainstaging_knob, &QAbstractSlider::valueChanged, this, &MainWindow::gain_staging_knob_changed);
209 connect(ui->gainstaging_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
210 global_mixer->get_audio_mixer()->set_gain_staging_auto(state == Qt::Checked);
212 connect(ui->makeup_gain_knob, &QAbstractSlider::valueChanged, this, &MainWindow::final_makeup_gain_knob_changed);
213 connect(ui->makeup_gain_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
214 global_mixer->get_audio_mixer()->set_final_makeup_gain_auto(state == Qt::Checked);
217 connect(ui->limiter_threshold_knob, &QDial::valueChanged, this, &MainWindow::limiter_threshold_knob_changed);
218 connect(ui->compressor_threshold_knob, &QDial::valueChanged, this, &MainWindow::compressor_threshold_knob_changed);
219 connect(ui->limiter_enabled, &QCheckBox::stateChanged, [this](int state){
220 global_mixer->get_audio_mixer()->set_limiter_enabled(state == Qt::Checked);
222 connect(ui->compressor_enabled, &QCheckBox::stateChanged, [this](int state){
223 global_mixer->get_audio_mixer()->set_compressor_enabled(state == Qt::Checked);
225 connect(ui->reset_meters_button, &QPushButton::clicked, this, &MainWindow::reset_meters_button_clicked);
226 mixer->get_audio_mixer()->set_audio_level_callback(bind(&MainWindow::audio_level_callback, this, _1, _2, _3, _4, _5, _6, _7, _8, _9));
228 struct sigaction act;
229 memset(&act, 0, sizeof(act));
230 act.sa_handler = schedule_cut_signal;
231 act.sa_flags = SA_RESTART;
232 sigaction(SIGHUP, &act, nullptr);
234 // Mostly for debugging. Don't override SIGINT, that's so evil if
235 // shutdown isn't instant.
236 memset(&act, 0, sizeof(act));
237 act.sa_handler = quit_signal;
238 act.sa_flags = SA_RESTART;
239 sigaction(SIGUSR1, &act, nullptr);
242 void MainWindow::setup_audio_miniview()
244 // Remove any existing channels.
245 for (QLayoutItem *item; (item = ui->faders->takeAt(0)) != nullptr; ) {
246 delete item->widget();
249 audio_miniviews.clear();
251 // Set up brand new ones from the input mapping.
252 InputMapping mapping = global_mixer->get_audio_mixer()->get_input_mapping();
253 audio_miniviews.resize(mapping.buses.size());
254 for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
255 QWidget *channel = new QWidget(this);
256 Ui::AudioMiniView *ui_audio_miniview = new Ui::AudioMiniView;
257 ui_audio_miniview->setupUi(channel);
258 ui_audio_miniview->bus_desc_label->setFullText(
259 QString::fromStdString(mapping.buses[bus_index].name));
260 audio_miniviews[bus_index] = ui_audio_miniview;
261 // TODO: Set the fader position.
262 ui->faders->addWidget(channel);
264 connect(ui_audio_miniview->fader, &NonLinearFader::dbValueChanged,
265 bind(&MainWindow::mini_fader_changed, this, ui_audio_miniview, bus_index, _1));
269 void MainWindow::mixer_shutting_down()
271 ui->me_live->clean_context();
272 ui->me_preview->clean_context();
273 for (Ui::Display *display : previews) {
274 display->display->clean_context();
278 void MainWindow::cut_triggered()
280 global_mixer->schedule_cut();
283 void MainWindow::x264_bitrate_triggered()
286 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);
287 if (ok && new_bitrate >= 100 && new_bitrate <= 100000) {
288 global_flags.x264_bitrate = new_bitrate;
289 global_mixer->change_x264_bitrate(new_bitrate);
293 void MainWindow::exit_triggered()
298 void MainWindow::about_triggered()
300 AboutDialog().exec();
303 void MainWindow::input_mapping_triggered()
305 if (InputMappingDialog().exec() == QDialog::Accepted) {
306 setup_audio_miniview();
310 void MainWindow::gain_staging_knob_changed(int value)
312 ui->gainstaging_auto_checkbox->setCheckState(Qt::Unchecked);
314 float gain_db = value * 0.1f;
315 global_mixer->get_audio_mixer()->set_gain_staging_db(gain_db);
317 // The label will be updated by the audio level callback.
320 void MainWindow::final_makeup_gain_knob_changed(int value)
322 ui->makeup_gain_auto_checkbox->setCheckState(Qt::Unchecked);
324 float gain_db = value * 0.1f;
325 global_mixer->get_audio_mixer()->set_final_makeup_gain_db(gain_db);
327 // The label will be updated by the audio level callback.
330 void MainWindow::cutoff_knob_changed(int value)
332 float octaves = value * 0.1f;
333 float cutoff_hz = 20.0 * pow(2.0, octaves);
334 global_mixer->get_audio_mixer()->set_locut_cutoff(cutoff_hz);
337 snprintf(buf, sizeof(buf), "%ld Hz", lrintf(cutoff_hz));
338 ui->locut_cutoff_display->setText(buf);
341 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
344 if (estimated_seconds_left < 60.0) {
345 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
346 } else if (estimated_seconds_left < 1800.0) { // Less than half an hour: Xm Ys (red).
347 int s = lrintf(estimated_seconds_left);
350 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
351 } else if (estimated_seconds_left < 3600.0) { // Less than an hour: Xm.
352 int m = lrintf(estimated_seconds_left / 60.0);
353 snprintf(time_str, sizeof(time_str), "%dm", m);
354 } else if (estimated_seconds_left < 36000.0) { // Less than ten hours: Xh Ym.
355 int m = lrintf(estimated_seconds_left / 60.0);
358 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
359 } else { // More than ten hours: Xh.
360 int h = lrintf(estimated_seconds_left / 3600.0);
361 snprintf(time_str, sizeof(time_str), "%dh", h);
364 snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
366 std::string label = buf;
368 post_to_main_thread([this, label]{
369 disk_free_label->setText(QString::fromStdString(label));
370 ui->menuBar->setCornerWidget(disk_free_label); // Need to set this again for the sizing to get right.
374 void MainWindow::limiter_threshold_knob_changed(int value)
376 float threshold_dbfs = value * 0.1f;
377 global_mixer->get_audio_mixer()->set_limiter_threshold_dbfs(threshold_dbfs);
378 ui->limiter_threshold_db_display->setText(
379 QString::fromStdString(format_db(threshold_dbfs, DB_WITH_SIGN)));
382 void MainWindow::compressor_threshold_knob_changed(int value)
384 float threshold_dbfs = value * 0.1f;
385 global_mixer->get_audio_mixer()->set_compressor_threshold_dbfs(threshold_dbfs);
386 ui->compressor_threshold_db_display->setText(
387 QString::fromStdString(format_db(threshold_dbfs, DB_WITH_SIGN)));
390 void MainWindow::mini_fader_changed(Ui::AudioMiniView *ui, int channel, double volume_db)
393 if (isfinite(volume_db)) {
394 snprintf(buf, sizeof(buf), "%+.1f dB", volume_db);
395 ui->fader_label->setText(buf);
397 ui->fader_label->setText("-∞ dB");
400 global_mixer->get_audio_mixer()->set_fader_volume(channel, volume_db);
403 void MainWindow::reset_meters_button_clicked()
405 global_mixer->get_audio_mixer()->reset_meters();
406 ui->peak_display->setText(QString::fromStdString(format_db(-HUGE_VAL, DB_WITH_SIGN | DB_BARE)));
407 ui->peak_display->setStyleSheet("");
410 void MainWindow::audio_level_callback(float level_lufs, float peak_db, vector<float> bus_level_lufs,
411 float global_level_lufs,
412 float range_low_lufs, float range_high_lufs,
413 float gain_staging_db, float final_makeup_gain_db,
416 steady_clock::time_point now = steady_clock::now();
418 // The meters are somewhat inefficient to update. Only update them
419 // every 100 ms or so (we get updates every 5–20 ms).
420 double last_update_age = duration<double>(now - last_audio_level_callback).count();
421 if (last_update_age < 0.100) {
424 last_audio_level_callback = now;
426 post_to_main_thread([=]() {
427 ui->vu_meter->set_level(level_lufs);
428 for (unsigned bus_index = 0; bus_index < bus_level_lufs.size(); ++bus_index) {
429 if (bus_index < audio_miniviews.size()) {
430 audio_miniviews[bus_index]->vu_meter_meter->set_level(
431 bus_level_lufs[bus_index]);
434 ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
435 ui->correlation_meter->set_correlation(correlation);
437 ui->peak_display->setText(QString::fromStdString(format_db(peak_db, DB_BARE)));
438 if (peak_db > -0.1f) { // -0.1 dBFS is EBU peak limit.
439 ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
441 ui->peak_display->setStyleSheet("");
444 ui->gainstaging_knob->blockSignals(true);
445 ui->gainstaging_knob->setValue(lrintf(gain_staging_db * 10.0f));
446 ui->gainstaging_knob->blockSignals(false);
447 ui->gainstaging_db_display->setText(
448 QString::fromStdString(format_db(gain_staging_db, DB_WITH_SIGN)));
450 ui->makeup_gain_knob->blockSignals(true);
451 ui->makeup_gain_knob->setValue(lrintf(final_makeup_gain_db * 10.0f));
452 ui->makeup_gain_knob->blockSignals(false);
453 ui->makeup_gain_db_display->setText(
454 QString::fromStdString(format_db(final_makeup_gain_db, DB_WITH_SIGN)));
458 void MainWindow::relayout()
460 int height = ui->vertical_layout->geometry().height();
462 double remaining_height = height;
464 // Allocate the height; the most important part is to keep the main displays
465 // at 16:9 if at all possible.
466 double me_width = ui->me_preview->width();
467 double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height() + ui->preview_vertical_layout->spacing();
469 // TODO: Scale the widths when we need to do this.
470 if (me_height / double(height) > 0.8) {
471 me_height = height * 0.8;
473 remaining_height -= me_height + ui->vertical_layout->spacing();
475 // Space between the M/E displays and the audio strip.
476 remaining_height -= ui->vertical_layout->spacing();
478 // The label above the audio strip.
479 double compact_label_height = ui->compact_label->geometry().height() +
480 ui->compact_audio_layout->spacing();
481 remaining_height -= compact_label_height;
483 // The previews will be constrained by the remaining height, and the width.
484 double preview_label_height = previews[0]->title_bar->geometry().height() +
485 previews[0]->main_vertical_layout->spacing();
486 int preview_total_width = ui->preview_displays->geometry().width() - (previews.size() - 1) * ui->preview_displays->spacing();
487 double preview_height = min(remaining_height - preview_label_height, (preview_total_width / double(previews.size())) * 9.0 / 16.0);
488 remaining_height -= preview_height + preview_label_height + ui->vertical_layout->spacing();
490 ui->vertical_layout->setStretch(0, lrintf(me_height));
491 ui->vertical_layout->setStretch(1,
492 lrintf(compact_label_height) +
493 lrintf(remaining_height) +
494 lrintf(preview_height + preview_label_height)); // Audio strip and previews together.
496 ui->compact_audio_layout->setStretch(0, lrintf(compact_label_height));
497 ui->compact_audio_layout->setStretch(1, lrintf(remaining_height)); // Audio strip.
498 ui->compact_audio_layout->setStretch(2, lrintf(preview_height + preview_label_height));
500 // Set the widths for the previews.
501 double preview_width = preview_height * 16.0 / 9.0;
502 for (unsigned i = 0; i < previews.size(); ++i) {
503 ui->preview_displays->setStretch(i, lrintf(preview_width));
506 // The preview horizontal spacer.
507 double remaining_preview_width = preview_total_width - previews.size() * preview_width;
508 ui->preview_displays->setStretch(previews.size(), lrintf(remaining_preview_width));
511 void MainWindow::set_transition_names(vector<string> transition_names)
513 if (transition_names.size() < 1 || transition_names[0].empty()) {
514 transition_btn1->setText(QString(""));
516 transition_btn1->setText(QString::fromStdString(transition_names[0] + " (J)"));
517 ui->transition_btn1->setShortcut(QKeySequence("J"));
519 if (transition_names.size() < 2 || transition_names[1].empty()) {
520 transition_btn2->setText(QString(""));
522 transition_btn2->setText(QString::fromStdString(transition_names[1] + " (K)"));
523 ui->transition_btn2->setShortcut(QKeySequence("K"));
525 if (transition_names.size() < 3 || transition_names[2].empty()) {
526 transition_btn3->setText(QString(""));
528 transition_btn3->setText(QString::fromStdString(transition_names[2] + " (L)"));
529 ui->transition_btn3->setShortcut(QKeySequence("L"));
533 void MainWindow::update_channel_name(Mixer::Output output, const string &name)
535 if (output >= Mixer::OUTPUT_INPUT0) {
536 unsigned channel = output - Mixer::OUTPUT_INPUT0;
537 previews[channel]->label->setText(name.c_str());
541 void MainWindow::update_channel_color(Mixer::Output output, const string &color)
543 if (output >= Mixer::OUTPUT_INPUT0) {
544 unsigned channel = output - Mixer::OUTPUT_INPUT0;
545 previews[channel]->frame->setStyleSheet(QString::fromStdString("background-color:" + color));
549 void MainWindow::transition_clicked(int transition_number)
551 global_mixer->transition_clicked(transition_number);
554 void MainWindow::channel_clicked(int channel_number)
556 if (current_wb_pick_display == channel_number) {
557 // The picking was already done from eventFilter(), since we don't get
558 // the mouse pointer here.
560 global_mixer->channel_clicked(channel_number);
564 void MainWindow::wb_button_clicked(int channel_number)
566 current_wb_pick_display = channel_number;
567 QApplication::setOverrideCursor(Qt::CrossCursor);
570 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
572 if (current_wb_pick_display != -1 &&
573 event->type() == QEvent::MouseButtonRelease &&
574 watched->isWidgetType()) {
575 QApplication::restoreOverrideCursor();
576 if (watched == previews[current_wb_pick_display]->display) {
577 const QMouseEvent *mouse_event = (QMouseEvent *)event;
578 set_white_balance(current_wb_pick_display, mouse_event->x(), mouse_event->y());
580 // The user clicked on something else, give up.
581 // (The click goes through, which might not be ideal, but, yes.)
582 current_wb_pick_display = -1;
590 double srgb_to_linear(double x)
595 return pow((x + 0.055) / 1.055, 2.4);
601 void MainWindow::set_white_balance(int channel_number, int x, int y)
603 // Set the white balance to neutral for the grab. It's probably going to
604 // flicker a bit, but hopefully this display is not live anyway.
605 global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, 0.5, 0.5, 0.5);
606 previews[channel_number]->display->updateGL();
607 QRgb reference_color = previews[channel_number]->display->grabFrameBuffer().pixel(x, y);
609 double r = srgb_to_linear(qRed(reference_color) / 255.0);
610 double g = srgb_to_linear(qGreen(reference_color) / 255.0);
611 double b = srgb_to_linear(qBlue(reference_color) / 255.0);
612 global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, r, g, b);
613 previews[channel_number]->display->updateGL();