]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Set the peak meter to red when peaking.
[nageru] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include <thread>
3 #include <vector>
4 #include <string>
5 #include <QSignalMapper>
6 #include <QMetaType>
7 #include <QShortcut>
8 #include <QResizeEvent>
9
10 #include "context.h"
11 #include "mixer.h"
12
13 #include "ui_mainwindow.h"
14
15 using namespace std;
16
17 Q_DECLARE_METATYPE(std::vector<std::string>);
18
19 MainWindow *global_mainwindow = nullptr;
20
21 MainWindow::MainWindow()
22         : ui(new Ui::MainWindow)
23 {
24         global_mainwindow = this;
25         ui->setupUi(this);
26
27         ui->me_live->set_output(Mixer::OUTPUT_LIVE);
28         ui->me_preview->set_output(Mixer::OUTPUT_PREVIEW);
29
30         // TODO: Ask for the real number.
31         ui->preview1->set_output(Mixer::OUTPUT_INPUT0);
32         ui->preview2->set_output(Mixer::OUTPUT_INPUT1);
33         ui->preview3->set_output(Mixer::OUTPUT_INPUT2);
34
35         // Hook up the preview clicks.
36         {
37                 QSignalMapper *mapper = new QSignalMapper(this);
38                 mapper->setMapping(ui->preview1, 0),
39                 mapper->setMapping(ui->preview2, 1);
40                 mapper->setMapping(ui->preview3, 2);
41                 connect(ui->preview1, SIGNAL(clicked()), mapper, SLOT(map()));
42                 connect(ui->preview2, SIGNAL(clicked()), mapper, SLOT(map()));
43                 connect(ui->preview3, SIGNAL(clicked()), mapper, SLOT(map()));
44
45                 connect(mapper, SIGNAL(mapped(int)), this, SLOT(channel_clicked(int)));
46         }
47
48         // Hook up the preview keyboard keys.
49         {
50                 QSignalMapper *mapper = new QSignalMapper(this);
51                 QShortcut *shortcut1 = new QShortcut(QKeySequence(Qt::Key_1), this);
52                 connect(shortcut1, SIGNAL(activated()), mapper, SLOT(map()));
53                 QShortcut *shortcut2 = new QShortcut(QKeySequence(Qt::Key_2), this);
54                 connect(shortcut2, SIGNAL(activated()), mapper, SLOT(map()));
55                 QShortcut *shortcut3 = new QShortcut(QKeySequence(Qt::Key_3), this);
56                 connect(shortcut3, SIGNAL(activated()), mapper, SLOT(map()));
57                 mapper->setMapping(shortcut1, 0),
58                 mapper->setMapping(shortcut2, 1);
59                 mapper->setMapping(shortcut3, 2);
60
61                 connect(mapper, SIGNAL(mapped(int)), this, SLOT(channel_clicked(int)));
62         }
63
64         // Hook up the transition buttons.
65         // TODO: Make them dynamic.
66         {
67                 QSignalMapper *mapper = new QSignalMapper(this);
68                 mapper->setMapping(ui->transition_btn1, 0),
69                 mapper->setMapping(ui->transition_btn2, 1);
70                 mapper->setMapping(ui->transition_btn3, 2);
71                 connect(ui->transition_btn1, SIGNAL(clicked()), mapper, SLOT(map()));
72                 connect(ui->transition_btn2, SIGNAL(clicked()), mapper, SLOT(map()));
73                 connect(ui->transition_btn3, SIGNAL(clicked()), mapper, SLOT(map()));
74                 connect(mapper, SIGNAL(mapped(int)), this, SLOT(transition_clicked(int)));
75         }
76
77         // Aiee...
78         transition_btn1 = ui->transition_btn1;
79         transition_btn2 = ui->transition_btn2;
80         transition_btn3 = ui->transition_btn3;
81         qRegisterMetaType<std::vector<std::string>>("std::vector<std::string>");
82         connect(ui->preview1, SIGNAL(transition_names_updated(std::vector<std::string>)),
83                 this, SLOT(set_transition_names(std::vector<std::string>)));
84 }
85
86 void MainWindow::resizeEvent(QResizeEvent* event)
87 {
88         QMainWindow::resizeEvent(event);
89
90         // Ask for a relayout, but only after the event loop is done doing relayout
91         // on everything else.
92         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
93 }
94
95 void MainWindow::mixer_created(Mixer *mixer)
96 {
97         mixer->set_audio_level_callback([this](float level_lufs, float peak_db){
98                 ui->vu_meter->set_level(level_lufs);
99
100                 char buf[256];
101                 snprintf(buf, sizeof(buf), "%.1f", peak_db);
102                 ui->peak_display->setText(buf);
103                 if (peak_db > -0.1f) {  // -0.1 dBFS is EBU peak limit.
104                         ui->peak_display->setStyleSheet("QLabel { background-color: red; color: white; }");
105                 } else {
106                         ui->peak_display->setStyleSheet("");
107                 }
108         });
109 }
110
111 void MainWindow::relayout()
112 {
113         int width = size().width();
114         int height = size().height();
115
116         // Allocate the height; the most important part is to keep the main displays
117         // at 16:9 if at all possible.
118         double me_width = ui->me_preview->width();
119         double me_height = me_width * 9.0 / 16.0 + ui->label_preview->height();
120
121         // TODO: Scale the widths when we need to do this.
122         if (me_height / double(height) > 0.8) {
123                 me_height = height * 0.8;
124         }
125
126         // The previews will be constrained by the remaining height, and the width.
127         // FIXME: spacing?
128         double preview_height = std::min(height - me_height, (width / 4.0) * 9.0 / 16.0);
129
130         ui->vertical_layout->setStretch(0, lrintf(me_height));
131         ui->vertical_layout->setStretch(1, std::max<int>(1, lrintf(height - me_height - preview_height)));
132         ui->vertical_layout->setStretch(2, lrintf(preview_height));
133
134         // Set the widths for the previews.
135         double preview_width = preview_height * 16.0 / 9.0;  // FIXME: spacing?
136
137         ui->preview_displays->setStretch(0, lrintf(preview_width));
138         ui->preview_displays->setStretch(1, lrintf(preview_width));
139         ui->preview_displays->setStretch(2, lrintf(preview_width));
140         ui->preview_displays->setStretch(3, lrintf(preview_width));
141         ui->preview_displays->setStretch(4, lrintf(width - 4.0 * preview_width));
142 }
143
144 void MainWindow::set_transition_names(vector<string> transition_names)
145 {
146         if (transition_names.size() < 1) {
147                 transition_btn1->setText(QString(""));
148         } else {
149                 transition_btn1->setText(QString::fromStdString(transition_names[0]));
150         }
151         if (transition_names.size() < 2) {
152                 transition_btn2->setText(QString(""));
153         } else {
154                 transition_btn2->setText(QString::fromStdString(transition_names[1]));
155         }
156         if (transition_names.size() < 3) {
157                 transition_btn3->setText(QString(""));
158         } else {
159                 transition_btn3->setText(QString::fromStdString(transition_names[2]));
160         }
161 }
162
163 void MainWindow::transition_clicked(int transition_number)
164 {
165         global_mixer->transition_clicked(transition_number);
166 }
167
168 void MainWindow::channel_clicked(int channel_number)
169 {
170         global_mixer->channel_clicked(channel_number);
171 }