Mercurial > audlegacy-plugins
diff src/adplug/core/lds.cxx @ 955:4709ce4e209e trunk
[svn] Run indent -ts4 -nut -bli0 -cdw on this messy lot. Upstream is not consistent with whitespace anyway, no loss there.
author | chainsaw |
---|---|
date | Sat, 14 Apr 2007 15:23:50 -0700 |
parents | cae46214b8bf |
children | d49fca0fc7fd |
line wrap: on
line diff
--- a/src/adplug/core/lds.cxx Sat Apr 14 08:48:41 2007 -0700 +++ b/src/adplug/core/lds.cxx Sat Apr 14 15:23:50 2007 -0700 @@ -25,7 +25,8 @@ #include "debug.h" // Note frequency table (16 notes / octave) -const unsigned short CldsPlayer::frequency[] = { +const unsigned short + CldsPlayer::frequency[] = { 343, 344, 345, 347, 348, 349, 350, 352, 353, 354, 356, 357, 358, 359, 361, 362, 363, 365, 366, 367, 369, 370, 371, 373, 374, 375, 377, 378, 379, 381, 382, 384, 385, 386, 388, 389, 391, 392, 393, @@ -44,7 +45,8 @@ }; // Vibrato (sine) table -const unsigned char CldsPlayer::vibtab[] = { +const unsigned char + CldsPlayer::vibtab[] = { 0, 13, 25, 37, 50, 62, 74, 86, 98, 109, 120, 131, 142, 152, 162, 171, 180, 189, 197, 205, 212, 219, 225, 231, 236, 240, 244, 247, 250, 252, 254, 255, 255, 255, 254, 252, 250, 247, 244, 240, 236, @@ -53,7 +55,8 @@ }; // Tremolo (sine * sine) table -const unsigned char CldsPlayer::tremtab[] = { +const unsigned char + CldsPlayer::tremtab[] = { 0, 0, 1, 1, 2, 4, 5, 7, 10, 12, 15, 18, 21, 25, 29, 33, 37, 42, 47, 52, 57, 62, 67, 73, 79, 85, 90, 97, 103, 109, 115, 121, 128, 134, 140, 146, 152, 158, 165, 170, 176, 182, 188, 193, 198, 203, 208, @@ -67,509 +70,618 @@ // 'maxsound' is maximum number of patches (instruments) // 'maxpos' is maximum number of entries in position list (orderlist) -const unsigned short CldsPlayer::maxsound = 0x3f, CldsPlayer::maxpos = 0xff; +const unsigned short + CldsPlayer::maxsound = 0x3f, CldsPlayer::maxpos = 0xff; /*** public methods *************************************/ -CldsPlayer::CldsPlayer(Copl *newopl) - : CPlayer(newopl), soundbank(0), positions(0), patterns(0) +CldsPlayer::CldsPlayer (Copl * newopl):CPlayer (newopl), soundbank (0), positions (0), +patterns (0) { } -CldsPlayer::~CldsPlayer() +CldsPlayer::~CldsPlayer () { - if(soundbank) delete [] soundbank; - if(positions) delete [] positions; - if(patterns) delete [] patterns; + if (soundbank) + delete[]soundbank; + if (positions) + delete[]positions; + if (patterns) + delete[]patterns; } -bool CldsPlayer::load(VFSFile *fd, const CFileProvider &fp) +bool +CldsPlayer::load (VFSFile * fd, const CFileProvider & fp) { - binistream *f; - unsigned int i, j; - SoundBank *sb; - std::string filename(fd->uri); + binistream *f; + unsigned int i, j; + SoundBank *sb; + std::string filename (fd->uri); // file validation section (actually just an extension check) - f = fp.open(fd); if(!f) return false; - if(!fp.extension(filename, ".lds")) return false; + f = fp.open (fd); + if (!f) + return false; + if (!fp.extension (filename, ".lds")) + return false; // file load section (header) - mode = f->readInt(1); - if(mode > 2) { fp.close(f); return false; } - speed = f->readInt(2); - tempo = f->readInt(1); - pattlen = f->readInt(1); - for(i = 0; i < 9; i++) chandelay[i] = f->readInt(1); - regbd = f->readInt(1); + mode = f->readInt (1); + if (mode > 2) + { + fp.close (f); + return false; + } + speed = f->readInt (2); + tempo = f->readInt (1); + pattlen = f->readInt (1); + for (i = 0; i < 9; i++) + chandelay[i] = f->readInt (1); + regbd = f->readInt (1); // load patches - numpatch = f->readInt(2); + numpatch = f->readInt (2); soundbank = new SoundBank[numpatch]; - for(i = 0; i < numpatch; i++) { + for (i = 0; i < numpatch; i++) + { sb = &soundbank[i]; - sb->mod_misc = f->readInt(1); sb->mod_vol = f->readInt(1); - sb->mod_ad = f->readInt(1); sb->mod_sr = f->readInt(1); - sb->mod_wave = f->readInt(1); sb->car_misc = f->readInt(1); - sb->car_vol = f->readInt(1); sb->car_ad = f->readInt(1); - sb->car_sr = f->readInt(1); sb->car_wave = f->readInt(1); - sb->feedback = f->readInt(1); sb->keyoff = f->readInt(1); - sb->portamento = f->readInt(1); sb->glide = f->readInt(1); - sb->finetune = f->readInt(1); sb->vibrato = f->readInt(1); - sb->vibdelay = f->readInt(1); sb->mod_trem = f->readInt(1); - sb->car_trem = f->readInt(1); sb->tremwait = f->readInt(1); - sb->arpeggio = f->readInt(1); - for(j = 0; j < 12; j++) sb->arp_tab[j] = f->readInt(1); - sb->start = f->readInt(2); sb->size = f->readInt(2); - sb->fms = f->readInt(1); sb->transp = f->readInt(2); - sb->midinst = f->readInt(1); sb->midvelo = f->readInt(1); - sb->midkey = f->readInt(1); sb->midtrans = f->readInt(1); - sb->middum1 = f->readInt(1); sb->middum2 = f->readInt(1); + sb->mod_misc = f->readInt (1); + sb->mod_vol = f->readInt (1); + sb->mod_ad = f->readInt (1); + sb->mod_sr = f->readInt (1); + sb->mod_wave = f->readInt (1); + sb->car_misc = f->readInt (1); + sb->car_vol = f->readInt (1); + sb->car_ad = f->readInt (1); + sb->car_sr = f->readInt (1); + sb->car_wave = f->readInt (1); + sb->feedback = f->readInt (1); + sb->keyoff = f->readInt (1); + sb->portamento = f->readInt (1); + sb->glide = f->readInt (1); + sb->finetune = f->readInt (1); + sb->vibrato = f->readInt (1); + sb->vibdelay = f->readInt (1); + sb->mod_trem = f->readInt (1); + sb->car_trem = f->readInt (1); + sb->tremwait = f->readInt (1); + sb->arpeggio = f->readInt (1); + for (j = 0; j < 12; j++) + sb->arp_tab[j] = f->readInt (1); + sb->start = f->readInt (2); + sb->size = f->readInt (2); + sb->fms = f->readInt (1); + sb->transp = f->readInt (2); + sb->midinst = f->readInt (1); + sb->midvelo = f->readInt (1); + sb->midkey = f->readInt (1); + sb->midtrans = f->readInt (1); + sb->middum1 = f->readInt (1); + sb->middum2 = f->readInt (1); } // load positions - numposi = f->readInt(2); + numposi = f->readInt (2); positions = new Position[9 * numposi]; - for(i = 0; i < numposi; i++) - for(j = 0; j < 9; j++) { + for (i = 0; i < numposi; i++) + for (j = 0; j < 9; j++) + { /* * patnum is a pointer inside the pattern space, but patterns are 16bit * word fields anyway, so it ought to be an even number (hopefully) and * we can just divide it by 2 to get our array index of 16bit words. */ - positions[i * 9 + j].patnum = f->readInt(2) / 2; - positions[i * 9 + j].transpose = f->readInt(1); + positions[i * 9 + j].patnum = f->readInt (2) / 2; + positions[i * 9 + j].transpose = f->readInt (1); } - AdPlug_LogWrite("CldsPlayer::load(\"%s\",fp): loading LOUDNESS file: mode = " - "%d, pattlen = %d, numpatch = %d, numposi = %d\n", - filename.c_str(), mode, pattlen, numpatch, numposi); + AdPlug_LogWrite + ("CldsPlayer::load(\"%s\",fp): loading LOUDNESS file: mode = " + "%d, pattlen = %d, numpatch = %d, numposi = %d\n", filename.c_str (), + mode, pattlen, numpatch, numposi); // load patterns - f->ignore(2); // ignore # of digital sounds (not played by this player) - patterns = new unsigned short[(fp.filesize(f) - f->pos()) / 2 + 1]; - for(i = 0; !f->eof(); i++) - patterns[i] = f->readInt(2); + f->ignore (2); // ignore # of digital sounds (not played by this player) + patterns = new unsigned short[(fp.filesize (f) - f->pos ()) / 2 + 1]; + for (i = 0; !f->eof (); i++) + patterns[i] = f->readInt (2); - fp.close(f); - rewind(0); + fp.close (f); + rewind (0); return true; } -bool CldsPlayer::update() +bool +CldsPlayer::update () { - unsigned short comword, freq, octave, chan, tune, wibc, tremc, arpreg; - bool vbreak; - unsigned char level, regnum, comhi, comlo; - int i; - Channel *c; + unsigned short comword, freq, octave, chan, tune, wibc, tremc, arpreg; + bool vbreak; + unsigned char level, regnum, comhi, comlo; + int i; + Channel *c; - if(!playing) return false; + if (!playing) + return false; // handle fading - if(fadeonoff) - if(fadeonoff <= 128) { - if(allvolume > fadeonoff || allvolume == 0) - allvolume -= fadeonoff; - else { - allvolume = 1; - fadeonoff = 0; - if(hardfade != 0) { - playing = false; - hardfade = 0; - for(i = 0; i < 9; i++) - channel[i].keycount = 1; - } + if (fadeonoff) + if (fadeonoff <= 128) + { + if (allvolume > fadeonoff || allvolume == 0) + allvolume -= fadeonoff; + else + { + allvolume = 1; + fadeonoff = 0; + if (hardfade != 0) + { + playing = false; + hardfade = 0; + for (i = 0; i < 9; i++) + channel[i].keycount = 1; + } } - } else - if((unsigned int)((allvolume + (0x100 - fadeonoff)) & 0xff) <= mainvolume) - allvolume += 0x100 - fadeonoff; - else { - allvolume = mainvolume; - fadeonoff = 0; - } + } + else + if ((unsigned int) ((allvolume + (0x100 - fadeonoff)) & 0xff) <= + mainvolume) + allvolume += 0x100 - fadeonoff; + else + { + allvolume = mainvolume; + fadeonoff = 0; + } // handle channel delay - for(chan = 0; chan < 9; chan++) { + for (chan = 0; chan < 9; chan++) + { c = &channel[chan]; - if(c->chancheat.chandelay) - if(!(--c->chancheat.chandelay)) - playsound(c->chancheat.sound, chan, c->chancheat.high); + if (c->chancheat.chandelay) + if (!(--c->chancheat.chandelay)) + playsound (c->chancheat.sound, chan, c->chancheat.high); } // handle notes - if(!tempo_now) { + if (!tempo_now) + { vbreak = false; - for(chan = 0; chan < 9; chan++) { + for (chan = 0; chan < 9; chan++) + { c = &channel[chan]; - if(!c->packwait) { - unsigned short patnum = positions[posplay * 9 + chan].patnum; - unsigned char transpose = positions[posplay * 9 + chan].transpose; + if (!c->packwait) + { + unsigned short patnum = positions[posplay * 9 + chan].patnum; + unsigned char transpose = positions[posplay * 9 + chan].transpose; - comword = patterns[patnum + c->packpos]; - comhi = comword >> 8; comlo = comword & 0xff; - if(comword) - if(comhi == 0x80) - c->packwait = comlo; - else - if(comhi >= 0x80) { - switch(comhi) { - case 0xff: - c->volcar = (((c->volcar & 0x3f) * comlo) >> 6) & 0x3f; - if(fmchip[0xc0 + chan] & 1) - c->volmod = (((c->volmod & 0x3f) * comlo) >> 6) & 0x3f; - break; - case 0xfe: - tempo = comword & 0x3f; - break; - case 0xfd: - c->nextvol = comlo; - break; - case 0xfc: - playing = false; - // in real player there's also full keyoff here, but we don't need it - break; - case 0xfb: - c->keycount = 1; - break; - case 0xfa: - vbreak = true; - jumppos = (posplay + 1) & maxpos; - break; - case 0xf9: - vbreak = true; - jumppos = comlo & maxpos; - jumping = 1; - if(jumppos < posplay) songlooped = true; - break; - case 0xf8: - c->lasttune = 0; - break; - case 0xf7: - c->vibwait = 0; - // PASCAL: c->vibspeed = ((comlo >> 4) & 15) + 2; - c->vibspeed = (comlo >> 4) + 2; - c->vibrate = (comlo & 15) + 1; - break; - case 0xf6: - c->glideto = comlo; - break; - case 0xf5: - c->finetune = comlo; - break; - case 0xf4: - if(!hardfade) { - allvolume = mainvolume = comlo; - fadeonoff = 0; - } - break; - case 0xf3: - if(!hardfade) fadeonoff = comlo; - break; - case 0xf2: - c->trmstay = comlo; - break; - case 0xf1: // panorama - case 0xf0: // progch - // MIDI commands (unhandled) - AdPlug_LogWrite("CldsPlayer(): not handling MIDI command 0x%x, " - "value = 0x%x\n", comhi); - break; - default: - if(comhi < 0xa0) - c->glideto = comhi & 0x1f; - else - AdPlug_LogWrite("CldsPlayer(): unknown command 0x%x encountered!" - " value = 0x%x\n", comhi, comlo); - break; - } - } else { - unsigned char sound; - unsigned short high; - signed char transp = transpose & 127; + comword = patterns[patnum + c->packpos]; + comhi = comword >> 8; + comlo = comword & 0xff; + if (comword) + if (comhi == 0x80) + c->packwait = comlo; + else if (comhi >= 0x80) + { + switch (comhi) + { + case 0xff: + c->volcar = (((c->volcar & 0x3f) * comlo) >> 6) & 0x3f; + if (fmchip[0xc0 + chan] & 1) + c->volmod = (((c->volmod & 0x3f) * comlo) >> 6) & 0x3f; + break; + case 0xfe: + tempo = comword & 0x3f; + break; + case 0xfd: + c->nextvol = comlo; + break; + case 0xfc: + playing = false; + // in real player there's also full keyoff here, but we don't need it + break; + case 0xfb: + c->keycount = 1; + break; + case 0xfa: + vbreak = true; + jumppos = (posplay + 1) & maxpos; + break; + case 0xf9: + vbreak = true; + jumppos = comlo & maxpos; + jumping = 1; + if (jumppos < posplay) + songlooped = true; + break; + case 0xf8: + c->lasttune = 0; + break; + case 0xf7: + c->vibwait = 0; + // PASCAL: c->vibspeed = ((comlo >> 4) & 15) + 2; + c->vibspeed = (comlo >> 4) + 2; + c->vibrate = (comlo & 15) + 1; + break; + case 0xf6: + c->glideto = comlo; + break; + case 0xf5: + c->finetune = comlo; + break; + case 0xf4: + if (!hardfade) + { + allvolume = mainvolume = comlo; + fadeonoff = 0; + } + break; + case 0xf3: + if (!hardfade) + fadeonoff = comlo; + break; + case 0xf2: + c->trmstay = comlo; + break; + case 0xf1: // panorama + case 0xf0: // progch + // MIDI commands (unhandled) + AdPlug_LogWrite + ("CldsPlayer(): not handling MIDI command 0x%x, " + "value = 0x%x\n", comhi); + break; + default: + if (comhi < 0xa0) + c->glideto = comhi & 0x1f; + else + AdPlug_LogWrite + ("CldsPlayer(): unknown command 0x%x encountered!" + " value = 0x%x\n", comhi, comlo); + break; + } + } + else + { + unsigned char sound; + unsigned short high; + signed char transp = transpose & 127; - /* - * Originally, in assembler code, the player first shifted - * logically left the transpose byte by 1 and then shifted - * arithmetically right the same byte to achieve the final, - * signed transpose value. Since we can't do arithmetic shifts - * in C, we just duplicate the 7th bit into the 8th one and - * discard the 8th one completely. - */ + /* + * Originally, in assembler code, the player first shifted + * logically left the transpose byte by 1 and then shifted + * arithmetically right the same byte to achieve the final, + * signed transpose value. Since we can't do arithmetic shifts + * in C, we just duplicate the 7th bit into the 8th one and + * discard the 8th one completely. + */ - if(transpose & 64) transp |= 128; - - if(transpose & 128) { - sound = (comlo + transp) & maxsound; - high = comhi << 4; - } else { - sound = comlo & maxsound; - high = (comhi + transp) << 4; - } + if (transpose & 64) + transp |= 128; - /* - PASCAL: - sound = comlo & maxsound; - high = (comhi + (((transpose + 0x24) & 0xff) - 0x24)) << 4; - */ + if (transpose & 128) + { + sound = (comlo + transp) & maxsound; + high = comhi << 4; + } + else + { + sound = comlo & maxsound; + high = (comhi + transp) << 4; + } - if(!chandelay[chan]) - playsound(sound, chan, high); - else { - c->chancheat.chandelay = chandelay[chan]; - c->chancheat.sound = sound; - c->chancheat.high = high; - } - } + /* + PASCAL: + sound = comlo & maxsound; + high = (comhi + (((transpose + 0x24) & 0xff) - 0x24)) << 4; + */ - c->packpos++; - } else - c->packwait--; + if (!chandelay[chan]) + playsound (sound, chan, high); + else + { + c->chancheat.chandelay = chandelay[chan]; + c->chancheat.sound = sound; + c->chancheat.high = high; + } + } + + c->packpos++; + } + else + c->packwait--; } tempo_now = tempo; /* - The continue table is updated here, but this is only used in the - original player, which can be paused in the middle of a song and then - unpaused. Since AdPlug does all this for us automatically, we don't - have a continue table here. The continue table update code is noted - here for reference only. + The continue table is updated here, but this is only used in the + original player, which can be paused in the middle of a song and then + unpaused. Since AdPlug does all this for us automatically, we don't + have a continue table here. The continue table update code is noted + here for reference only. - if(!pattplay) { - conttab[speed & maxcont].position = posplay & 0xff; - conttab[speed & maxcont].tempo = tempo; - } - */ + if(!pattplay) { + conttab[speed & maxcont].position = posplay & 0xff; + conttab[speed & maxcont].tempo = tempo; + } + */ pattplay++; - if(vbreak) { + if (vbreak) + { pattplay = 0; - for(i = 0; i < 9; i++) channel[i].packpos = channel[i].packwait = 0; + for (i = 0; i < 9; i++) + channel[i].packpos = channel[i].packwait = 0; posplay = jumppos; - } else - if(pattplay >= pattlen) { - pattplay = 0; - for(i = 0; i < 9; i++) channel[i].packpos = channel[i].packwait = 0; - posplay = (posplay + 1) & maxpos; - } - } else + } + else if (pattplay >= pattlen) + { + pattplay = 0; + for (i = 0; i < 9; i++) + channel[i].packpos = channel[i].packwait = 0; + posplay = (posplay + 1) & maxpos; + } + } + else tempo_now--; // make effects - for(chan = 0; chan < 9; chan++) { + for (chan = 0; chan < 9; chan++) + { c = &channel[chan]; regnum = op_table[chan]; - if(c->keycount > 0) { - if(c->keycount == 1) - setregs_adv(0xb0 + chan, 0xdf, 0); + if (c->keycount > 0) + { + if (c->keycount == 1) + setregs_adv (0xb0 + chan, 0xdf, 0); c->keycount--; } // arpeggio - if(c->arp_size == 0) + if (c->arp_size == 0) arpreg = 0; - else { + else + { arpreg = c->arp_tab[c->arp_pos] << 4; - if(arpreg == 0x800) { - if(c->arp_pos > 0) c->arp_tab[0] = c->arp_tab[c->arp_pos - 1]; - c->arp_size = 1; c->arp_pos = 0; - arpreg = c->arp_tab[0] << 4; + if (arpreg == 0x800) + { + if (c->arp_pos > 0) + c->arp_tab[0] = c->arp_tab[c->arp_pos - 1]; + c->arp_size = 1; + c->arp_pos = 0; + arpreg = c->arp_tab[0] << 4; } - if(c->arp_count == c->arp_speed) { - c->arp_pos++; - if(c->arp_pos >= c->arp_size) c->arp_pos = 0; - c->arp_count = 0; - } else - c->arp_count++; + if (c->arp_count == c->arp_speed) + { + c->arp_pos++; + if (c->arp_pos >= c->arp_size) + c->arp_pos = 0; + c->arp_count = 0; + } + else + c->arp_count++; } // glide & portamento - if(c->lasttune && (c->lasttune != c->gototune)) { - if(c->lasttune > c->gototune) { - if(c->lasttune - c->gototune < c->portspeed) - c->lasttune = c->gototune; - else - c->lasttune -= c->portspeed; - } else { - if(c->gototune - c->lasttune < c->portspeed) - c->lasttune = c->gototune; - else - c->lasttune += c->portspeed; + if (c->lasttune && (c->lasttune != c->gototune)) + { + if (c->lasttune > c->gototune) + { + if (c->lasttune - c->gototune < c->portspeed) + c->lasttune = c->gototune; + else + c->lasttune -= c->portspeed; + } + else + { + if (c->gototune - c->lasttune < c->portspeed) + c->lasttune = c->gototune; + else + c->lasttune += c->portspeed; } - if(arpreg >= 0x800) - arpreg = c->lasttune - (arpreg ^ 0xff0) - 16; + if (arpreg >= 0x800) + arpreg = c->lasttune - (arpreg ^ 0xff0) - 16; else - arpreg += c->lasttune; + arpreg += c->lasttune; freq = frequency[arpreg % (12 * 16)]; octave = arpreg / (12 * 16) - 1; - setregs(0xa0 + chan, freq & 0xff); - setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf); - } else { + setregs (0xa0 + chan, freq & 0xff); + setregs_adv (0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf); + } + else + { // vibrato - if(!c->vibwait) { - if(c->vibrate) { - wibc = vibtab[c->vibcount & 0x3f] * c->vibrate; + if (!c->vibwait) + { + if (c->vibrate) + { + wibc = vibtab[c->vibcount & 0x3f] * c->vibrate; - if((c->vibcount & 0x40) == 0) - tune = c->lasttune + (wibc >> 8); - else - tune = c->lasttune - (wibc >> 8); + if ((c->vibcount & 0x40) == 0) + tune = c->lasttune + (wibc >> 8); + else + tune = c->lasttune - (wibc >> 8); - if(arpreg >= 0x800) - tune = tune - (arpreg ^ 0xff0) - 16; - else - tune += arpreg; + if (arpreg >= 0x800) + tune = tune - (arpreg ^ 0xff0) - 16; + else + tune += arpreg; - freq = frequency[tune % (12 * 16)]; - octave = tune / (12 * 16) - 1; - setregs(0xa0 + chan, freq & 0xff); - setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf); - c->vibcount += c->vibspeed; - } else - if(c->arp_size != 0) { // no vibrato, just arpeggio - if(arpreg >= 0x800) - tune = c->lasttune - (arpreg ^ 0xff0) - 16; - else - tune = c->lasttune + arpreg; + freq = frequency[tune % (12 * 16)]; + octave = tune / (12 * 16) - 1; + setregs (0xa0 + chan, freq & 0xff); + setregs_adv (0xb0 + chan, 0x20, + ((octave << 2) + (freq >> 8)) & 0xdf); + c->vibcount += c->vibspeed; + } + else if (c->arp_size != 0) + { // no vibrato, just arpeggio + if (arpreg >= 0x800) + tune = c->lasttune - (arpreg ^ 0xff0) - 16; + else + tune = c->lasttune + arpreg; - freq = frequency[tune % (12 * 16)]; - octave = tune / (12 * 16) - 1; - setregs(0xa0 + chan, freq & 0xff); - setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf); - } - } else { // no vibrato, just arpeggio - c->vibwait--; + freq = frequency[tune % (12 * 16)]; + octave = tune / (12 * 16) - 1; + setregs (0xa0 + chan, freq & 0xff); + setregs_adv (0xb0 + chan, 0x20, + ((octave << 2) + (freq >> 8)) & 0xdf); + } + } + else + { // no vibrato, just arpeggio + c->vibwait--; - if(c->arp_size != 0) { - if(arpreg >= 0x800) - tune = c->lasttune - (arpreg ^ 0xff0) - 16; - else - tune = c->lasttune + arpreg; + if (c->arp_size != 0) + { + if (arpreg >= 0x800) + tune = c->lasttune - (arpreg ^ 0xff0) - 16; + else + tune = c->lasttune + arpreg; - freq = frequency[tune % (12 * 16)]; - octave = tune / (12 * 16) - 1; - setregs(0xa0 + chan, freq & 0xff); - setregs_adv(0xb0 + chan, 0x20, ((octave << 2) + (freq >> 8)) & 0xdf); - } + freq = frequency[tune % (12 * 16)]; + octave = tune / (12 * 16) - 1; + setregs (0xa0 + chan, freq & 0xff); + setregs_adv (0xb0 + chan, 0x20, + ((octave << 2) + (freq >> 8)) & 0xdf); + } } } // tremolo (modulator) - if(!c->trmwait) { - if(c->trmrate) { - tremc = tremtab[c->trmcount & 0x7f] * c->trmrate; - if((tremc >> 8) <= (c->volmod & 0x3f)) - level = (c->volmod & 0x3f) - (tremc >> 8); - else - level = 0; + if (!c->trmwait) + { + if (c->trmrate) + { + tremc = tremtab[c->trmcount & 0x7f] * c->trmrate; + if ((tremc >> 8) <= (c->volmod & 0x3f)) + level = (c->volmod & 0x3f) - (tremc >> 8); + else + level = 0; - if(allvolume != 0 && (fmchip[0xc0 + chan] & 1)) - setregs_adv(0x40 + regnum, 0xc0, ((level * allvolume) >> 8) ^ 0x3f); - else - setregs_adv(0x40 + regnum, 0xc0, level ^ 0x3f); + if (allvolume != 0 && (fmchip[0xc0 + chan] & 1)) + setregs_adv (0x40 + regnum, 0xc0, + ((level * allvolume) >> 8) ^ 0x3f); + else + setregs_adv (0x40 + regnum, 0xc0, level ^ 0x3f); - c->trmcount += c->trmspeed; - } else - if(allvolume != 0 && (fmchip[0xc0 + chan] & 1)) - setregs_adv(0x40 + regnum, 0xc0, ((((c->volmod & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); - else - setregs_adv(0x40 + regnum, 0xc0, (c->volmod ^ 0x3f) & 0x3f); - } else { + c->trmcount += c->trmspeed; + } + else if (allvolume != 0 && (fmchip[0xc0 + chan] & 1)) + setregs_adv (0x40 + regnum, 0xc0, + ((((c->volmod & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); + else + setregs_adv (0x40 + regnum, 0xc0, (c->volmod ^ 0x3f) & 0x3f); + } + else + { c->trmwait--; - if(allvolume != 0 && (fmchip[0xc0 + chan] & 1)) - setregs_adv(0x40 + regnum, 0xc0, ((((c->volmod & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); + if (allvolume != 0 && (fmchip[0xc0 + chan] & 1)) + setregs_adv (0x40 + regnum, 0xc0, + ((((c->volmod & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); } // tremolo (carrier) - if(!c->trcwait) { - if(c->trcrate) { - tremc = tremtab[c->trccount & 0x7f] * c->trcrate; - if((tremc >> 8) <= (c->volcar & 0x3f)) - level = (c->volcar & 0x3f) - (tremc >> 8); - else - level = 0; + if (!c->trcwait) + { + if (c->trcrate) + { + tremc = tremtab[c->trccount & 0x7f] * c->trcrate; + if ((tremc >> 8) <= (c->volcar & 0x3f)) + level = (c->volcar & 0x3f) - (tremc >> 8); + else + level = 0; - if(allvolume != 0) - setregs_adv(0x43 + regnum, 0xc0, ((level * allvolume) >> 8) ^ 0x3f); - else - setregs_adv(0x43 + regnum, 0xc0, level ^ 0x3f); - c->trccount += c->trcspeed; - } else - if(allvolume != 0) - setregs_adv(0x43 + regnum, 0xc0, ((((c->volcar & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); - else - setregs_adv(0x43 + regnum, 0xc0, (c->volcar ^ 0x3f) & 0x3f); - } else { + if (allvolume != 0) + setregs_adv (0x43 + regnum, 0xc0, + ((level * allvolume) >> 8) ^ 0x3f); + else + setregs_adv (0x43 + regnum, 0xc0, level ^ 0x3f); + c->trccount += c->trcspeed; + } + else if (allvolume != 0) + setregs_adv (0x43 + regnum, 0xc0, + ((((c->volcar & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); + else + setregs_adv (0x43 + regnum, 0xc0, (c->volcar ^ 0x3f) & 0x3f); + } + else + { c->trcwait--; - if(allvolume != 0) - setregs_adv(0x43 + regnum, 0xc0, ((((c->volcar & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); + if (allvolume != 0) + setregs_adv (0x43 + regnum, 0xc0, + ((((c->volcar & 0x3f) * allvolume) >> 8) ^ 0x3f) & 0x3f); } } return (!playing || songlooped) ? false : true; } -void CldsPlayer::rewind(int subsong) +void +CldsPlayer::rewind (int subsong) { int i; // init all with 0 - tempo_now = 3; playing = true; songlooped = false; + tempo_now = 3; + playing = true; + songlooped = false; jumping = fadeonoff = allvolume = hardfade = pattplay = posplay = jumppos = mainvolume = 0; - memset(channel, 0, sizeof(channel)); - memset(fmchip, 0, sizeof(fmchip)); + memset (channel, 0, sizeof (channel)); + memset (fmchip, 0, sizeof (fmchip)); // OPL2 init - opl->init(); // Reset OPL chip - opl->write(1, 0x20); - opl->write(8, 0); - opl->write(0xbd, regbd); + opl->init (); // Reset OPL chip + opl->write (1, 0x20); + opl->write (8, 0); + opl->write (0xbd, regbd); - for(i = 0; i < 9; i++) { - opl->write(0x20 + op_table[i], 0); - opl->write(0x23 + op_table[i], 0); - opl->write(0x40 + op_table[i], 0x3f); - opl->write(0x43 + op_table[i], 0x3f); - opl->write(0x60 + op_table[i], 0xff); - opl->write(0x63 + op_table[i], 0xff); - opl->write(0x80 + op_table[i], 0xff); - opl->write(0x83 + op_table[i], 0xff); - opl->write(0xe0 + op_table[i], 0); - opl->write(0xe3 + op_table[i], 0); - opl->write(0xa0 + i, 0); - opl->write(0xb0 + i, 0); - opl->write(0xc0 + i, 0); + for (i = 0; i < 9; i++) + { + opl->write (0x20 + op_table[i], 0); + opl->write (0x23 + op_table[i], 0); + opl->write (0x40 + op_table[i], 0x3f); + opl->write (0x43 + op_table[i], 0x3f); + opl->write (0x60 + op_table[i], 0xff); + opl->write (0x63 + op_table[i], 0xff); + opl->write (0x80 + op_table[i], 0xff); + opl->write (0x83 + op_table[i], 0xff); + opl->write (0xe0 + op_table[i], 0); + opl->write (0xe3 + op_table[i], 0); + opl->write (0xa0 + i, 0); + opl->write (0xb0 + i, 0); + opl->write (0xc0 + i, 0); } } /*** private methods *************************************/ -void CldsPlayer::playsound(int inst_number, int channel_number, int tunehigh) +void +CldsPlayer::playsound (int inst_number, int channel_number, int tunehigh) { - Channel *c = &channel[channel_number]; // current channel - SoundBank *i = &soundbank[inst_number]; // current instrument - unsigned int regnum = op_table[channel_number]; // channel's OPL2 register - unsigned char volcalc, octave; - unsigned short freq; + Channel *c = &channel[channel_number]; // current channel + SoundBank *i = &soundbank[inst_number]; // current instrument + unsigned int regnum = op_table[channel_number]; // channel's OPL2 register + unsigned char volcalc, octave; + unsigned short freq; // set fine tune tunehigh += ((i->finetune + c->finetune + 0x80) & 0xff) - 0x80; // arpeggio handling - if(!i->arpeggio) { - unsigned short arpcalc = i->arp_tab[0] << 4; + if (!i->arpeggio) + { + unsigned short arpcalc = i->arp_tab[0] << 4; - if(arpcalc > 0x800) + if (arpcalc > 0x800) tunehigh = tunehigh - (arpcalc ^ 0xff0) - 16; else tunehigh += arpcalc; } // glide handling - if(c->glideto != 0) { + if (c->glideto != 0) + { c->gototune = tunehigh; c->portspeed = c->glideto; c->glideto = c->finetune = 0; @@ -577,69 +689,83 @@ } // set modulator registers - setregs(0x20 + regnum, i->mod_misc); + setregs (0x20 + regnum, i->mod_misc); volcalc = i->mod_vol; - if(!c->nextvol || !(i->feedback & 1)) + if (!c->nextvol || !(i->feedback & 1)) c->volmod = volcalc; else c->volmod = (volcalc & 0xc0) | ((((volcalc & 0x3f) * c->nextvol) >> 6)); - if((i->feedback & 1) == 1 && allvolume != 0) - setregs(0x40 + regnum, ((c->volmod & 0xc0) | (((c->volmod & 0x3f) * allvolume) >> 8)) ^ 0x3f); + if ((i->feedback & 1) == 1 && allvolume != 0) + setregs (0x40 + regnum, + ((c-> + volmod & 0xc0) | (((c->volmod & 0x3f) * + allvolume) >> 8)) ^ 0x3f); else - setregs(0x40 + regnum, c->volmod ^ 0x3f); - setregs(0x60 + regnum, i->mod_ad); - setregs(0x80 + regnum, i->mod_sr); - setregs(0xe0 + regnum, i->mod_wave); + setregs (0x40 + regnum, c->volmod ^ 0x3f); + setregs (0x60 + regnum, i->mod_ad); + setregs (0x80 + regnum, i->mod_sr); + setregs (0xe0 + regnum, i->mod_wave); // Set carrier registers - setregs(0x23 + regnum, i->car_misc); + setregs (0x23 + regnum, i->car_misc); volcalc = i->car_vol; - if(!c->nextvol) + if (!c->nextvol) c->volcar = volcalc; else c->volcar = (volcalc & 0xc0) | ((((volcalc & 0x3f) * c->nextvol) >> 6)); - if(allvolume) - setregs(0x43 + regnum, ((c->volcar & 0xc0) | (((c->volcar & 0x3f) * allvolume) >> 8)) ^ 0x3f); + if (allvolume) + setregs (0x43 + regnum, + ((c-> + volcar & 0xc0) | (((c->volcar & 0x3f) * + allvolume) >> 8)) ^ 0x3f); else - setregs(0x43 + regnum, c->volcar ^ 0x3f); - setregs(0x63 + regnum, i->car_ad); - setregs(0x83 + regnum, i->car_sr); - setregs(0xe3 + regnum, i->car_wave); - setregs(0xc0 + channel_number, i->feedback); - setregs_adv(0xb0 + channel_number, 0xdf, 0); // key off + setregs (0x43 + regnum, c->volcar ^ 0x3f); + setregs (0x63 + regnum, i->car_ad); + setregs (0x83 + regnum, i->car_sr); + setregs (0xe3 + regnum, i->car_wave); + setregs (0xc0 + channel_number, i->feedback); + setregs_adv (0xb0 + channel_number, 0xdf, 0); // key off freq = frequency[tunehigh % (12 * 16)]; octave = tunehigh / (12 * 16) - 1; - if(!i->glide) { - if(!i->portamento || !c->lasttune) { - setregs(0xa0 + channel_number, freq & 0xff); - setregs(0xb0 + channel_number, (octave << 2) + 0x20 + (freq >> 8)); + if (!i->glide) + { + if (!i->portamento || !c->lasttune) + { + setregs (0xa0 + channel_number, freq & 0xff); + setregs (0xb0 + channel_number, (octave << 2) + 0x20 + (freq >> 8)); c->lasttune = c->gototune = tunehigh; - } else { + } + else + { c->gototune = tunehigh; c->portspeed = i->portamento; - setregs_adv(0xb0 + channel_number, 0xdf, 0x20); // key on + setregs_adv (0xb0 + channel_number, 0xdf, 0x20); // key on } - } else { - setregs(0xa0 + channel_number, freq & 0xff); - setregs(0xb0 + channel_number, (octave << 2) + 0x20 + (freq >> 8)); + } + else + { + setregs (0xa0 + channel_number, freq & 0xff); + setregs (0xb0 + channel_number, (octave << 2) + 0x20 + (freq >> 8)); c->lasttune = tunehigh; - c->gototune = tunehigh + ((i->glide + 0x80) & 0xff) - 0x80; // set destination + c->gototune = tunehigh + ((i->glide + 0x80) & 0xff) - 0x80; // set destination c->portspeed = i->portamento; } - if(!i->vibrato) + if (!i->vibrato) c->vibwait = c->vibspeed = c->vibrate = 0; - else { + else + { c->vibwait = i->vibdelay; // PASCAL: c->vibspeed = ((i->vibrato >> 4) & 15) + 1; c->vibspeed = (i->vibrato >> 4) + 2; c->vibrate = (i->vibrato & 15) + 1; } - if(!(c->trmstay & 0xf0)) { + if (!(c->trmstay & 0xf0)) + { c->trmwait = (i->tremwait & 0xf0) >> 3; // PASCAL: c->trmspeed = (i->mod_trem >> 4) & 15; c->trmspeed = i->mod_trem >> 4; @@ -647,7 +773,8 @@ c->trmcount = 0; } - if(!(c->trmstay & 0x0f)) { + if (!(c->trmstay & 0x0f)) + { c->trcwait = (i->tremwait & 15) << 1; // PASCAL: c->trcspeed = (i->car_trem >> 4) & 15; c->trcspeed = i->car_trem >> 4; @@ -657,21 +784,25 @@ c->arp_size = i->arpeggio & 15; c->arp_speed = i->arpeggio >> 4; - memcpy(c->arp_tab, i->arp_tab, 12); + memcpy (c->arp_tab, i->arp_tab, 12); c->keycount = i->keyoff; - c->nextvol = c->glideto = c->finetune = c->vibcount = c->arp_pos = c->arp_count = 0; + c->nextvol = c->glideto = c->finetune = c->vibcount = c->arp_pos = + c->arp_count = 0; } -inline void CldsPlayer::setregs(unsigned char reg, unsigned char val) +inline void +CldsPlayer::setregs (unsigned char reg, unsigned char val) { - if(fmchip[reg] == val) return; + if (fmchip[reg] == val) + return; fmchip[reg] = val; - opl->write(reg, val); + opl->write (reg, val); } -inline void CldsPlayer::setregs_adv(unsigned char reg, unsigned char mask, - unsigned char val) +inline void +CldsPlayer::setregs_adv (unsigned char reg, unsigned char mask, + unsigned char val) { - setregs(reg, (fmchip[reg] & mask) | val); + setregs (reg, (fmchip[reg] & mask) | val); }