1 #include "mainwindow.h"
10 #include <QKeySequence>
13 #include <QPushButton>
14 #include <QResizeEvent>
19 #include "aboutdialog.h"
23 #include "post_to_main_thread.h"
24 #include "ui_display.h"
25 #include "ui_mainwindow.h"
31 using namespace std::placeholders;
33 Q_DECLARE_METATYPE(std::string);
34 Q_DECLARE_METATYPE(std::vector<std::string>);
36 MainWindow *global_mainwindow = nullptr;
40 void schedule_cut_signal(int ignored)
42 global_mixer->schedule_cut();
45 void quit_signal(int ignored)
47 global_mainwindow->close();
52 MainWindow::MainWindow()
53 : ui(new Ui::MainWindow)
55 global_mainwindow = this;
58 ui->me_live->set_output(Mixer::OUTPUT_LIVE);
59 ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
62 connect(ui->cut_action, &QAction::triggered, this, &MainWindow::cut_triggered);
63 connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
64 connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered),
66 // Hook up the transition buttons. (Keyboard shortcuts are set in set_transition_names().)
67 // TODO: Make them dynamic.
68 connect(ui->transition_btn1, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 0));
69 connect(ui->transition_btn2, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 1));
70 connect(ui->transition_btn3, &QPushButton::clicked, bind(&MainWindow::transition_clicked, this, 2));
73 transition_btn1 = ui->transition_btn1;
74 transition_btn2 = ui->transition_btn2;
75 transition_btn3 = ui->transition_btn3;
76 qRegisterMetaType<string>("std::string");
77 qRegisterMetaType<vector<string>>("std::vector<std::string>");
78 connect(ui->me_preview, &GLWidget::transition_names_updated, this, &MainWindow::set_transition_names);
79 qRegisterMetaType<Mixer::Output>("Mixer::Output");
82 void MainWindow::resizeEvent(QResizeEvent* event)
84 QMainWindow::resizeEvent(event);
86 // Ask for a relayout, but only after the event loop is done doing relayout
87 // on everything else.
88 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
91 void MainWindow::mixer_created(Mixer *mixer)
94 unsigned num_previews = mixer->get_num_channels();
96 for (unsigned i = 0; i < num_previews; ++i) {
97 Mixer::Output output = Mixer::Output(Mixer::OUTPUT_INPUT0 + i);
99 QWidget *preview = new QWidget(this);
100 Ui::Display *ui_display = new Ui::Display;
101 ui_display->setupUi(preview);
102 ui_display->label->setText(mixer->get_channel_name(output).c_str());
103 ui_display->display->set_output(output);
104 ui->preview_displays->insertWidget(previews.size(), preview, 1);
105 previews.push_back(ui_display);
107 // Hook up the click.
108 connect(ui_display->display, &GLWidget::clicked, bind(&MainWindow::channel_clicked, this, i));
110 // Let the theme update the text whenever the resolution or color changed.
111 connect(ui_display->display, &GLWidget::name_updated, this, &MainWindow::update_channel_name);
112 connect(ui_display->display, &GLWidget::color_updated, this, &MainWindow::update_channel_color);
114 // Hook up the keyboard key.
115 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
116 connect(shortcut, &QShortcut::activated, bind(&MainWindow::channel_clicked, this, i));
118 // Hook up the white balance button (irrelevant if invisible).
119 ui_display->wb_button->setVisible(mixer->get_supports_set_wb(output));
120 connect(ui_display->wb_button, &QPushButton::clicked, bind(&MainWindow::wb_button_clicked, this, i));
124 snprintf(buf, sizeof(buf), "%.1f dB", mixer->get_limiter_threshold_dbfs());
125 ui->limiter_threshold_db_display->setText(buf);
126 snprintf(buf, sizeof(buf), "%.1f dB", mixer->get_compressor_threshold_dbfs());
127 ui->compressor_threshold_db_display->setText(buf);
129 connect(ui->locut_cutoff_knob, &QDial::valueChanged, this, &MainWindow::cutoff_knob_changed);
130 cutoff_knob_changed(ui->locut_cutoff_knob->value());
131 connect(ui->locut_enabled, &QCheckBox::stateChanged, [this](int state){
132 global_mixer->set_locut_enabled(state == Qt::Checked);
135 // Not QDial::valueChanged, as we call setValue() all the time.
136 connect(ui->gainstaging_knob, &QAbstractSlider::sliderMoved, this, &MainWindow::gain_staging_knob_changed);
137 connect(ui->gainstaging_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
138 global_mixer->set_gain_staging_auto(state == Qt::Checked);
140 connect(ui->makeup_gain_knob, &QAbstractSlider::sliderMoved, this, &MainWindow::final_makeup_gain_knob_changed);
141 connect(ui->makeup_gain_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
142 global_mixer->set_final_makeup_gain_auto(state == Qt::Checked);
145 connect(ui->limiter_threshold_knob, &QDial::valueChanged, this, &MainWindow::limiter_threshold_knob_changed);
146 connect(ui->compressor_threshold_knob, &QDial::valueChanged, this, &MainWindow::compressor_threshold_knob_changed);
147 connect(ui->limiter_enabled, &QCheckBox::stateChanged, [this](int state){
148 global_mixer->set_limiter_enabled(state == Qt::Checked);
150 connect(ui->compressor_enabled, &QCheckBox::stateChanged, [this](int state){
151 global_mixer->set_compressor_enabled(state == Qt::Checked);
153 connect(ui->reset_meters_button, &QPushButton::clicked, this, &MainWindow::reset_meters_button_clicked);
154 mixer->set_audio_level_callback(bind(&MainWindow::audio_level_callback, this, _1, _2, _3, _4, _5, _6, _7, _8));
156 struct sigaction act;
157 memset(&act, 0, sizeof(act));
158 act.sa_handler = schedule_cut_signal;
159 act.sa_flags = SA_RESTART;
160 sigaction(SIGHUP, &act, nullptr);
162 // Mostly for debugging. Don't override SIGINT, that's so evil if
163 // shutdown isn't instant.
164 memset(&act, 0, sizeof(act));
165 act.sa_handler = quit_signal;
166 act.sa_flags = SA_RESTART;
167 sigaction(SIGUSR1, &act, nullptr);
170 void MainWindow::mixer_shutting_down()
172 ui->me_live->clean_context();
173 ui->me_preview->clean_context();
174 for (Ui::Display *display : previews) {
175 display->display->clean_context();
179 void MainWindow::cut_triggered()
181 global_mixer->schedule_cut();
184 void MainWindow::exit_triggered()
189 void MainWindow::about_triggered()
191 AboutDialog().exec();
194 void MainWindow::gain_staging_knob_changed(int value)
196 ui->gainstaging_auto_checkbox->setCheckState(Qt::Unchecked);
198 float gain_db = value * 0.1f;
199 global_mixer->set_gain_staging_db(gain_db);
201 // The label will be updated by the audio level callback.
204 void MainWindow::final_makeup_gain_knob_changed(int value)
206 ui->makeup_gain_auto_checkbox->setCheckState(Qt::Unchecked);
208 float gain_db = value * 0.1f;
209 global_mixer->set_final_makeup_gain_db(gain_db);
211 // The label will be updated by the audio level callback.
214 void MainWindow::cutoff_knob_changed(int value)
216 float octaves = value * 0.1f;
217 float cutoff_hz = 20.0 * pow(2.0, octaves);
218 global_mixer->set_locut_cutoff(cutoff_hz);
221 snprintf(buf, sizeof(buf), "%ld Hz", lrintf(cutoff_hz));
222 ui->locut_cutoff_display->setText(buf);
225 void MainWindow::limiter_threshold_knob_changed(int value)
227 float threshold_dbfs = value * 0.1f;
228 global_mixer->set_limiter_threshold_dbfs(threshold_dbfs);
231 snprintf(buf, sizeof(buf), "%.1f dB", threshold_dbfs);
232 ui->limiter_threshold_db_display->setText(buf);
235 void MainWindow::compressor_threshold_knob_changed(int value)
237 float threshold_dbfs = value * 0.1f;
238 global_mixer->set_compressor_threshold_dbfs(threshold_dbfs);
241 snprintf(buf, sizeof(buf), "%.1f dB", threshold_dbfs);
242 ui->compressor_threshold_db_display->setText(buf);
245 void MainWindow::reset_meters_button_clicked()
247 global_mixer->reset_meters();
248 ui->peak_display->setText("-inf");
249 ui->peak_display->setStyleSheet("");
252 void MainWindow::audio_level_callback(float level_lufs, float peak_db, float global_level_lufs,
253 float range_low_lufs, float range_high_lufs,
254 float gain_staging_db, float final_makeup_gain_db,
258 gettimeofday(&now, nullptr);
260 // The meters are somewhat inefficient to update. Only update them
261 // every 100 ms or so (we get updates every 5–20 ms).
262 double last_update_age = now.tv_sec - last_audio_level_callback.tv_sec +
263 1e-6 * (now.tv_usec - last_audio_level_callback.tv_usec);
264 if (last_update_age < 0.100) {
267 last_audio_level_callback = now;
269 post_to_main_thread([=]() {
270 ui->vu_meter->set_level(level_lufs);
271 ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
272 ui->correlation_meter->set_correlation(correlation);
275 snprintf(buf, sizeof(buf), "%.1f", peak_db);
276 ui->peak_display->setText(buf);
277 if (peak_db > -0.1f) { // -0.1 dBFS is EBU peak limit.
278 ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
280 ui->peak_display->setStyleSheet("");
283 ui->gainstaging_knob->setValue(lrintf(gain_staging_db * 10.0f));
284 snprintf(buf, sizeof(buf), "%+.1f dB", gain_staging_db);
285 ui->gainstaging_db_display->setText(buf);
287 ui->makeup_gain_knob->setValue(lrintf(final_makeup_gain_db * 10.0f));
288 snprintf(buf, sizeof(buf), "%+.1f dB", final_makeup_gain_db);
289 ui->makeup_gain_db_display->setText(buf);
293 void MainWindow::relayout()
295 int height = ui->vertical_layout->geometry().height();
297 double remaining_height = height;
299 // Allocate the height; the most important part is to keep the main displays
300 // at 16:9 if at all possible.
301 double me_width = ui->me_preview->width();
302 double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height() + ui->preview_vertical_layout->spacing();
304 // TODO: Scale the widths when we need to do this.
305 if (me_height / double(height) > 0.8) {
306 me_height = height * 0.8;
308 remaining_height -= me_height + ui->vertical_layout->spacing();
310 double audiostrip_height = ui->audiostrip->geometry().height();
311 remaining_height -= audiostrip_height + ui->vertical_layout->spacing();
313 // The previews will be constrained by the remaining height, and the width.
314 double preview_label_height = previews[0]->title_bar->geometry().height() +
315 previews[0]->main_vertical_layout->spacing();
316 int preview_total_width = ui->preview_displays->geometry().width() - (previews.size() - 1) * ui->preview_displays->spacing();
317 double preview_height = min(remaining_height - preview_label_height, (preview_total_width / double(previews.size())) * 9.0 / 16.0);
318 remaining_height -= preview_height + preview_label_height + ui->vertical_layout->spacing();
320 ui->vertical_layout->setStretch(0, lrintf(me_height));
321 ui->vertical_layout->setStretch(1, 0); // Don't stretch the audiostrip.
322 ui->vertical_layout->setStretch(2, max<int>(1, remaining_height)); // Spacer.
323 ui->vertical_layout->setStretch(3, lrintf(preview_height + preview_label_height));
325 // Set the widths for the previews.
326 double preview_width = preview_height * 16.0 / 9.0;
327 for (unsigned i = 0; i < previews.size(); ++i) {
328 ui->preview_displays->setStretch(i, lrintf(preview_width));
331 // The preview horizontal spacer.
332 double remaining_preview_width = preview_total_width - previews.size() * preview_width;
333 ui->preview_displays->setStretch(previews.size(), lrintf(remaining_preview_width));
336 void MainWindow::set_transition_names(vector<string> transition_names)
338 if (transition_names.size() < 1 || transition_names[0].empty()) {
339 transition_btn1->setText(QString(""));
341 transition_btn1->setText(QString::fromStdString(transition_names[0] + " (J)"));
342 ui->transition_btn1->setShortcut(QKeySequence("J"));
344 if (transition_names.size() < 2 || transition_names[1].empty()) {
345 transition_btn2->setText(QString(""));
347 transition_btn2->setText(QString::fromStdString(transition_names[1] + " (K)"));
348 ui->transition_btn2->setShortcut(QKeySequence("K"));
350 if (transition_names.size() < 3 || transition_names[2].empty()) {
351 transition_btn3->setText(QString(""));
353 transition_btn3->setText(QString::fromStdString(transition_names[2] + " (L)"));
354 ui->transition_btn3->setShortcut(QKeySequence("L"));
358 void MainWindow::update_channel_name(Mixer::Output output, const string &name)
360 if (output >= Mixer::OUTPUT_INPUT0) {
361 unsigned channel = output - Mixer::OUTPUT_INPUT0;
362 previews[channel]->label->setText(name.c_str());
366 void MainWindow::update_channel_color(Mixer::Output output, const string &color)
368 if (output >= Mixer::OUTPUT_INPUT0) {
369 unsigned channel = output - Mixer::OUTPUT_INPUT0;
370 previews[channel]->frame->setStyleSheet(QString::fromStdString("background-color:" + color));
374 void MainWindow::transition_clicked(int transition_number)
376 global_mixer->transition_clicked(transition_number);
379 void MainWindow::channel_clicked(int channel_number)
381 if (current_wb_pick_display == channel_number) {
382 // The picking was already done from eventFilter(), since we don't get
383 // the mouse pointer here.
385 global_mixer->channel_clicked(channel_number);
389 void MainWindow::wb_button_clicked(int channel_number)
391 current_wb_pick_display = channel_number;
392 QApplication::setOverrideCursor(Qt::CrossCursor);
395 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
397 if (current_wb_pick_display != -1 &&
398 event->type() == QEvent::MouseButtonRelease &&
399 watched->isWidgetType()) {
400 QApplication::restoreOverrideCursor();
401 if (watched == previews[current_wb_pick_display]->display) {
402 const QMouseEvent *mouse_event = (QMouseEvent *)event;
403 set_white_balance(current_wb_pick_display, mouse_event->x(), mouse_event->y());
405 // The user clicked on something else, give up.
406 // (The click goes through, which might not be ideal, but, yes.)
407 current_wb_pick_display = -1;
415 double srgb_to_linear(double x)
420 return pow((x + 0.055) / 1.055, 2.4);
426 void MainWindow::set_white_balance(int channel_number, int x, int y)
428 // Set the white balance to neutral for the grab. It's probably going to
429 // flicker a bit, but hopefully this display is not live anyway.
430 global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, 0.5, 0.5, 0.5);
431 previews[channel_number]->display->updateGL();
432 QRgb reference_color = previews[channel_number]->display->grabFrameBuffer().pixel(x, y);
434 double r = srgb_to_linear(qRed(reference_color) / 255.0);
435 double g = srgb_to_linear(qGreen(reference_color) / 255.0);
436 double b = srgb_to_linear(qBlue(reference_color) / 255.0);
437 global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, r, g, b);
438 previews[channel_number]->display->updateGL();