comparison Plugins/Input/adplug/core/s3m.cpp @ 359:8df427a314a8 trunk

[svn] Adlib synthesizer (AdPlug) support.
author chainsaw
date Fri, 30 Dec 2005 16:31:39 -0800
parents
children 1cd8716972df
comparison
equal deleted inserted replaced
358:70075730e187 359:8df427a314a8
1 /*
2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999 - 2003 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 * s3m.c - S3M Player by Simon Peter <dn.tlp@gmx.net>
20 *
21 * BUGS:
22 * Extra Fine Slides (EEx, FEx) & Fine Vibrato (Uxy) are inaccurate
23 */
24
25 #include "s3m.h"
26
27 const char Cs3mPlayer::chnresolv[] = // S3M -> adlib channel conversion
28 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,-1,-1,-1,-1,-1,-1,-1};
29
30 const unsigned short Cs3mPlayer::notetable[12] = // S3M adlib note table
31 {340,363,385,408,432,458,485,514,544,577,611,647};
32
33 const unsigned char Cs3mPlayer::vibratotab[32] = // vibrato rate table
34 {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
35
36 /*** public methods *************************************/
37
38 CPlayer *Cs3mPlayer::factory(Copl *newopl)
39 {
40 return new Cs3mPlayer(newopl);
41 }
42
43 Cs3mPlayer::Cs3mPlayer(Copl *newopl): CPlayer(newopl)
44 {
45 int i,j,k;
46
47 memset(pattern,255,sizeof(pattern));
48 memset(orders,255,sizeof(orders));
49
50 for(i=0;i<99;i++) // setup pattern
51 for(j=0;j<64;j++)
52 for(k=0;k<32;k++) {
53 pattern[i][j][k].instrument = 0;
54 pattern[i][j][k].info = 0;
55 }
56 }
57
58 bool Cs3mPlayer::load(const std::string &filename, const CFileProvider &fp)
59 {
60 binistream *f = fp.open(filename); if(!f) return false;
61 unsigned short insptr[99],pattptr[99];
62 int i,row;
63 unsigned char bufval,bufval2;
64 unsigned short ppatlen;
65 s3mheader *checkhead;
66 bool adlibins=false;
67
68 // file validation section
69 checkhead = new s3mheader;
70 load_header(f, checkhead);
71 if((checkhead->kennung != 0x1a) || (checkhead->typ != 16)) {
72 delete checkhead; fp.close(f); return false;
73 } else
74 if(strncmp(checkhead->scrm,"SCRM",4)) {
75 delete checkhead; fp.close(f); return false;
76 } else { // is an adlib module?
77 f->seek(checkhead->ordnum, binio::Add);
78 for(i = 0; i < checkhead->insnum; i++)
79 insptr[i] = f->readInt(2);
80 for(i=0;i<checkhead->insnum;i++) {
81 f->seek(insptr[i]*16);
82 if(f->readInt(1) >= 2) {
83 adlibins = true;
84 break;
85 }
86 }
87 delete checkhead;
88 if(!adlibins) { fp.close(f); return false; }
89 }
90
91 // load section
92 f->seek(0); // rewind for load
93 load_header(f, &header); // read header
94 for(i = 0; i < header.ordnum; i++) orders[i] = f->readInt(1); // read orders
95 for(i = 0; i < header.insnum; i++) insptr[i] = f->readInt(2); // instrument parapointers
96 for(i = 0; i < header.patnum; i++) pattptr[i] = f->readInt(2); // pattern parapointers
97
98 for(i=0;i<header.insnum;i++) { // load instruments
99 f->seek(insptr[i]*16);
100 inst[i].type = f->readInt(1);
101 f->readString(inst[i].filename, 15);
102 inst[i].d00 = f->readInt(1); inst[i].d01 = f->readInt(1);
103 inst[i].d02 = f->readInt(1); inst[i].d03 = f->readInt(1);
104 inst[i].d04 = f->readInt(1); inst[i].d05 = f->readInt(1);
105 inst[i].d06 = f->readInt(1); inst[i].d07 = f->readInt(1);
106 inst[i].d08 = f->readInt(1); inst[i].d09 = f->readInt(1);
107 inst[i].d0a = f->readInt(1); inst[i].d0b = f->readInt(1);
108 inst[i].volume = f->readInt(1); inst[i].dsk = f->readInt(1);
109 f->ignore(2);
110 inst[i].c2spd = f->readInt(4);
111 f->ignore(12);
112 f->readString(inst[i].name, 28);
113 f->readString(inst[i].scri, 4);
114 }
115
116 for(i=0;i<header.patnum;i++) { // depack patterns
117 f->seek(pattptr[i]*16);
118 ppatlen = f->readInt(2);
119 unsigned long pattpos = f->pos();
120 for(row=0;(row<64) && (pattpos-pattptr[i]*16<=ppatlen);row++)
121 do {
122 bufval = f->readInt(1);
123 if(bufval & 32) {
124 bufval2 = f->readInt(1);
125 pattern[i][row][bufval & 31].note = bufval2 & 15;
126 pattern[i][row][bufval & 31].oct = (bufval2 & 240) >> 4;
127 pattern[i][row][bufval & 31].instrument = f->readInt(1);
128 }
129 if(bufval & 64)
130 pattern[i][row][bufval & 31].volume = f->readInt(1);
131 if(bufval & 128) {
132 pattern[i][row][bufval & 31].command = f->readInt(1);
133 pattern[i][row][bufval & 31].info = f->readInt(1);
134 }
135 } while(bufval);
136 }
137
138 fp.close(f);
139 rewind(0);
140 return true; // done
141 }
142
143 bool Cs3mPlayer::update()
144 {
145 unsigned char pattbreak=0,donote; // remember vars
146 unsigned char pattnr,chan,row,info; // cache vars
147 signed char realchan;
148
149 // effect handling (timer dependant)
150 for(realchan=0; realchan<9; realchan++) {
151 info = channel[realchan].info; // fill infobyte cache
152 switch(channel[realchan].fx) {
153 case 11:
154 case 12: if(channel[realchan].fx == 11) // dual command: H00 and Dxy
155 vibrato(realchan,channel[realchan].dualinfo);
156 else // dual command: G00 and Dxy
157 tone_portamento(realchan,channel[realchan].dualinfo);
158 case 4: if(info <= 0x0f) // volume slide down
159 if(channel[realchan].vol - info >= 0)
160 channel[realchan].vol -= info;
161 else
162 channel[realchan].vol = 0;
163 if((info & 0x0f) == 0) // volume slide up
164 if(channel[realchan].vol + (info >> 4) <= 63)
165 channel[realchan].vol += info >> 4;
166 else
167 channel[realchan].vol = 63;
168 setvolume(realchan);
169 break;
170 case 5: if(info == 0xf0 || info <= 0xe0) { // slide down
171 slide_down(realchan,info);
172 setfreq(realchan);
173 }
174 break;
175 case 6: if(info == 0xf0 || info <= 0xe0) { // slide up
176 slide_up(realchan,info);
177 setfreq(realchan);
178 }
179 break;
180 case 7: tone_portamento(realchan,channel[realchan].dualinfo); break; // tone portamento
181 case 8: vibrato(realchan,channel[realchan].dualinfo); break; // vibrato
182 case 10: channel[realchan].nextfreq = channel[realchan].freq; // arpeggio
183 channel[realchan].nextoct = channel[realchan].oct;
184 switch(channel[realchan].trigger) {
185 case 0: channel[realchan].freq = notetable[channel[realchan].note]; break;
186 case 1: if(channel[realchan].note + ((info & 0xf0) >> 4) < 12)
187 channel[realchan].freq = notetable[channel[realchan].note + ((info & 0xf0) >> 4)];
188 else {
189 channel[realchan].freq = notetable[channel[realchan].note + ((info & 0xf0) >> 4) - 12];
190 channel[realchan].oct++;
191 }
192 break;
193 case 2: if(channel[realchan].note + (info & 0x0f) < 12)
194 channel[realchan].freq = notetable[channel[realchan].note + (info & 0x0f)];
195 else {
196 channel[realchan].freq = notetable[channel[realchan].note + (info & 0x0f) - 12];
197 channel[realchan].oct++;
198 }
199 break;
200 }
201 if(channel[realchan].trigger < 2)
202 channel[realchan].trigger++;
203 else
204 channel[realchan].trigger = 0;
205 setfreq(realchan);
206 channel[realchan].freq = channel[realchan].nextfreq;
207 channel[realchan].oct = channel[realchan].nextoct;
208 break;
209 case 21: vibrato(realchan,(unsigned char) (info / 4)); break; // fine vibrato
210 }
211 }
212
213 if(del) { // speed compensation
214 del--;
215 return !songend;
216 }
217
218 // arrangement handling
219 pattnr = orders[ord];
220 if(pattnr == 0xff || ord > header.ordnum) { // "--" end of song
221 songend = 1; // set end-flag
222 ord = 0;
223 pattnr = orders[ord];
224 if(pattnr == 0xff)
225 return !songend;
226 }
227 if(pattnr == 0xfe) { // "++" skip marker
228 ord++; pattnr = orders[ord];
229 }
230
231 // play row
232 row = crow; // fill row cache
233 for(chan=0;chan<32;chan++) {
234 if(!(header.chanset[chan] & 128)) // resolve S3M -> AdLib channels
235 realchan = chnresolv[header.chanset[chan] & 127];
236 else
237 realchan = -1; // channel disabled
238 if(realchan != -1) { // channel playable?
239 // set channel values
240 donote = 0;
241 if(pattern[pattnr][row][chan].note < 14)
242 // tone portamento
243 if(pattern[pattnr][row][chan].command == 7 || pattern[pattnr][row][chan].command == 12) {
244 channel[realchan].nextfreq = notetable[pattern[pattnr][row][chan].note];
245 channel[realchan].nextoct = pattern[pattnr][row][chan].oct;
246 } else { // normal note
247 channel[realchan].note = pattern[pattnr][row][chan].note;
248 channel[realchan].freq = notetable[pattern[pattnr][row][chan].note];
249 channel[realchan].oct = pattern[pattnr][row][chan].oct;
250 channel[realchan].key = 1;
251 donote = 1;
252 }
253 if(pattern[pattnr][row][chan].note == 14) { // key off (is 14 here, cause note is only first 4 bits)
254 channel[realchan].key = 0;
255 setfreq(realchan);
256 }
257 if((channel[realchan].fx != 8 && channel[realchan].fx != 11) && // vibrato begins
258 (pattern[pattnr][row][chan].command == 8 || pattern[pattnr][row][chan].command == 11)) {
259 channel[realchan].nextfreq = channel[realchan].freq;
260 channel[realchan].nextoct = channel[realchan].oct;
261 }
262 if(pattern[pattnr][row][chan].note >= 14)
263 if((channel[realchan].fx == 8 || channel[realchan].fx == 11) && // vibrato ends
264 (pattern[pattnr][row][chan].command != 8 && pattern[pattnr][row][chan].command != 11)) {
265 channel[realchan].freq = channel[realchan].nextfreq;
266 channel[realchan].oct = channel[realchan].nextoct;
267 setfreq(realchan);
268 }
269 if(pattern[pattnr][row][chan].instrument) { // set instrument
270 channel[realchan].inst = pattern[pattnr][row][chan].instrument - 1;
271 if(inst[channel[realchan].inst].volume < 64)
272 channel[realchan].vol = inst[channel[realchan].inst].volume;
273 else
274 channel[realchan].vol = 63;
275 if(pattern[pattnr][row][chan].command != 7)
276 donote = 1;
277 }
278 if(pattern[pattnr][row][chan].volume != 255)
279 if(pattern[pattnr][row][chan].volume < 64) // set volume
280 channel[realchan].vol = pattern[pattnr][row][chan].volume;
281 else
282 channel[realchan].vol = 63;
283 channel[realchan].fx = pattern[pattnr][row][chan].command; // set command
284 if(pattern[pattnr][row][chan].info) // set infobyte
285 channel[realchan].info = pattern[pattnr][row][chan].info;
286
287 // play note
288 if(donote)
289 playnote(realchan);
290 if(pattern[pattnr][row][chan].volume != 255) // set volume
291 setvolume(realchan);
292
293 // command handling (row dependant)
294 info = channel[realchan].info; // fill infobyte cache
295 switch(channel[realchan].fx) {
296 case 1: speed = info; break; // set speed
297 case 2: if(info <= ord) songend = 1; ord = info; crow = 0; pattbreak = 1; break; // jump to order
298 case 3: if(!pattbreak) { crow = info; ord++; pattbreak = 1; } break; // pattern break
299 case 4: if(info > 0xf0) // fine volume down
300 if(channel[realchan].vol - (info & 0x0f) >= 0)
301 channel[realchan].vol -= info & 0x0f;
302 else
303 channel[realchan].vol = 0;
304 if((info & 0x0f) == 0x0f && info >= 0x1f) // fine volume up
305 if(channel[realchan].vol + ((info & 0xf0) >> 4) <= 63)
306 channel[realchan].vol += (info & 0xf0) >> 4;
307 else
308 channel[realchan].vol = 63;
309 setvolume(realchan);
310 break;
311 case 5: if(info > 0xf0) { // fine slide down
312 slide_down(realchan,(unsigned char) (info & 0x0f));
313 setfreq(realchan);
314 }
315 if(info > 0xe0 && info < 0xf0) { // extra fine slide down
316 slide_down(realchan,(unsigned char) ((info & 0x0f) / 4));
317 setfreq(realchan);
318 }
319 break;
320 case 6: if(info > 0xf0) { // fine slide up
321 slide_up(realchan,(unsigned char) (info & 0x0f));
322 setfreq(realchan);
323 }
324 if(info > 0xe0 && info < 0xf0) { // extra fine slide up
325 slide_up(realchan,(unsigned char) ((info & 0x0f) / 4));
326 setfreq(realchan);
327 }
328 break;
329 case 7: // tone portamento
330 case 8: if((channel[realchan].fx == 7 || // vibrato (remember info for dual commands)
331 channel[realchan].fx == 8) && pattern[pattnr][row][chan].info)
332 channel[realchan].dualinfo = info;
333 break;
334 case 10: channel[realchan].trigger = 0; break; // arpeggio (set trigger)
335 case 19: if(info == 0xb0) // set loop start
336 loopstart = row;
337 if(info > 0xb0 && info <= 0xbf) // pattern loop
338 if(!loopcnt) {
339 loopcnt = info & 0x0f;
340 crow = loopstart;
341 pattbreak = 1;
342 } else
343 if(--loopcnt > 0) {
344 crow = loopstart;
345 pattbreak = 1;
346 }
347 if((info & 0xf0) == 0xe0) // patterndelay
348 del = speed * (info & 0x0f) - 1;
349 break;
350 case 20: tempo = info; break; // set tempo
351 }
352 }
353 }
354
355 if(!del)
356 del = speed - 1;// speed compensation
357 if(!pattbreak) { // next row (only if no manual advance)
358 crow++;
359 if(crow > 63) {
360 crow = 0;
361 ord++;
362 loopstart = 0;
363 }
364 }
365
366 return !songend; // still playing
367 }
368
369 void Cs3mPlayer::rewind(int subsong)
370 {
371 // set basic variables
372 songend = 0; ord = 0; crow = 0; tempo = header.it;
373 speed = header.is; del = 0; loopstart = 0; loopcnt = 0;
374
375 memset(channel,0,sizeof(channel));
376
377 opl->init(); // reset OPL chip
378 opl->write(1,32); // Go to ym3812 mode
379 }
380
381 std::string Cs3mPlayer::gettype()
382 {
383 char filever[5];
384
385 switch(header.cwtv) { // determine version number
386 case 0x1300: strcpy(filever,"3.00"); break;
387 case 0x1301: strcpy(filever,"3.01"); break;
388 case 0x1303: strcpy(filever,"3.03"); break;
389 case 0x1320: strcpy(filever,"3.20"); break;
390 default: strcpy(filever,"3.??");
391 }
392
393 return (std::string("Scream Tracker ") + filever);
394 }
395
396 float Cs3mPlayer::getrefresh()
397 {
398 return (float) (tempo / 2.5);
399 }
400
401 /*** private methods *************************************/
402
403 void Cs3mPlayer::load_header(binistream *f, s3mheader *h)
404 {
405 int i;
406
407 f->readString(h->name, 28);
408 h->kennung = f->readInt(1); h->typ = f->readInt(1);
409 f->ignore(2);
410 h->ordnum = f->readInt(2); h->insnum = f->readInt(2);
411 h->patnum = f->readInt(2); h->flags = f->readInt(2);
412 h->cwtv = f->readInt(2); h->ffi = f->readInt(2);
413 f->readString(h->scrm, 4);
414 h->gv = f->readInt(1); h->is = f->readInt(1); h->it = f->readInt(1);
415 h->mv = f->readInt(1); h->uc = f->readInt(1); h->dp = f->readInt(1);
416 f->ignore(8);
417 h->special = f->readInt(2);
418 for(i = 0; i < 32; i++) h->chanset[i] = f->readInt(1);
419 }
420
421 void Cs3mPlayer::setvolume(unsigned char chan)
422 {
423 unsigned char op = op_table[chan], insnr = channel[chan].inst;
424
425 opl->write(0x43 + op,(int)(63-((63-(inst[insnr].d03 & 63))/63.0)*channel[chan].vol) + (inst[insnr].d03 & 192));
426 if(inst[insnr].d0a & 1)
427 opl->write(0x40 + op,(int)(63-((63-(inst[insnr].d02 & 63))/63.0)*channel[chan].vol) + (inst[insnr].d02 & 192));
428 }
429
430 void Cs3mPlayer::setfreq(unsigned char chan)
431 {
432 opl->write(0xa0 + chan, channel[chan].freq & 255);
433 if(channel[chan].key)
434 opl->write(0xb0 + chan, ((channel[chan].freq & 768) >> 8) + (channel[chan].oct << 2) | 32);
435 else
436 opl->write(0xb0 + chan, ((channel[chan].freq & 768) >> 8) + (channel[chan].oct << 2));
437 }
438
439 void Cs3mPlayer::playnote(unsigned char chan)
440 {
441 unsigned char op = op_table[chan], insnr = channel[chan].inst;
442
443 opl->write(0xb0 + chan, 0); // stop old note
444
445 // set instrument data
446 opl->write(0x20 + op, inst[insnr].d00);
447 opl->write(0x23 + op, inst[insnr].d01);
448 opl->write(0x40 + op, inst[insnr].d02);
449 opl->write(0x43 + op, inst[insnr].d03);
450 opl->write(0x60 + op, inst[insnr].d04);
451 opl->write(0x63 + op, inst[insnr].d05);
452 opl->write(0x80 + op, inst[insnr].d06);
453 opl->write(0x83 + op, inst[insnr].d07);
454 opl->write(0xe0 + op, inst[insnr].d08);
455 opl->write(0xe3 + op, inst[insnr].d09);
456 opl->write(0xc0 + chan, inst[insnr].d0a);
457
458 // set frequency & play
459 channel[chan].key = 1;
460 setfreq(chan);
461 }
462
463 void Cs3mPlayer::slide_down(unsigned char chan, unsigned char amount)
464 {
465 if(channel[chan].freq - amount > 340)
466 channel[chan].freq -= amount;
467 else
468 if(channel[chan].oct > 0) {
469 channel[chan].oct--;
470 channel[chan].freq = 684;
471 } else
472 channel[chan].freq = 340;
473 }
474
475 void Cs3mPlayer::slide_up(unsigned char chan, unsigned char amount)
476 {
477 if(channel[chan].freq + amount < 686)
478 channel[chan].freq += amount;
479 else
480 if(channel[chan].oct < 7) {
481 channel[chan].oct++;
482 channel[chan].freq = 341;
483 } else
484 channel[chan].freq = 686;
485 }
486
487 void Cs3mPlayer::vibrato(unsigned char chan, unsigned char info)
488 {
489 unsigned char i,speed,depth;
490
491 speed = info >> 4;
492 depth = (info & 0x0f) / 2;
493
494 for(i=0;i<speed;i++) {
495 channel[chan].trigger++;
496 while(channel[chan].trigger >= 64)
497 channel[chan].trigger -= 64;
498 if(channel[chan].trigger >= 16 && channel[chan].trigger < 48)
499 slide_down(chan,(unsigned char) (vibratotab[channel[chan].trigger - 16] / (16-depth)));
500 if(channel[chan].trigger < 16)
501 slide_up(chan,(unsigned char) (vibratotab[channel[chan].trigger + 16] / (16-depth)));
502 if(channel[chan].trigger >= 48)
503 slide_up(chan,(unsigned char) (vibratotab[channel[chan].trigger - 48] / (16-depth)));
504 }
505 setfreq(chan);
506 }
507
508 void Cs3mPlayer::tone_portamento(unsigned char chan, unsigned char info)
509 {
510 if(channel[chan].freq + (channel[chan].oct << 10) < channel[chan].nextfreq +
511 (channel[chan].nextoct << 10))
512 slide_up(chan,info);
513 if(channel[chan].freq + (channel[chan].oct << 10) > channel[chan].nextfreq +
514 (channel[chan].nextoct << 10))
515 slide_down(chan,info);
516 setfreq(chan);
517 }