Debian Codegraph @

src/engine/platform/pong.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 "pong.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23
24 #define CHIP_DIVIDER 1024
25
26 void DivPlatformPong::acquire(short** buf, size_t len) {
27 int out=0;
28 oscBuf->begin(len);
29 for (size_t i=0; i<len; i++) {
30 if (on) {
31 if (--pos<=0) {
32 pos=(freq?2:1)<<4;
33 flip=!flip;
34 }
35 out=(flip && !isMuted[0])?32767:0;
36 buf[0][i]=out;
37 oscBuf->putSample(i,out);
38 } else {
39 buf[0][i]=0;
40 oscBuf->putSample(i,0);
41 flip=false;
42 }
43 }
44 oscBuf->end(len);
45 }
46
47 void DivPlatformPong::tick(bool sysTick) {
48 for (int i=0; i<1; i++) {
49 chan[i].std.next();
50 if (chan[i].std.vol.had) {
51 chan[i].outVol=(chan[i].vol && chan[i].std.vol.val);
52 on=chan[i].outVol;
53 }
54 if (NEW_ARP_STRAT) {
55 chan[i].handleArp();
56 } else if (chan[i].std.arp.had) {
57 if (!chan[i].inPorta) {
58 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
59 }
60 chan[i].freqChanged=true;
61 }
62 if (sysTick) {
63 flip=false;
64 pos=0;
65 }
66 if (chan[i].std.pitch.had) {
67 if (chan[i].std.pitch.mode) {
68 chan[i].pitch2+=chan[i].std.pitch.val;
69 CLAMP_VAR(chan[i].pitch2,-32768,32767);
70 } else {
71 chan[i].pitch2=chan[i].std.pitch.val;
72 }
73 chan[i].freqChanged=true;
74 }
75 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
76 chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,true,0,chan[i].pitch2,chipClock,CHIP_DIVIDER)-1;
77 if (chan[i].freq<0) chan[i].freq=0;
78 if (chan[i].freq>1) chan[i].freq=1;
79 if (chan[i].keyOn) {
80 on=true;
81 }
82 if (chan[i].keyOff) {
83 on=false;
84 }
85 freq=chan[i].freq;
86 if (chan[i].keyOn) chan[i].keyOn=false;
87 if (chan[i].keyOff) chan[i].keyOff=false;
88 chan[i].freqChanged=false;
89 }
90 }
91 }
92
93 int DivPlatformPong::dispatch(DivCommand c) {
94 switch (c.cmd) {
95 case DIV_CMD_NOTE_ON:
96 if (c.value!=DIV_NOTE_NULL) {
97 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
98 chan[c.chan].freqChanged=true;
99 chan[c.chan].note=c.value;
100 }
101 chan[c.chan].active=true;
102 chan[c.chan].keyOn=true;
103 chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_BEEPER));
104 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
105 chan[c.chan].outVol=chan[c.chan].vol;
106 }
107 break;
108 case DIV_CMD_NOTE_OFF:
109 chan[c.chan].active=false;
110 chan[c.chan].keyOff=true;
111 chan[c.chan].macroInit(NULL);
112 break;
113 case DIV_CMD_NOTE_OFF_ENV:
114 case DIV_CMD_ENV_RELEASE:
115 chan[c.chan].std.release();
116 break;
117 case DIV_CMD_INSTRUMENT:
118 if (chan[c.chan].ins!=c.value || c.value2==1) {
119 chan[c.chan].ins=c.value;
120 }
121 break;
122 case DIV_CMD_VOLUME:
123 if (chan[c.chan].vol!=c.value) {
124 chan[c.chan].vol=c.value;
125 if (!chan[c.chan].std.vol.has) {
126 chan[c.chan].outVol=c.value;
127 }
128 if (chan[c.chan].active) {
129 on=chan[c.chan].vol;
130 }
131 }
132 break;
133 case DIV_CMD_GET_VOLUME:
134 return chan[c.chan].vol;
135 break;
136 case DIV_CMD_PITCH:
137 chan[c.chan].pitch=c.value;
138 chan[c.chan].freqChanged=true;
139 break;
140 case DIV_CMD_NOTE_PORTA: {
141 int destFreq=NOTE_PERIODIC(c.value2);
142 bool return2=false;
143 if (destFreq>chan[c.chan].baseFreq) {
144 chan[c.chan].baseFreq+=c.value;
145 if (chan[c.chan].baseFreq>=destFreq) {
146 chan[c.chan].baseFreq=destFreq;
147 return2=true;
148 }
149 } else {
150 chan[c.chan].baseFreq-=c.value;
151 if (chan[c.chan].baseFreq<=destFreq) {
152 chan[c.chan].baseFreq=destFreq;
153 return2=true;
154 }
155 }
156 chan[c.chan].freqChanged=true;
157 if (return2) {
158 chan[c.chan].inPorta=false;
159 return 2;
160 }
161 break;
162 }
163 case DIV_CMD_LEGATO:
164 if (c.chan==3) break;
165 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
166 chan[c.chan].freqChanged=true;
167 chan[c.chan].note=c.value;
168 break;
169 case DIV_CMD_PRE_PORTA:
170 if (chan[c.chan].active && c.value2) {
171 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_BEEPER));
172 }
173 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_PERIODIC(chan[c.chan].note);
174 chan[c.chan].inPorta=c.value;
175 break;
176 case DIV_CMD_GET_VOLMAX:
177 return 1;
178 break;
179 case DIV_CMD_MACRO_OFF:
180 chan[c.chan].std.mask(c.value,true);
181 break;
182 case DIV_CMD_MACRO_ON:
183 chan[c.chan].std.mask(c.value,false);
184 break;
185 case DIV_CMD_MACRO_RESTART:
186 chan[c.chan].std.restart(c.value);
187 break;
188 default:
189 break;
190 }
191 return 1;
192 }
193
194 void DivPlatformPong::muteChannel(int ch, bool mute) {
195 isMuted[ch]=mute;
196 }
197
198 void DivPlatformPong::forceIns() {
199 for (int i=0; i<1; i++) {
200 chan[i].insChanged=true;
201 }
202 }
203
204 void* DivPlatformPong::getChanState(int ch) {
205 return &chan[ch];
206 }
207
208 DivMacroInt* DivPlatformPong::getChanMacroInt(int ch) {
209 return &chan[ch].std;
210 }
211
212 DivDispatchOscBuffer* DivPlatformPong::getOscBuffer(int ch) {
213 return oscBuf;
214 }
215
216 void DivPlatformPong::reset() {
217 for (int i=0; i<1; i++) {
218 chan[i]=DivPlatformPong::Channel();
219 chan[i].std.setEngine(parent);
220 }
221 if (dumpWrites) {
222 addWrite(0xffffffff,0);
223 }
224
225 on=false;
226 lastOn=false;
227 freq=0;
228 pos=0;
229 flip=false;
230
231 memset(regPool,0,2);
232 }
233
234 bool DivPlatformPong::keyOffAffectsArp(int ch) {
235 return true;
236 }
237
238 void DivPlatformPong::setFlags(const DivConfig& flags) {
239 chipClock=1000000;
240 CHECK_CUSTOM_CLOCK;
241 rate=chipClock/64;
242 oscBuf->setRate(rate);
243 }
244
245 void DivPlatformPong::notifyInsDeletion(void* ins) {
246 for (int i=0; i<1; i++) {
247 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
248 }
249 }
250
251 void DivPlatformPong::poke(unsigned int addr, unsigned short val) {
252 // ???
253 }
254
255 void DivPlatformPong::poke(std::vector<DivRegWrite>& wlist) {
256 // ???
257 }
258
259 int DivPlatformPong::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
260 parent=p;
261 dumpWrites=false;
262 skipRegisterWrites=false;
263 for (int i=0; i<1; i++) {
264 isMuted[i]=false;
265 }
266 oscBuf=new DivDispatchOscBuffer;
267 setFlags(flags);
268
269 reset();
270 return 5;
271 }
272
273 void DivPlatformPong::quit() {
274 delete oscBuf;
275 }
276
277 DivPlatformPong::~DivPlatformPong() {
278 }