| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program 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 |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "../dispatch.h" |
| 21 |
#include "../../fixedQueue.h" |
| 22 |
#include "../../../extern/ESFMu/esfm.h" |
| 23 |
|
| 24 |
// ESFM register address space technically spans 0x800 (2048) bytes, |
| 25 |
// but we only need the first 0x254 (596) during normal use. |
| 26 |
// Rounding it up to 0x400 bytes, the nearest power of 2. |
| 27 |
#define ESFM_REG_POOL_SIZE 0x400 |
| 28 |
|
| 29 |
class DivPlatformESFM: public DivDispatch { |
| 30 |
struct Channel: public SharedChannel<int> { |
| 31 |
struct { |
| 32 |
DivInstrumentFM fm; |
| 33 |
DivInstrumentESFM esfm; |
| 34 |
} state; |
| 35 |
unsigned char freqL[4], freqH[4]; |
| 36 |
bool hardReset; |
| 37 |
unsigned char globalPan; |
| 38 |
int macroVolMul; |
| 39 |
struct { |
| 40 |
int baseNoteOverride; |
| 41 |
bool fixedArp; |
| 42 |
int arpOff; |
| 43 |
int pitch2; |
| 44 |
bool hasOpArp; |
| 45 |
bool hasOpPitch; |
| 46 |
} opsState[4]; |
| 47 |
|
| 48 |
void handleArpFmOp(int offset=0, int o=0) { |
| 49 |
DivMacroInt::IntOp& m=this->std.op[o]; |
| 50 |
if (m.ssg.had) { |
| 51 |
opsState[o].hasOpArp=true; |
| 52 |
|
| 53 |
if (m.ssg.val<0) { |
| 54 |
if (!(m.ssg.val&0x40000000)) { |
| 55 |
opsState[o].baseNoteOverride=(m.ssg.val|0x40000000)+offset; |
| 56 |
opsState[o].fixedArp=true; |
| 57 |
} else { |
| 58 |
opsState[o].arpOff=m.ssg.val; |
| 59 |
opsState[o].fixedArp=false; |
| 60 |
} |
| 61 |
} else { |
| 62 |
if (m.ssg.val&0x40000000) { |
| 63 |
opsState[o].baseNoteOverride=(m.ssg.val&(~0x40000000))+offset; |
| 64 |
opsState[o].fixedArp=true; |
| 65 |
} else { |
| 66 |
opsState[o].arpOff=m.ssg.val; |
| 67 |
opsState[o].fixedArp=false; |
| 68 |
} |
| 69 |
} |
| 70 |
freqChanged=true; |
| 71 |
} |
| 72 |
|
| 73 |
else |
| 74 |
{ |
| 75 |
opsState[o].hasOpArp=false; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void handlePitchFmOp(int o) |
| 80 |
{ |
| 81 |
DivMacroInt::IntOp& m=this->std.op[o]; |
| 82 |
|
| 83 |
if (m.dt.had) { |
| 84 |
opsState[o].hasOpPitch=true; |
| 85 |
|
| 86 |
if (m.dt.mode) { |
| 87 |
opsState[o].pitch2+=m.dt.val; |
| 88 |
CLAMP_VAR(opsState[o].pitch2,-131071,131071); |
| 89 |
} else { |
| 90 |
opsState[o].pitch2=m.dt.val; |
| 91 |
} |
| 92 |
this->freqChanged=true; |
| 93 |
} |
| 94 |
|
| 95 |
else |
| 96 |
{ |
| 97 |
opsState[o].hasOpPitch=false; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
Channel(): |
| 102 |
SharedChannel<int>(0), |
| 103 |
freqL{0, 0, 0, 0}, |
| 104 |
freqH{0, 0, 0, 0}, |
| 105 |
hardReset(false), |
| 106 |
globalPan(3), |
| 107 |
macroVolMul(64) { |
| 108 |
memset(opsState, 0, sizeof(opsState)); |
| 109 |
} |
| 110 |
}; |
| 111 |
Channel chan[18]; |
| 112 |
DivDispatchOscBuffer* oscBuf[18]; |
| 113 |
bool isMuted[18]; |
| 114 |
struct QueuedWrite { |
| 115 |
unsigned short addr; |
| 116 |
unsigned char val; |
| 117 |
bool addrOrVal; |
| 118 |
QueuedWrite(): addr(0), val(0), addrOrVal(false) {} |
| 119 |
QueuedWrite(unsigned short a, unsigned char v): addr(a), val(v), addrOrVal(false) {} |
| 120 |
}; |
| 121 |
FixedQueue<QueuedWrite,2048> writes; |
| 122 |
esfm_chip chip; |
| 123 |
short oldOut[2]; |
| 124 |
bool isFast; |
| 125 |
|
| 126 |
unsigned char regPool[ESFM_REG_POOL_SIZE]; |
| 127 |
short oldWrites[ESFM_REG_POOL_SIZE]; |
| 128 |
short pendingWrites[ESFM_REG_POOL_SIZE]; |
| 129 |
|
| 130 |
int octave(int freq, int fixedBlock); |
| 131 |
int toFreq(int freq, int fixedBlock); |
| 132 |
void commitState(int ch, DivInstrument* ins); |
| 133 |
|
| 134 |
friend void putDispatchChip(void*,int); |
| 135 |
friend void putDispatchChan(void*,int,int); |
| 136 |
|
| 137 |
inline void rWrite(unsigned short a, short v) { |
| 138 |
if (!skipRegisterWrites && a<ESFM_REG_POOL_SIZE) { |
| 139 |
pendingWrites[a]=v; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
inline void immWrite(unsigned short a, unsigned char v) { |
| 144 |
if (!skipRegisterWrites) { |
| 145 |
writes.push_back(QueuedWrite(a,v)); |
| 146 |
if (dumpWrites) { |
| 147 |
addWrite(a,v); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* ESFM doesn't have predef algorithms, so a custom KVS heuristic for auto mode is needed. |
| 154 |
* This is a bit too complex for a macro. |
| 155 |
* The heuristic for auto mode is expressed as: |
| 156 |
* true for an operator o |
| 157 |
* where op[o].outLvl = 7, |
| 158 |
* or op[o].outLvl > 0 and o == 3 (last operator), |
| 159 |
* or op[o].outLvl > 0 and (op[o].outLvl - op[o + 1].modIn) >= 2, |
| 160 |
* or op[o].outLvl > 0 and op[o + 1].modIn == 0. |
| 161 |
*/ |
| 162 |
inline bool KVS_ES(int c, int o) { |
| 163 |
if (c<0 || c>=18 || o<0 || o>=4) return false; |
| 164 |
|
| 165 |
if (chan[c].state.fm.op[o].kvs==1) return true; |
| 166 |
|
| 167 |
if (chan[c].state.fm.op[o].kvs==2) { |
| 168 |
if (chan[c].state.esfm.op[o].outLvl==7) return true; |
| 169 |
else if (chan[c].state.esfm.op[o].outLvl>0) { |
| 170 |
if (o==3) return true; |
| 171 |
else if ((chan[c].state.esfm.op[o].outLvl-chan[c].state.esfm.op[o+1].modIn)>=2) { |
| 172 |
return true; |
| 173 |
} |
| 174 |
else if (chan[c].state.esfm.op[o+1].modIn==0) { |
| 175 |
return true; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
public: |
| 183 |
void acquire(short** buf, size_t len); |
| 184 |
void acquireDirect(blip_buffer_t** bb, size_t len); |
| 185 |
int dispatch(DivCommand c); |
| 186 |
void* getChanState(int chan); |
| 187 |
DivMacroInt* getChanMacroInt(int ch); |
| 188 |
unsigned short getPan(int ch); |
| 189 |
DivDispatchOscBuffer* getOscBuffer(int chan); |
| 190 |
unsigned char* getRegisterPool(); |
| 191 |
int getRegisterPoolSize(); |
| 192 |
int getOutputCount(); |
| 193 |
void reset(); |
| 194 |
void forceIns(); |
| 195 |
void tick(bool sysTick=true); |
| 196 |
void muteChannel(int ch, bool mute); |
| 197 |
bool keyOffAffectsArp(int ch); |
| 198 |
bool keyOffAffectsPorta(int ch); |
| 199 |
bool hasAcquireDirect(); |
| 200 |
bool getLegacyAlwaysSetVolume(); |
| 201 |
void toggleRegisterDump(bool enable); |
| 202 |
void notifyInsChange(int ins); |
| 203 |
void notifyInsDeletion(void* ins); |
| 204 |
int mapVelocity(int ch, float vel); |
| 205 |
void poke(unsigned int addr, unsigned short val); |
| 206 |
void poke(std::vector<DivRegWrite>& wlist); |
| 207 |
void setFlags(const DivConfig& flags); |
| 208 |
void setFast(bool fast); |
| 209 |
int init(DivEngine* parent, int channels, int sugRate, const DivConfig& flags); |
| 210 |
void quit(); |
| 211 |
~DivPlatformESFM(); |
| 212 |
}; |