-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtfmxhelper.cpp
333 lines (287 loc) · 8.46 KB
/
tfmxhelper.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
#include "tfmxhelper.h"
static const char* findFilenameStart(const char *path, int *offset)
{
for(size_t i = strlen(path) - 1; i > 0; --i)
{
const char c = path[i];
if(c == '/' || c == '\\')
{
*offset = (int)(i + 1);
return &path[i + 1];
}
}
*offset = 0;
return path;
}
static const char* findExtension(const char *path, int *offset)
{
for(size_t i = strlen(path) - 1; i > 0; --i)
{
if(path[i] == '.')
{
*offset = (int)(i + 1);
return &path[i];
}
}
*offset = 0;
return path;
}
static bool loadMDAT(TfmxState *state, const char *mdatName, const char *smplName)
{
TfmxData mdat = {0};
TfmxData smpl = {0};
// first load mdat to memory
QFile mdatFile(mdatName);
if(!mdatFile.open(QIODevice::ReadOnly))
{
return false;
}
const QByteArray &mdatBuffer = mdatFile.readAll();
mdat.size = mdatBuffer.length();
mdat.data = (U8*)mdatBuffer.data();
// Load sample file to memory
QFile smplFile(smplName);
if(!smplFile.open(QIODevice::ReadOnly))
{
return false;
}
const QByteArray &smplBuffer = smplFile.readAll();
smpl.size = smplBuffer.length();
smpl.data = (U8*)smplBuffer.data();
return LoadTFMXFile(state, &mdat, &smpl);
}
static bool loadTFM(TfmxState *state, const char *path)
{
// first load mdat to memory
QFile mdatFile(path);
if(!mdatFile.open(QIODevice::ReadOnly))
{
return false;
}
TfmxData mdat = {0};
const QByteArray &mdatBuffer = mdatFile.readAll();
mdat.data = (U8*)mdatBuffer.data();
if(mdat.size <= 0)
{
return false;
}
// validate that the size is valid
if(mdat.size < sizeof(TFMXHeader) + 8 + (3 * 4))
{
qWarning("The file is too small (can't contain a full tfx file)");
return false;
}
U8 *raw = mdat.data;
if(strncmp("TFMX-MOD", (S8*)mdat.data, 8) != 0)
{
qWarning("The file doesn't have correct header data");
return false;
}
mdat.data += 8;
const U32 sOffset = *((U32*)mdat.data);
mdat.data += 4;
const U32 tOffset = *((U32*)mdat.data);
mdat.data += 8; // skipping res
mdat.size = sOffset - 20 - sizeof(TFMXHeader);
TfmxData smpl = {0};
smpl.data = raw + sOffset;
smpl.size = tOffset - sOffset;
if(mdat.size > 0x10000)
{
mdat.size = 0x10000;
}
return LoadTFMXFile(state, &mdat, &smpl);
}
static int TFMXLoad(TfmxState *state, const char *path)
{
int sOffset = 0;
int eOffset = 0;
bool status = false;
const char *filename = findFilenameStart(path, &sOffset);
const char *extension = findExtension(path, &eOffset);
if(strncasecmp(filename, "mdat.", 5) == 0)
{
// first check if we have mdat.<filename>,smpl.<filename> which is the orignal Amiga naming
char *smplName = strdup(path);
// Case-preserving conversion of "mdat" to "smpl"
smplName[sOffset + 0] ^= 'm' ^ 's';
smplName[sOffset + 1] ^= 'd' ^ 'm';
smplName[sOffset + 2] ^= 'a' ^ 'p';
smplName[sOffset + 3] ^= 't' ^ 'l';
status = loadMDAT(state, path, smplName);
free(smplName);
}
else if(strncasecmp(extension, ".mdat", 5) == 0)
{
// check if we have <filename>.mdat., <filename>.smpl
char *smplName = strdup(path);
// Case-preserving conversion of "mdat" to "smpl"
smplName[eOffset + 0] ^= 'm' ^ 's';
smplName[eOffset + 1] ^= 'd' ^ 'm';
smplName[eOffset + 2] ^= 'a' ^ 'p';
smplName[eOffset + 3] ^= 't' ^ 'l';
status = loadMDAT(state, path, smplName);
free(smplName);
}
else if(strncasecmp(filename, "tfmx.", 5) == 0)
{
// first check if we have tfmx.<filename>,smpl.<filename> which is the orignal Amiga naming
char *smplName = strdup(path);
// Case-preserving conversion of "tfmx" to "smpl"
smplName[sOffset + 0] ^= 't' ^ 's';
smplName[sOffset + 1] ^= 'f' ^ 'm';
smplName[sOffset + 2] ^= 'm' ^ 'p';
smplName[sOffset + 2] ^= 'x' ^ 'l';
status = loadMDAT(state, path, smplName);
free(smplName);
}
else if(strncasecmp(extension, ".tfmx", 5) == 0)
{
// Check for <filename>.tfmx, <filename>.sam
char *smplName = strdup(path);
// Case-preserving conversion of "tfmx" to "smpl"
smplName[eOffset + 0] ^= 't' ^ 's';
smplName[eOffset + 1] ^= 'f' ^ 'm';
smplName[eOffset + 2] ^= 'm' ^ 'p';
smplName[eOffset + 2] ^= 'x' ^ 'l';
status = loadMDAT(state, path, smplName);
free(smplName);
}
else if(strncasecmp(extension, ".tfx", 4) == 0)
{
if(!status)
{
// Check for <filename>.tfx, <filename>.sam
char *smplName = strdup(path);
// Case-preserving conversion of "tfx" to "sam"
smplName[eOffset + 0] ^= 't' ^ 's';
smplName[eOffset + 1] ^= 'f' ^ 'a';
smplName[eOffset + 2] ^= 'x' ^ 'm';
status = loadMDAT(state, path, smplName);
free(smplName);
}
if(!status)
{
// Check for <filename>.tfx, <filename>.smp
char *smplName = strdup(path);
// Case-preserving conversion of "tfx" to "smp"
smplName[eOffset + 0] ^= 't' ^ 's';
smplName[eOffset + 1] ^= 'f' ^ 'm';
smplName[eOffset + 2] ^= 'x' ^ 'p';
status = loadMDAT(state, path, smplName);
free(smplName);
}
}
else if(strncasecmp(extension, ".tfm", 3) == 0)
{
// Check for <filename>.tfm which is a single format
status = loadTFM(state, path);
}
return status;
}
TFMXHelper::TFMXHelper(const QString &path)
: m_path(path)
{
m_state = (TfmxState*)calloc(sizeof(TfmxState), 1);
}
TFMXHelper::~TFMXHelper()
{
deinit();
}
void TFMXHelper::deinit()
{
if(m_state)
{
TFMXQuit(m_state);
free(m_state);
}
}
bool TFMXHelper::initialize()
{
const int track = m_path.section("#", -1).toInt();
const QString &path = cleanPath();
TfmxState_init(m_state);
if(!TFMXLoad(m_state, qPrintable(path)))
{
qWarning("TFMXHelper: Unable to open file");
return false;
}
const int count = TFMXGetSubSongs(m_state);
if(track > count || track < 0)
{
qWarning("TFMXHelper: track number is out of range");
return false;
}
TFMXSetSubSong(m_state, (track == 0 ? count : track) - 1);
TFMXRewind(m_state);
return true;
}
qint64 TFMXHelper::read(unsigned char *data, qint64)
{
if(TFMXTryToMakeBlock(m_state) < 0)
{
return 0;
}
TFMXGetBlock(m_state, data);
return TFMXGetBlockSize(m_state);
}
QList<TrackInfo*> TFMXHelper::createPlayList(TrackInfo::Parts parts)
{
QList<TrackInfo*> playlist;
if(!m_state)
{
return playlist;
}
QFileInfo fin(cleanPath());
QString title = fin.baseName();
if(!filters().contains("*." + fin.suffix().toLower()))
{
title = fin.suffix();
}
for(int i = 1; i <= TFMXGetSubSongs(m_state); ++i)
{
TrackInfo *info = new TrackInfo();
if(parts & TrackInfo::MetaData)
{
info->setValue(Qmmp::TITLE, title);
info->setValue(Qmmp::TRACK, i);
}
if(parts & TrackInfo::Properties)
{
info->setValue(Qmmp::BITRATE, bitrate());
info->setValue(Qmmp::SAMPLERATE, sampleRate());
info->setValue(Qmmp::CHANNELS, channels());
info->setValue(Qmmp::BITS_PER_SAMPLE, depth());
info->setValue(Qmmp::FORMAT_NAME, "TFMX");
}
TFMXSetSubSong(m_state, i - 1);
TFMXRewind(m_state);
info->setPath("tfmx://" + cleanPath() + QString("#%1").arg(i));
info->setDuration(totalTime());
playlist << info;
}
return playlist;
}
QString TFMXHelper::cleanPath() const
{
QString path = m_path;
if(m_path.contains("://"))
{
path.remove("tfmx://");
path.remove(RegularExpression("#\\d+$"));
}
return path;
}
QStringList TFMXHelper::filters()
{
QStringList filters;
filters << "*.tfm";
// pair suffix section
filters << "*.tfmx"; // (tfmx, smpl)
filters << "*.mdat"; // (mdat, smpl))
filters << "*.tfx"; // (tfx, (smp, sam))
// pair prefix section
filters << "tfmx.*"; // (tfmx, smpl)
filters << "mdat.*"; // (mdat, smpl)
return filters;
}