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