-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainwindow.cpp
384 lines (349 loc) · 10.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
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QFileDialog>
#include <QTableWidgetItem>
#include <QMessageBox>
#include <QMimeData>
#include <QRegularExpression>
#include <QFontDatabase>
#include <QMediaDevices>
#include "outputselectiondialog.hpp"
#include "loopedpcmstreamer.hpp"
#ifdef _WIN32
#define NOMINMAX //Windows API breaks STL, shit.
#include <Windows.h>
#endif
QString fsstr_to_qstring(const fs::path::string_type &s)
{
#if PATH_VALSIZE == 2 //the degenerate platform
return QString::fromStdWString(s);
#else
return QString::fromStdString(s);
#endif
}
fs::path qstring_to_path(const QString &s)
{
#if PATH_VALSIZE == 2 //the degenerate platform
return fs::path(s.toStdWString());
#else
return fs::path(s.toStdString());
#endif
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setPlayListTableHeader();
ui->playlistTable->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->playlistTable->setSortingEnabled(false);
datw = nullptr;
devi = -1;
timer = new QTimer();
timer->setInterval(100);
connect(timer, &QTimer::timeout, this, &MainWindow::updateWidgets);
connect(ui->progressslider, &QSlider::sliderReleased, this, &MainWindow::seek);
timer->start();
}
bool MainWindow::args(QCommandLineParser &p)
{
argp = &p;
if (argp->isSet("list-devices"))
{
printf("List of available output devices:\n");
printf("Device ID\tDevice Name\n");
int id = 0;
for (auto &di : QMediaDevices::audioOutputs())
{
printf("%d \t%s\n", id++, di.description().toStdString().c_str());
}
return true;
}
if (argp->isSet("device"))
{
bool ok = false;
int t = argp->value("device").toInt(&ok);
if (!ok)
{
printf("--device: Number expected.\n");
return true;
}
if (t >= QMediaDevices::audioOutputs().size() || t < 0)
{
printf("--device: device ID out of range.\n");
return true;
}
devi = t;
}
if (argp->positionalArguments().size())
this->LoadFile(argp->positionalArguments()[0]);
return false;
}
/*! \brief Load file.
*
* Loads the game folder or any file in the folder.
* Determine game version.
* Returns true on success.
*/
bool MainWindow::LoadFile(QString filepath)
{
QUrl url(filepath), bgmurl;
bool isTrial = false;
if (QFileInfo(filepath).isFile())
{
url = url.adjusted(QUrl::RemoveFilename);
}
if (QFile::exists(url.url() + "/thbgm.dat"))
{
bgmurl = QUrl(url.url() + "/thbgm.dat");
}
else if (QFile::exists(url.url() + "/thbgm_tr.dat"))
{
isTrial = true;
bgmurl = QUrl(url.url() + "/thbgm_tr.dat");
}
else if (QFile::exists(url.url() + "/albgm.dat"))
{
bgmurl = QUrl(url.url() + "/albgm.dat");
}
songs.thbgmFilePath = bgmurl.url();
songs.isTrial = isTrial;
QDir gamedir = QDir(url.url());
QStringList sl;
sl << "*.dat";
QFileInfoList fil = gamedir.entryInfoList(sl, QDir::NoFilter, QDir::Name);
QString datf = "";
int ver = -1;
for (auto &i : fil)
{
if (~(ver = thVersionDetect(i)))
{
datf = i.absoluteFilePath();
break;
}
}
if (!datf.length())return false;
stop();
if (datw) delete datw;
datw = new thDatWrapper(qstring_to_path(datf), ver);
if (ver > 50) ver /= 10; //95,125,128,143 etc
thver = ver;
if (ver == 6)
{
songs.thbgmFilePath = url.url();
songs.LoadFile_th6(datw, fs::path(datf.toStdString()));
}
else
songs.LoadFile(datw, ver < 13 ? true : false);
ui->thnameLabel->setText(url.url());
SetupSongList();
return true;
}
/*! \brief Load song data from thbgm.songs file.
*
* Call to load thbgm.songs file.
* Then load song data from SongList to playlist table.
*/
bool MainWindow::SetupSongList()
{
ui->playlistTable->clear();
setPlayListTableHeader();
ui->playlistTable->setRowCount(songs.songCnt);
ui->playlistTable->setSortingEnabled(false);
QFont fnt = QFontDatabase::systemFont(QFontDatabase::SystemFont::FixedFont);
for (int i = 0; i < songs.songCnt; i++)
{
song_t *song = &songs.songs[i];
QString fileName(song->filename);
QTableWidgetItem *itemTitle = new QTableWidgetItem(song->title);
QTableWidgetItem *itemName = new QTableWidgetItem(fileName);
QTableWidgetItem *itemStart = new QTableWidgetItem("0x" + QString::number(song->start, 16));
QTableWidgetItem *itemLpSt = new QTableWidgetItem("0x" + QString::number(song->loopStart, 16));
QTableWidgetItem *itemLen = new QTableWidgetItem("0x" + QString::number(song->length, 16));
QTableWidgetItem *itemRate = new QTableWidgetItem(QString::number(song->rate));
ui->playlistTable->setItem(i, 0, itemTitle);
ui->playlistTable->setItem(i, 1, itemName);
ui->playlistTable->setItem(i, 2, itemStart);
ui->playlistTable->setItem(i, 3, itemLpSt);
ui->playlistTable->setItem(i, 4, itemLen);
ui->playlistTable->setItem(i, 5, itemRate);
for (int j = 1; j < 6; ++j)
ui->playlistTable->item(i, j)->setData(Qt::ItemDataRole::FontRole, fnt);
}
//ui->playlistTable->setSortingEnabled(true);
return true;
}
MainWindow::~MainWindow()
{
stop();
delete ui;
if (datw)delete datw;
}
void MainWindow::stop()
{
ui->pauseButton->setEnabled(false);
ui->pauseButton->setChecked(false);
if (audioOutput)
{
audioOutput->stop();
delete audioOutput;
audioOutput = nullptr;
st->close();
delete st;
st = nullptr;
}
}
// drag n drop
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
//check droped file
event->acceptProposedAction();
}
void MainWindow::dropEvent(QDropEvent *event)
{
QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty()) return;
QString fileName = urls.first().toLocalFile();
LoadFile(fileName);
}
void MainWindow::updateWidgets()
{
if (!st) return;
if (!cursong.length) return;
ui->progressslider->setValue((int)100.*st->pos_sample() / (cursong.length / 4)); //TODO: don't hardcode the 4 here
}
void MainWindow::seek()
{
st->seek_sample(ui->progressslider->value() / 100. * (cursong.length / 4.)); //TODO: don't hardcode the 4 here
}
QAudioFormat MainWindow::getAudioFormat(unsigned rate)
{
QAudioFormat audioFormat;
audioFormat.setChannelCount(2);
audioFormat.setSampleRate(rate);
audioFormat.setSampleFormat(QAudioFormat::SampleFormat::Int16);
return audioFormat;
}
int MainWindow::thVersionDetect(QFileInfo i)
{
auto str = i.fileName();
QRegularExpression re06("[Mm][Dd]\\.[Dd][Aa][Tt]");
auto mch = re06.match(str);
if (mch.hasMatch())
{
thDatWrapper mdw(qstring_to_path(i.filePath()), 6);
if (~mdw.getFileSize("musiccmt.txt")) return 6;
return -1;
}
if (str.startsWith("alcostg"))
return 103;
QRegularExpression re("^[Tt][Hh](\\d{2,3})");
mch = re.match(str);
if (!mch.hasMatch()) return -1;
QString ret = mch.captured(1);
return ret.toInt();
}
void MainWindow::setPlayListTableHeader()
{
ui->playlistTable->setHorizontalHeaderItem(0, new QTableWidgetItem("Title"));
ui->playlistTable->setHorizontalHeaderItem(1, new QTableWidgetItem("File"));
ui->playlistTable->setHorizontalHeaderItem(2, new QTableWidgetItem("Start"));
ui->playlistTable->setHorizontalHeaderItem(3, new QTableWidgetItem("Loop"));
ui->playlistTable->setHorizontalHeaderItem(4, new QTableWidgetItem("Length"));
ui->playlistTable->setHorizontalHeaderItem(5, new QTableWidgetItem("Rate"));
}
void MainWindow::play(int index)
{
int songIdx = -1;
if (index != -1) songIdx = index;
if (songIdx < 0 || songIdx >= songs.songCnt) return;
ui->songnameLabel->setText(songs.songs[songIdx].title.length() ? songs.songs[songIdx].title : songs.songs[songIdx].filename);
ui->commentTB->setText(songs.songs[songIdx].comment);
cursong = songs.songs[songIdx];
// audio playback:
QAudioFormat desiredFormat1 = getAudioFormat(songs.songs[songIdx].rate);
QAudioDevice info1(
~devi ? QMediaDevices::audioOutputs()[devi]
: QMediaDevices::defaultAudioOutput());
if (!info1.isFormatSupported(desiredFormat1))
{
qWarning() << "Default format not supported, trying to use the nearest.";
desiredFormat1 = info1.preferredFormat();
}
stop();
audioOutput = new QAudioSink(info1, desiredFormat1, this);
audioOutput->setVolume(1.0);
fs::path srcfile = qstring_to_path(songs.thbgmFilePath);
if (thver == 6)
srcfile /= fs::path("bgm") / songs.songs[songIdx].filename.toStdString();
st = new LoopedPCMStreamer(srcfile,
songs.songs[songIdx].start,
songs.songs[songIdx].length,
songs.songs[songIdx].loopStart);
st->open(QIODevice::OpenModeFlag::ReadOnly);
audioOutput->start(st);
if (audioOutput->error())
{
OutputSelectionDialog d;
d.init(devi);
d.exec();
devi = d.selection();
return play(index);
}
ui->pauseButton->setEnabled(true);
ui->pauseButton->setChecked(false);
}
void MainWindow::on_playlistTable_doubleClicked(const QModelIndex &index)
{
play(index.row());
}
void MainWindow::on_loopButton_clicked()
{
// TODO: ???
// ui->loopButton->setText(loopEnabled ? tr("Loop: On") : tr("Loop: Off"));
}
void MainWindow::on_prevButton_clicked()
{
int r = ui->playlistTable->currentRow();
int c = ui->playlistTable->currentColumn();
int rc = ui->playlistTable->rowCount();
r = (r + rc - 1) % rc;
ui->playlistTable->setCurrentCell(r, c);
play(r);
}
void MainWindow::on_nextButton_clicked()
{
int r = ui->playlistTable->currentRow();
int c = ui->playlistTable->currentColumn();
int rc = ui->playlistTable->rowCount();
r = (r + 1) % rc;
ui->playlistTable->setCurrentCell(r, c);
play(r);
}
void MainWindow::on_pauseButton_clicked(bool checked)
{
if (!audioOutput) return;
if (checked) audioOutput->suspend();
else audioOutput->resume();
}
void MainWindow::on_action_Open_triggered()
{
LoadFile(QFileDialog::getExistingDirectory(this, "Select game directory"));
}
void MainWindow::on_actionAbout_Qt_triggered()
{
qApp->aboutQt();
}
void MainWindow::on_action_About_triggered()
{
QMessageBox::about(this, "About TouHou Player",
QString("TouHou Player") + "\n"
"TouHou BGM player for all platform." + "\n\n" +
"https:://github.com/BearKidsTeam/thplayer");
}