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