Debian Codegraph @

src/engine/platform/bifurcator.cpp

raw
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 #define _USE_MATH_DEFINES
21 #include "bifurcator.h"
22 #include "../engine.h"
23 #include "../filter.h"
24 #include <math.h>
25
26 #define CHIP_FREQBASE 65536
27
28 #define rWrite(a,v) {if(!skipRegisterWrites) {regPool[a]=v; if(dumpWrites) addWrite(a,v); }}
29
30 const char* regCheatSheetBifurcator[]={
31 "CHx_State", "x*8+0",
32 "CHx_Param", "x*8+2",
33 "CHx_Freq", "x*8+4",
34 "CHx_LVol", "x*8+6",
35 "CHx_RVol", "x*8+7",
36 NULL
37 };
38
39 const char** DivPlatformBifurcator::getRegisterSheet() {
40 return regCheatSheetBifurcator;
41 }
42
43 void DivPlatformBifurcator::acquire(short** buf, size_t len) {
44 for (int i=0; i<4; i++) {
45 chan[i].curx=regPool[i*8]|(regPool[i*8+1]<<8);
46 chan[i].param=regPool[i*8+2]|(regPool[i*8+3]<<8);
47 chan[i].freq=regPool[i*8+4]|(regPool[i*8+5]<<8);
48 chan[i].chVolL=regPool[i*8+6];
49 chan[i].chVolR=regPool[i*8+7];
50 }
51 for (int i=0; i<4; i++) {
52 oscBuf[i]->begin(len);
53 }
54 for (size_t h=0; h<len; h++) {
55 int l=0;
56 int r=0;
57 for (int i=0; i<4; i++) {
58 chan[i].audSub+=chan[i].freq;
59 if (chan[i].audSub>=65536) {
60 int64_t newx=(int64_t)chan[i].curx*(chan[i].param+65536)/32768;
61 newx*=65536-chan[i].curx;
62 chan[i].curx=(int)(newx/65536);
63 chan[i].audSub&=65535;
64 }
65 int out=chan[i].curx-32768;
66 int outL=out*chan[i].chVolL/256;
67 int outR=out*chan[i].chVolR/256;
68 oscBuf[i]->putSample(h,(short)((outL+outR)/2));
69 l+=outL/4;
70 r+=outR/4;
71 }
72 buf[0][h]=(short)l;
73 buf[1][h]=(short)r;
74 }
75 for (int i=0; i<4; i++) {
76 oscBuf[i]->end(len);
77 }
78 for (int i=0; i<4; i++) {
79 regPool[i*8]=chan[i].curx&0xff;
80 regPool[i*8+1]=chan[i].curx>>8;
81 }
82 }
83
84 void DivPlatformBifurcator::tick(bool sysTick) {
85 for (int i=0; i<4; i++) {
86 chan[i].std.next();
87 if (chan[i].std.vol.had) {
88 chan[i].outVol=(chan[i].vol*MIN(chan[i].std.vol.val,255))/255;
89 chan[i].volChangedL=true;
90 chan[i].volChangedR=true;
91 }
92 if (NEW_ARP_STRAT) {
93 chan[i].handleArp();
94 } else if (chan[i].std.arp.had) {
95 if (!chan[i].inPorta) {
96 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
97 }
98 chan[i].freqChanged=true;
99 }
100 if (chan[i].std.duty.had) {
101 rWrite(i*8+2,chan[i].std.duty.val&0xff);
102 rWrite(i*8+3,chan[i].std.duty.val>>8);
103 }
104 if (chan[i].std.pitch.had) {
105 if (chan[i].std.pitch.mode) {
106 chan[i].pitch2+=chan[i].std.pitch.val;
107 CLAMP_VAR(chan[i].pitch2,-32768,32767);
108 } else {
109 chan[i].pitch2=chan[i].std.pitch.val;
110 }
111 chan[i].freqChanged=true;
112 }
113 if (chan[i].std.panL.had) {
114 chan[i].chPanL=(255*(chan[i].std.panL.val&255))/255;
115 chan[i].volChangedL=true;
116 }
117
118 if (chan[i].std.panR.had) {
119 chan[i].chPanR=(255*(chan[i].std.panR.val&255))/255;
120 chan[i].volChangedR=true;
121 }
122 if (chan[i].std.phaseReset.had) {
123 if ((chan[i].std.phaseReset.val==1) && chan[i].active) {
124 rWrite(i*8,1);
125 rWrite(i*8+1,0);
126 }
127 }
128 if (chan[i].std.ex1.had) {
129 rWrite(i*8,chan[i].std.ex1.val&0xff);
130 rWrite(i*8+1,chan[i].std.ex1.val>>8);
131 }
132 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
133 chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE);
134 if (chan[i].freq>65535) chan[i].freq=65535;
135 rWrite(i*8+4,chan[i].freq&0xff);
136 rWrite(i*8+5,chan[i].freq>>8);
137 if (chan[i].keyOn) {
138 if (!chan[i].std.vol.had) {
139 chan[i].outVol=chan[i].vol;
140 }
141 chan[i].volChangedL=true;
142 chan[i].volChangedR=true;
143 chan[i].keyOn=false;
144 }
145 if (chan[i].keyOff) {
146 chan[i].volChangedL=true;
147 chan[i].volChangedR=true;
148 chan[i].keyOff=false;
149 }
150 if (chan[i].freqChanged) {
151 chan[i].freqChanged=false;
152 }
153 }
154 if (chan[i].volChangedL) {
155 int vol=(isMuted[i] || !chan[i].active)?0:(chan[i].outVol*chan[i].chPanL/255);
156 rWrite(i*8+6,vol);
157 chan[i].volChangedL=false;
158 }
159 if (chan[i].volChangedR) {
160 int vol=(isMuted[i] || !chan[i].active)?0:(chan[i].outVol*chan[i].chPanR/255);
161 rWrite(i*8+7,vol);
162 chan[i].volChangedR=false;
163 }
164 }
165 }
166
167 int DivPlatformBifurcator::dispatch(DivCommand c) {
168 switch (c.cmd) {
169 case DIV_CMD_NOTE_ON: {
170 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_BIFURCATOR);
171 if (c.value!=DIV_NOTE_NULL) {
172 chan[c.chan].baseFreq=round(NOTE_FREQUENCY(c.value));
173 chan[c.chan].freqChanged=true;
174 chan[c.chan].note=c.value;
175 }
176 chan[c.chan].active=true;
177 chan[c.chan].keyOn=true;
178 chan[c.chan].macroInit(ins);
179 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
180 chan[c.chan].outVol=chan[c.chan].vol;
181 }
182 break;
183 }
184 case DIV_CMD_NOTE_OFF:
185 chan[c.chan].active=false;
186 chan[c.chan].keyOff=true;
187 chan[c.chan].macroInit(NULL);
188 break;
189 case DIV_CMD_NOTE_OFF_ENV:
190 case DIV_CMD_ENV_RELEASE:
191 chan[c.chan].std.release();
192 break;
193 case DIV_CMD_INSTRUMENT:
194 if (chan[c.chan].ins!=c.value || c.value2==1) {
195 chan[c.chan].ins=c.value;
196 }
197 break;
198 case DIV_CMD_VOLUME:
199 chan[c.chan].vol=c.value;
200 if (!chan[c.chan].std.vol.has) {
201 chan[c.chan].outVol=c.value;
202 }
203 chan[c.chan].volChangedL=true;
204 chan[c.chan].volChangedR=true;
205 break;
206 case DIV_CMD_GET_VOLUME:
207 if (chan[c.chan].std.vol.has) {
208 return chan[c.chan].vol;
209 }
210 return chan[c.chan].outVol;
211 break;
212 case DIV_CMD_PANNING:
213 chan[c.chan].chPanL=c.value;
214 chan[c.chan].chPanR=c.value2;
215 chan[c.chan].volChangedL=true;
216 chan[c.chan].volChangedR=true;
217 break;
218 case DIV_CMD_PITCH:
219 chan[c.chan].pitch=c.value;
220 chan[c.chan].freqChanged=true;
221 break;
222 case DIV_CMD_NOTE_PORTA: {
223 int destFreq=NOTE_FREQUENCY(c.value2);
224 bool return2=false;
225 if (destFreq>chan[c.chan].baseFreq) {
226 chan[c.chan].baseFreq+=c.value;
227 if (chan[c.chan].baseFreq>=destFreq) {
228 chan[c.chan].baseFreq=destFreq;
229 return2=true;
230 }
231 } else {
232 chan[c.chan].baseFreq-=c.value;
233 if (chan[c.chan].baseFreq<=destFreq) {
234 chan[c.chan].baseFreq=destFreq;
235 return2=true;
236 }
237 }
238 chan[c.chan].freqChanged=true;
239 if (return2) {
240 chan[c.chan].inPorta=false;
241 return 2;
242 }
243 break;
244 }
245 case DIV_CMD_LEGATO: {
246 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0)));
247 chan[c.chan].freqChanged=true;
248 chan[c.chan].note=c.value;
249 break;
250 }
251 case DIV_CMD_PRE_PORTA:
252 if (chan[c.chan].active && c.value2) {
253 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA));
254 }
255 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_FREQUENCY(chan[c.chan].note);
256 chan[c.chan].inPorta=c.value;
257 break;
258 case DIV_CMD_BIFURCATOR_STATE_LOAD:
259 rWrite(c.chan*8+c.value,c.value2);
260 break;
261 case DIV_CMD_BIFURCATOR_PARAMETER:
262 rWrite(c.chan*8+2+c.value,c.value2);
263 break;
264 case DIV_CMD_GET_VOLMAX:
265 return 255;
266 break;
267 case DIV_CMD_MACRO_OFF:
268 chan[c.chan].std.mask(c.value,true);
269 break;
270 case DIV_CMD_MACRO_ON:
271 chan[c.chan].std.mask(c.value,false);
272 break;
273 case DIV_CMD_MACRO_RESTART:
274 chan[c.chan].std.restart(c.value);
275 break;
276 default:
277 break;
278 }
279 return 1;
280 }
281
282 void DivPlatformBifurcator::muteChannel(int ch, bool mute) {
283 isMuted[ch]=mute;
284 chan[ch].volChangedL=true;
285 chan[ch].volChangedR=true;
286 }
287
288 void DivPlatformBifurcator::forceIns() {
289 for (int i=0; i<4; i++) {
290 chan[i].insChanged=true;
291 chan[i].volChangedL=true;
292 chan[i].volChangedR=true;
293 chan[i].active=false;
294 }
295 }
296
297 void* DivPlatformBifurcator::getChanState(int ch) {
298 return &chan[ch];
299 }
300
301 DivDispatchOscBuffer* DivPlatformBifurcator::getOscBuffer(int ch) {
302 return oscBuf[ch];
303 }
304
305 unsigned char* DivPlatformBifurcator::getRegisterPool() {
306 return (unsigned char*)regPool;
307 }
308
309 int DivPlatformBifurcator::getRegisterPoolSize() {
310 return 8*4;
311 }
312
313 void DivPlatformBifurcator::reset() {
314 memset(regPool,0,8*4);
315 for (int i=0; i<4; i++) {
316 chan[i]=DivPlatformBifurcator::Channel();
317 chan[i].std.setEngine(parent);
318 rWrite(i*8,chan[i].curx&0xff);
319 rWrite(i*8+1,chan[i].curx>>8);
320 rWrite(i*8+2,chan[i].param&0xff);
321 rWrite(i*8+3,chan[i].param>>8);
322 }
323 }
324
325 int DivPlatformBifurcator::getOutputCount() {
326 return 2;
327 }
328
329 DivMacroInt* DivPlatformBifurcator::getChanMacroInt(int ch) {
330 return &chan[ch].std;
331 }
332
333 unsigned short DivPlatformBifurcator::getPan(int ch) {
334 return (chan[ch].chPanL<<8)|(chan[ch].chPanR);
335 }
336
337 void DivPlatformBifurcator::notifyInsChange(int ins) {
338 for (int i=0; i<4; i++) {
339 if (chan[i].ins==ins) {
340 chan[i].insChanged=true;
341 }
342 }
343 }
344
345 void DivPlatformBifurcator::notifyInsDeletion(void* ins) {
346 for (int i=0; i<4; i++) {
347 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
348 }
349 }
350
351 void DivPlatformBifurcator::setFlags(const DivConfig& flags) {
352 chipClock=1000000;
353 CHECK_CUSTOM_CLOCK;
354 rate=chipClock/16;
355 for (int i=0; i<4; i++) {
356 oscBuf[i]->setRate(rate);
357 }
358 }
359
360 void DivPlatformBifurcator::poke(unsigned int addr, unsigned short val) {
361 rWrite(addr,val);
362 }
363
364 void DivPlatformBifurcator::poke(std::vector<DivRegWrite>& wlist) {
365 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
366 }
367
368 int DivPlatformBifurcator::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
369 parent=p;
370 dumpWrites=false;
371 skipRegisterWrites=false;
372 for (int i=0; i<4; i++) {
373 isMuted[i]=false;
374 oscBuf[i]=new DivDispatchOscBuffer;
375 }
376 setFlags(flags);
377 reset();
378 return 4;
379 }
380
381 void DivPlatformBifurcator::quit() {
382 for (int i=0; i<4; i++) {
383 delete oscBuf[i];
384 }
385 }