2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999 - 2004 Simon Peter, <dn.tlp@gmx.net>, et al.
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.
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.
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
19 * lds.cpp - LOUDNESS Player by Simon Peter <dn.tlp@gmx.net>
27 // Note frequency table (16 notes / octave)
28 const unsigned short CldsPlayer::frequency[] = {
29 343, 344, 345, 347, 348, 349, 350, 352, 353, 354, 356, 357, 358,
30 359, 361, 362, 363, 365, 366, 367, 369, 370, 371, 373, 374, 375,
31 377, 378, 379, 381, 382, 384, 385, 386, 388, 389, 391, 392, 393,
32 395, 396, 398, 399, 401, 402, 403, 405, 406, 408, 409, 411, 412,
33 414, 415, 417, 418, 420, 421, 423, 424, 426, 427, 429, 430, 432,
34 434, 435, 437, 438, 440, 442, 443, 445, 446, 448, 450, 451, 453,
35 454, 456, 458, 459, 461, 463, 464, 466, 468, 469, 471, 473, 475,
36 476, 478, 480, 481, 483, 485, 487, 488, 490, 492, 494, 496, 497,
37 499, 501, 503, 505, 506, 508, 510, 512, 514, 516, 518, 519, 521,
38 523, 525, 527, 529, 531, 533, 535, 537, 538, 540, 542, 544, 546,
39 548, 550, 552, 554, 556, 558, 560, 562, 564, 566, 568, 571, 573,
40 575, 577, 579, 581, 583, 585, 587, 589, 591, 594, 596, 598, 600,
41 602, 604, 607, 609, 611, 613, 615, 618, 620, 622, 624, 627, 629,
42 631, 633, 636, 638, 640, 643, 645, 647, 650, 652, 654, 657, 659,
43 662, 664, 666, 669, 671, 674, 676, 678, 681, 683
46 // Vibrato (sine) table
47 const unsigned char CldsPlayer::vibtab[] = {
48 0, 13, 25, 37, 50, 62, 74, 86, 98, 109, 120, 131, 142, 152, 162,
49 171, 180, 189, 197, 205, 212, 219, 225, 231, 236, 240, 244, 247,
50 250, 252, 254, 255, 255, 255, 254, 252, 250, 247, 244, 240, 236,
51 231, 225, 219, 212, 205, 197, 189, 180, 171, 162, 152, 142, 131,
52 120, 109, 98, 86, 74, 62, 50, 37, 25, 13
55 // Tremolo (sine * sine) table
56 const unsigned char CldsPlayer::tremtab[] = {
57 0, 0, 1, 1, 2, 4, 5, 7, 10, 12, 15, 18, 21, 25, 29, 33, 37, 42, 47,
58 52, 57, 62, 67, 73, 79, 85, 90, 97, 103, 109, 115, 121, 128, 134,
59 140, 146, 152, 158, 165, 170, 176, 182, 188, 193, 198, 203, 208,
60 213, 218, 222, 226, 230, 234, 237, 240, 243, 245, 248, 250, 251,
61 253, 254, 254, 255, 255, 255, 254, 254, 253, 251, 250, 248, 245,
62 243, 240, 237, 234, 230, 226, 222, 218, 213, 208, 203, 198, 193,
63 188, 182, 176, 170, 165, 158, 152, 146, 140, 134, 127, 121, 115,
64 109, 103, 97, 90, 85, 79, 73, 67, 62, 57, 52, 47, 42, 37, 33, 29,
65 25, 21, 18, 15, 12, 10, 7, 5, 4, 2, 1, 1, 0
68 // 'maxsound' is maximum number of patches (instruments)
69 // 'maxpos' is maximum number of entries in position list (orderlist)
70 const unsigned short CldsPlayer::maxsound = 0x3f, CldsPlayer::maxpos = 0xff;
72 /*** public methods *************************************/
74 CldsPlayer::CldsPlayer(Copl *newopl)
75 : CPlayer(newopl), soundbank(0), positions(0), patterns(0)
79 CldsPlayer::~CldsPlayer()
81 if(soundbank) delete [] soundbank;
82 if(positions) delete [] positions;
83 if(patterns) delete [] patterns;
86 bool CldsPlayer::load(const std::string &filename, const CFileProvider &fp)
92 // file validation section (actually just an extension check)
93 if(!fp.extension(filename, ".lds")) return false;
94 f = fp.open(filename); if(!f) return false;
96 // file load section (header)
98 if(mode > 2) { fp.close(f); return false; }
99 speed = f->readInt(2);
100 tempo = f->readInt(1);
101 pattlen = f->readInt(1);
102 for(i = 0; i < 9; i++) chandelay[i] = f->readInt(1);
103 regbd = f->readInt(1);
106 numpatch = f->readInt(2);
107 soundbank = new SoundBank[numpatch];
108 for(i = 0; i < numpatch; i++) {
110 sb->mod_misc = f->readInt(1); sb->mod_vol = f->readInt(1);
111 sb->mod_ad = f->readInt(1); sb->mod_sr = f->readInt(1);
112 sb->mod_wave = f->readInt(1); sb->car_misc = f->readInt(1);
113 sb->car_vol = f->readInt(1); sb->car_ad = f->readInt(1);
114 sb->car_sr = f->readInt(1); sb->car_wave = f->readInt(1);
115 sb->feedback = f->readInt(1); sb->keyoff = f->readInt(1);
116 sb->portamento = f->readInt(1); sb->glide = f->readInt(1);
117 sb->finetune = f->readInt(1); sb->vibrato = f->readInt(1);
118 sb->vibdelay = f->readInt(1); sb->mod_trem = f->readInt(1);
119 sb->car_trem = f->readInt(1); sb->tremwait = f->readInt(1);
120 sb->arpeggio = f->readInt(1);
121 for(j = 0; j < 12; j++) sb->arp_tab[j] = f->readInt(1);
122 sb->start = f->readInt(2); sb->size = f->readInt(2);
123 sb->fms = f->readInt(1); sb->transp = f->readInt(2);
124 sb->midinst = f->readInt(1); sb->midvelo = f->readInt(1);
125 sb->midkey = f->readInt(1); sb->midtrans = f->readInt(1);
126 sb->middum1 = f->readInt(1); sb->middum2 = f->readInt(1);
130 numposi = f->readInt(2);
131 positions = new Position[9 * numposi];
132 for(i = 0; i < numposi; i++)
133 for(j = 0; j < 9; j++) {
135 * patnum is a pointer inside the pattern space, but patterns are 16bit
136 * word fields anyway, so it ought to be an even number (hopefully) and
137 * we can just divide it by 2 to get our array index of 16bit words.
139 positions[i * 9 + j].patnum = f->readInt(2) / 2;
140 positions[i * 9 + j].transpose = f->readInt(1);
143 AdPlug_LogWrite("CldsPlayer::load(\"%s\",fp): loading LOUDNESS file: mode = "
144 "%d, pattlen = %d, numpatch = %d, numposi = %d\n",
145 filename.c_str(), mode, pattlen, numpatch, numposi);
148 f->ignore(2); // ignore # of digital sounds (not played by this player)
149 patterns = new unsigned short[(fp.filesize(f) - f->pos()) / 2 + 1];
150 for(i = 0; !f->eof(); i++)
151 patterns[i] = f->readInt(2);
158 bool CldsPlayer::update()
160 unsigned short comword, freq, octave, chan, tune, wibc, tremc, arpreg;
162 unsigned char level, regnum, comhi, comlo;
166 if(!playing) return false;
170 if(fadeonoff <= 128) {
171 if(allvolume > fadeonoff || allvolume == 0)
172 allvolume -= fadeonoff;
179 for(i = 0; i < 9; i++)
180 channel[i].keycount = 1;
184 if(((allvolume + (0x100 - fadeonoff)) & 0xff) <= mainvolume)
185 allvolume += 0x100 - fadeonoff;
187 allvolume = mainvolume;
191 // handle channel delay
192 for(chan = 0; chan < 9; chan++) {
194 if(c->chancheat.chandelay)
195 if(!(--c->chancheat.chandelay))
196 playsound(c->chancheat.sound, chan, c->chancheat.high);
202 for(chan = 0; chan < 9; chan++) {
205 unsigned short patnum = positions[posplay * 9 + chan].patnum;
206 unsigned char transpose = positions[posplay * 9 + chan].transpose;
208 comword = patterns[patnum + c->packpos];
209 comhi = comword >> 8; comlo = comword & 0xff;
217 c->volcar = (((c->volcar & 0x3f) * comlo) >> 6) & 0x3f;
218 if(fmchip[0xc0 + chan] & 1)
219 c->volmod = (((c->volmod & 0x3f) * comlo) >> 6) & 0x3f;
222 tempo = comword & 0x3f;
229 // in real player there's also full keyoff here, but we don't need it
236 jumppos = (posplay + 1) & maxpos;
240 jumppos = comlo & maxpos;
242 if(jumppos < posplay) songlooped = true;
249 // PASCAL: c->vibspeed = ((comlo >> 4) & 15) + 2;
250 c->vibspeed = (comlo >> 4) + 2;
251 c->vibrate = (comlo & 15) + 1;
261 allvolume = mainvolume = comlo;
266 if(!hardfade) fadeonoff = comlo;
271 case 0xf1: // panorama
273 // MIDI commands (unhandled)
274 AdPlug_LogWrite("CldsPlayer(): not handling MIDI command 0x%x, "
275 "value = 0x%x\n", comhi);
279 c->glideto = comhi & 0x1f;
281 AdPlug_LogWrite("CldsPlayer(): unknown command 0x%x encountered!"
282 " value = 0x%x\n", comhi, comlo);
288 signed char transp = transpose & 127;
291 * Originally, in assembler code, the player first shifted
292 * logically left the transpose byte by 1 and then shifted
293 * arithmetically right the same byte to achieve the final,
294 * signed transpose value. Since we can't do arithmetic shifts
295 * in C, we just duplicate the 7th bit into the 8th one and
296 * discard the 8th one completely.
299 if(transpose & 64) transp |= 128;
301 if(transpose & 128) {
302 sound = (comlo + transp) & maxsound;
305 sound = comlo & maxsound;
306 high = (comhi + transp) << 4;
311 sound = comlo & maxsound;
312 high = (comhi + (((transpose + 0x24) & 0xff) - 0x24)) << 4;
316 playsound(sound, chan, high);
318 c->chancheat.chandelay = chandelay[chan];
319 c->chancheat.sound = sound;
320 c->chancheat.high = high;
331 The continue table is updated here, but this is only used in the
332 original player, which can be paused in the middle of a song and then
333 unpaused. Since AdPlug does all this for us automatically, we don't
334 have a continue table here. The continue table update code is noted
335 here for reference only.
338 conttab[speed & maxcont].position = posplay & 0xff;
339 conttab[speed & maxcont].tempo = tempo;
345 for(i = 0; i < 9; i++) channel[i].packpos = channel[i].packwait = 0;
348 if(pattplay >= pattlen) {
350 for(i = 0; i < 9; i++) channel[i].packpos = channel[i].packwait = 0;
351 posplay = (posplay + 1) & maxpos;
357 for(chan = 0; chan < 9; chan++) {
359 regnum = op_table[chan];
360 if(c->keycount > 0) {
362 setregs_adv(0xb0 + chan, 0xdf, 0);
370 arpreg = c->arp_tab[c->arp_pos] << 4;
371 if(arpreg == 0x800) {
372 if(c->arp_pos > 0) c->arp_tab[0] = c->arp_tab[c->arp_pos - 1];
373 c->arp_size = 1; c->arp_pos = 0;
374 arpreg = c->arp_tab[0] << 4;
377 if(c->arp_count == c->arp_speed) {
379 if(c->arp_pos >= c->arp_size) c->arp_pos = 0;
385 // glide & portamento
386 if(c->lasttune && (c->lasttune != c->gototune)) {
387 if(c->lasttune > c->gototune) {
388 if(c->lasttune - c->gototune < c->portspeed)
389 c->lasttune = c->gototune;
391 c->lasttune -= c->portspeed;
393 if(c->gototune - c->lasttune < c->portspeed)
394 c->lasttune = c->gototune;
396 c->lasttune += c->portspeed;
400 arpreg = c->lasttune - (arpreg ^ 0xff0) - 16;
402 arpreg += c->lasttune;
404 freq = frequency[arpreg % (12 * 16)];
405 octave = arpreg / (12 * 16) - 1;
406 setregs(0xa0 + chan, freq & 0xff);
407 setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf);
412 wibc = vibtab[c->vibcount & 0x3f] * c->vibrate;
414 if((c->vibcount & 0x40) == 0)
415 tune = c->lasttune + (wibc >> 8);
417 tune = c->lasttune - (wibc >> 8);
420 tune = tune - (arpreg ^ 0xff0) - 16;
424 freq = frequency[tune % (12 * 16)];
425 octave = tune / (12 * 16) - 1;
426 setregs(0xa0 + chan, freq & 0xff);
427 setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf);
428 c->vibcount += c->vibspeed;
430 if(c->arp_size != 0) { // no vibrato, just arpeggio
432 tune = c->lasttune - (arpreg ^ 0xff0) - 16;
434 tune = c->lasttune + arpreg;
436 freq = frequency[tune % (12 * 16)];
437 octave = tune / (12 * 16) - 1;
438 setregs(0xa0 + chan, freq & 0xff);
439 setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf);
441 } else { // no vibrato, just arpeggio
444 if(c->arp_size != 0) {
446 tune = c->lasttune - (arpreg ^ 0xff0) - 16;
448 tune = c->lasttune + arpreg;
450 freq = frequency[tune % (12 * 16)];
451 octave = tune / (12 * 16) - 1;
452 setregs(0xa0 + chan, freq & 0xff);
453 setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf);
458 // tremolo (modulator)
461 tremc = tremtab[c->trmcount & 0x7f] * c->trmrate;
462 if((tremc >> 8) <= (c->volmod & 0x3f))
463 level = (c->volmod & 0x3f) - (tremc >> 8);
467 if(allvolume != 0 && (fmchip[0xc0 + chan] & 1))
468 setregs_adv(0x40 + regnum, 0xc0, ((level * allvolume) >> 8) ^ 0x3f);
470 setregs_adv(0x40 + regnum, 0xc0, level ^ 0x3f);
472 c->trmcount += c->trmspeed;
474 if(allvolume != 0 && (fmchip[0xc0 + chan] & 1))
475 setregs_adv(0x40 + regnum, 0xc0, ((((c->volmod & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f);
477 setregs_adv(0x40 + regnum, 0xc0, (c->volmod ^ 0x3f) & 0x3f);
480 if(allvolume != 0 && (fmchip[0xc0 + chan] & 1))
481 setregs_adv(0x40 + regnum, 0xc0, ((((c->volmod & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f);
487 tremc = tremtab[c->trccount & 0x7f] * c->trcrate;
488 if((tremc >> 8) <= (c->volcar & 0x3f))
489 level = (c->volcar & 0x3f) - (tremc >> 8);
494 setregs_adv(0x43 + regnum, 0xc0, ((level * allvolume) >> 8) ^ 0x3f);
496 setregs_adv(0x43 + regnum, 0xc0, level ^ 0x3f);
497 c->trccount += c->trcspeed;
500 setregs_adv(0x43 + regnum, 0xc0, ((((c->volcar & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f);
502 setregs_adv(0x43 + regnum, 0xc0, (c->volcar ^ 0x3f) & 0x3f);
506 setregs_adv(0x43 + regnum, 0xc0, ((((c->volcar & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f);
510 return (!playing || songlooped) ? false : true;
513 void CldsPlayer::rewind(int subsong)
518 tempo_now = 3; playing = true; songlooped = false;
519 jumping = fadeonoff = allvolume = hardfade = pattplay = posplay = jumppos =
521 memset(channel, 0, sizeof(channel));
522 memset(fmchip, 0, sizeof(fmchip));
525 opl->init(); // Reset OPL chip
528 opl->write(0xbd, regbd);
530 for(i = 0; i < 9; i++) {
531 opl->write(0x20 + op_table[i], 0);
532 opl->write(0x23 + op_table[i], 0);
533 opl->write(0x40 + op_table[i], 0x3f);
534 opl->write(0x43 + op_table[i], 0x3f);
535 opl->write(0x60 + op_table[i], 0xff);
536 opl->write(0x63 + op_table[i], 0xff);
537 opl->write(0x80 + op_table[i], 0xff);
538 opl->write(0x83 + op_table[i], 0xff);
539 opl->write(0xe0 + op_table[i], 0);
540 opl->write(0xe3 + op_table[i], 0);
541 opl->write(0xa0 + i, 0);
542 opl->write(0xb0 + i, 0);
543 opl->write(0xc0 + i, 0);
547 /*** private methods *************************************/
549 void CldsPlayer::playsound(int inst_number, int channel_number, int tunehigh)
551 Channel *c = &channel[channel_number]; // current channel
552 SoundBank *i = &soundbank[inst_number]; // current instrument
553 unsigned int regnum = op_table[channel_number]; // channel's OPL2 register
554 unsigned char volcalc, octave;
558 tunehigh += ((i->finetune + c->finetune + 0x80) & 0xff) - 0x80;
562 unsigned short arpcalc = i->arp_tab[0] << 4;
565 tunehigh = tunehigh - (arpcalc ^ 0xff0) - 16;
571 if(c->glideto != 0) {
572 c->gototune = tunehigh;
573 c->portspeed = c->glideto;
574 c->glideto = c->finetune = 0;
578 // set modulator registers
579 setregs(0x20 + regnum, i->mod_misc);
580 volcalc = i->mod_vol;
581 if(!c->nextvol || !(i->feedback & 1))
584 c->volmod = (volcalc & 0xc0) | ((((volcalc & 0x3f) * c->nextvol) >> 6));
586 if((i->feedback & 1) == 1 && allvolume != 0)
587 setregs(0x40 + regnum, ((c->volmod & 0xc0) | (((c->volmod & 0x3f) * allvolume) >> 8)) ^ 0x3f);
589 setregs(0x40 + regnum, c->volmod ^ 0x3f);
590 setregs(0x60 + regnum, i->mod_ad);
591 setregs(0x80 + regnum, i->mod_sr);
592 setregs(0xe0 + regnum, i->mod_wave);
594 // Set carrier registers
595 setregs(0x23 + regnum, i->car_misc);
596 volcalc = i->car_vol;
600 c->volcar = (volcalc & 0xc0) | ((((volcalc & 0x3f) * c->nextvol) >> 6));
603 setregs(0x43 + regnum, ((c->volcar & 0xc0) | (((c->volcar & 0x3f) * allvolume) >> 8)) ^ 0x3f);
605 setregs(0x43 + regnum, c->volcar ^ 0x3f);
606 setregs(0x63 + regnum, i->car_ad);
607 setregs(0x83 + regnum, i->car_sr);
608 setregs(0xe3 + regnum, i->car_wave);
609 setregs(0xc0 + channel_number, i->feedback);
610 setregs_adv(0xb0 + channel_number, 0xdf, 0); // key off
612 freq = frequency[tunehigh % (12 * 16)];
613 octave = tunehigh / (12 * 16) - 1;
615 if(!i->portamento || !c->lasttune) {
616 setregs(0xa0 + channel_number, freq & 0xff);
617 setregs(0xb0 + channel_number, (octave << 2) + 0x20 + (freq >> 8));
618 c->lasttune = c->gototune = tunehigh;
620 c->gototune = tunehigh;
621 c->portspeed = i->portamento;
622 setregs_adv(0xb0 + channel_number, 0xdf, 0x20); // key on
625 setregs(0xa0 + channel_number, freq & 0xff);
626 setregs(0xb0 + channel_number, (octave << 2) + 0x20 + (freq >> 8));
627 c->lasttune = tunehigh;
628 c->gototune = tunehigh + ((i->glide + 0x80) & 0xff) - 0x80; // set destination
629 c->portspeed = i->portamento;
633 c->vibwait = c->vibspeed = c->vibrate = 0;
635 c->vibwait = i->vibdelay;
636 // PASCAL: c->vibspeed = ((i->vibrato >> 4) & 15) + 1;
637 c->vibspeed = (i->vibrato >> 4) + 2;
638 c->vibrate = (i->vibrato & 15) + 1;
641 if(!(c->trmstay & 0xf0)) {
642 c->trmwait = (i->tremwait & 0xf0) >> 3;
643 // PASCAL: c->trmspeed = (i->mod_trem >> 4) & 15;
644 c->trmspeed = i->mod_trem >> 4;
645 c->trmrate = i->mod_trem & 15;
649 if(!(c->trmstay & 0x0f)) {
650 c->trcwait = (i->tremwait & 15) << 1;
651 // PASCAL: c->trcspeed = (i->car_trem >> 4) & 15;
652 c->trcspeed = i->car_trem >> 4;
653 c->trcrate = i->car_trem & 15;
657 c->arp_size = i->arpeggio & 15;
658 c->arp_speed = i->arpeggio >> 4;
659 memcpy(c->arp_tab, i->arp_tab, 12);
660 c->keycount = i->keyoff;
661 c->nextvol = c->glideto = c->finetune = c->vibcount = c->arp_pos = c->arp_count = 0;
664 inline void CldsPlayer::setregs(unsigned char reg, unsigned char val)
666 if(fmchip[reg] == val) return;
669 opl->write(reg, val);
672 inline void CldsPlayer::setregs_adv(unsigned char reg, unsigned char mask,
675 setregs(reg, (fmchip[reg] & mask) | val);