-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathdtm.cpp
More file actions
323 lines (258 loc) · 8.12 KB
/
Copy pathdtm.cpp
File metadata and controls
323 lines (258 loc) · 8.12 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
/*
Adplug - Replayer for many OPL2/OPL3 audio file formats.
Copyright (C) 1999 - 2006 Simon Peter, <dn.tlp@gmx.net>, et al.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
dtm.cpp - DTM loader by Riven the Mage <riven@ok.ru>
*/
/*
NOTE: Panning (Ex) effect is ignored.
*/
#include <algorithm>
#include <cstring>
#include "dtm.h"
/* -------- Public Methods -------------------------------- */
CPlayer *CdtmLoader::factory(Copl *newopl)
{
return new CdtmLoader(newopl);
}
bool CdtmLoader::load(const std::string &filename, const CFileProvider &fp)
{
static const unsigned char conv_inst[11] = {
2, 1, 10, 9, 4, 3, 6, 5, 0, 8, 7
};
static const unsigned short conv_note[12] = {
0x16B, 0x181, 0x198, 0x1B0, 0x1CA, 0x1E5,
0x202, 0x220, 0x241, 0x263, 0x287, 0x2AE
};
binistream *f = fp.open(filename);
if (!f) return false;
// read header
f->readString(header.id, sizeof(header.id));
header.version = f->readInt(1);
f->readString(header.title, sizeof(header.title));
f->readString(header.author, sizeof(header.author));
// Ensure title and author are NUL terminated. We may overwrite the
// last character because the arrays are too short.
header.author[sizeof(header.author) - 1] =
header.title[sizeof(header.title) - 1] = 0;
header.numpat = f->readInt(1);
header.numinst = f->readInt(1) + 1;
// check header
if (memcmp(header.id, "DeFy DTM ", 9) || // signature exists?
header.version != 0x10 || // good version?
header.numinst > MAX_INST ||
header.numinst < N_CHAN || // need a default instrument for each channel
header.numpat == 0 ||
f->error()) {
fp.close (f);
return false;
}
// load description
memset(desc, 0, sizeof(desc));
char *bufstr = desc;
for (int i = 0; i < DESC_ROWS; i++) {
// get line length
unsigned char bufstr_length = f->readInt(1);
if (bufstr_length > DESC_COLS) {
// "desc" is too small to hold DESC_ROWS lines with DESC_COLS chars
// each plus DESC_ROWS newlines and a NUL terminator. Accept a line
// length of DESC_COLS anyway and truncate the last line if necessary.
// Maybe we should grow desc or allocate it dynamically instead.
fp.close(f);
return false;
}
int max_length = desc + sizeof(desc) - 1 - bufstr;
int discard = bufstr_length > max_length ? bufstr_length - max_length : 0;
bufstr_length -= discard;
// read line
if (bufstr_length) {
f->readString(bufstr,bufstr_length);
for (int j = 0; j < bufstr_length; j++)
if (!bufstr[j])
bufstr[j] = 0x20;
bufstr += bufstr_length;
if (discard)
f->ignore(discard);
}
if (bufstr_length < max_length)
*(bufstr++) = '\n';
}
*bufstr = 0;
// init CmodPlayer
realloc_instruments(header.numinst);
realloc_order(N_ORD);
realloc_patterns(header.numpat, N_ROW, N_CHAN);
init_notetable(conv_note);
init_trackord();
// load instruments
for (int i = 0; i < header.numinst; i++) {
unsigned char name_length = f->readInt(1);
if (name_length >= sizeof(instruments[i].name)) {
fp.close(f);
return false; // or truncate the name instead?
}
if (name_length)
f->readString(instruments[i].name, name_length);
instruments[i].name[name_length] = 0;
f->readString((char *)instruments[i].data, sizeof(instruments[i].data));
for (unsigned int j = 0; j < sizeof(conv_inst); j++)
inst[i].data[conv_inst[j]] = instruments[i].data[j];
}
// load order
f->readString((char *)order, N_ORD);
// load tracks
dtm_event pattern[N_ROW][N_CHAN];
nop = header.numpat;
for (int t = 0, i = 0; i < nop; i++) {
unsigned short packed_length = f->readInt(2);
if (!unpack_pattern(f, packed_length, pattern, sizeof(pattern))) {
fp.close(f);
return false;
}
// convert pattern
for (int j = 0; j < N_CHAN; j++, t++) {
for (int k = 0; k < N_ROW; k++) {
dtm_event *event = &pattern[k][j];
// instrument
if (event->byte0 == 0x80) {
if (event->byte1 < header.numinst) // not <= 0x80 !
tracks[t][k].inst = event->byte1 + 1;
}
// note + effect
else {
tracks[t][k].note = event->byte0;
if ((event->byte0 != 0) && (event->byte0 != 127))
tracks[t][k].note++;
// convert effects
unsigned char ev_param = event->byte1 & 0x0F;
switch (event->byte1 >> 4) {
case 0x0: // pattern break
if (ev_param == 1)
tracks[t][k].command = 13;
break;
case 0x1: // freq. slide up
tracks[t][k].command = 28;
tracks[t][k].param1 = ev_param;
break;
case 0x2: // freq. slide down
tracks[t][k].command = 28;
tracks[t][k].param2 = ev_param;
break;
case 0xA: // set carrier volume
case 0xC: // set instrument volume
tracks[t][k].command = 22;
tracks[t][k].param1 = (0x3F - ev_param) >> 4; // always 3
tracks[t][k].param2 = (0x3F - ev_param) & 0xF;
break;
case 0xB: // set modulator volume
tracks[t][k].command = 21;
tracks[t][k].param1 = (0x3F - ev_param) >> 4; // always 3
tracks[t][k].param2 = (0x3F - ev_param) & 0xF;
break;
case 0xE: // set panning
break;
case 0xF: // set speed
tracks[t][k].command = 13;
tracks[t][k].param2 = ev_param;
break;
}
}
}
}
}
if (f->error()) {
fp.close(f);
return false;
}
fp.close(f);
// order length
length = N_ORD;
for (unsigned int i = 0; i < N_ORD; i++) {
if (order[i] & 0x80) {
length = i;
if (order[i] == 0xFF)
restartpos = 0;
else
restartpos = order[i] - 0x80;
if (restartpos >= i) // bad restart position or empty order list
return false;
break;
} else if (order[i] >= nop) {
return false;
}
}
// initial speed
initspeed = 2;
rewind(0);
return true;
}
void CdtmLoader::rewind(int subsong)
{
CmodPlayer::rewind(subsong);
// default instruments
for (int i = 0; i < N_CHAN; i++) {
channel[i].inst = i;
channel[i].vol1 = 63 - (inst[i].data[10] & 63);
channel[i].vol2 = 63 - (inst[i].data[9] & 63);
}
}
float CdtmLoader::getrefresh()
{
return 18.2f;
}
std::string CdtmLoader::gettype()
{
return std::string("DeFy Adlib Tracker");
}
std::string CdtmLoader::gettitle()
{
return std::string(header.title);
}
std::string CdtmLoader::getauthor()
{
return std::string(header.author);
}
std::string CdtmLoader::getdesc()
{
return std::string(desc);
}
std::string CdtmLoader::getinstrument(unsigned int n)
{
return n < header.numinst ? std::string(instruments[n].name) : std::string();
}
unsigned int CdtmLoader::getinstruments()
{
return header.numinst;
}
/* -------- Private Methods ------------------------------- */
bool CdtmLoader::unpack_pattern(binistream *f, size_t ilen,
void *obuf, size_t olen)
{
unsigned char *outp = (unsigned char *)obuf;
// RLE
while (ilen--) {
size_t repeat_counter = 1;
unsigned char repeat_byte = f->readInt(1);
if ((repeat_byte & 0xF0) == 0xD0) {
if (!ilen--) return false; // truncated input
repeat_counter = repeat_byte & 0x0F;
repeat_byte = f->readInt(1);
}
// Attempts to generate too much data are normal, ignore the excess data.
repeat_counter = std::min(repeat_counter, olen);
memset(outp, repeat_byte, repeat_counter);
outp += repeat_counter;
olen -= repeat_counter;
}
return olen == 0 && !f->error(); // generated enough data?
}