Debian Codegraph @

src/engine/platform/pcmdac.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 "pcmdac.h"
22 #include "../engine.h"
23 #include "../filter.h"
24 #include <math.h>
25
26 // to ease the driver, freqency register is a 8.16 counter relative to output sample rate
27 #define CHIP_FREQBASE 65536
28
29 void DivPlatformPCMDAC::acquire(short** buf, size_t len) {
30 const int depthScale=(15-outDepth);
31 int output=0;
32
33 oscBuf->begin(len);
34
35 for (size_t h=0; h<len; h++) {
36 if (!chan[0].active) {
37 buf[0][h]=0;
38 buf[1][h]=0;
39 oscBuf->putSample(h,0);
40 continue;
41 }
42 if (chan[0].useWave || (chan[0].sample>=0 && chan[0].sample<parent->song.sampleLen)) {
43 chan[0].audSub+=chan[0].freq;
44 if (chan[0].useWave) {
45 while (chan[0].audSub>=0x10000) {
46 chan[0].audSub-=0x10000;
47 chan[0].audPos+=((!chan[0].useWave) && chan[0].audDir)?-1:1;
48 if (chan[0].audPos>=(int)chan[0].audLen) {
49 chan[0].audPos%=chan[0].audLen;
50 chan[0].audDir=false;
51 }
52 chan[0].audDat[0]=chan[0].audDat[1];
53 chan[0].audDat[1]=chan[0].audDat[2];
54 chan[0].audDat[2]=chan[0].audDat[3];
55 chan[0].audDat[3]=chan[0].audDat[4];
56 chan[0].audDat[4]=chan[0].audDat[5];
57 chan[0].audDat[5]=chan[0].audDat[6];
58 chan[0].audDat[6]=chan[0].audDat[7];
59 chan[0].audDat[7]=(chan[0].ws.output[chan[0].audPos]-0x80)<<8;
60 }
61
62 const short s0=chan[0].audDat[0];
63 const short s1=chan[0].audDat[1];
64 const short s2=chan[0].audDat[2];
65 const short s3=chan[0].audDat[3];
66 const short s4=chan[0].audDat[4];
67 const short s5=chan[0].audDat[5];
68 const short s6=chan[0].audDat[6];
69 const short s7=chan[0].audDat[7];
70
71 switch (interp) {
72 case 1: // linear
73 output=s6+(((int)((int)s7-(int)s6)*((chan[0].audSub>>1)&0x7fff))>>15);
74 break;
75 case 2: { // cubic
76 float* cubicTable=DivFilterTables::getCubicTable();
77 float* t=&cubicTable[((chan[0].audSub&0xffff)>>6)<<2];
78 float result=(float)s4*t[0]+(float)s5*t[1]+(float)s6*t[2]+(float)s7*t[3];
79 if (result<-32768) result=-32768;
80 if (result>32767) result=32767;
81 output=result;
82 break;
83 }
84 case 3: { // sinc
85 float* sincTable=DivFilterTables::getSincTable8();
86 float* t1=&sincTable[(8191-((chan[0].audSub&0xffff)>>3))<<2];
87 float* t2=&sincTable[((chan[0].audSub&0xffff)>>3)<<2];
88 float result=(
89 s0*t2[3]+
90 s1*t2[2]+
91 s2*t2[1]+
92 s3*t2[0]+
93 s4*t1[0]+
94 s5*t1[1]+
95 s6*t1[2]+
96 s7*t1[3]
97 );
98 if (result<-32768) result=-32768;
99 if (result>32767) result=32767;
100 output=result;
101 break;
102 }
103 default: // none
104 output=s7;
105 break;
106 }
107 } else {
108 DivSample* s=parent->getSample(chan[0].sample);
109 if (s->samples>0) {
110 while (chan[0].audSub>=0x10000) {
111 chan[0].audSub-=0x10000;
112 chan[0].audPos+=((!chan[0].useWave) && chan[0].audDir)?-1:1;
113 if (chan[0].audDir) {
114 if (s->isLoopable()) {
115 switch (s->loopMode) {
116 case DIV_SAMPLE_LOOP_FORWARD:
117 case DIV_SAMPLE_LOOP_PINGPONG:
118 if (chan[0].audPos<s->loopStart) {
119 chan[0].audPos=s->loopStart+(s->loopStart-chan[0].audPos);
120 chan[0].audDir=false;
121 }
122 break;
123 case DIV_SAMPLE_LOOP_BACKWARD:
124 if (chan[0].audPos<s->loopStart) {
125 chan[0].audPos=s->loopEnd-1-(s->loopStart-chan[0].audPos);
126 chan[0].audDir=true;
127 }
128 break;
129 default:
130 if (chan[0].audPos<0) {
131 chan[0].sample=-1;
132 }
133 break;
134 }
135 } else if (chan[0].audPos>=(int)s->samples) {
136 chan[0].sample=-1;
137 }
138 } else {
139 if (s->isLoopable()) {
140 switch (s->loopMode) {
141 case DIV_SAMPLE_LOOP_FORWARD:
142 if (chan[0].audPos>=s->loopEnd) {
143 chan[0].audPos=(chan[0].audPos+s->loopStart)-s->loopEnd;
144 chan[0].audDir=false;
145 }
146 break;
147 case DIV_SAMPLE_LOOP_BACKWARD:
148 case DIV_SAMPLE_LOOP_PINGPONG:
149 if (chan[0].audPos>=s->loopEnd) {
150 chan[0].audPos=s->loopEnd-1-(s->loopEnd-1-chan[0].audPos);
151 chan[0].audDir=true;
152 }
153 break;
154 default:
155 if (chan[0].audPos>=(int)s->samples) {
156 chan[0].sample=-1;
157 }
158 break;
159 }
160 } else if (chan[0].audPos>=(int)s->samples) {
161 chan[0].sample=-1;
162 }
163 }
164 chan[0].audDat[0]=chan[0].audDat[1];
165 chan[0].audDat[1]=chan[0].audDat[2];
166 chan[0].audDat[2]=chan[0].audDat[3];
167 chan[0].audDat[3]=chan[0].audDat[4];
168 chan[0].audDat[4]=chan[0].audDat[5];
169 chan[0].audDat[5]=chan[0].audDat[6];
170 chan[0].audDat[6]=chan[0].audDat[7];
171 if (chan[0].audPos>=0 && chan[0].audPos<(int)s->samples) {
172 chan[0].audDat[7]=s->data16[chan[0].audPos];
173 } else {
174 chan[0].audDat[7]=0;
175 }
176 }
177 } else {
178 chan[0].sample=-1;
179 chan[0].audSub=0;
180 chan[0].audPos=0;
181 }
182
183 const short s0=chan[0].audDat[0];
184 const short s1=chan[0].audDat[1];
185 const short s2=chan[0].audDat[2];
186 const short s3=chan[0].audDat[3];
187 const short s4=chan[0].audDat[4];
188 const short s5=chan[0].audDat[5];
189 const short s6=chan[0].audDat[6];
190 const short s7=chan[0].audDat[7];
191
192 switch (interp) {
193 case 1: // linear
194 output=s6+(((int)((int)s7-(int)s6)*((chan[0].audSub>>1)&0x7fff))>>15);
195 break;
196 case 2: { // cubic
197 float* cubicTable=DivFilterTables::getCubicTable();
198 float* t=&cubicTable[((chan[0].audSub&0xffff)>>6)<<2];
199 float result=(float)s4*t[0]+(float)s5*t[1]+(float)s6*t[2]+(float)s7*t[3];
200 if (result<-32768) result=-32768;
201 if (result>32767) result=32767;
202 output=result;
203 break;
204 }
205 case 3: { // sinc
206 float* sincTable=DivFilterTables::getSincTable8();
207 float* t1=&sincTable[(8191-((chan[0].audSub&0xffff)>>3))<<2];
208 float* t2=&sincTable[((chan[0].audSub&0xffff)>>3)<<2];
209 float result=(
210 s0*t2[3]+
211 s1*t2[2]+
212 s2*t2[1]+
213 s3*t2[0]+
214 s4*t1[0]+
215 s5*t1[1]+
216 s6*t1[2]+
217 s7*t1[3]
218 );
219 if (result<-32768) result=-32768;
220 if (result>32767) result=32767;
221 output=result;
222 break;
223 }
224 default: // none
225 output=s7;
226 break;
227 }
228 }
229 }
230 if (isMuted) {
231 output=0;
232 } else {
233 output=((output*MIN(volMax,chan[0].vol)*MIN(chan[0].envVol,64))>>6)/volMax;
234 }
235 oscBuf->putSample(h,((output>>depthScale)<<depthScale)>>1);
236 if (outStereo) {
237 buf[0][h]=((output*chan[0].panL)>>(depthScale+8))<<depthScale;
238 buf[1][h]=((output*chan[0].panR)>>(depthScale+8))<<depthScale;
239 } else {
240 output=(output>>depthScale)<<depthScale;
241 buf[0][h]=output;
242 buf[1][h]=output;
243 }
244 }
245
246 oscBuf->end(len);
247 }
248
249 void DivPlatformPCMDAC::tick(bool sysTick) {
250 chan[0].std.next();
251 if (chan[0].std.vol.had) {
252 chan[0].envVol=chan[0].std.vol.val;
253 }
254 if (NEW_ARP_STRAT) {
255 chan[0].handleArp();
256 } else if (chan[0].std.arp.had) {
257 if (!chan[0].inPorta) {
258 chan[0].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[0].note,chan[0].std.arp.val));
259 }
260 chan[0].freqChanged=true;
261 }
262 if (chan[0].useWave && chan[0].std.wave.had) {
263 if (chan[0].wave!=chan[0].std.wave.val || chan[0].ws.activeChanged()) {
264 chan[0].wave=chan[0].std.wave.val;
265 chan[0].ws.changeWave1(chan[0].wave);
266 if (!chan[0].keyOff) chan[0].keyOn=true;
267 }
268 }
269 if (chan[0].useWave && chan[0].active) {
270 chan[0].ws.tick();
271 }
272 if (chan[0].std.pitch.had) {
273 if (chan[0].std.pitch.mode) {
274 chan[0].pitch2+=chan[0].std.pitch.val;
275 CLAMP_VAR(chan[0].pitch2,-32768,32767);
276 } else {
277 chan[0].pitch2=chan[0].std.pitch.val;
278 }
279 chan[0].freqChanged=true;
280 }
281 if (chan[0].std.panL.had) {
282 int val=chan[0].std.panL.val&0x7f;
283 chan[0].panL=val*2;
284 }
285 if (chan[0].std.panR.had) {
286 int val=chan[0].std.panR.val&0x7f;
287 chan[0].panR=val*2;
288 }
289 if (chan[0].std.phaseReset.had) {
290 if (chan[0].std.phaseReset.val==1) {
291 chan[0].audDir=false;
292 chan[0].audPos=0;
293 }
294 }
295 if (chan[0].freqChanged || chan[0].keyOn || chan[0].keyOff) {
296 //DivInstrument* ins=parent->getIns(chan[0].ins,DIV_INS_AMIGA);
297 double off=1.0;
298 if (!chan[0].useWave && chan[0].sample>=0 && chan[0].sample<parent->song.sampleLen) {
299 DivSample* s=parent->getSample(chan[0].sample);
300 off=(s->centerRate>=1)?((double)s->centerRate/parent->getCenterRate()):1.0;
301 }
302 chan[0].freq=off*parent->calcFreq(chan[0].baseFreq,chan[0].pitch,chan[0].fixedArp?chan[0].baseNoteOverride:chan[0].arpOff,chan[0].fixedArp,false,2,chan[0].pitch2,chipClock,CHIP_FREQBASE);
303 if (chan[0].freq>16777215) chan[0].freq=16777215;
304 if (chan[0].keyOn) {
305 if (!chan[0].std.vol.had) {
306 chan[0].envVol=64;
307 }
308 chan[0].keyOn=false;
309 }
310 if (chan[0].keyOff) {
311 chan[0].keyOff=false;
312 }
313 chan[0].freqChanged=false;
314 }
315 }
316
317 int DivPlatformPCMDAC::dispatch(DivCommand c) {
318 switch (c.cmd) {
319 case DIV_CMD_NOTE_ON: {
320 DivInstrument* ins=parent->getIns(chan[0].ins,DIV_INS_AMIGA);
321 if (ins->amiga.useWave) {
322 chan[c.chan].sampleNote=DIV_NOTE_NULL;
323 chan[c.chan].sampleNoteDelta=0;
324 chan[0].useWave=true;
325 chan[0].audLen=ins->amiga.waveLen+1;
326 if (chan[0].insChanged) {
327 if (chan[0].wave<0) {
328 chan[0].wave=0;
329 chan[0].ws.setWidth(chan[0].audLen);
330 chan[0].ws.changeWave1(chan[0].wave);
331 }
332 }
333 } else {
334 if (c.value!=DIV_NOTE_NULL) {
335 chan[0].sample=ins->amiga.getSample(c.value);
336 chan[c.chan].sampleNote=c.value;
337 c.value=ins->amiga.getFreq(c.value);
338 chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote;
339 } else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) {
340 chan[c.chan].sample=ins->amiga.getSample(chan[c.chan].sampleNote);
341 c.value=ins->amiga.getFreq(chan[c.chan].sampleNote);
342 }
343 chan[0].useWave=false;
344 }
345 if (c.value!=DIV_NOTE_NULL) {
346 chan[0].baseFreq=round(NOTE_FREQUENCY(c.value));
347 }
348 if (chan[0].useWave || chan[0].sample<0 || chan[0].sample>=parent->song.sampleLen) {
349 chan[0].sample=-1;
350 chan[c.chan].sampleNote=DIV_NOTE_NULL;
351 chan[c.chan].sampleNoteDelta=0;
352 }
353 if (chan[0].setPos) {
354 chan[0].setPos=false;
355 } else {
356 chan[0].audDir=false;
357 chan[0].audPos=0;
358 }
359 chan[0].audSub=0;
360 memset(chan[0].audDat,0,8*sizeof(short));
361 if (c.value!=DIV_NOTE_NULL) {
362 chan[0].freqChanged=true;
363 chan[0].note=c.value;
364 }
365 chan[0].active=true;
366 chan[0].keyOn=true;
367 chan[0].macroInit(ins);
368 if (!parent->song.brokenOutVol && !chan[0].std.vol.will) {
369 chan[0].envVol=64;
370 }
371 if (chan[0].useWave) {
372 chan[0].ws.init(ins,chan[0].audLen,255,chan[0].insChanged);
373 }
374 chan[0].insChanged=false;
375 break;
376 }
377 case DIV_CMD_NOTE_OFF:
378 chan[0].sample=-1;
379 chan[0].active=false;
380 chan[0].keyOff=true;
381 chan[0].macroInit(NULL);
382 break;
383 case DIV_CMD_NOTE_OFF_ENV:
384 case DIV_CMD_ENV_RELEASE:
385 chan[0].std.release();
386 break;
387 case DIV_CMD_INSTRUMENT:
388 if (chan[0].ins!=c.value || c.value2==1) {
389 chan[0].ins=c.value;
390 chan[0].insChanged=true;
391 }
392 break;
393 case DIV_CMD_VOLUME:
394 if (chan[0].vol!=c.value) {
395 chan[0].vol=c.value;
396 if (!chan[0].std.vol.has) {
397 chan[0].envVol=64;
398 }
399 }
400 break;
401 case DIV_CMD_GET_VOLUME:
402 return chan[0].vol;
403 break;
404 case DIV_CMD_PANNING:
405 chan[0].panL=c.value;
406 chan[0].panR=c.value2;
407 break;
408 case DIV_CMD_PITCH:
409 chan[0].pitch=c.value;
410 chan[0].freqChanged=true;
411 break;
412 case DIV_CMD_WAVE:
413 if (!chan[0].useWave) break;
414 chan[0].wave=c.value;
415 chan[0].keyOn=true;
416 chan[0].ws.changeWave1(chan[0].wave);
417 break;
418 case DIV_CMD_NOTE_PORTA: {
419 int destFreq=round(NOTE_FREQUENCY(c.value2+chan[c.chan].sampleNoteDelta));
420 bool return2=false;
421 if (destFreq>chan[0].baseFreq) {
422 chan[0].baseFreq+=c.value;
423 if (chan[0].baseFreq>=destFreq) {
424 chan[0].baseFreq=destFreq;
425 return2=true;
426 }
427 } else {
428 chan[0].baseFreq-=c.value;
429 if (chan[0].baseFreq<=destFreq) {
430 chan[0].baseFreq=destFreq;
431 return2=true;
432 }
433 }
434 chan[0].freqChanged=true;
435 if (return2) {
436 chan[0].inPorta=false;
437 return 2;
438 }
439 break;
440 }
441 case DIV_CMD_LEGATO: {
442 chan[0].baseFreq=round(NOTE_FREQUENCY(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[0].std.arp.val):(0))));
443 chan[0].freqChanged=true;
444 chan[0].note=c.value;
445 break;
446 }
447 case DIV_CMD_PRE_PORTA:
448 if (chan[0].active && c.value2) {
449 if (parent->song.resetMacroOnPorta) chan[0].macroInit(parent->getIns(chan[0].ins,DIV_INS_AMIGA));
450 }
451 chan[0].inPorta=c.value;
452 break;
453 case DIV_CMD_SAMPLE_POS:
454 if (chan[0].useWave) break;
455 chan[0].audPos=c.value;
456 chan[0].setPos=true;
457 break;
458 case DIV_CMD_GET_VOLMAX:
459 return volMax;
460 break;
461 case DIV_CMD_MACRO_OFF:
462 chan[c.chan].std.mask(c.value,true);
463 break;
464 case DIV_CMD_MACRO_ON:
465 chan[c.chan].std.mask(c.value,false);
466 break;
467 case DIV_CMD_MACRO_RESTART:
468 chan[c.chan].std.restart(c.value);
469 break;
470 default:
471 break;
472 }
473 return 1;
474 }
475
476 void DivPlatformPCMDAC::muteChannel(int ch, bool mute) {
477 isMuted=mute;
478 }
479
480 void DivPlatformPCMDAC::forceIns() {
481 chan[0].insChanged=true;
482 chan[0].freqChanged=true;
483 chan[0].audDir=false;
484 chan[0].audPos=0;
485 chan[0].sample=-1;
486 }
487
488 void* DivPlatformPCMDAC::getChanState(int ch) {
489 return &chan;
490 }
491
492 DivDispatchOscBuffer* DivPlatformPCMDAC::getOscBuffer(int ch) {
493 return oscBuf;
494 }
495
496 void DivPlatformPCMDAC::reset() {
497 chan[0]=DivPlatformPCMDAC::Channel();
498 chan[0].std.setEngine(parent);
499 chan[0].ws.setEngine(parent);
500 chan[0].ws.init(NULL,32,255);
501 memset(chan[0].audDat,0,8*sizeof(short));
502 }
503
504 int DivPlatformPCMDAC::getOutputCount() {
505 return 2;
506 }
507
508 DivMacroInt* DivPlatformPCMDAC::getChanMacroInt(int ch) {
509 return &chan[0].std;
510 }
511
512 unsigned short DivPlatformPCMDAC::getPan(int ch) {
513 return (chan[0].panL<<8)|chan[0].panR;
514 }
515
516 DivSamplePos DivPlatformPCMDAC::getSamplePos(int ch) {
517 if (ch>=1) return DivSamplePos();
518 return DivSamplePos(
519 chan[ch].sample,
520 chan[ch].audPos,
521 chan[ch].freq
522 );
523 }
524
525 void DivPlatformPCMDAC::notifyInsChange(int ins) {
526 if (chan[0].ins==ins) {
527 chan[0].insChanged=true;
528 }
529 }
530
531 void DivPlatformPCMDAC::notifyWaveChange(int wave) {
532 if (chan[0].useWave && chan[0].wave==wave) {
533 chan[0].ws.changeWave1(wave);
534 }
535 }
536
537 void DivPlatformPCMDAC::notifyInsDeletion(void* ins) {
538 chan[0].std.notifyInsDeletion((DivInstrument*)ins);
539 }
540
541 void DivPlatformPCMDAC::setFlags(const DivConfig& flags) {
542 // default to 44100Hz 16-bit stereo
543 rate=flags.getInt("rate",44100);
544 // rate can't be too low or the resampler will break
545 if (rate<1000) rate=1000;
546 chipClock=rate;
547 outDepth=(flags.getInt("outDepth",15))&15;
548 outStereo=flags.getBool("stereo",true);
549 interp=flags.getInt("interpolation",0);
550 oscBuf->setRate(rate);
551 volMax=flags.getInt("volMax",255);
552 if (volMax<1) volMax=1;
553 }
554
555 int DivPlatformPCMDAC::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
556 parent=p;
557 dumpWrites=false;
558 skipRegisterWrites=false;
559 oscBuf=new DivDispatchOscBuffer;
560 isMuted=false;
561 setFlags(flags);
562 reset();
563 return 1;
564 }
565
566 void DivPlatformPCMDAC::quit() {
567 delete oscBuf;
568 }