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