From 9a97c38cc6adb2861bddaaa6d85f35f2662f9487 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Sat, 15 May 2021 03:13:21 -0500 Subject: [PATCH] implement more effects and bugfixes enough to play time trax intro near flawless --- src/engine/dispatch.h | 16 ++++++- src/engine/engine.h | 5 ++- src/engine/platform/genesis.cpp | 57 +++++++++++++++++++++--- src/engine/playback.cpp | 77 +++++++++++++++++++++++++++++++-- 4 files changed, 141 insertions(+), 14 deletions(-) diff --git a/src/engine/dispatch.h b/src/engine/dispatch.h index 0b400547..37a48d53 100644 --- a/src/engine/dispatch.h +++ b/src/engine/dispatch.h @@ -9,8 +9,22 @@ enum DivDispatchCmds { DIV_CMD_NOTE_PORTA, DIV_CMD_PITCH, DIV_CMD_PANNING, + DIV_CMD_LEGATO, - DIV_CMD_SAMPLE_MODE + DIV_CMD_SAMPLE_MODE, + + DIV_CMD_FM_TL, + DIV_CMD_FM_AR, + DIV_CMD_FM_FB, + DIV_CMD_FM_MULT, + DIV_CMD_FM_EXTCH, + + DIV_CMD_GENESIS_LFO, + + DIV_CMD_ARCADE_LFO, + + DIV_CMD_STD_NOISE_FREQ, + DIV_CMD_STD_NOISE_MODE }; struct DivCommand { diff --git a/src/engine/engine.h b/src/engine/engine.h index babe2b7f..57b3c845 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -11,7 +11,7 @@ struct DivChannelState { int volume, volSpeed; int vibratoDepth, vibratoRate, vibratoPos; int tremoloDepth, tremoloRate, tremoloPos; - bool doNote; + bool doNote, legato; DivChannelState(): note(-1), @@ -26,7 +26,7 @@ struct DivChannelState { tremoloDepth(0), tremoloRate(0), tremoloPos(0), - doNote(false) {} + doNote(false), legato(false) {} }; class DivEngine { @@ -50,6 +50,7 @@ class DivEngine { void nextRow(); void nextTick(); bool perSystemEffect(int ch, unsigned char effect, unsigned char effectVal); + bool perSystemPostEffect(int ch, unsigned char effect, unsigned char effectVal); void renderSamples(); public: diff --git a/src/engine/platform/genesis.cpp b/src/engine/platform/genesis.cpp index 2821ec15..b0cd80a2 100644 --- a/src/engine/platform/genesis.cpp +++ b/src/engine/platform/genesis.cpp @@ -68,6 +68,9 @@ static unsigned char dtTable[8]={ static int dacRates[6]={ 160,160,116,80,58,40 }; +static int orderedOps[4]={ + 0,2,1,3 +}; void DivPlatformGenesis::tick() { for (int i=0; i<6; i++) { @@ -86,25 +89,25 @@ void DivPlatformGenesis::tick() { for (int i=0; i<6; i++) { if (chan[i].freqChanged) { - if (chan[i].freq>=131072) { + if (chan[i].freq>=82432) { chan[i].freqH=((chan[i].freq>>15)&7)|0x38; chan[i].freqL=(chan[i].freq>>7)&0xff; - } else if (chan[i].freq>=65536) { + } else if (chan[i].freq>=41216) { chan[i].freqH=((chan[i].freq>>14)&7)|0x30; chan[i].freqL=(chan[i].freq>>6)&0xff; - } else if (chan[i].freq>=32768) { + } else if (chan[i].freq>=20608) { chan[i].freqH=((chan[i].freq>>13)&7)|0x28; chan[i].freqL=(chan[i].freq>>5)&0xff; - } else if (chan[i].freq>=16384) { + } else if (chan[i].freq>=10304) { chan[i].freqH=((chan[i].freq>>12)&7)|0x20; chan[i].freqL=(chan[i].freq>>4)&0xff; - } else if (chan[i].freq>=8192) { + } else if (chan[i].freq>=5152) { chan[i].freqH=((chan[i].freq>>11)&7)|0x18; chan[i].freqL=(chan[i].freq>>3)&0xff; - } else if (chan[i].freq>=4096) { + } else if (chan[i].freq>=2576) { chan[i].freqH=((chan[i].freq>>10)&7)|0x10; chan[i].freqL=(chan[i].freq>>2)&0xff; - } else if (chan[i].freq>=2048) { + } else if (chan[i].freq>=1288) { chan[i].freqH=((chan[i].freq>>9)&7)|0x08; chan[i].freqL=(chan[i].freq>>1)&0xff; } else { @@ -241,7 +244,47 @@ int DivPlatformGenesis::dispatch(DivCommand c) { } break; } + case DIV_CMD_LEGATO: { + chan[c.chan].baseFreq=644.0f*pow(2.0f,((float)c.value/12.0f)); + chan[c.chan].freq=(chan[c.chan].baseFreq*(2048+chan[c.chan].pitch))>>11; + chan[c.chan].freqChanged=true; + break; + } + case DIV_CMD_FM_MULT: { + unsigned short baseAddr=chanOffs[c.chan]|opOffs[c.value]; + DivInstrument* ins=parent->song.ins[chan[c.chan].ins]; + DivInstrumentFM::Operator op=ins->fm.op[c.value]; + rWrite(baseAddr+0x30,(c.value2&15)|(dtTable[op.dt&7]<<4)); + break; + } + case DIV_CMD_FM_TL: { + unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; + DivInstrument* ins=parent->song.ins[chan[c.chan].ins]; + if (isOutput[ins->fm.alg][c.value]) { + rWrite(baseAddr+0x40,127-(((127-c.value2)*chan[c.chan].vol)/127)); + } else { + rWrite(baseAddr+0x40,c.value2); + } + break; + } + case DIV_CMD_FM_AR: { + DivInstrument* ins=parent->song.ins[chan[c.chan].ins]; + if (c.value<0) { + for (int i=0; i<4; i++) { + DivInstrumentFM::Operator op=ins->fm.op[i]; + unsigned short baseAddr=chanOffs[c.chan]|opOffs[i]; + rWrite(baseAddr+0x50,(c.value2&31)|(op.rs<<6)); + } + } else { + DivInstrumentFM::Operator op=ins->fm.op[orderedOps[c.value]]; + unsigned short baseAddr=chanOffs[c.chan]|opOffs[orderedOps[c.value]]; + rWrite(baseAddr+0x50,(c.value2&31)|(op.rs<<6)); + } + + break; + } default: + printf("WARNING: unimplemented command %d\n",c.cmd); break; } return 1; diff --git a/src/engine/playback.cpp b/src/engine/playback.cpp index 1330c349..2e3fa62e 100644 --- a/src/engine/playback.cpp +++ b/src/engine/playback.cpp @@ -39,6 +39,58 @@ bool DivEngine::perSystemEffect(int ch, unsigned char effect, unsigned char effe return true; } +bool DivEngine::perSystemPostEffect(int ch, unsigned char effect, unsigned char effectVal) { + switch (song.system) { + case DIV_SYSTEM_GENESIS: + switch (effect) { + case 0x11: // FB + dispatch->dispatch(DivCommand(DIV_CMD_FM_FB,ch,effectVal&7)); + break; + case 0x12: // TL op1 + dispatch->dispatch(DivCommand(DIV_CMD_FM_TL,ch,0,effectVal&0x7f)); + break; + case 0x13: // TL op2 + dispatch->dispatch(DivCommand(DIV_CMD_FM_TL,ch,1,effectVal&0x7f)); + break; + case 0x14: // TL op3 + dispatch->dispatch(DivCommand(DIV_CMD_FM_TL,ch,2,effectVal&0x7f)); + break; + case 0x15: // TL op4 + dispatch->dispatch(DivCommand(DIV_CMD_FM_TL,ch,3,effectVal&0x7f)); + break; + case 0x16: // MULT + if ((effectVal>>4)>0 && (effectVal>>4)<5) { + dispatch->dispatch(DivCommand(DIV_CMD_FM_MULT,ch,(effectVal>>4)-1,effectVal&15)); + } + break; + case 0x18: // EXT + dispatch->dispatch(DivCommand(DIV_CMD_FM_EXTCH,ch,effectVal)); + break; + case 0x19: // AR global + dispatch->dispatch(DivCommand(DIV_CMD_FM_AR,ch,-1,effectVal&31)); + break; + case 0x1a: // AR op1 + dispatch->dispatch(DivCommand(DIV_CMD_FM_AR,ch,0,effectVal&31)); + break; + case 0x1b: // AR op2 + dispatch->dispatch(DivCommand(DIV_CMD_FM_AR,ch,1,effectVal&31)); + break; + case 0x1c: // AR op3 + dispatch->dispatch(DivCommand(DIV_CMD_FM_AR,ch,2,effectVal&31)); + break; + case 0x1d: // AR op4 + dispatch->dispatch(DivCommand(DIV_CMD_FM_AR,ch,3,effectVal&31)); + break; + default: + return false; + } + break; + default: + return false; + } + return true; +} + void DivEngine::nextRow() { static char pb[4096]; static char pb1[4096]; @@ -173,7 +225,7 @@ void DivEngine::nextRow() { case 0x04: // vibrato chan[i].vibratoDepth=effectVal&15; chan[i].vibratoRate=effectVal>>4; - dispatch->dispatch(DivCommand(DIV_CMD_PITCH,i,(chan[i].vibratoDepth*vibTable[chan[i].vibratoPos])>>4)); + dispatch->dispatch(DivCommand(DIV_CMD_PITCH,i,chan[i].pitch+((chan[i].vibratoDepth*vibTable[chan[i].vibratoPos])>>4))); break; case 0x0a: // volume ramp if (effectVal!=0) { @@ -197,16 +249,33 @@ void DivEngine::nextRow() { break; case 0xe5: // pitch chan[i].pitch=effectVal-0x80; + dispatch->dispatch(DivCommand(DIV_CMD_PITCH,i,chan[i].pitch+((chan[i].vibratoDepth*vibTable[chan[i].vibratoPos])>>2))); + break; + case 0xea: // legato mode + chan[i].legato=effectVal; break; } } if (chan[i].doNote) { chan[i].vibratoPos=0; - dispatch->dispatch(DivCommand(DIV_CMD_PITCH,i,(chan[i].vibratoDepth*vibTable[chan[i].vibratoPos])>>2)); - dispatch->dispatch(DivCommand(DIV_CMD_NOTE_ON,i,chan[i].note)); + dispatch->dispatch(DivCommand(DIV_CMD_PITCH,i,chan[i].pitch+((chan[i].vibratoDepth*vibTable[chan[i].vibratoPos])>>4))); + if (chan[i].legato) { + dispatch->dispatch(DivCommand(DIV_CMD_LEGATO,i,chan[i].note)); + } else { + dispatch->dispatch(DivCommand(DIV_CMD_NOTE_ON,i,chan[i].note)); + } chan[i].doNote=false; } + + // post effects + for (int j=0; jeffectRows; j++) { + short effect=pat->data[curRow][4+(j<<1)]; + short effectVal=pat->data[curRow][5+(j<<1)]; + + if (effectVal==-1) effectVal=0; + perSystemPostEffect(i,effect,effectVal); + } } } @@ -240,7 +309,7 @@ void DivEngine::nextTick() { if (chan[i].vibratoDepth>0) { chan[i].vibratoPos+=chan[i].vibratoRate; if (chan[i].vibratoPos>=60) chan[i].vibratoPos-=60; - dispatch->dispatch(DivCommand(DIV_CMD_PITCH,i,(chan[i].vibratoDepth*vibTable[chan[i].vibratoPos])>>4)); + dispatch->dispatch(DivCommand(DIV_CMD_PITCH,i,chan[i].pitch+((chan[i].vibratoDepth*vibTable[chan[i].vibratoPos])>>4))); } if (chan[i].portaSpeed>0) { if (dispatch->dispatch(DivCommand(DIV_CMD_NOTE_PORTA,i,chan[i].portaSpeed,chan[i].portaNote))==2) {