-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
558 lines (471 loc) · 16.8 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QByteArray>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
lastSelectedDir = QDir::homePath() + "/Desktop";
}
MainWindow::~MainWindow()
{
delete ui;
}
#define RIFF 0x46464952
#define WAVE 0x45564157
#define NWAV 0x5641574E
#define smpl 0x6C706D73
#define STRM_BUF_PAGESIZE (64 * 32)
void MainWindow::printClrCon()
{
ui->console->clear();
conv_warning_fired = false;
qApp->processEvents();
}
void MainWindow::printCon(const QString& string)
{
ui->console->append(string);
qApp->processEvents();
}
void MainWindow::setEditMode(int mode)
{
ui->sampleRate_sb->setEnabled(mode == 2);
ui->loopStart_sb->setEnabled(mode != 0);
ui->loopEnd_sb->setEnabled(mode != 0);
ui->pcm8_rb->setEnabled(mode == 2);
ui->pcm16_rb->setEnabled(mode == 2);
ui->stereo_cb->setEnabled(mode == 2);
ui->changeEvents_pb->setEnabled(mode != 0);
if(mode == 0)
{
ui->sampleRate_sb->setValue(32728);
ui->loopStart_sb->setValue(0);
ui->loopEnd_sb->setValue(0);
ui->pcm8_rb->setChecked(true);
ui->pcm16_rb->setChecked(false);
ui->stereo_cb->setChecked(false);
}
}
QString MainWindow::ReadWAVTag(WAVStruct* wav, int fileSize, const QString& tagName)
{
for(int i = wav->Subchunk2Size; i < fileSize; i++)
{
if(QString(&wav->Data[i]) == tagName)
{
return QString(&wav->Data[i + tagName.length() + 1]).remove("TXXX");
}
}
return "";
}
//Gets the loop points from SMPL block
bool MainWindow::GetLoopSamples(WAVStruct* wav, int fileSize, int& loopStart, int& loopEnd)
{
for(int i = wav->Subchunk2Size; i < fileSize; i++)
{
int* data = reinterpret_cast<int*>(&wav->Data[i]);
if(data[0] == smpl)
{
loopStart = data[13];
loopEnd = data[14];
return true;
}
}
return false;
}
//Gets the loop points from WAV ID3 markers
bool MainWindow::GetLoopMarkers(WAVStruct* wav, int fileSize, int& loopStart, int& loopEnd)
{
bool ok;
QString aloopStart = ReadWAVTag(wav, fileSize, "loopStart");
loopStart = aloopStart.toInt(&ok);
if(!ok)
{
aloopStart = ReadWAVTag(wav, fileSize, "[");
if(aloopStart == "")
return false;
loopStart = aloopStart.toInt(&ok);
}
if(!ok)
{
printCon("Warning: loopStart tag is invalid.");
printCon("tagError1: Invalid number: " + aloopStart);
return false;
}
QString aloopEnd = ReadWAVTag(wav, fileSize, "loopEnd");
loopEnd = aloopEnd.toInt(&ok);
if(!ok)
{
aloopEnd = ReadWAVTag(wav, fileSize, "]");
if(aloopEnd == "")
return false;
loopEnd = aloopEnd.toInt(&ok);
}
if(loopEnd == 0 || !ok)
{
printCon("Warning: loopEnd tag is invalid.");
if(!ok)
printCon("tagError1: Invalid number: " + aloopEnd);
if(loopEnd == 0)
printCon("tagError2: loopEnd can't be 0.");
return false;
}
return true;
}
//Returns if RAW file can be read.
bool MainWindow::ReadWAV(QFile& file)
{
file.seek(0);
QByteArray input_file = file.readAll();
WAVStruct* wav = reinterpret_cast<WAVStruct*>(input_file.data());
printCon("Info: Checking for RIFF and WAVE header...");
if(wav->ChunkID == RIFF &&
wav->Format == WAVE)
{
printCon("Info: Checking if PCM format...");
if(wav->NumChannels > 2)
{
QMessageBox::critical(this, "Error.", "WAV file is not PCM format.\nCheck your exporting options before trying again.");
printCon("Error: WAV file is not PCM format. Check your exporting options before trying again.");
return false;
}
bool plural = wav->NumChannels > 1;
QString channel = plural ? " channels." : "channel.";
printCon("Info: Found " + QString::number(wav->NumChannels) + channel);
if(wav->NumChannels > 2)
{
QMessageBox::critical(this, "Error.", "WAV file has more than 2 channels.\nMake sure it only has 1 or 2 before trying again.");
printCon("Error: WAV file has more than 2 channels. Make sure it only has 1 or 2 before trying again.");
return false;
}
printCon("Info: Found " + QString::number(wav->SampleRate) + " sample rate.");
if(wav->SampleRate > 32728)
{
QMessageBox::critical(this, "Error.", "WAV file exceeds maximum sample rate 32728.\nLower it before trying again.");
printCon("Error: WAV file exceeds maximum sample rate 32728. Lower it before trying again.");
return false;
}
printCon("Info: Found " + QString::number(wav->BitsPerSample) + " bits per sample.");
if(wav->BitsPerSample != 8 && wav->BitsPerSample != 16)
{
QMessageBox::critical(this, "Error.", "The audio must be either PCM-8 or PCM-16.\nCheck your exporting options before trying again.");
printCon("Error: The audio must be either PCM-8 or PCM-16. Check your exporting options before trying again.");
return false;
}
ui->sampleRate_sb->setValue(wav->SampleRate);
ui->stereo_cb->setChecked(wav->NumChannels == 2);
ui->pcm8_rb->setChecked(wav->BitsPerSample == 8);
ui->pcm16_rb->setChecked(wav->BitsPerSample == 16);
int tagStart = wav->Subchunk2Size + static_cast<int>(sizeof(WAVStruct));
int fileSize = input_file.size();
if(fileSize > tagStart)
{
printCon("Info: Searching for loopStart and loopEnd...");
int loopStart[3];
int loopEnd[3];
bool gotSamples = GetLoopSamples(wav, fileSize, loopStart[1], loopEnd[1]);
bool gotMarkers = GetLoopMarkers(wav, fileSize, loopStart[2], loopEnd[2]);
bool useMarkers = false;
if(gotMarkers || gotSamples)
{
if(gotMarkers && !gotSamples)
{
useMarkers = true;
}
else if (gotMarkers && gotSamples)
{
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowTitle("Conflicting loop data.");
msgBox.setText("Both loop points and loop markers were found, which one do you want to use?");
msgBox.addButton("Loop Points", QMessageBox::YesRole);
QAbstractButton* pButtonMarkers = msgBox.addButton("Loop Markers", QMessageBox::NoRole);
msgBox.exec();
if (msgBox.clickedButton() == pButtonMarkers)
useMarkers = true;
}
}
else
{
goto loop_search_fail;
}
loopStart[0] = loopStart[1 + useMarkers];
loopEnd[0] = loopEnd[1 + useMarkers];
if(loopStart[0] > loopEnd[0])
{
QMessageBox::critical(this, "Error.", "loopStart can't be bigger than loopEnd, please try again after changing the tags.");
printCon("Error: loopStart can't be bigger than loopEnd.");
goto loop_search_fail;
}
printCon("Info: Found loopStart with value: " + QString::number(loopStart[0]) + ".");
printCon("Info: Found loopEnd with value: " + QString::number(loopEnd[0]) + ".");
ui->loopStart_sb->setValue(loopStart[0]);
ui->loopEnd_sb->setValue(loopEnd[0]);
goto loop_seach_succeed;
loop_search_fail:
printCon("Info: No valid loop data was found.");
loop_seach_succeed:
printCon("Info: Searching for events...");
for(int i = wav->Subchunk2Size; i < fileSize; i++)
{
QString tagName(&wav->Data[i]);
if(tagName.startsWith("event"))
{
QString trimmed = tagName;
trimmed = trimmed.remove("event");
QStringList list = trimmed.split('_');
QString eventID = list[1];
QString sample = QString(&wav->Data[i + tagName.length() + 1]).remove("TXXX");
bool ok[2];
int eventIDVal = eventID.toInt(&ok[0]);
int sampleVal = sample.toInt(&ok[1]);
if(!ok[0] || !ok[1])
{
printCon("Warning: Invalid event named: " + tagName + ".");
}
else
{
printCon("Info: Found " + tagName + " with value: " + sample + ".");
eventEditor.append(eventIDVal, sampleVal);
}
}
}
}
printCon("Info: Loading WAV raw data...");
raw_data.resize(wav->Subchunk2Size);
memcpy(raw_data.data(), wav->Data, wav->Subchunk2Size);
if(wav->BitsPerSample == 8)
{
printCon("Info: Converting unsigned PCM-8 to signed PCM-8...");
for(int i = 0; i < raw_data.size(); i++)
raw_data.data()[i] -= 0x80;
}
printCon("Info: Done and ready for conversion!");
setEditMode(1);
return false;
}
printCon("Info: Header not found, proceeding to RAW verification...");
return true;
}
void MainWindow::ReadRAW(QFile& file, QFileInfo& fInfo)
{
file.seek(0);
printCon("Info: Checking RAW extension...");
if(fInfo.suffix().toLower() != "raw")
{
QMessageBox::StandardButton reply =
QMessageBox::warning(this, "RAW File.",
"The detected file is not a WAV and it is missing the RAW file extension. Do you still want to import it?",
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (reply == QMessageBox::No)
{
printCon("Info: Operation canceled by the user.");
return;
}
}
printCon("Info: Loading raw data...");
raw_data = file.readAll();
printCon("Warning: Don't forget to adjust the settings!");
printCon("Info: Done and ready for conversion!");
setEditMode(2);
}
void MainWindow::on_search_pb_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open sound file", lastSelectedDir, "Sound Files (*.raw *.wav)");
if(fileName == "")
return;
lastSelectedDir = fileName;
ui->wavPath_le->setText(fileName);
ui->wavPath_le->editingFinished();
}
void MainWindow::on_wavPath_le_editingFinished()
{
static QString lastPath;
QString newPath = ui->wavPath_le->text();
if(lastPath == newPath)
return;
lastPath = newPath;
printClrCon();
raw_data.clear();
eventEditor.clear();
setEditMode(0);
if(newPath == "")
return;
QFileInfo fInfo(newPath);
if(!fInfo.exists())
{
QMessageBox::warning(this, "Invalid path.", "The path you introduced is invalid, please try again.");
printCon("Error: The path you introduced is invalid, please try again.");
return;
}
QFile file(newPath);
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::critical(this, "Error.", "Could not open file for reading, please try again.");
printCon("Error: Could not open file for reading, please try again.");
return;
}
fileSuffix = "." + fInfo.suffix();
if(ReadWAV(file))
ReadRAW(file, fInfo);
file.close();
}
QByteArray MainWindow::processRawData()
{
if(ui->stereo_cb->isChecked())
{
int dataSize = raw_data.size();
int chSize = dataSize / 2;
QByteArray buffer;
QByteArray left;
QByteArray right;
buffer.resize(dataSize);
left.resize(chSize);
right.resize(chSize);
//Desinterleave stereo
printCon("Info: Desinterleaving stereo...");
if(ui->pcm8_rb->isChecked())
{
for(int i = 0, j = 0; i < chSize; i++)
{
left[i] = raw_data[j++];
right[i] = raw_data[j++];
}
}
else
{
short* _left = reinterpret_cast<short*>(left.data());
short* _right = reinterpret_cast<short*>(right.data());
short* _raw_data = reinterpret_cast<short*>(raw_data.data());
for(int i = 0, j = 0; i < chSize / 2; i++)
{
_left[i] = _raw_data[j++];
_right[i] = _raw_data[j++];
}
}
//Interleave with page size
printCon("Info: Interleaving stereo with page size 0x" + QString::number(STRM_BUF_PAGESIZE, 16) + " interval per channel...");
char* pBuffer = buffer.data();
char* pChannels[2] = { left.data(), right.data() };
for(int bCursor = 0, cCursor = 0; cCursor < chSize;)
{
int len = STRM_BUF_PAGESIZE;
int remain = chSize - cCursor;
if(remain < len)
len = remain;
for(int c = 0; c < 2; c++)
{
memcpy(&pBuffer[bCursor], &pChannels[c][cCursor], len);
bCursor += len;
}
cCursor += len;
}
printCon("Info: Stereo conversion finished.");
return buffer;
}
else
{
return raw_data;
}
}
QByteArray MainWindow::nwavStructToBin(NWAVStruct& nwav)
{
QByteArray result;
result.resize(nwav.header.fileSize);
char* dest = result.data();
memcpy(dest,
&nwav.header, sizeof(NWAVHeader));
dest += sizeof(NWAVHeader);
if(nwav.header.numEvents)
{
int* lDest = reinterpret_cast<int*>(dest + eventIDBlockSize);
for(int i = 0; i < nwav.header.numEvents; i++)
{
dest[i] = nwav.events[i].eventID;
lDest[i] = nwav.events[i].sample;
}
dest += eventBlockSize;
}
memcpy(dest,
nwav.data.data(), nwav.data.size());
return result;
}
void MainWindow::on_convert_pb_clicked()
{
if(raw_data.size() == 0)
{
QMessageBox::warning(this, "Warning.", "Input file is missing, please try again after selecting one.");
if(!conv_warning_fired)
{
printCon("Warning: Input file is missing, please try again after selecting one.");
conv_warning_fired = true;
}
return;
}
int loopStart = ui->loopStart_sb->value();
int loopEnd = ui->loopEnd_sb->value();
if(loopStart != 0 || loopEnd != 0)
{
int diff = loopEnd - loopStart;
if(diff < 0)
{
printCon("Error: loopStart is bigger than loopEnd.");
return;
}
else if(diff < STRM_BUF_PAGESIZE)
{
printCon("Error: Diference between loopStart and loopEnd must be at least: " + QString::number(STRM_BUF_PAGESIZE) + ".");
return;
}
}
QString fileName = QFileDialog::getSaveFileName(this, "Save sound file", lastSelectedDir.remove(fileSuffix), "Sound Files (*.nwav)");
if(fileName == "")
return;
lastSelectedDir = fileName;
printClrCon();
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly))
{
QMessageBox::critical(this, "Error.", "Could not open file for writing, please try again.");
printCon("Error: Could not open file for writing, please try again.");
return;
}
QByteArray procRawData = processRawData();
int eventCount = eventEditor.eventEntries.size();
unalignedEvents = (eventCount % 4);
eventIDBlockSize = eventCount + (4 - unalignedEvents);
eventBlockSize = eventIDBlockSize + (eventCount * 4);
int dataOffset = sizeof(NWAVHeader) + eventBlockSize;
int nwavSize = dataOffset;
nwavSize += procRawData.size();
NWAVStruct nwav;
nwav.header.magic = NWAV;
nwav.header.fileSize = nwavSize;
nwav.header.sampleRate = ui->sampleRate_sb->value();
nwav.header.loopStart = loopStart;
nwav.header.loopEnd = loopEnd;
nwav.header.format = ui->pcm16_rb->isChecked();
nwav.header.stereo = ui->stereo_cb->isChecked();
nwav.header.numEvents = eventCount;
nwav.header.padding = 0;
nwav.events.resize(eventCount);
for(int i = 0; i < eventCount; i++)
{
nwav.events[i].eventID = eventEditor.eventEntries[i]->eventID;
nwav.events[i].sample = eventEditor.eventEntries[i]->sample;
}
nwav.data = procRawData;
QByteArray resultData = nwavStructToBin(nwav);
printCon("Info: Writing to file...");
file.write(resultData);
file.close();
printCon("Done! The file was saved to the destination.");
}
void MainWindow::on_changeEvents_pb_clicked()
{
eventEditor.show();
}