-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSongModule.cpp
More file actions
495 lines (426 loc) · 12.5 KB
/
Copy pathSongModule.cpp
File metadata and controls
495 lines (426 loc) · 12.5 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
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
#include "SongModule.hpp"
#include "FileUtils.hpp"
#include <QDebug>
#include <QFile>
#include <QByteArray>
template<>
int InstanceCounter<SongModule>::s_count;
template<>
int InstanceCounter<SongModule>::s_maxCount;
using namespace bb::cascades;
SongModule::SongModule(QObject *parent) :
SongExtendedInfo(parent),
m_currentOrder(0),
m_currentPattern(0),
m_currentRow(0),
m_currentSpeed(0),
m_currentTempo(0),
m_playingChannels(0),
m_modPlug(NULL) {
memset(&m_channelVU[0], 0, sizeof(m_channelVU));
}
SongModule::~SongModule() {
if (m_modPlug != NULL) {
::ModPlug_Unload(m_modPlug);
m_modPlug = NULL;
}
#ifdef VERBOSE_LOGGING
qDebug() << "SongModule::~SongModule()";
#endif
}
void SongModule::reset() {
setCurrentOrder(0);
setCurrentPattern(0);
setCurrentRow(0);
setCurrentSpeed(0);
setCurrentTempo(0);
setPlayingChannels(0);
setChannels(0);
setInstruments(0);
setChannels(0);
setSamples(0);
setPatterns(0);
setOrders(0);
setArtist("");
setArtistId(0);
setFormat("");
setTracker("");
setTrackerId(0);
setGenre("");
setGenreId(0);
setIconPath("");
setAbsoluteFileName("");
setDescription("");
memset(&m_channelVU[0], 0, sizeof(m_channelVU));
}
bool SongModule::songLoaded() const {
if(m_modPlug != NULL)
return true;
if(isHttpSong()) {
return true;
}
return m_absoluteFileName.length() > 0 && !isTrackerSong();
}
QString const& SongModule::absoluteFileName() const {
return m_absoluteFileName;
}
QString const& SongModule::description() const {
return m_description;
}
void SongModule::setDescription(const QString &value) {
QString description = value.trimmed();
if (m_description != description) {
m_description = description;
emit descriptionChanged();
}
}
int SongModule::currentOrder() const {
return m_currentOrder;
}
void SongModule::setCurrentOrder(int value) {
if (m_currentOrder != value) {
m_currentOrder = value;
emit currentOrderChanged();
}
}
int SongModule::currentPattern() const {
return m_currentPattern;
}
void SongModule::setCurrentPattern(int value) {
if (m_currentPattern != value) {
m_currentPattern = value;
emit currentPatternChanged();
}
}
int SongModule::currentRow() const {
return m_currentRow;
}
void SongModule::setCurrentRow(int value) {
if (m_currentRow != value) {
m_currentRow = value;
emit currentRowChanged();
}
}
int SongModule::currentSpeed() const {
return m_currentSpeed;
}
void SongModule::setCurrentSpeed(int value) {
if (m_currentSpeed != value) {
m_currentSpeed = value;
emit currentSpeedChanged();
}
}
int SongModule::currentTempo() const {
return m_currentTempo;
}
void SongModule::setCurrentTempo(int value) {
if (m_currentTempo != value) {
m_currentTempo = value;
emit currentTempoChanged();
}
}
int SongModule::playingChannels() const {
return m_playingChannels;
}
void SongModule::setPlayingChannels(int value) {
if (m_playingChannels != value) {
m_playingChannels = value;
emit playingChannelsChanged();
}
}
int SongModule::masterVolume() const {
if(m_modPlug != NULL) {
return ::ModPlug_GetMasterVolume(m_modPlug);
}
return 128;
}
void SongModule::setMasterVolume(int value) {
#ifdef VERBOSE_LOGGING
qDebug() << "*** SongModule::setMasterVolume" << value;
#endif
if(value < 1) {
value = 1;
}
if(value > 512) {
value = 512;
}
if (masterVolume() != value) {
if (m_modPlug != NULL) {
::ModPlug_SetMasterVolume(m_modPlug, value);
}
emit masterVolumeChanged();
}
}
bool SongModule::load(SongExtendedInfo const& info, QString const& fileName) {
if (m_absoluteFileName == fileName) {
return true; // not loading the same song twice
}
const bool songWasLoaded = m_modPlug != NULL || m_absoluteFileName.length() > 0;
if (m_modPlug != NULL) {
::ModPlug_Unload(m_modPlug);
m_modPlug = NULL;
}
reset();
if(SongFormat::isTrackerSong(fileName)) {
QFile fileIn(fileName);
if(fileIn.open(QFile::ReadOnly)) {
QByteArray data = fileIn.readAll();
m_modPlug = ::ModPlug_Load(data.data(), data.size());
if (m_modPlug != NULL) {
setAbsoluteFileName(fileName);
assignInfo(info);
setFileName(fileName);
setIconPath(SongFormat::getIconPath(fileName));
setTitle(::ModPlug_GetName(m_modPlug));
const char * description = ::ModPlug_GetMessage(m_modPlug);
setDescription(description ? description : "");
setInstruments(::ModPlug_NumInstruments(m_modPlug));
setChannels(::ModPlug_NumChannels(m_modPlug));
setSamples(::ModPlug_NumSamples(m_modPlug));
setPatterns(::ModPlug_NumPatterns(m_modPlug));
setOrders(::ModPlug_NumOrders(m_modPlug));
setFileSize(data.size());
setSongLength(::ModPlug_GetLength(m_modPlug));
update();
emit songLoadedChanged();
emit isTrackerSongChanged();
emit isHttpSongChanged();
} else {
qDebug() << "Failed to load ModPlug file" << fileName;
unload();
}
} else {
qDebug() << "Could not open tracker song" << fileName;
if(songWasLoaded) {
emit songLoadedChanged();
}
}
} else {
setAbsoluteFileName(fileName);
setFileName(fileName);
setIconPath(info.iconPath());
setDescription("");
setCurrentOrder(0);
setCurrentPattern(0);
setCurrentRow(0);
setCurrentSpeed(0);
setCurrentTempo(0);
setChannels(0);
setInstruments(0);
setChannels(0);
setSamples(0);
setPatterns(0);
setOrders(0);
assignInfo(info);
setCurrentOrder(0);
setOrders(info.songLength()); // song length in milliseconds
update();
emit songLoadedChanged();
emit isTrackerSongChanged();
emit isHttpSongChanged();
}
return songLoaded();
}
void SongModule::save(QString const& fileName) {
const QString originalFileName = absoluteFileName();
bool copyOk;
if(FileUtils::fileExists(fileName))
{
if(QFile::remove(fileName)) {
copyOk = true;
} else {
qDebug() << "Failed to remove file " << fileName;
copyOk = false;
}
} else {
copyOk = true;
}
if(copyOk) {
if(QFile::copy(originalFileName, fileName) == true) {
if(!FileUtils::adjustPermissions(fileName)) {
qDebug() << "Failed to set permissions for file " << fileName;
}
}
}
}
bool SongModule::unload() {
bool songWasLoaded = songLoaded();
if (m_modPlug != NULL) {
::ModPlug_Unload(m_modPlug);
m_modPlug = NULL;
}
reset();
update(true);
if(songWasLoaded) {
emit songLoadedChanged();
}
return true;
}
bool SongModule::rewind() {
if (m_modPlug != NULL) {
::ModPlug_SeekOrder(m_modPlug, 0);
update();
return true;
}
return false;
}
bool SongModule::seekToOrder(int order) {
if (m_modPlug != NULL) {
if(order != currentOrder()) {
::ModPlug_SeekOrder(m_modPlug, order);
update();
}
return true;
}
return false;
}
ArrayDataModel* SongModule::getSampleNames() {
ArrayDataModel * model = new ArrayDataModel();
if(m_modPlug != NULL) {
unsigned sampleCount = ::ModPlug_NumSamples(m_modPlug);
unsigned int i = 0;
while (sampleCount-- > 0) {
char buffer[64];
::ModPlug_SampleName(m_modPlug, i++, buffer);
QVariantMap sampleInfo;
sampleInfo["id"] = i;
sampleInfo["name"] = QVariant::fromValue(QString(buffer));
model->append(QVariant::fromValue(sampleInfo));
}
}
return model;
}
ArrayDataModel* SongModule::getInstrumentNames() {
ArrayDataModel * model = new ArrayDataModel();
if(m_modPlug != NULL) {
unsigned sampleCount = ::ModPlug_NumInstruments(m_modPlug);
unsigned int i = 0;
while (sampleCount-- > 0) {
char buffer[64];
::ModPlug_InstrumentName(m_modPlug, i++, buffer);
QVariantMap instrumentInfo;
instrumentInfo["id"] = i;
instrumentInfo["name"] = QVariant::fromValue(QString(buffer));
model->append(QVariant::fromValue(instrumentInfo));
}
}
return model;
}
void SongModule::setAbsoluteFileName(QString const& fileName) {
if(fileName != m_absoluteFileName) {
bool oldSongLoaded = songLoaded();
bool oldIsTrackerSong = isTrackerSong();
bool oldIsHttpSong = isHttpSong();
m_absoluteFileName = fileName;
emit absoluteFileNameChanged();
if(oldSongLoaded != songLoaded()) {
emit songLoadedChanged();
}
if(oldIsTrackerSong != isTrackerSong()) {
emit isTrackerSongChanged();
}
if(oldIsHttpSong != isHttpSong()) {
emit isHttpSongChanged();
}
}
}
void SongModule::assignInfo(SongExtendedInfo const& other) {
setId(other.id());
setTitle(other.title());
setFileSize(other.fileSize());
setSongLength(other.songLength());
setDownloads(other.downloads());
setFavourited(other.favourited());
setScore(other.score());
setPlayCount(other.playCount());
setLastPlayed(other.lastPlayed());
setMyFavourite(other.myFavourite());
setFormat(other.format());
setTracker(other.tracker());
setGenre(other.genre());
setGenreId(other.genreId());
setArtistId(other.artistId());
setArtist(other.artist());
}
void SongModule::update(bool endOfSong) {
if(m_modPlug != NULL && !endOfSong) {
setCurrentOrder(::ModPlug_GetCurrentOrder(m_modPlug));
setCurrentPattern(::ModPlug_GetCurrentPattern(m_modPlug));
setCurrentRow(::ModPlug_GetCurrentRow(m_modPlug));
setCurrentSpeed(::ModPlug_GetCurrentSpeed(m_modPlug));
setCurrentTempo(::ModPlug_GetCurrentTempo(m_modPlug));
setPlayingChannels(::ModPlug_GetPlayingChannels(m_modPlug));
setSongLength(::ModPlug_GetLength(m_modPlug));
} else {
if(endOfSong) {
setCurrentOrder(0);
setCurrentPattern(0);
setCurrentRow(0);
setCurrentSpeed(0);
setCurrentTempo(0);
setPlayingChannels(0);
}
}
updateChannelVU(endOfSong);
}
void SongModule::updateChannelVU(bool endOfSong) {
bool bChanged = false;
if(m_modPlug != NULL)
{
if(endOfSong)
{
// Set all channel VU to 0
memset(&m_channelVU[0], 0, sizeof(m_channelVU));
bChanged = true;
}
else
{
// Update all channel VU values
const int numChannels = channels();
unsigned result[sizeof(m_channelVU) / sizeof(m_channelVU[0])];
::ModPlug_GetChannelVUs(m_modPlug, 0, numChannels, result);
const size_t numBytes = sizeof(unsigned) * numChannels;
if(memcmp(m_channelVU, result, numBytes))
{
memcpy(m_channelVU, result, numBytes);
bChanged = true;
}
}
}
if(bChanged) {
emit channelVUChanged();
}
}
int SongModule::getChannelVU(int channel) {
if(m_modPlug != NULL)
{
if(channel >= 0 && channel < static_cast<int>(sizeof(m_channelVU)/sizeof(m_channelVU[0])))
{
return m_channelVU[channel];
}
}
return 0;
}
ModPlugNote* SongModule::getPattern(int pattern, int* numrows) {
ModPlugNote* result = NULL;
if(m_modPlug != NULL) {
result = ::ModPlug_GetPattern(m_modPlug,
pattern,
reinterpret_cast<unsigned int*>(numrows));
}
return result;
}
void SongModule::muteChannel(int channel, bool mute) {
if(m_modPlug != NULL) {
::ModPlug_MuteChannel(m_modPlug, channel, mute);
}
}
bool SongModule::isChannelMuted(int channel) {
if(m_modPlug != NULL) {
return ::ModPlug_IsChannelMuted(m_modPlug, channel);
}
return false;
}
SongModule::operator ModPlugFile*() {
return m_modPlug;
}