furnace/src/engine/macroInt.cpp

485 lines
12 KiB
C++
Raw Normal View History

2022-02-15 03:12:20 +00:00
/**
* Furnace Tracker - multi-system chiptune tracker
2023-01-20 00:18:40 +00:00
* Copyright (C) 2021-2023 tildearrow and contributors
2022-02-15 03:12:20 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "macroInt.h"
#include "instrument.h"
#include "engine.h"
2023-03-17 23:16:04 +00:00
#include "../ta-log.h"
2022-10-07 09:11:45 +00:00
#define ADSR_LOW source.val[0]
#define ADSR_HIGH source.val[1]
#define ADSR_AR source.val[2]
#define ADSR_HT source.val[3]
#define ADSR_DR source.val[4]
#define ADSR_SL source.val[5]
#define ADSR_ST source.val[6]
#define ADSR_SR source.val[7]
#define ADSR_RR source.val[8]
2022-10-07 19:17:25 +00:00
#define LFO_SPEED source.val[11]
#define LFO_WAVE source.val[12]
#define LFO_PHASE source.val[13]
#define LFO_LOOP source.val[14]
#define LFO_GLOBAL source.val[15]
void DivMacroStruct::prepare(DivInstrumentMacro& source, DivEngine* e) {
has=had=actualHad=will=true;
mode=source.mode;
2022-10-07 09:11:45 +00:00
type=(source.open>>1)&3;
activeRelease=source.open&8;
linger=(source.macroType==DIV_MACRO_VOL && e->song.volMacroLinger);
2022-10-07 19:17:25 +00:00
lfoPos=LFO_PHASE;
}
2022-04-15 19:38:25 +00:00
void DivMacroStruct::doMacro(DivInstrumentMacro& source, bool released, bool tick) {
if (!tick) {
had=false;
return;
}
2022-12-17 05:09:56 +00:00
if (masked) {
had=false;
2023-03-17 23:16:04 +00:00
has=false;
2022-12-17 05:09:56 +00:00
return;
}
2023-08-25 22:22:04 +00:00
if (released && type==1 && lastPos<3) delay=0;
if (released && type==0 && pos<source.rel && source.rel<source.len && activeRelease) {
delay=0;
pos=source.rel;
}
2022-08-22 08:52:32 +00:00
if (delay>0) {
delay--;
if (!linger) had=false;
2022-08-22 08:52:32 +00:00
return;
}
if (began && source.delay>0) {
delay=source.delay;
} else {
delay=source.speed-1;
}
if (began) {
began=false;
}
2022-04-10 23:07:30 +00:00
if (finished) {
finished=false;
}
2022-04-15 19:38:25 +00:00
if (actualHad!=has) {
2022-04-10 05:01:55 +00:00
finished=true;
}
2022-04-15 19:38:25 +00:00
actualHad=has;
had=actualHad;
2022-10-07 09:11:45 +00:00
2022-04-10 05:01:55 +00:00
if (has) {
2022-10-07 09:11:45 +00:00
if (type==0) { // sequence
lastPos=pos;
val=source.val[pos++];
if (pos>source.rel && !released) {
if (source.loop<source.len && source.loop<source.rel) {
pos=source.loop;
} else {
pos--;
}
}
if (pos>=source.len) {
if (source.loop<source.len && (source.loop>=source.rel || source.rel>=source.len)) {
pos=source.loop;
} else if (linger) {
pos--;
2022-10-07 09:11:45 +00:00
} else {
has=false;
}
2022-04-10 05:01:55 +00:00
}
}
2022-10-07 19:17:25 +00:00
if (type==1) { // ADSR
2022-10-07 09:11:45 +00:00
if (released && lastPos<3) lastPos=3;
switch (lastPos) {
case 0: // attack
pos+=ADSR_AR;
if (pos>255) {
pos=255;
lastPos=1;
delay=ADSR_HT;
}
break;
case 1: // decay
pos-=ADSR_DR;
if (pos<=ADSR_SL) {
pos=ADSR_SL;
lastPos=2;
delay=ADSR_ST;
}
break;
case 2: // sustain
pos-=ADSR_SR;
if (pos<0) {
pos=0;
lastPos=4;
}
break;
case 3: // release
pos-=ADSR_RR;
if (pos<0) {
pos=0;
lastPos=4;
}
break;
case 4: // end
pos=0;
if (!linger) has=false;
2022-10-07 09:11:45 +00:00
break;
2022-04-10 05:01:55 +00:00
}
2022-10-07 09:11:45 +00:00
val=ADSR_LOW+((pos+(ADSR_HIGH-ADSR_LOW)*pos)>>8);
}
2022-10-07 19:17:25 +00:00
if (type==2) { // LFO
lfoPos+=LFO_SPEED;
lfoPos&=1023;
int lfoOut=0;
switch (LFO_WAVE&3) {
case 0: // triangle
lfoOut=((lfoPos&512)?(1023-lfoPos):(lfoPos))>>1;
break;
case 1: // saw
lfoOut=lfoPos>>2;
break;
case 2: // pulse
lfoOut=(lfoPos&512)?255:0;
break;
}
val=ADSR_LOW+((lfoOut+(ADSR_HIGH-ADSR_LOW)*lfoOut)>>8);
2022-04-10 05:01:55 +00:00
}
}
}
void DivMacroInt::next() {
if (ins==NULL) return;
2022-04-10 21:52:03 +00:00
// run macros
// TODO: potentially get rid of list to avoid allocations
2022-04-15 19:38:25 +00:00
subTick--;
for (size_t i=0; i<macroListLen; i++) {
if (macroList[i]!=NULL && macroSource[i]!=NULL) {
macroList[i]->doMacro(*macroSource[i],released,subTick==0);
}
}
if (subTick<=0) {
if (e==NULL) {
subTick=1;
} else {
subTick=e->tickMult;
}
}
}
2022-12-17 05:09:56 +00:00
#define CONSIDER(x,y) \
case y: \
x.masked=enabled; \
break;
#define CONSIDER_OP(oi,o) \
CONSIDER(op[oi].am,0+o) \
CONSIDER(op[oi].ar,1+o) \
CONSIDER(op[oi].dr,2+o) \
CONSIDER(op[oi].mult,3+o) \
CONSIDER(op[oi].rr,4+o) \
CONSIDER(op[oi].sl,5+o) \
CONSIDER(op[oi].tl,6+o) \
CONSIDER(op[oi].dt2,7+o) \
CONSIDER(op[oi].rs,8+o) \
CONSIDER(op[oi].dt,9+o) \
CONSIDER(op[oi].d2r,10+o) \
CONSIDER(op[oi].ssg,11+o) \
CONSIDER(op[oi].dam,12+o) \
CONSIDER(op[oi].dvb,13+o) \
CONSIDER(op[oi].egt,14+o) \
CONSIDER(op[oi].ksl,15+o) \
CONSIDER(op[oi].sus,16+o) \
CONSIDER(op[oi].vib,17+o) \
CONSIDER(op[oi].ws,18+o) \
CONSIDER(op[oi].ksr,19+o)
void DivMacroInt::mask(unsigned char id, bool enabled) {
switch (id) {
CONSIDER(vol,0)
CONSIDER(arp,1)
CONSIDER(duty,2)
CONSIDER(wave,3)
CONSIDER(pitch,4)
CONSIDER(ex1,5)
CONSIDER(ex2,6)
CONSIDER(ex3,7)
CONSIDER(alg,8)
CONSIDER(fb,9)
CONSIDER(fms,10)
CONSIDER(ams,11)
CONSIDER(panL,12)
CONSIDER(panR,13)
CONSIDER(phaseReset,14)
CONSIDER(ex4,15)
CONSIDER(ex5,16)
CONSIDER(ex6,17)
CONSIDER(ex7,18)
CONSIDER(ex8,19)
CONSIDER_OP(0,0x20)
CONSIDER_OP(2,0x40)
CONSIDER_OP(1,0x60)
CONSIDER_OP(3,0x80)
}
}
#undef CONSIDER_OP
#undef CONSIDER
void DivMacroInt::release() {
released=true;
}
void DivMacroInt::setEngine(DivEngine* eng) {
e=eng;
}
2022-04-10 23:07:30 +00:00
#define ADD_MACRO(m,s) \
2023-03-17 23:16:04 +00:00
if (!m.masked) { \
macroList[macroListLen]=&m; \
macroSource[macroListLen++]=&s; \
}
2022-04-10 23:07:30 +00:00
void DivMacroInt::init(DivInstrument* which) {
ins=which;
// initialize
2022-04-10 23:07:30 +00:00
for (size_t i=0; i<macroListLen; i++) {
if (macroList[i]!=NULL) macroList[i]->init();
}
2022-04-10 23:07:30 +00:00
macroListLen=0;
2022-04-15 19:38:25 +00:00
subTick=1;
2022-06-05 21:24:12 +00:00
hasRelease=false;
released=false;
if (ins==NULL) return;
2022-04-10 05:01:55 +00:00
// prepare common macro
if (ins->std.volMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(vol,ins->std.volMacro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.arpMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(arp,ins->std.arpMacro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.dutyMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(duty,ins->std.dutyMacro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.waveMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(wave,ins->std.waveMacro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.pitchMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(pitch,ins->std.pitchMacro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.ex1Macro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(ex1,ins->std.ex1Macro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.ex2Macro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(ex2,ins->std.ex2Macro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.ex3Macro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(ex3,ins->std.ex3Macro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.algMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(alg,ins->std.algMacro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.fbMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(fb,ins->std.fbMacro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.fmsMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(fms,ins->std.fmsMacro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.amsMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(ams,ins->std.amsMacro);
2022-04-10 05:01:55 +00:00
}
2021-05-16 17:47:05 +00:00
2022-04-10 05:01:55 +00:00
if (ins->std.panLMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(panL,ins->std.panLMacro);
}
2022-04-10 05:01:55 +00:00
if (ins->std.panRMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(panR,ins->std.panRMacro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.phaseResetMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(phaseReset,ins->std.phaseResetMacro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.ex4Macro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(ex4,ins->std.ex4Macro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.ex5Macro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(ex5,ins->std.ex5Macro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.ex6Macro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(ex6,ins->std.ex6Macro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.ex7Macro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(ex7,ins->std.ex7Macro);
2022-04-10 05:01:55 +00:00
}
if (ins->std.ex8Macro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(ex8,ins->std.ex8Macro);
2021-05-16 17:47:05 +00:00
}
2022-04-10 05:01:55 +00:00
// prepare FM operator macros
for (int i=0; i<4; i++) {
DivInstrumentSTD::OpMacro& m=ins->std.opMacros[i];
IntOp& o=op[i];
2022-04-10 05:01:55 +00:00
if (m.amMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.am,m.amMacro);
}
2022-04-10 05:01:55 +00:00
if (m.arMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.ar,m.arMacro);
}
2022-04-10 05:01:55 +00:00
if (m.drMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.dr,m.drMacro);
}
2022-04-10 05:01:55 +00:00
if (m.multMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.mult,m.multMacro);
}
2022-04-10 05:01:55 +00:00
if (m.rrMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.rr,m.rrMacro);
}
2022-04-10 05:01:55 +00:00
if (m.slMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.sl,m.slMacro);
}
2022-04-10 05:01:55 +00:00
if (m.tlMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.tl,m.tlMacro);
}
2022-04-10 05:01:55 +00:00
if (m.dt2Macro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.dt2,m.dt2Macro);
}
2022-04-10 05:01:55 +00:00
if (m.rsMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.rs,m.rsMacro);
}
2022-04-10 05:01:55 +00:00
if (m.dtMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.dt,m.dtMacro);
}
2022-04-10 05:01:55 +00:00
if (m.d2rMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.d2r,m.d2rMacro);
}
2022-04-10 05:01:55 +00:00
if (m.ssgMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.ssg,m.ssgMacro);
}
2022-04-10 05:01:55 +00:00
if (m.damMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.dam,m.damMacro);
2022-04-10 05:01:55 +00:00
}
if (m.dvbMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.dvb,m.dvbMacro);
}
2022-04-10 05:01:55 +00:00
if (m.egtMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.egt,m.egtMacro);
}
2022-04-10 05:01:55 +00:00
if (m.kslMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.ksl,m.kslMacro);
}
2022-04-10 05:01:55 +00:00
if (m.susMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.sus,m.susMacro);
}
2022-04-10 05:01:55 +00:00
if (m.vibMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.vib,m.vibMacro);
}
2022-04-10 05:01:55 +00:00
if (m.wsMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.ws,m.wsMacro);
}
2022-04-10 05:01:55 +00:00
if (m.ksrMacro.len>0) {
2022-04-10 23:07:30 +00:00
ADD_MACRO(o.ksr,m.ksrMacro);
}
2022-04-10 05:01:55 +00:00
}
2022-04-10 23:07:30 +00:00
for (size_t i=0; i<macroListLen; i++) {
2022-06-05 21:24:12 +00:00
if (macroSource[i]!=NULL) {
macroList[i]->prepare(*macroSource[i],e);
2022-10-07 21:47:18 +00:00
// check ADSR mode
2023-08-25 01:22:41 +00:00
if ((macroSource[i]->open&6)==2) {
2023-08-24 20:28:04 +00:00
if (macroSource[i]->val[8]>0) {
hasRelease=true;
}
2023-08-25 01:22:41 +00:00
} else if (macroSource[i]->rel<macroSource[i]->len) {
hasRelease=true;
2022-10-07 21:47:18 +00:00
}
2022-06-05 21:24:12 +00:00
}
}
}
void DivMacroInt::notifyInsDeletion(DivInstrument* which) {
if (ins==which) {
init(NULL);
}
}
2023-10-06 04:51:34 +00:00
#define CONSIDER(x,y) case (y&0x1f): return &x; break;
DivMacroStruct* DivMacroInt::structByType(unsigned char type) {
if (type>=0x20) {
unsigned char o=((type>>5)-1)&3;
switch (type&0x1f) {
CONSIDER(op[o].am,DIV_MACRO_OP_AM)
CONSIDER(op[o].ar,DIV_MACRO_OP_AR)
CONSIDER(op[o].dr,DIV_MACRO_OP_DR)
CONSIDER(op[o].mult,DIV_MACRO_OP_MULT)
CONSIDER(op[o].rr,DIV_MACRO_OP_RR)
CONSIDER(op[o].sl,DIV_MACRO_OP_SL)
CONSIDER(op[o].tl,DIV_MACRO_OP_TL)
CONSIDER(op[o].dt2,DIV_MACRO_OP_DT2)
CONSIDER(op[o].rs,DIV_MACRO_OP_RS)
CONSIDER(op[o].dt,DIV_MACRO_OP_DT)
CONSIDER(op[o].d2r,DIV_MACRO_OP_D2R)
CONSIDER(op[o].ssg,DIV_MACRO_OP_SSG)
CONSIDER(op[o].dam,DIV_MACRO_OP_DAM)
CONSIDER(op[o].dvb,DIV_MACRO_OP_DVB)
CONSIDER(op[o].egt,DIV_MACRO_OP_EGT)
CONSIDER(op[o].ksl,DIV_MACRO_OP_KSL)
CONSIDER(op[o].sus,DIV_MACRO_OP_SUS)
CONSIDER(op[o].vib,DIV_MACRO_OP_VIB)
CONSIDER(op[o].ws,DIV_MACRO_OP_WS)
CONSIDER(op[o].ksr,DIV_MACRO_OP_KSR)
}
2023-10-06 04:51:34 +00:00
return NULL;
}
switch (type) {
CONSIDER(vol,DIV_MACRO_VOL)
CONSIDER(arp,DIV_MACRO_ARP)
CONSIDER(duty,DIV_MACRO_DUTY)
CONSIDER(wave,DIV_MACRO_WAVE)
CONSIDER(pitch,DIV_MACRO_PITCH)
CONSIDER(ex1,DIV_MACRO_EX1)
CONSIDER(ex2,DIV_MACRO_EX2)
CONSIDER(ex3,DIV_MACRO_EX3)
CONSIDER(alg,DIV_MACRO_ALG)
CONSIDER(fb,DIV_MACRO_FB)
CONSIDER(fms,DIV_MACRO_FMS)
CONSIDER(ams,DIV_MACRO_AMS)
CONSIDER(panL,DIV_MACRO_PAN_LEFT)
CONSIDER(panR,DIV_MACRO_PAN_RIGHT)
CONSIDER(phaseReset,DIV_MACRO_PHASE_RESET)
CONSIDER(ex4,DIV_MACRO_EX4)
CONSIDER(ex5,DIV_MACRO_EX5)
CONSIDER(ex6,DIV_MACRO_EX6)
CONSIDER(ex7,DIV_MACRO_EX7)
CONSIDER(ex8,DIV_MACRO_EX8)
}
return NULL;
}
2022-12-17 05:09:56 +00:00
#undef CONSIDER