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