Debian Codegraph @

src/engine/platform/zxbeeperquadtone.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 #include "zxbeeperquadtone.h"
21 #include "../engine.h"
22 #include <math.h>
23
24 #define rWrite(a,v) regPool[a]=v
25 #define CHIP_FREQBASE 2048*320
26 #define CHIP_DIVIDER 16*13
27
28 const char** DivPlatformZXBeeperQuadTone::getRegisterSheet() {
29 return NULL;
30 }
31
32 void DivPlatformZXBeeperQuadTone::acquire(short** buf, size_t len) {
33 for (int i=0; i<5; i++) {
34 oscBuf[i]->begin(len);
35 }
36
37 for (size_t h=0; h<len; h++) {
38 bool sampleActive=false;
39 if (curSample>=0 && curSample<parent->song.sampleLen) {
40 sampleActive=!isMuted[4];
41 while (curSamplePeriod>=chan[4].freq) {
42 DivSample* s=parent->getSample(curSample);
43 if (s->samples>0) {
44 chan[4].out=(s->data8[curSamplePos++]>0);
45 if (curSamplePos>=s->samples) curSample=-1;
46 // (theoretical) 32KiB limit
47 if (curSamplePos>=32768*8) curSample=-1;
48 } else {
49 curSample=-1;
50 }
51 curSamplePeriod-=chan[4].freq;
52 }
53 curSamplePeriod+=40;
54 }
55 if (sampleActive) {
56 buf[0][h]=chan[4].out?32767:0;
57 if (outputClock==0) {
58 oscBuf[0]->putSample(h,0);
59 oscBuf[1]->putSample(h,0);
60 oscBuf[2]->putSample(h,0);
61 oscBuf[3]->putSample(h,0);
62 }
63 oscBuf[4]->putSample(h,buf[0][h]);
64 } else {
65 int ch=outputClock/2;
66 int b=ch*4;
67 bool o=false;
68 if ((outputClock&1)==0) {
69 short oscOut;
70 chan[ch].sPosition+=(regPool[1+b]<<8)|regPool[0+b];
71 chan[ch].out=regPool[3+b]+((((chan[ch].sPosition>>8)&0xff)<regPool[2+b])?1:0);
72 if (isMuted[ch] || ((chan[ch].out&0x18)==0)) {
73 oscOut=0;
74 } else if ((chan[ch].out&0x18)==0x18) {
75 oscOut=32767;
76 } else {
77 oscOut=16383;
78 }
79 oscBuf[ch]->putSample(h,oscOut);
80 }
81 if (!isMuted[ch]) o=chan[ch].out&0x10;
82 if (noHiss) {
83 deHisser[outputClock]=o;
84 buf[0][h]=-1;
85 for (int i=0; i<8; i++) {
86 buf[0][h]+=deHisser[i]?4096:0;
87 }
88 } else {
89 buf[0][h]=o?32767:0;
90 }
91 chan[ch].out<<=1;
92 oscBuf[4]->putSample(h,0);
93 }
94 outputClock=(outputClock+1)&7;
95 }
96
97 for (int i=0; i<5; i++) {
98 oscBuf[i]->end(len);
99 }
100 }
101
102 void DivPlatformZXBeeperQuadTone::tick(bool sysTick) {
103 for (int i=0; i<4; i++) {
104 chan[i].std.next();
105 if (chan[i].std.vol.had) {
106 chan[i].outVol=(MIN(chan[i].vol,2)*MIN(chan[i].std.vol.val,2)/2);
107 writeOutVol(i);
108 }
109 if (chan[i].std.duty.had) {
110 chan[i].duty=chan[i].std.duty.val;
111 rWrite(2+i*4,chan[i].duty^0xff);
112 }
113 if (NEW_ARP_STRAT) {
114 chan[i].handleArp();
115 } else if (chan[i].std.arp.had) {
116 if (!chan[i].inPorta) {
117 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
118 }
119 chan[i].freqChanged=true;
120 }
121 if (chan[i].std.pitch.had) {
122 if (chan[i].std.pitch.mode) {
123 chan[i].pitch2+=chan[i].std.pitch.val;
124 CLAMP_VAR(chan[i].pitch2,-65535,65535);
125 } else {
126 chan[i].pitch2=chan[i].std.pitch.val;
127 }
128 chan[i].freqChanged=true;
129 }
130 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
131 if (chan[i].active) {
132 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);
133 if (chan[i].freq<0) chan[i].freq=0;
134 if (chan[i].freq>32768) chan[i].freq=32768;
135 rWrite(0+i*4,chan[i].freq&0xff);
136 rWrite(1+i*4,chan[i].freq>>8);
137 }
138 if (chan[i].keyOn) chan[i].keyOn=false;
139 if (chan[i].keyOff) chan[i].keyOff=false;
140 chan[i].freqChanged=false;
141 }
142 }
143 if (NEW_ARP_STRAT) {
144 chan[4].handleArp();
145 } else if (chan[4].std.arp.had) {
146 if (!chan[4].inPorta) {
147 chan[4].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[4].note,chan[4].std.arp.val));
148 }
149 chan[4].freqChanged=true;
150 }
151 if (chan[4].std.pitch.had) {
152 if (chan[4].std.pitch.mode) {
153 chan[4].pitch2+=chan[4].std.pitch.val;
154 CLAMP_VAR(chan[4].pitch2,-65535,65535);
155 } else {
156 chan[4].pitch2=chan[4].std.pitch.val;
157 }
158 chan[4].freqChanged=true;
159 }
160 if (chan[4].freqChanged || chan[4].keyOn || chan[4].keyOff) {
161 if (chan[4].active) {
162 double off=CHIP_DIVIDER;
163 if (curSample>=0 && curSample<parent->song.sampleLen) {
164 DivSample* s=parent->getSample(curSample);
165 off=(s->centerRate>=1)?(CHIP_DIVIDER*(double)s->centerRate/parent->getCenterRate()):CHIP_DIVIDER;
166 }
167 chan[4].freq=parent->calcFreq(chan[4].baseFreq,chan[4].pitch,chan[4].fixedArp?chan[4].baseNoteOverride:chan[4].arpOff,chan[4].fixedArp,true,2,chan[4].pitch2,chipClock,off);
168 if (chan[4].freq>258) chan[4].freq=258;
169 if (chan[4].freq<3) chan[4].freq=3;
170 rWrite(16,(chan[4].freq-2)&255);
171 chan[4].freq*=13;
172 }
173 if (chan[4].keyOn) chan[4].keyOn=false;
174 if (chan[4].keyOff) chan[4].keyOff=false;
175 chan[4].freqChanged=false;
176 }
177 }
178
179 int DivPlatformZXBeeperQuadTone::dispatch(DivCommand c) {
180 switch (c.cmd) {
181 case DIV_CMD_NOTE_ON: {
182 if (c.chan<4) {
183 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_POKEMINI);
184 if (c.value!=DIV_NOTE_NULL) {
185 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
186 chan[c.chan].freqChanged=true;
187 chan[c.chan].note=c.value;
188 }
189 chan[c.chan].active=true;
190 chan[c.chan].keyOn=true;
191 chan[c.chan].macroInit(ins);
192 chan[c.chan].insChanged=false;
193 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
194 chan[c.chan].outVol=chan[c.chan].vol;
195 writeOutVol(c.chan);
196 }
197 } else {
198 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA);
199 if (c.value!=DIV_NOTE_NULL) {
200 curSample=ins->amiga.getSample(c.value);
201 chan[c.chan].sampleNote=c.value;
202 c.value=ins->amiga.getFreq(c.value);
203 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
204 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
205 chan[c.chan].freqChanged=true;
206 chan[c.chan].note=c.value;
207 // TODO support offset commands
208 curSamplePos=0;
209 curSamplePeriod=0;
210 } else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) {
211 curSample=ins->amiga.getSample(chan[c.chan].sampleNote);
212 c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
213 // TODO support offset commands
214 curSamplePos=0;
215 curSamplePeriod=0;
216 }
217 chan[c.chan].active=true;
218 chan[c.chan].keyOn=true;
219 chan[c.chan].macroInit(ins);
220 chan[c.chan].insChanged=false;
221 }
222 break;
223 }
224 case DIV_CMD_NOTE_OFF:
225 chan[c.chan].active=false;
226 chan[c.chan].keyOff=true;
227 chan[c.chan].macroInit(NULL);
228 writeOutVol(c.chan);
229 if (c.chan>=4) {
230 curSample=-1;
231 }
232 break;
233 case DIV_CMD_NOTE_OFF_ENV:
234 case DIV_CMD_ENV_RELEASE:
235 chan[c.chan].std.release();
236 break;
237 case DIV_CMD_INSTRUMENT:
238 if (chan[c.chan].ins!=c.value || c.value2==1) {
239 chan[c.chan].ins=c.value;
240 chan[c.chan].insChanged=true;
241 }
242 break;
243 case DIV_CMD_VOLUME:
244 if (chan[c.chan].vol!=c.value) {
245 chan[c.chan].vol=c.value;
246 if (!chan[c.chan].std.vol.has) {
247 chan[c.chan].outVol=c.value;
248 writeOutVol(c.chan);
249 }
250 }
251 break;
252 case DIV_CMD_GET_VOLUME:
253 if (chan[c.chan].std.vol.has) {
254 return chan[c.chan].vol;
255 }
256 return chan[c.chan].outVol;
257 break;
258 case DIV_CMD_PITCH:
259 chan[c.chan].pitch=c.value;
260 chan[c.chan].freqChanged=true;
261 break;
262 case DIV_CMD_NOTE_PORTA: {
263 int destFreq=NOTE_FREQUENCY(c.value2+chan[c.chan].sampleNoteDelta);
264 bool return2=false;
265 if (destFreq>chan[c.chan].baseFreq) {
266 chan[c.chan].baseFreq+=c.value;
267 if (chan[c.chan].baseFreq>=destFreq) {
268 chan[c.chan].baseFreq=destFreq;
269 return2=true;
270 }
271 } else {
272 chan[c.chan].baseFreq-=c.value;
273 if (chan[c.chan].baseFreq<=destFreq) {
274 chan[c.chan].baseFreq=destFreq;
275 return2=true;
276 }
277 }
278 chan[c.chan].freqChanged=true;
279 if (return2) {
280 chan[c.chan].inPorta=false;
281 return 2;
282 }
283 break;
284 }
285 case DIV_CMD_STD_NOISE_MODE:
286 chan[c.chan].duty=c.value;
287 if (c.chan<4) rWrite(2+c.chan*4,chan[c.chan].duty^0xff);
288 break;
289 case DIV_CMD_LEGATO:
290 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
291 chan[c.chan].freqChanged=true;
292 chan[c.chan].note=c.value;
293 break;
294 case DIV_CMD_PRE_PORTA:
295 if (chan[c.chan].active && c.value2) {
296 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_POKEMINI));
297 }
298 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);
299 chan[c.chan].inPorta=c.value;
300 break;
301 case DIV_CMD_GET_VOLMAX:
302 return 2;
303 break;
304 case DIV_CMD_MACRO_OFF:
305 chan[c.chan].std.mask(c.value,true);
306 break;
307 case DIV_CMD_MACRO_ON:
308 chan[c.chan].std.mask(c.value,false);
309 break;
310 case DIV_CMD_MACRO_RESTART:
311 chan[c.chan].std.restart(c.value);
312 break;
313 default:
314 break;
315 }
316 return 1;
317 }
318
319 void DivPlatformZXBeeperQuadTone::writeOutVol(int ch) {
320 if (ch>=4) return;
321 unsigned char val=(chan[ch].outVol>=1)?((chan[ch].outVol>=2)?31:7):0;
322 rWrite(3+ch*4,(chan[ch].active)?val:0);
323 }
324
325 void DivPlatformZXBeeperQuadTone::muteChannel(int ch, bool mute) {
326 isMuted[ch]=mute;
327 writeOutVol(ch);
328 }
329
330 void DivPlatformZXBeeperQuadTone::forceIns() {
331 for (int i=0; i<5; i++) {
332 chan[i].insChanged=true;
333 chan[i].freqChanged=true;
334 }
335 }
336
337 void* DivPlatformZXBeeperQuadTone::getChanState(int ch) {
338 return &chan[ch];
339 }
340
341 DivMacroInt* DivPlatformZXBeeperQuadTone::getChanMacroInt(int ch) {
342 return &chan[ch].std;
343 }
344
345 DivDispatchOscBuffer* DivPlatformZXBeeperQuadTone::getOscBuffer(int ch) {
346 return oscBuf[ch];
347 }
348
349 unsigned char* DivPlatformZXBeeperQuadTone::getRegisterPool() {
350 return regPool;
351 }
352
353 int DivPlatformZXBeeperQuadTone::getRegisterPoolSize() {
354 return 17;
355 }
356
357 void DivPlatformZXBeeperQuadTone::reset() {
358 memset(regPool,0,17);
359 memset(deHisser,0,8);
360 for (int i=0; i<5; i++) {
361 chan[i]=DivPlatformZXBeeperQuadTone::Channel();
362 chan[i].std.setEngine(parent);
363 if (i<4) rWrite(2+i*4,128);
364 }
365 cycles=0;
366 curChan=0;
367 sOffTimer=0;
368 curSample=-1;
369 curSamplePos=0;
370 curSamplePeriod=0;
371 outputClock=0;
372 }
373
374 bool DivPlatformZXBeeperQuadTone::keyOffAffectsArp(int ch) {
375 return true;
376 }
377
378 void DivPlatformZXBeeperQuadTone::notifyWaveChange(int wave) {
379 }
380
381 void DivPlatformZXBeeperQuadTone::notifyInsDeletion(void* ins) {
382 for (int i=0; i<5; i++) {
383 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
384 }
385 }
386
387 void DivPlatformZXBeeperQuadTone::setFlags(const DivConfig& flags) {
388 if (flags.getInt("clockSel",0)) {
389 chipClock=COLOR_PAL*4.0/5.0;
390 } else {
391 chipClock=COLOR_NTSC;
392 }
393 CHECK_CUSTOM_CLOCK;
394 rate=chipClock/40;
395 noHiss=flags.getBool("noHiss",false);
396 for (int i=0; i<5; i++) {
397 oscBuf[i]->setRate(rate);
398 }
399 }
400
401 void DivPlatformZXBeeperQuadTone::poke(unsigned int addr, unsigned short val) {
402 rWrite(addr,val);
403 }
404
405 void DivPlatformZXBeeperQuadTone::poke(std::vector<DivRegWrite>& wlist) {
406 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
407 }
408
409 int DivPlatformZXBeeperQuadTone::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
410 parent=p;
411 for (int i=0; i<5; i++) {
412 isMuted[i]=false;
413 oscBuf[i]=new DivDispatchOscBuffer;
414 }
415 setFlags(flags);
416 reset();
417 return 5;
418 }
419
420 void DivPlatformZXBeeperQuadTone::quit() {
421 for (int i=0; i<5; i++) {
422 delete oscBuf[i];
423 }
424 }
425
426 DivPlatformZXBeeperQuadTone::~DivPlatformZXBeeperQuadTone() {
427 }