]> git.sesse.net Git - nageru/blob - mainwindow.cpp
627d558609910c6f5c69900076d7fc2640bec741
[nageru] / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include <math.h>
4 #include <stdio.h>
5 #include <signal.h>
6 #include <algorithm>
7 #include <string>
8 #include <vector>
9 #include <QBoxLayout>
10 #include <QKeySequence>
11 #include <QLabel>
12 #include <QMetaType>
13 #include <QPushButton>
14 #include <QResizeEvent>
15 #include <QShortcut>
16 #include <QSize>
17 #include <QString>
18
19 #include "aboutdialog.h"
20 #include "glwidget.h"
21 #include "lrameter.h"
22 #include "mixer.h"
23 #include "post_to_main_thread.h"
24 #include "ui_display.h"
25 #include "ui_mainwindow.h"
26 #include "vumeter.h"
27
28 class QResizeEvent;
29
30 using namespace std;
31 using namespace std::placeholders;
32
33 Q_DECLARE_METATYPE(std::string);
34 Q_DECLARE_METATYPE(std::vector<std::string>);
35
36 MainWindow *global_mainwindow = nullptr;
37
38 namespace {
39
40 void schedule_cut_signal(int ignored)
41 {
42         global_mixer->schedule_cut();
43 }
44
45 void quit_signal(int ignored)
46 {
47         global_mainwindow->close();
48 }
49
50 }  // namespace
51
52 MainWindow::MainWindow()
53         : ui(new Ui::MainWindow)
54 {
55         global_mainwindow = this;
56         ui->setupUi(this);
57
58         ui->me_live->set_output(Mixer::OUTPUT_LIVE);
59         ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
60
61         // The menus.
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),
65
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));
71
72         // Aiee...
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_live, &GLWidget::transition_names_updated, this, &MainWindow::set_transition_names);
79         qRegisterMetaType<Mixer::Output>("Mixer::Output");
80 }
81
82 void MainWindow::resizeEvent(QResizeEvent* event)
83 {
84         QMainWindow::resizeEvent(event);
85
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);
89 }
90
91 void MainWindow::mixer_created(Mixer *mixer)
92 {
93         // Make the previews.
94         unsigned num_previews = mixer->get_num_channels();
95
96         for (unsigned i = 0; i < num_previews; ++i) {
97                 Mixer::Output output = Mixer::Output(Mixer::OUTPUT_INPUT0 + i);
98
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);
106
107                 // Hook up the click.
108                 connect(ui_display->display, &GLWidget::clicked, bind(&MainWindow::channel_clicked, this, i));
109
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);
113
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));
117
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));
121         }
122
123         // TODO: Fetch all of the values these for completeness,
124         // not just the enable knobs implied by --flat-audio.
125         ui->locut_enabled->setChecked(global_mixer->get_locut_enabled());
126         ui->gainstaging_auto_checkbox->setChecked(global_mixer->get_gain_staging_auto());
127         ui->limiter_enabled->setChecked(global_mixer->get_limiter_enabled());
128         ui->compressor_enabled->setChecked(global_mixer->get_compressor_enabled());
129
130         char buf[256];
131         snprintf(buf, sizeof(buf), "%.1f dB", mixer->get_limiter_threshold_dbfs());
132         ui->limiter_threshold_db_display->setText(buf);
133         snprintf(buf, sizeof(buf), "%.1f dB", mixer->get_compressor_threshold_dbfs());
134         ui->compressor_threshold_db_display->setText(buf);
135
136         connect(ui->locut_cutoff_knob, &QDial::valueChanged, this, &MainWindow::cutoff_knob_changed);
137         cutoff_knob_changed(ui->locut_cutoff_knob->value());
138         connect(ui->locut_enabled, &QCheckBox::stateChanged, [this](int state){
139                 global_mixer->set_locut_enabled(state == Qt::Checked);
140         });
141
142         connect(ui->gainstaging_knob, &QAbstractSlider::valueChanged, this, &MainWindow::gain_staging_knob_changed);
143         connect(ui->gainstaging_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
144                 global_mixer->set_gain_staging_auto(state == Qt::Checked);
145         });
146         connect(ui->makeup_gain_knob, &QAbstractSlider::valueChanged, this, &MainWindow::final_makeup_gain_knob_changed);
147         connect(ui->makeup_gain_auto_checkbox, &QCheckBox::stateChanged, [this](int state){
148                 global_mixer->set_final_makeup_gain_auto(state == Qt::Checked);
149         });
150
151         connect(ui->limiter_threshold_knob, &QDial::valueChanged, this, &MainWindow::limiter_threshold_knob_changed);
152         connect(ui->compressor_threshold_knob, &QDial::valueChanged, this, &MainWindow::compressor_threshold_knob_changed);
153         connect(ui->limiter_enabled, &QCheckBox::stateChanged, [this](int state){
154                 global_mixer->set_limiter_enabled(state == Qt::Checked);
155         });
156         connect(ui->compressor_enabled, &QCheckBox::stateChanged, [this](int state){
157                 global_mixer->set_compressor_enabled(state == Qt::Checked);
158         });
159         connect(ui->reset_meters_button, &QPushButton::clicked, this, &MainWindow::reset_meters_button_clicked);
160         mixer->set_audio_level_callback(bind(&MainWindow::audio_level_callback, this, _1, _2, _3, _4, _5, _6, _7, _8));
161
162         struct sigaction act;
163         memset(&act, 0, sizeof(act));
164         act.sa_handler = schedule_cut_signal;
165         act.sa_flags = SA_RESTART;
166         sigaction(SIGHUP, &act, nullptr);
167
168         // Mostly for debugging. Don't override SIGINT, that's so evil if
169         // shutdown isn't instant.
170         memset(&act, 0, sizeof(act));
171         act.sa_handler = quit_signal;
172         act.sa_flags = SA_RESTART;
173         sigaction(SIGUSR1, &act, nullptr);
174 }
175
176 void MainWindow::mixer_shutting_down()
177 {
178         ui->me_live->clean_context();
179         ui->me_preview->clean_context();
180         for (Ui::Display *display : previews) {
181                 display->display->clean_context();
182         }
183 }
184
185 void MainWindow::cut_triggered()
186 {
187         global_mixer->schedule_cut();
188 }
189
190 void MainWindow::exit_triggered()
191 {
192         close();
193 }
194
195 void MainWindow::about_triggered()
196 {
197         AboutDialog().exec();
198 }
199
200 void MainWindow::gain_staging_knob_changed(int value)
201 {
202         ui->gainstaging_auto_checkbox->setCheckState(Qt::Unchecked);
203
204         float gain_db = value * 0.1f;
205         global_mixer->set_gain_staging_db(gain_db);
206
207         // The label will be updated by the audio level callback.
208 }
209
210 void MainWindow::final_makeup_gain_knob_changed(int value)
211 {
212         ui->makeup_gain_auto_checkbox->setCheckState(Qt::Unchecked);
213
214         float gain_db = value * 0.1f;
215         global_mixer->set_final_makeup_gain_db(gain_db);
216
217         // The label will be updated by the audio level callback.
218 }
219
220 void MainWindow::cutoff_knob_changed(int value)
221 {
222         float octaves = value * 0.1f;
223         float cutoff_hz = 20.0 * pow(2.0, octaves);
224         global_mixer->set_locut_cutoff(cutoff_hz);
225
226         char buf[256];
227         snprintf(buf, sizeof(buf), "%ld Hz", lrintf(cutoff_hz));
228         ui->locut_cutoff_display->setText(buf);
229 }
230
231 void MainWindow::limiter_threshold_knob_changed(int value)
232 {
233         float threshold_dbfs = value * 0.1f;
234         global_mixer->set_limiter_threshold_dbfs(threshold_dbfs);
235
236         char buf[256];
237         snprintf(buf, sizeof(buf), "%.1f dB", threshold_dbfs);
238         ui->limiter_threshold_db_display->setText(buf);
239 }
240
241 void MainWindow::compressor_threshold_knob_changed(int value)
242 {
243         float threshold_dbfs = value * 0.1f;
244         global_mixer->set_compressor_threshold_dbfs(threshold_dbfs);
245
246         char buf[256];
247         snprintf(buf, sizeof(buf), "%.1f dB", threshold_dbfs);
248         ui->compressor_threshold_db_display->setText(buf);
249 }
250
251 void MainWindow::reset_meters_button_clicked()
252 {
253         global_mixer->reset_meters();
254         ui->peak_display->setText("-inf");
255         ui->peak_display->setStyleSheet("");
256 }
257
258 void MainWindow::audio_level_callback(float level_lufs, float peak_db, float global_level_lufs,
259                                       float range_low_lufs, float range_high_lufs,
260                                       float gain_staging_db, float final_makeup_gain_db,
261                                       float correlation)
262 {
263         timeval now;
264         gettimeofday(&now, nullptr);
265
266         // The meters are somewhat inefficient to update. Only update them
267         // every 100 ms or so (we get updates every 5–20 ms).
268         double last_update_age = now.tv_sec - last_audio_level_callback.tv_sec +
269                 1e-6 * (now.tv_usec - last_audio_level_callback.tv_usec);
270         if (last_update_age < 0.100) {
271                 return;
272         }
273         last_audio_level_callback = now;
274
275         post_to_main_thread([=]() {
276                 ui->vu_meter->set_level(level_lufs);
277                 ui->lra_meter->set_levels(global_level_lufs, range_low_lufs, range_high_lufs);
278                 ui->correlation_meter->set_correlation(correlation);
279
280                 char buf[256];
281                 snprintf(buf, sizeof(buf), "%.1f", peak_db);
282                 ui->peak_display->setText(buf);
283                 if (peak_db > -0.1f) {  // -0.1 dBFS is EBU peak limit.
284                         ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
285                 } else {
286                         ui->peak_display->setStyleSheet("");
287                 }
288
289                 ui->gainstaging_knob->blockSignals(true);
290                 ui->gainstaging_knob->setValue(lrintf(gain_staging_db * 10.0f));
291                 ui->gainstaging_knob->blockSignals(false);
292                 snprintf(buf, sizeof(buf), "%+.1f dB", gain_staging_db);
293                 ui->gainstaging_db_display->setText(buf);
294
295                 ui->makeup_gain_knob->blockSignals(true);
296                 ui->makeup_gain_knob->setValue(lrintf(final_makeup_gain_db * 10.0f));
297                 ui->makeup_gain_knob->blockSignals(false);
298                 snprintf(buf, sizeof(buf), "%+.1f dB", final_makeup_gain_db);
299                 ui->makeup_gain_db_display->setText(buf);
300         });
301 }
302
303 void MainWindow::relayout()
304 {
305         int height = ui->vertical_layout->geometry().height();
306
307         double remaining_height = height;
308
309         // Allocate the height; the most important part is to keep the main displays
310         // at 16:9 if at all possible.
311         double me_width = ui->me_preview->width();
312         double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height() + ui->preview_vertical_layout->spacing();
313
314         // TODO: Scale the widths when we need to do this.
315         if (me_height / double(height) > 0.8) {
316                 me_height = height * 0.8;
317         }
318         remaining_height -= me_height + ui->vertical_layout->spacing();
319
320         double audiostrip_height = ui->audiostrip->geometry().height();
321         remaining_height -= audiostrip_height + ui->vertical_layout->spacing();
322
323         // The previews will be constrained by the remaining height, and the width.
324         double preview_label_height = previews[0]->title_bar->geometry().height() +
325                 previews[0]->main_vertical_layout->spacing();
326         int preview_total_width = ui->preview_displays->geometry().width() - (previews.size() - 1) * ui->preview_displays->spacing();
327         double preview_height = min(remaining_height - preview_label_height, (preview_total_width / double(previews.size())) * 9.0 / 16.0);
328         remaining_height -= preview_height + preview_label_height + ui->vertical_layout->spacing();
329
330         ui->vertical_layout->setStretch(0, lrintf(me_height));
331         ui->vertical_layout->setStretch(1, 0);  // Don't stretch the audiostrip.
332         ui->vertical_layout->setStretch(2, max<int>(1, remaining_height));  // Spacer.
333         ui->vertical_layout->setStretch(3, lrintf(preview_height + preview_label_height));
334
335         // Set the widths for the previews.
336         double preview_width = preview_height * 16.0 / 9.0;
337         for (unsigned i = 0; i < previews.size(); ++i) {
338                 ui->preview_displays->setStretch(i, lrintf(preview_width));
339         }
340
341         // The preview horizontal spacer.
342         double remaining_preview_width = preview_total_width - previews.size() * preview_width;
343         ui->preview_displays->setStretch(previews.size(), lrintf(remaining_preview_width));
344 }
345
346 void MainWindow::set_transition_names(vector<string> transition_names)
347 {
348         if (transition_names.size() < 1 || transition_names[0].empty()) {
349                 transition_btn1->setText(QString(""));
350         } else {
351                 transition_btn1->setText(QString::fromStdString(transition_names[0] + " (J)"));
352                 ui->transition_btn1->setShortcut(QKeySequence("J"));
353         }
354         if (transition_names.size() < 2 || transition_names[1].empty()) {
355                 transition_btn2->setText(QString(""));
356         } else {
357                 transition_btn2->setText(QString::fromStdString(transition_names[1] + " (K)"));
358                 ui->transition_btn2->setShortcut(QKeySequence("K"));
359         }
360         if (transition_names.size() < 3 || transition_names[2].empty()) {
361                 transition_btn3->setText(QString(""));
362         } else {
363                 transition_btn3->setText(QString::fromStdString(transition_names[2] + " (L)"));
364                 ui->transition_btn3->setShortcut(QKeySequence("L"));
365         }
366 }
367
368 void MainWindow::update_channel_name(Mixer::Output output, const string &name)
369 {
370         if (output >= Mixer::OUTPUT_INPUT0) {
371                 unsigned channel = output - Mixer::OUTPUT_INPUT0;
372                 previews[channel]->label->setText(name.c_str());
373         }
374 }
375
376 void MainWindow::update_channel_color(Mixer::Output output, const string &color)
377 {
378         if (output >= Mixer::OUTPUT_INPUT0) {
379                 unsigned channel = output - Mixer::OUTPUT_INPUT0;
380                 previews[channel]->frame->setStyleSheet(QString::fromStdString("background-color:" + color));
381         }
382 }
383
384 void MainWindow::transition_clicked(int transition_number)
385 {
386         global_mixer->transition_clicked(transition_number);
387 }
388
389 void MainWindow::channel_clicked(int channel_number)
390 {
391         if (current_wb_pick_display == channel_number) {
392                 // The picking was already done from eventFilter(), since we don't get
393                 // the mouse pointer here.
394         } else {
395                 global_mixer->channel_clicked(channel_number);
396         }
397 }
398
399 void MainWindow::wb_button_clicked(int channel_number)
400 {
401         current_wb_pick_display = channel_number;
402         QApplication::setOverrideCursor(Qt::CrossCursor);
403 }
404
405 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
406 {
407         if (current_wb_pick_display != -1 &&
408             event->type() == QEvent::MouseButtonRelease &&
409             watched->isWidgetType()) {
410                 QApplication::restoreOverrideCursor();
411                 if (watched == previews[current_wb_pick_display]->display) {
412                         const QMouseEvent *mouse_event = (QMouseEvent *)event;
413                         set_white_balance(current_wb_pick_display, mouse_event->x(), mouse_event->y());
414                 } else {
415                         // The user clicked on something else, give up.
416                         // (The click goes through, which might not be ideal, but, yes.)
417                         current_wb_pick_display = -1;
418                 }
419         }
420         return false;
421 }
422
423 namespace {
424
425 double srgb_to_linear(double x)
426 {
427         if (x < 0.04045) {
428                 return x / 12.92;
429         } else {
430                 return pow((x + 0.055) / 1.055, 2.4);
431         }
432 }
433
434 }  // namespace
435
436 void MainWindow::set_white_balance(int channel_number, int x, int y)
437 {
438         // Set the white balance to neutral for the grab. It's probably going to
439         // flicker a bit, but hopefully this display is not live anyway.
440         global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, 0.5, 0.5, 0.5);
441         previews[channel_number]->display->updateGL();
442         QRgb reference_color = previews[channel_number]->display->grabFrameBuffer().pixel(x, y);
443
444         double r = srgb_to_linear(qRed(reference_color) / 255.0);
445         double g = srgb_to_linear(qGreen(reference_color) / 255.0);
446         double b = srgb_to_linear(qBlue(reference_color) / 255.0);
447         global_mixer->set_wb(Mixer::OUTPUT_INPUT0 + channel_number, r, g, b);
448         previews[channel_number]->display->updateGL();
449 }