-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainWindow.cpp
More file actions
280 lines (253 loc) · 8.55 KB
/
mainWindow.cpp
File metadata and controls
280 lines (253 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "mainWindow.hpp"
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/messagedialog.h>
#include <cassert>
#include "footPedal.hpp"
bool MainWindow::updatePosition() {
unsigned seconds = player->getPositionMilliseconds() / 1000u;
char time[9]; std::snprintf(time, 9, "%2u:%02u", seconds / 60u, seconds % 60u);
positionLabel.set_text(time);
if (!adjustingSlider) slider.set_value(player->getPositionPercentage());
// update the play button if we reach the end of the file
if (playButton.get_image() == &pauseIcon && player->isPaused()) {
playButton.set_image(playIcon);
}
return true;
}
void MainWindow::updateNameAndDurationLabels() {
char *fileName; player->getFilename(&fileName);
if (fileName != NULL) {
int start = 0, end = -1;
while (fileName[++end] != '\0') {
if (fileName[end] == '/') start = end+1;
}
char time[9];
register const unsigned seconds = player->getLengthMilliseconds() / 1000u;
std::snprintf(time, 9, "%2u:%02u", seconds / 60u, seconds % 60u);
audioFileLabel.set_text(&fileName[start]);
lengthLabel.set_text(time);
delete[] fileName;
} else {
audioFileLabel.set_text("No audio file loaded.");
lengthLabel.set_text(" 0:00");
}
}
void MainWindow::playButtonPressed() {
if (player->isPaused()) {
player->skipBack((int)options.skipBackOnPlay);
player->play();
playButton.set_image(pauseIcon);
} else {
player->pause();
playButton.set_image(playIcon);
}
}
void MainWindow::restartButtonPressed() {
player->setPositionMilliseconds(0);
}
void MainWindow::skipBack10ButtonPressed() {
player->skipBack(10000);
}
void MainWindow::skipBack5ButtonPressed() {
player->skipBack(5000);
}
void MainWindow::slowButtonPressed() {
player->toggleSlow();
}
void MainWindow::rewindButtonPressed() {
player->startRewind();
}
void MainWindow::rewindButtonReleased() {
player->stopRewind();
}
void MainWindow::fastForwardButtonPressed() {
player->startFastForward();
}
void MainWindow::fastForwardButtonReleased() {
player->stopFastForward();
}
void MainWindow::helpButtonPressed() {
WindowList::help->show();
}
void MainWindow::openButtonPressed() {
Gtk::FileChooserDialog fchooser("Select an audio file", Gtk::FILE_CHOOSER_ACTION_OPEN);
fchooser.set_transient_for(*this);
Glib::RefPtr<Gtk::FileFilter> filter = Gtk::FileFilter::create();
filter->set_name("Supported Audio Files");
filter->add_mime_type("application/ogg");
filter->add_mime_type("audio/aiff");
filter->add_mime_type("audio/basic");
filter->add_mime_type("audio/flac");
filter->add_mime_type("audio/ogg");
filter->add_mime_type("audio/x-aiff");
filter->add_mime_type("audio/x-mp3");
filter->add_mime_type("audio/x-wav");
filter->add_pattern("*.ogg");
filter->add_pattern("*.flac");
filter->add_pattern("*.wav");
filter->add_pattern("*.snd");
filter->add_pattern("*.au");
filter->add_pattern("*.aiff");
filter->add_pattern("*.aif");
filter->add_pattern("*.aifc");
filter->add_pattern("*.mp3");
filter->add_pattern("*.mp2");
filter->add_pattern("*.iff");
filter->add_pattern("*.8svx");
filter->add_pattern("*.pvf");
filter->add_pattern("*.caf");
filter->add_pattern("*.voc");
filter->add_pattern("*.vox");
filter->add_pattern("*.cda");
filter->add_pattern("*.avr");
filter->add_pattern("*.cvs");
filter->add_pattern("*.vms");
filter->add_pattern("*.gsm");
filter->add_pattern("*.htk");
filter->add_pattern("*.hcom");
fchooser.add_filter(filter);
Glib::RefPtr<Gtk::FileFilter> all = Gtk::FileFilter::create();
all->set_name("All Files");
all->add_pattern("*");
fchooser.add_filter(all);
fchooser.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
fchooser.add_button("_Open", Gtk::RESPONSE_OK);
if (fchooser.run() == Gtk::RESPONSE_OK) {
player->closeFile();
try {
player->openFile(fchooser.get_filename().c_str(), options);
} catch (const std::exception &ex) {
player->closeFile();
Gtk::MessageDialog(*this, Glib::ustring(ex.what()), false, Gtk::MESSAGE_ERROR).run();
}
updateNameAndDurationLabels();
}
playButton.set_image(playIcon);
}
FLATTEN void MainWindow::optionsButtonPressed() {
WindowList::options->show();
}
FLATTEN void MainWindow::pedalConfigButtonPressed() {
WindowList::config->show();
}
void MainWindow::showSlowSpeedButtonPressed() {
if (slowSpeedPopout.get_visible()) {
slowSpeedPopout.hide();
} else {
slowSpeedPopout.show_all();
}
resize(1,1);
}
bool MainWindow::sliderPressed(__attribute__((unused)) GdkEventButton *ev) {
adjustingSlider = true;
wasPlaying = !player->isPaused();
player->pause();
playButton.set_image(playIcon);
return false;
}
bool MainWindow::sliderMoved(__attribute__((unused)) Gtk::ScrollType type, double value) {
player->setPositionPercentage(value);
return true;
}
bool MainWindow::sliderReleased(__attribute__((unused)) GdkEventButton *ev) {
adjustingSlider = false;
if (wasPlaying) player->play();
return false;
}
void MainWindow::updateOptions(const Options &opt) {
player->setOptions(opt);
slowSpeedSlider.set_value(opt.slowSpeed);
player->setSlowSpeed(opt.slowSpeed);
options = opt;
}
void MainWindow::onPedalEvent(Action cmd) {
/* Perform action */
switch (cmd.type) {
case Action::PLAY: player->play(); player->skipBack((int)options.skipBackOnPlay); break;
case Action::PAUSE: player->pause(); break;
case Action::TOGGLE_PLAY: player->togglePlay(); if (!player->isPaused()) player->skipBack((int)options.skipBackOnPlay); break;
case Action::SLOW: player->slow(); player->play(); player->skipBack((int)options.skipBackOnPlay); break;
case Action::UNSLOW: player->unslow(); player->pause(); break;
case Action::TOGGLE_SLOW: player->toggleSlow(); break; //toggling SLOW should not play, pause, or skip back
case Action::REWIND: player->startRewind(); break;
case Action::STOP_REWIND: player->stopRewind(); break;
case Action::TOGGLE_REWIND: player->toggleRewind(); break;
case Action::FAST_FORWARD: player->startFastForward(); break;
case Action::STOP_FAST_FORWARD: player->stopFastForward(); break;
case Action::TOGGLE_FAST_FORWARD: player->toggleFastForward(); break;
case Action::SKIP: player->skipForward(100 * (int)cmd.deciseconds); return;
case Action::RESTART: player->setPositionMilliseconds(0); return;
case Action::CHANGE_SLOW_SPEED:
newSlowSpeed = (unsigned char) (100.f * player->increaseSlowSpeed(0.01f * (float)cmd.percent));
refreshSlowSpeed.emit(); break;
default: return;
}
/* Tell the UI to update accordingly */
actionLock.lock();
actionQueue.push(cmd.type);
actionLock.unlock();
updateUI.emit();
}
/*
* Update the UI as appropriate when the user performs an action
* with the footpedal. This function only updates the UI. The
* actual actions are performed in the onPedalEvent function above
*/
void MainWindow::onUpdateRequest() {
actionLock.lock();
while (!actionQueue.empty()) {
auto event = actionQueue.front();
actionQueue.pop();
switch (event) {
case Action::PLAY:
case Action::TOGGLE_PLAY:
case Action::PAUSE:
playButton.set_image(player->isPaused() ? playIcon : pauseIcon);
break;
case Action::SLOW:
case Action::UNSLOW:
case Action::TOGGLE_SLOW:
if (player->isSlowed()) {
slowButton.set_state_flags(Gtk::StateFlags::STATE_FLAG_ACTIVE);
} else {
slowButton.unset_state_flags(Gtk::StateFlags::STATE_FLAG_ACTIVE);
}
break;
case Action::FAST_FORWARD:
case Action::STOP_FAST_FORWARD:
case Action::TOGGLE_FAST_FORWARD:
if (player->isFastForwarding()) {
fastForwardButton.set_state_flags(Gtk::StateFlags::STATE_FLAG_ACTIVE);
} else {
fastForwardButton.unset_state_flags(Gtk::StateFlags::STATE_FLAG_ACTIVE);
}
break;
case Action::REWIND:
case Action::STOP_REWIND:
case Action::TOGGLE_REWIND:
if (player->isRewinding()) {
rewindButton.set_state_flags(Gtk::StateFlags::STATE_FLAG_ACTIVE);
} else {
rewindButton.unset_state_flags(Gtk::StateFlags::STATE_FLAG_ACTIVE);
}
break;
default: break;
}
}
actionLock.unlock();
}
void MainWindow::onStreamError() const {
Gtk::MessageDialog(Glib::ustring("An error occurred while reading the audio file."), false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, false).run();
}
// this function is called when the slow speed is changed by a footpedal (using the increase/decrease slow speed action)
void MainWindow::onSlowSpeedChanged() {
register const double speed = 0.01 * (double)newSlowSpeed;
options.slowSpeed = (float)speed;
slowSpeedSlider.set_value(speed);
}
// ...and this one is called when the slow speed UI slider is moved
bool MainWindow::onSlowSpeedSliderMoved(__attribute__((unused)) Gtk::ScrollType type, double value) {
options.slowSpeed = (float)value;
player->setSlowSpeed((float)value);
return true;
}