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