Debian Codegraph @

src/engine/waveSynth.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 "waveSynth.h"
21 #include "engine.h"
22 #include "instrument.h"
23 #include "../ta-log.h"
24
25 inline bool effectOnlyAltersOutput(unsigned char effect) {
26 switch (effect) {
27 case DIV_WS_NONE:
28 case DIV_WS_INVERT:
29 case DIV_WS_ADD:
30 case DIV_WS_SUBTRACT:
31 case DIV_WS_AVERAGE:
32 return true;
33 break;
34 default:
35 return false;
36 break;
37 }
38 return false;
39 }
40
41 bool DivWaveSynth::activeChanged() {
42 if (activeChangedB) {
43 activeChangedB=false;
44 return true;
45 }
46 if (first) return true;
47 return false;
48 }
49
50 #define WS_BEGIN int _oldOut=output[pos];
51 #define WS_END if (output[pos]!=_oldOut) updated=true;
52
53 bool DivWaveSynth::tick(bool skipSubDiv) {
54 bool updated=first;
55 first=false;
56 if (--subDivCounter>0 && !skipSubDiv) {
57 return updated;
58 }
59
60 subDivCounter=e->tickMult;
61 if (!state.enabled) return updated;
62 if (width<1) return false;
63
64 if (--divCounter<=0) {
65 // run effect
66 switch (state.effect) {
67 case DIV_WS_INVERT:
68 for (int i=0; i<=state.speed; i++) {
69 WS_BEGIN;
70 output[pos]=height-output[pos];
71 WS_END;
72
73 if (++pos>=width) pos=0;
74 }
75 break;
76 case DIV_WS_ADD:
77 for (int i=0; i<=state.speed; i++) {
78 WS_BEGIN;
79 output[pos]+=MIN(height,state.param1);
80 if (output[pos]>=height) output[pos]-=height;
81 WS_END;
82
83 if (++pos>=width) pos=0;
84 }
85 break;
86 case DIV_WS_SUBTRACT:
87 for (int i=0; i<=state.speed; i++) {
88 WS_BEGIN;
89 output[pos]-=MIN(height,state.param1);
90 if (output[pos]<0) output[pos]+=height;
91 WS_END;
92
93 if (++pos>=width) pos=0;
94 }
95 break;
96 case DIV_WS_AVERAGE:
97 for (int i=0; i<=state.speed; i++) {
98 int pos1=(pos+1>=width)?0:(pos+1);
99 WS_BEGIN;
100 output[pos]=(128+output[pos]*(256-state.param1)+output[pos1]*state.param1)>>8;
101 if (output[pos]<0) output[pos]=0;
102 if (output[pos]>height) output[pos]=height;
103 WS_END;
104
105 if (++pos>=width) pos=0;
106 }
107 break;
108 case DIV_WS_PHASE:
109 for (int i=0; i<=state.speed; i++) {
110 WS_BEGIN;
111 output[pos]=wave1[(pos+stage)%width];
112 WS_END;
113
114 if (++pos>=width) {
115 pos=0;
116 if (++stage>=width) stage=0;
117 }
118 }
119 break;
120 case DIV_WS_CHORUS:
121 for (int i=0; i<=state.speed; i++) {
122 WS_BEGIN;
123 output[pos]=(wave1[pos]+wave1[(pos+stage)%width])>>1;
124 WS_END;
125
126 if (++pos>=width) {
127 pos=0;
128 stage+=state.param1;
129 while (stage>=width) stage-=width;
130 }
131 }
132 break;
133 case DIV_WS_WIPE:
134 for (int i=0; i<=state.speed; i++) {
135 WS_BEGIN;
136 output[pos]=(stage&1)?wave1[pos]:wave2[pos];
137 if (output[pos]<0) output[pos]=0;
138 if (output[pos]>height) output[pos]=height;
139 WS_END;
140
141 if (++pos>=width) {
142 pos=0;
143 stage=!stage;
144 }
145 }
146 break;
147 case DIV_WS_FADE:
148 for (int i=0; i<=state.speed; i++) {
149 WS_BEGIN;
150 output[pos]=wave1[pos]+(((wave2[pos]-wave1[pos])*stage)>>9);
151 WS_END;
152
153 if (++pos>=width) {
154 pos=0;
155 stage+=1+state.param1;
156 if (stage>512) stage=512;
157 }
158 }
159 break;
160 case DIV_WS_PING_PONG:
161 for (int i=0; i<=state.speed; i++) {
162 WS_BEGIN;
163 output[pos]=wave1[pos]+(((wave2[pos]-wave1[pos])*stage)>>8);
164 WS_END;
165
166 if (++pos>=width) {
167 pos=0;
168 if (stageDir) {
169 stage-=1+state.param1;
170 if (stage<=0) {
171 stageDir=false;
172 stage=0;
173 }
174 } else {
175 stage+=1+state.param1;
176 if (stage>=256) {
177 stageDir=true;
178 stage=256;
179 }
180 }
181 }
182 }
183 break;
184 case DIV_WS_OVERLAY:
185 for (int i=0; i<=state.speed; i++) {
186 WS_BEGIN;
187 output[pos]+=wave2[pos];
188 if (output[pos]>=height) output[pos]-=height;
189 WS_END;
190
191 if (++pos>=width) pos=0;
192 }
193 break;
194 case DIV_WS_NEGATIVE_OVERLAY:
195 for (int i=0; i<=state.speed; i++) {
196 WS_BEGIN;
197 output[pos]-=wave2[pos];
198 if (output[pos]<0) output[pos]+=height;
199 WS_END;
200
201 if (++pos>=width) pos=0;
202 }
203 break;
204 case DIV_WS_SLIDE:
205 for (int i=0; i<=state.speed; i++) {
206 int newPos=(pos+stage)%(width*2);
207 WS_BEGIN;
208 if (newPos>=width) {
209 output[pos]=wave2[newPos-width];
210 } else {
211 output[pos]=wave1[newPos];
212 }
213 WS_END;
214
215 if (++pos>=width) {
216 pos=0;
217 if (++stage>=width*2) stage=0;
218 }
219 }
220 break;
221 case DIV_WS_MIX:
222 for (int i=0; i<=state.speed; i++) {
223 WS_BEGIN;
224 output[pos]=(wave1[pos]+wave2[(pos+stage)%width])>>1;
225 WS_END;
226
227 if (++pos>=width) {
228 pos=0;
229 stage+=state.param1;
230 while (stage>=width) stage-=width;
231 }
232 }
233 break;
234 case DIV_WS_PHASE_MOD:
235 for (int i=0; i<=state.speed; i++) {
236 int mod=(wave2[pos]*(state.param2-stage)*width)/(64*(height+1));
237 WS_BEGIN;
238 output[pos]=wave1[(pos+mod)%width];
239 WS_END;
240
241 if (++pos>=width) {
242 pos=0;
243 stage+=state.param1;
244 if (stage>state.param2) stage=state.param2;
245 }
246 }
247 break;
248 }
249 divCounter=state.rateDivider;
250 }
251
252 return updated;
253 }
254
255 void DivWaveSynth::setWidth(int val) {
256 width=val;
257 if (width<0) width=0;
258 if (width>256) width=256;
259 }
260
261 #define SHALL_UPDATE_OUT (!state.enabled || force || (state.enabled && effectOnlyAltersOutput(state.effect)))
262
263 void DivWaveSynth::changeWave1(int num, bool force) {
264 DivWavetable* w1=e->getWave(num);
265 if (width<1) return;
266 for (int i=0; i<width; i++) {
267 if (w1->max<1 || w1->len<1) {
268 wave1[i]=0;
269 if (SHALL_UPDATE_OUT) output[i]=0;
270 } else {
271 int data=w1->data[i*w1->len/width]*height/w1->max;
272 if (data<0) data=0;
273 if (data>height) data=height;
274 wave1[i]=data;
275 if (SHALL_UPDATE_OUT) output[i]=data;
276 }
277 }
278 first=true;
279 }
280
281 void DivWaveSynth::changeWave2(int num) {
282 DivWavetable* w2=e->getWave(num);
283 if (width<1) return;
284 for (int i=0; i<width; i++) {
285 if (w2->max<1 || w2->len<1) {
286 wave2[i]=0;
287 } else {
288 int data=w2->data[i*w2->len/width]*height/w2->max;
289 if (data<0) data=0;
290 if (data>height) data=height;
291 wave2[i]=data;
292 }
293 }
294 first=true;
295 }
296
297 void DivWaveSynth::setEngine(DivEngine* engine, int waveFloor) {
298 e=engine;
299 memset(wave1,waveFloor,256);
300 memset(wave2,waveFloor,256);
301 for (int i=0; i<256; i++) {
302 output[i]=waveFloor;
303 }
304 }
305
306 void DivWaveSynth::init(DivInstrument* which, int w, int h, bool insChanged) {
307 width=w;
308 height=h;
309 if (width<0) width=0;
310 if (width>256) width=256;
311 if (e==NULL) return;
312 if (which==NULL) {
313 if (state.enabled) activeChangedB=true;
314 state=DivInstrumentWaveSynth();
315 return;
316 }
317 if (!which->ws.enabled) {
318 if (state.enabled) activeChangedB=true;
319 state=DivInstrumentWaveSynth();
320 return;
321 } else {
322 if (!state.enabled) activeChangedB=true;
323 }
324 state=which->ws;
325 if (insChanged || !state.global) {
326 pos=0;
327 stage=0;
328 stageDir=false;
329 divCounter=0;
330 subDivCounter=0;
331
332 changeWave1(state.wave1,true);
333 changeWave2(state.wave2);
334 //tick(true); // ???
335 first=true;
336 }
337 }