-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
411 lines (337 loc) · 10 KB
/
mainwindow.cpp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <fluidsynth.h>
#include <QDebug>
#include <QFileDialog>
#include <fstream>
fluid_settings_t* settings;
fluid_synth_t* synth;
fluid_player_t* player;
fluid_audio_driver_t* adriver;
bool font_loaded = false;
bool midi_loaded = false;
bool played_first_time = false;
bool isPlaying = false;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
settings = new_fluid_settings();
synth = new_fluid_synth(settings);
player = new_fluid_player(synth);
adriver = new_fluid_audio_driver(settings, synth);
scanFolders();
}
MainWindow::~MainWindow()
{
/* cleanup */
delete_fluid_audio_driver(adriver);
delete_fluid_player(player);
delete_fluid_synth(synth);
delete_fluid_settings(settings);
delete ui;
}
void MainWindow::play()
{
int status = fluid_player_get_status(player);
if (status == FLUID_PLAYER_READY)
fluid_player_play(player);
if (true && status == FLUID_PLAYER_DONE)
{
fluid_player_stop(player);
delete_fluid_player(player);
player = new_fluid_player(synth);
if (player == NULL)
{
//setError("Failed to play MIDI.");
return;
}
fluid_player_add_mem(player, midi.c_str(), midi.size());
fluid_player_play(player);
}
isPlaying = true;
//if (fluid_player_get_status(player) != FLUID_PLAYER_DONE)
// SoundStream::play();
/* play the midi files, if any */
//fluid_player_play(player);
/* wait for playback termination */
//fluid_player_join(player);
}
void MainWindow::stop()
{
// seems to work. turning sound off on all channels
fluid_synth_all_sounds_off(synth, -1);
//fluid_synth_all_notes_off(synth, -1);
fluid_player_stop(player);
fluid_synth_stop(synth, 1);
isPlaying = false;
}
// check if we're playing and do stuff accordingly
// also disable this button until we have both a soundfont and midi selected
void MainWindow::on_btn_Play_clicked()
{
//_midi_player.play();
play();
played_first_time = true;
updateStatus();
}
void MainWindow::on_btn_Stop_clicked()
{
//_midi_player.stop();
stop();
played_first_time = false;
updateStatus();
}
// Opens dialog window to load soundfonts and add to the listview (have to remove all the other shit so it's neater to look at)
void MainWindow::on_btn_AddSF_clicked()
{
QStringList fileNames = QFileDialog::getOpenFileNames(this,
tr("Add SoundFont"), "", tr("SF2 Files (*.sf2)"));
for (int i = 0; i < fileNames.count(); i++)
{
// verify filename before adding
// add the real path to a private list
// add just the name to the list (and use the index to get the real name later)
qDebug() << fileNames.at(i);
QString name = shortenString(fileNames[i]);
qDebug() << name;
sf_filenames.push_back(fileNames[i]);
ui->lst_SF2->addItem(name);
}
}
void MainWindow::on_btn_AddMidi_clicked()
{
QStringList fileNames = QFileDialog::getOpenFileNames(this,
tr("Add MIDI"), "", tr("MIDI Files (*.mid *.midi)"));
for (int i = 0; i < fileNames.count(); i++)
{
addMidi(fileNames.at(i));
}
}
void MainWindow::on_lst_SF2_itemSelectionChanged()
{
QString selectedName = sf_filenames[ui->lst_SF2->currentRow()];
if (fluid_is_soundfont(selectedName.toStdString().c_str()))
{
fluid_synth_sfload(synth, selectedName.toStdString().c_str(), 1);
font_loaded = true;
}
else
{
font_loaded = false;
}
if (midi_loaded && font_loaded)
{
ui->btn_Play->setEnabled(true);
ui->btn_Stop->setEnabled(true);
}
updateStatus();
}
void MainWindow::on_lst_MIDI_itemSelectionChanged()
{
QString selectedName = midi_filenames[ui->lst_MIDI->currentRow()];
if (fluid_is_midifile(selectedName.toStdString().c_str()))
{
// have to look over this
loadMidiFromFile(selectedName.toStdString()); // this doesnt work properly when deploying (only works in editor)
//if (played_first_time)
if (ui->chk_AutoPlay->isChecked())
play();
midi_loaded = true;
}
else
{
midi_loaded = false;
}
if (midi_loaded && font_loaded)
{
ui->btn_Play->setEnabled(true);
ui->btn_Stop->setEnabled(true);
}
updateStatus();
}
// this works somewhat
// instead of loading the midi file we load the memory
// it's not complete though
bool MainWindow::loadMidiFromFile(const std::string& filename)
{
stop();
if (player != NULL)
delete_fluid_player(player);
player = new_fluid_player(synth);
if (player == NULL)
return false;
// it crashed because not all dll files were included correctly
std::ifstream file(filename.c_str(), std::ios::binary);
if (!file.is_open())
{
//setError("Failed to load MIDI.");
return false;
}
file.seekg(0, std::ios::end);
unsigned int size = static_cast<unsigned int>(file.tellg());
file.seekg(0, std::ios::beg);
char* data = new char[size];
if (data == NULL)
{
//setError("Failed to load MIDI.");
return false;
}
file.read(data, size);
midi = std::string(data, size);
delete[] data;
file.close();
if (fluid_player_add_mem(player, midi.c_str(), midi.size()) != FLUID_OK)
return false;
return true;
// working solution with QFile instead of ifstream
/*
QFile file(filename);
if (!file.open(QIODevice::ReadOnly ))
return false;
unsigned int size = file.size();
char* data = new char[size];
if (data == NULL)
return false;
// seems like it's reading 0 bytes of data. Need to look into
auto b = file.read(data, size);
qDebug() << "qfile read " << b;
midi = std::string(data, size);
delete[] data;
file.close();
qDebug() << "printing data? " << midi.c_str();
if (fluid_player_add_mem(player, midi.c_str(), midi.size()) !=
FLUID_OK) {
return false;
}
*/
}
QString MainWindow::shortenString(const QString &filename)
{
// start backwards and remove only keep the last part of the name (stored backwards at first, then we flip it)
QString flippedName;
for (int i = filename.length(); i > 0; i--)
{
if (filename[i] == '/' || filename[i] == '\\')
break;
else
flippedName += filename[i];
}
// next we want to flip the name
QString normalName;
for (int i = flippedName.length(); i > 0; i--)
{
normalName += flippedName[i];
}
return normalName;
}
void MainWindow::addMidi(const QString &filename)
{
// verify filename before adding
// add the real path to a private list
// add just the name to the list (and use the index to get the real name later)
qDebug() << filename;
QString name = shortenString(filename);
qDebug() << name;
midi_filenames.push_back(filename);
ui->lst_MIDI->addItem(name);
}
void MainWindow::addSoundFont(const QString &filename)
{
// verify filename before adding
// add the real path to a private list
// add just the name to the list (and use the index to get the real name later)
qDebug() << filename;
QString name = shortenString(filename);
qDebug() << name;
midi_filenames.push_back(filename);
ui->lst_SF2->addItem(name);
}
QString MainWindow::getCurrentSF()
{
QString current = "No Soundfont";
if (ui->lst_SF2->currentItem() != nullptr)
current = ui->lst_SF2->currentItem()->text();
return current;
}
QString MainWindow::getCurrentSong()
{
QString current = "No Song";
if (ui->lst_MIDI->currentItem() != nullptr)
current = ui->lst_MIDI->currentItem()->text();
//if (midiSelected != -1)
// current = ui->lst_MIDI->item(midiSelected)->text();
return current;
}
void MainWindow::updateStatus()
{
QString playingStatus = _midi_player.getPlayingStatus() ? "Playing " : "Stopped ";
ui->lbl_Status->setText(playingStatus + getCurrentSong() + " using " + getCurrentSF());
}
void MainWindow::scanFolders()
{
scanMidiFolder();
scanSFFolder();
}
// BUGGY
void MainWindow::scanMidiFolder()
{
/*
QDirIterator dirIt("/midis", QDirIterator::Subdirectories);
while (dirIt.hasNext())
{
dirIt.next();
if (QFileInfo(dirIt.filePath()).isFile())
if (QFileInfo(dirIt.filePath()).suffix() == "mid" || QFileInfo(dirIt.filePath()).suffix() == "midi")
qDebug()<<dirIt.filePath();
}
*/
QStringList nameFilter("*.mid");
QDir directory(QDir::currentPath() + "/midis/");
QStringList midFiles = directory.entryList(nameFilter);
for (int i = 0; i < midFiles.size(); i++)
{
//qDebug() << midFiles.at(i);
//addMidi(midFiles.at(i));
qDebug() << midFiles.at(i);
QString name = shortenString(midFiles.at(i));
qDebug() << name;
midi_filenames.push_back(midFiles.at(i));
ui->lst_MIDI->addItem(name);
}
QStringList nameFilter2("*.midi");
QStringList midiFiles = directory.entryList(nameFilter2);
for (int i = 0; i < midiFiles.size(); i++)
{
//qDebug() << midiFiles.at(i);
//addMidi(midiFiles.at(i));
qDebug() << midFiles.at(i);
QString name = shortenString(midFiles.at(i));
qDebug() << name;
midi_filenames.push_back(midFiles.at(i));
ui->lst_MIDI->addItem(name);
}
}
void MainWindow::scanSFFolder()
{/*
QDirIterator dirIt("/soundfonts",QDirIterator::Subdirectories);
while (dirIt.hasNext())
{
dirIt.next();
if (QFileInfo(dirIt.filePath()).isFile())
if (QFileInfo(dirIt.filePath()).suffix() == "sf2")
qDebug()<<dirIt.filePath();
}*/
/*
QStringList nameFilter("*.sf2");
QDir directory(QDir::currentPath() + "/soundfonts");
QStringList sfFiles = directory.entryList(nameFilter);
for (int i = 0; i < sfFiles.size(); i++)
{
QString name = shortenString(sfFiles[i]);
qDebug() << name;
sf_filenames.push_back(sfFiles[i]);
ui->lst_SF2->addItem(name);
}*/
}