359
|
1 /*
|
|
2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
|
|
3 * Copyright (C) 1999 - 2002 Simon Peter, <dn.tlp@gmx.net>, et al.
|
|
4 *
|
|
5 * This library is free software; you can redistribute it and/or
|
|
6 * modify it under the terms of the GNU Lesser General Public
|
|
7 * License as published by the Free Software Foundation; either
|
|
8 * version 2.1 of the License, or (at your option) any later version.
|
|
9 *
|
|
10 * This library is distributed in the hope that it will be useful,
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 * Lesser General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU Lesser General Public
|
|
16 * License along with this library; if not, write to the Free Software
|
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 *
|
|
19 * a2m.cpp - A2M Loader by Simon Peter <dn.tlp@gmx.net>
|
|
20 *
|
|
21 * NOTES:
|
|
22 * This loader detects and loads version 1, 4, 5 & 8 files.
|
|
23 *
|
|
24 * version 1-4 files:
|
|
25 * Following commands are ignored:
|
|
26 * FF1 - FF9, FAx - FEx
|
|
27 *
|
|
28 * version 5-8 files:
|
|
29 * Instrument panning is ignored. Flags byte is ignored.
|
|
30 * Following commands are ignored:
|
|
31 * Gxy, Hxy, Kxy - &xy
|
|
32 */
|
|
33
|
|
34 #include "a2m.h"
|
|
35
|
|
36 const unsigned int Ca2mLoader::MAXFREQ = 2000,
|
|
37 Ca2mLoader::MINCOPY = ADPLUG_A2M_MINCOPY,
|
|
38 Ca2mLoader::MAXCOPY = ADPLUG_A2M_MAXCOPY,
|
|
39 Ca2mLoader::COPYRANGES = ADPLUG_A2M_COPYRANGES,
|
|
40 Ca2mLoader::CODESPERRANGE = ADPLUG_A2M_CODESPERRANGE,
|
|
41 Ca2mLoader::TERMINATE = 256,
|
|
42 Ca2mLoader::FIRSTCODE = ADPLUG_A2M_FIRSTCODE,
|
|
43 Ca2mLoader::MAXCHAR = FIRSTCODE + COPYRANGES * CODESPERRANGE - 1,
|
|
44 Ca2mLoader::SUCCMAX = MAXCHAR + 1,
|
|
45 Ca2mLoader::TWICEMAX = ADPLUG_A2M_TWICEMAX,
|
|
46 Ca2mLoader::ROOT = 1, Ca2mLoader::MAXBUF = 42 * 1024,
|
|
47 Ca2mLoader::MAXDISTANCE = 21389, Ca2mLoader::MAXSIZE = 21389 + MAXCOPY;
|
|
48
|
|
49 const unsigned short Ca2mLoader::bitvalue[14] =
|
|
50 {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192};
|
|
51
|
|
52 const signed short Ca2mLoader::copybits[COPYRANGES] =
|
|
53 {4, 6, 8, 10, 12, 14};
|
|
54
|
|
55 const signed short Ca2mLoader::copymin[COPYRANGES] =
|
|
56 {0, 16, 80, 336, 1360, 5456};
|
|
57
|
|
58 CPlayer *Ca2mLoader::factory(Copl *newopl)
|
|
59 {
|
|
60 return new Ca2mLoader(newopl);
|
|
61 }
|
|
62
|
|
63 bool Ca2mLoader::load(const std::string &filename, const CFileProvider &fp)
|
|
64 {
|
|
65 binistream *f = fp.open(filename); if(!f) return false;
|
|
66 struct {
|
|
67 char id[10];
|
|
68 unsigned long crc;
|
|
69 unsigned char version,numpats;
|
|
70 } ch;
|
|
71 int i,j,k,t;
|
|
72 unsigned int l;
|
645
|
73 unsigned char *org = 0, *orgptr;
|
359
|
74 unsigned long alength;
|
|
75 unsigned short len[9], *secdata, *secptr;
|
|
76 const unsigned char convfx[16] = {0,1,2,23,24,3,5,4,6,9,17,13,11,19,7,14};
|
|
77 const unsigned char convinf1[16] = {0,1,2,6,7,8,9,4,5,3,10,11,12,13,14,15};
|
|
78 const unsigned char newconvfx[] = {0,1,2,3,4,5,6,23,24,21,10,11,17,13,7,19,
|
|
79 255,255,22,25,255,15,255,255,255,255,255,
|
|
80 255,255,255,255,255,255,255,255,14,255};
|
|
81
|
|
82 // read header
|
|
83 f->readString(ch.id, 10); ch.crc = f->readInt(4);
|
|
84 ch.version = f->readInt(1); ch.numpats = f->readInt(1);
|
|
85
|
|
86 // file validation section
|
|
87 if(strncmp(ch.id,"_A2module_",10) || (ch.version != 1 && ch.version != 5 &&
|
|
88 ch.version != 4 && ch.version != 8)) {
|
|
89 fp.close(f);
|
|
90 return false;
|
|
91 }
|
|
92
|
|
93 // load, depack & convert section
|
|
94 nop = ch.numpats; length = 128; restartpos = 0; activechan = 0xffff;
|
|
95 if(ch.version == 1 || ch.version == 4) {
|
|
96 for(i=0;i<5;i++) len[i] = f->readInt(2);
|
|
97 t = 9;
|
|
98 } else {
|
|
99 for(i=0;i<9;i++) len[i] = f->readInt(2);
|
|
100 t = 18;
|
|
101 }
|
|
102
|
|
103 // block 0
|
|
104 secdata = new unsigned short [len[0] / 2];
|
|
105 if(ch.version == 1 || ch.version == 5) {
|
|
106 for(i=0;i<len[0]/2;i++) secdata[i] = f->readInt(2);
|
|
107 org = new unsigned char [MAXBUF]; orgptr = org;
|
|
108 sixdepak(secdata,org,len[0]);
|
|
109 } else {
|
|
110 orgptr = (unsigned char *)secdata;
|
|
111 for(i=0;i<len[0];i++) orgptr[i] = f->readInt(1);
|
|
112 }
|
|
113 memcpy(songname,orgptr,43); orgptr += 43;
|
|
114 memcpy(author,orgptr,43); orgptr += 43;
|
|
115 memcpy(instname,orgptr,250*33); orgptr += 250*33;
|
|
116 for(i=0;i<250;i++) { // instruments
|
|
117 inst[i].data[0] = *(orgptr+i*13+10);
|
|
118 inst[i].data[1] = *(orgptr+i*13);
|
|
119 inst[i].data[2] = *(orgptr+i*13+1);
|
|
120 inst[i].data[3] = *(orgptr+i*13+4);
|
|
121 inst[i].data[4] = *(orgptr+i*13+5);
|
|
122 inst[i].data[5] = *(orgptr+i*13+6);
|
|
123 inst[i].data[6] = *(orgptr+i*13+7);
|
|
124 inst[i].data[7] = *(orgptr+i*13+8);
|
|
125 inst[i].data[8] = *(orgptr+i*13+9);
|
|
126 inst[i].data[9] = *(orgptr+i*13+2);
|
|
127 inst[i].data[10] = *(orgptr+i*13+3);
|
|
128 if(ch.version == 1 || ch.version == 4)
|
|
129 inst[i].misc = *(orgptr+i*13+11);
|
|
130 else
|
|
131 inst[i].misc = 0;
|
|
132 inst[i].slide = *(orgptr+i*13+12);
|
|
133 }
|
|
134
|
|
135 orgptr += 250*13;
|
|
136 memcpy(order,orgptr,128); orgptr += 128;
|
|
137 bpm = *orgptr; orgptr += 1;
|
|
138 initspeed = *orgptr;
|
|
139 // v5-8 files have an additional flag byte here
|
|
140 if(ch.version == 1 || ch.version == 5)
|
|
141 delete [] org;
|
|
142 delete [] secdata;
|
|
143
|
|
144 // blocks 1-4 or 1-8
|
|
145 alength = len[1];
|
|
146 for(i=0;i<(ch.version == 1 || ch.version == 4 ? ch.numpats/16 : ch.numpats/8);i++)
|
|
147 alength += len[i+2];
|
|
148
|
|
149 secdata = new unsigned short [alength / 2];
|
|
150 if(ch.version == 1 || ch.version == 5) {
|
|
151 for(l=0;l<alength/2;l++) secdata[l] = f->readInt(2);
|
|
152 org = new unsigned char [MAXBUF * (ch.numpats / (ch.version == 1 ? 16 : 8) + 1)];
|
|
153 orgptr = org; secptr = secdata;
|
|
154 orgptr += sixdepak(secptr,orgptr,len[1]); secptr += len[1] / 2;
|
|
155 if(ch.version == 1) {
|
|
156 if(ch.numpats > 16)
|
|
157 orgptr += sixdepak(secptr,orgptr,len[2]); secptr += len[2] / 2;
|
|
158 if(ch.numpats > 32)
|
|
159 orgptr += sixdepak(secptr,orgptr,len[3]); secptr += len[3] / 2;
|
|
160 if(ch.numpats > 48)
|
|
161 sixdepak(secptr,orgptr,len[4]);
|
|
162 } else {
|
|
163 if(ch.numpats > 8)
|
|
164 orgptr += sixdepak(secptr,orgptr,len[2]); secptr += len[2] / 2;
|
|
165 if(ch.numpats > 16)
|
|
166 orgptr += sixdepak(secptr,orgptr,len[3]); secptr += len[3] / 2;
|
|
167 if(ch.numpats > 24)
|
|
168 orgptr += sixdepak(secptr,orgptr,len[4]); secptr += len[4] / 2;
|
|
169 if(ch.numpats > 32)
|
|
170 orgptr += sixdepak(secptr,orgptr,len[5]); secptr += len[5] / 2;
|
|
171 if(ch.numpats > 40)
|
|
172 orgptr += sixdepak(secptr,orgptr,len[6]); secptr += len[6] / 2;
|
|
173 if(ch.numpats > 48)
|
|
174 orgptr += sixdepak(secptr,orgptr,len[7]); secptr += len[7] / 2;
|
|
175 if(ch.numpats > 56)
|
|
176 sixdepak(secptr,orgptr,len[8]);
|
|
177 }
|
|
178 delete [] secdata;
|
|
179 } else {
|
|
180 org = (unsigned char *)secdata;
|
|
181 for(l=0;l<alength;l++) org[l] = f->readInt(1);
|
|
182 }
|
|
183
|
|
184 for(i=0;i<64*9;i++) // patterns
|
|
185 trackord[i/9][i%9] = i+1;
|
|
186
|
|
187 if(ch.version == 1 || ch.version == 4) {
|
|
188 for(i=0;i<ch.numpats;i++)
|
|
189 for(j=0;j<64;j++)
|
|
190 for(k=0;k<9;k++) {
|
|
191 tracks[i*9+k][j].note = org[i*64*t*4+j*t*4+k*4] == 255 ? 127 : org[i*64*t*4+j*t*4+k*4];
|
|
192 tracks[i*9+k][j].inst = org[i*64*t*4+j*t*4+k*4+1];
|
|
193 tracks[i*9+k][j].command = convfx[org[i*64*t*4+j*t*4+k*4+2]];
|
|
194 tracks[i*9+k][j].param2 = org[i*64*t*4+j*t*4+k*4+3] & 0x0f;
|
|
195 if(tracks[i*9+k][j].command != 14)
|
|
196 tracks[i*9+k][j].param1 = org[i*64*t*4+j*t*4+k*4+3] >> 4;
|
|
197 else {
|
|
198 tracks[i*9+k][j].param1 = convinf1[org[i*64*t*4+j*t*4+k*4+3] >> 4];
|
|
199 if(tracks[i*9+k][j].param1 == 15 && !tracks[i*9+k][j].param2) { // convert key-off
|
|
200 tracks[i*9+k][j].command = 8;
|
|
201 tracks[i*9+k][j].param1 = 0;
|
|
202 tracks[i*9+k][j].param2 = 0;
|
|
203 }
|
|
204 }
|
|
205 if(tracks[i*9+k][j].command == 14) {
|
|
206 switch(tracks[i*9+k][j].param1) {
|
|
207 case 2: // convert define waveform
|
|
208 tracks[i*9+k][j].command = 25;
|
|
209 tracks[i*9+k][j].param1 = tracks[i*9+k][j].param2;
|
|
210 tracks[i*9+k][j].param2 = 0xf;
|
|
211 break;
|
|
212 case 8: // convert volume slide up
|
|
213 tracks[i*9+k][j].command = 26;
|
|
214 tracks[i*9+k][j].param1 = tracks[i*9+k][j].param2;
|
|
215 tracks[i*9+k][j].param2 = 0;
|
|
216 break;
|
|
217 case 9: // convert volume slide down
|
|
218 tracks[i*9+k][j].command = 26;
|
|
219 tracks[i*9+k][j].param1 = 0;
|
|
220 break;
|
|
221 }
|
|
222 }
|
|
223 }
|
|
224 } else {
|
|
225 for(i=0;i<ch.numpats;i++)
|
|
226 for(j=0;j<9;j++)
|
|
227 for(k=0;k<64;k++) {
|
|
228 tracks[i*9+j][k].note = org[i*64*t*4+j*64*4+k*4] == 255 ? 127 : org[i*64*t*4+j*64*4+k*4];
|
|
229 tracks[i*9+j][k].inst = org[i*64*t*4+j*64*4+k*4+1];
|
|
230 tracks[i*9+j][k].command = newconvfx[org[i*64*t*4+j*64*4+k*4+2]];
|
|
231 tracks[i*9+j][k].param1 = org[i*64*t*4+j*64*4+k*4+3] >> 4;
|
|
232 tracks[i*9+j][k].param2 = org[i*64*t*4+j*64*4+k*4+3] & 0x0f;
|
|
233 }
|
|
234 }
|
|
235
|
|
236 if(ch.version == 1 || ch.version == 5)
|
|
237 delete [] org;
|
|
238 else
|
|
239 delete [] secdata;
|
|
240 fp.close(f);
|
|
241 rewind(0);
|
|
242 return true;
|
|
243 }
|
|
244
|
|
245 float Ca2mLoader::getrefresh()
|
|
246 {
|
|
247 if(tempo != 18)
|
|
248 return (float) (tempo);
|
|
249 else
|
|
250 return 18.2f;
|
|
251 }
|
|
252
|
|
253 /*** private methods *************************************/
|
|
254
|
|
255 void Ca2mLoader::inittree()
|
|
256 {
|
|
257 unsigned short i;
|
|
258
|
|
259 for(i=2;i<=TWICEMAX;i++) {
|
|
260 dad[i] = i / 2;
|
|
261 freq[i] = 1;
|
|
262 }
|
|
263
|
|
264 for(i=1;i<=MAXCHAR;i++) {
|
|
265 leftc[i] = 2 * i;
|
|
266 rghtc[i] = 2 * i + 1;
|
|
267 }
|
|
268 }
|
|
269
|
|
270 void Ca2mLoader::updatefreq(unsigned short a,unsigned short b)
|
|
271 {
|
|
272 do {
|
|
273 freq[dad[a]] = freq[a] + freq[b];
|
|
274 a = dad[a];
|
|
275 if(a != ROOT)
|
|
276 if(leftc[dad[a]] == a)
|
|
277 b = rghtc[dad[a]];
|
|
278 else
|
|
279 b = leftc[dad[a]];
|
|
280 } while(a != ROOT);
|
|
281
|
|
282 if(freq[ROOT] == MAXFREQ)
|
|
283 for(a=1;a<=TWICEMAX;a++)
|
|
284 freq[a] >>= 1;
|
|
285 }
|
|
286
|
|
287 void Ca2mLoader::updatemodel(unsigned short code)
|
|
288 {
|
|
289 unsigned short a=code+SUCCMAX,b,c,code1,code2;
|
|
290
|
|
291 freq[a]++;
|
|
292 if(dad[a] != ROOT) {
|
|
293 code1 = dad[a];
|
|
294 if(leftc[code1] == a)
|
|
295 updatefreq(a,rghtc[code1]);
|
|
296 else
|
|
297 updatefreq(a,leftc[code1]);
|
|
298
|
|
299 do {
|
|
300 code2 = dad[code1];
|
|
301 if(leftc[code2] == code1)
|
|
302 b = rghtc[code2];
|
|
303 else
|
|
304 b = leftc[code2];
|
|
305
|
|
306 if(freq[a] > freq[b]) {
|
|
307 if(leftc[code2] == code1)
|
|
308 rghtc[code2] = a;
|
|
309 else
|
|
310 leftc[code2] = a;
|
|
311
|
|
312 if(leftc[code1] == a) {
|
|
313 leftc[code1] = b;
|
|
314 c = rghtc[code1];
|
|
315 } else {
|
|
316 rghtc[code1] = b;
|
|
317 c = leftc[code1];
|
|
318 }
|
|
319
|
|
320 dad[b] = code1;
|
|
321 dad[a] = code2;
|
|
322 updatefreq(b,c);
|
|
323 a = b;
|
|
324 }
|
|
325
|
|
326 a = dad[a];
|
|
327 code1 = dad[a];
|
|
328 } while(code1 != ROOT);
|
|
329 }
|
|
330 }
|
|
331
|
|
332 unsigned short Ca2mLoader::inputcode(unsigned short bits)
|
|
333 {
|
|
334 unsigned short i,code=0;
|
|
335
|
|
336 for(i=1;i<=bits;i++) {
|
|
337 if(!ibitcount) {
|
|
338 if(ibitcount == MAXBUF)
|
|
339 ibufcount = 0;
|
|
340 ibitbuffer = wdbuf[ibufcount];
|
|
341 ibufcount++;
|
|
342 ibitcount = 15;
|
|
343 } else
|
|
344 ibitcount--;
|
|
345
|
|
346 if(ibitbuffer > 0x7fff)
|
|
347 code |= bitvalue[i-1];
|
|
348 ibitbuffer <<= 1;
|
|
349 }
|
|
350
|
|
351 return code;
|
|
352 }
|
|
353
|
|
354 unsigned short Ca2mLoader::uncompress()
|
|
355 {
|
|
356 unsigned short a=1;
|
|
357
|
|
358 do {
|
|
359 if(!ibitcount) {
|
|
360 if(ibufcount == MAXBUF)
|
|
361 ibufcount = 0;
|
|
362 ibitbuffer = wdbuf[ibufcount];
|
|
363 ibufcount++;
|
|
364 ibitcount = 15;
|
|
365 } else
|
|
366 ibitcount--;
|
|
367
|
|
368 if(ibitbuffer > 0x7fff)
|
|
369 a = rghtc[a];
|
|
370 else
|
|
371 a = leftc[a];
|
|
372 ibitbuffer <<= 1;
|
|
373 } while(a <= MAXCHAR);
|
|
374
|
|
375 a -= SUCCMAX;
|
|
376 updatemodel(a);
|
|
377 return a;
|
|
378 }
|
|
379
|
|
380 void Ca2mLoader::decode()
|
|
381 {
|
|
382 unsigned short i,j,k,t,c,count=0,dist,len,index;
|
|
383
|
|
384 inittree();
|
|
385 c = uncompress();
|
|
386
|
|
387 while(c != TERMINATE) {
|
|
388 if(c < 256) {
|
|
389 obuf[obufcount] = (unsigned char)c;
|
|
390 obufcount++;
|
|
391 if(obufcount == MAXBUF) {
|
|
392 output_size = MAXBUF;
|
|
393 obufcount = 0;
|
|
394 }
|
|
395
|
|
396 buf[count] = (unsigned char)c;
|
|
397 count++;
|
|
398 if(count == MAXSIZE)
|
|
399 count = 0;
|
|
400 } else {
|
|
401 t = c - FIRSTCODE;
|
|
402 index = t / CODESPERRANGE;
|
|
403 len = t + MINCOPY - index * CODESPERRANGE;
|
|
404 dist = inputcode(copybits[index]) + len + copymin[index];
|
|
405
|
|
406 j = count;
|
|
407 k = count - dist;
|
|
408 if(count < dist)
|
|
409 k += MAXSIZE;
|
|
410
|
|
411 for(i=0;i<=len-1;i++) {
|
|
412 obuf[obufcount] = buf[k];
|
|
413 obufcount++;
|
|
414 if(obufcount == MAXBUF) {
|
|
415 output_size = MAXBUF;
|
|
416 obufcount = 0;
|
|
417 }
|
|
418
|
|
419 buf[j] = buf[k];
|
|
420 j++; k++;
|
|
421 if(j == MAXSIZE) j = 0;
|
|
422 if(k == MAXSIZE) k = 0;
|
|
423 }
|
|
424
|
|
425 count += len;
|
|
426 if(count >= MAXSIZE)
|
|
427 count -= MAXSIZE;
|
|
428 }
|
|
429 c = uncompress();
|
|
430 }
|
|
431 output_size = obufcount;
|
|
432 }
|
|
433
|
|
434 unsigned short Ca2mLoader::sixdepak(unsigned short *source, unsigned char *dest,
|
|
435 unsigned short size)
|
|
436 {
|
|
437 if((unsigned int)size + 4096 > MAXBUF)
|
|
438 return 0;
|
|
439
|
|
440 buf = new unsigned char [MAXSIZE];
|
|
441 input_size = size;
|
|
442 ibitcount = 0; ibitbuffer = 0;
|
|
443 obufcount = 0; ibufcount = 0;
|
|
444 wdbuf = source; obuf = dest;
|
|
445
|
|
446 decode();
|
|
447 delete [] buf;
|
|
448 return output_size;
|
|
449 }
|