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