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