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