Debian Codegraph @

src/pt2_scopes.c

raw symbols SCIP
1 #cxx . . $ `<file>/src/pt2_scopes.c`/include <stdint.h>
2 #include <stdbool.h>
3 #include <math.h> // modf()
4 #ifndef _WIN32
5 #include <unistd.h> // usleep()
6 #endif
7 #include "pt2_helpers.h"cxx . . $ `<file>/src/pt2_helpers.h`/
8 #include "pt2_visuals.h"cxx . . $ `<file>/src/pt2_visuals.h`/
9 #include "pt2_scopes.h"cxx . . $ `<file>/src/pt2_scopes.h`/
10 #include "pt2_config.h"cxx . . $ `<file>/src/pt2_config.h`/
11
12 // this uses code that is not entirely thread safe, but I have never had any issues so far...
13
14 static volatile boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! scopesUpdatingFlagcxx . . $ scopesUpdatingFlag., scopesDisplayingFlagcxx . . $ scopesDisplayingFlag.;
15 static hpc_tcxx . . $ hpc_t# scopeHpccxx . . $ scopeHpc.;
16 static SDL_Threadcxx . . $ SDL_Thread# *scopeThreadcxx . . $ scopeThread.;
17
18 scope_tcxx . . $ scope_t# scopecxx . . $ scope.[PAULA_VOICEScxx . . $ `src/pt2_paula.h:13:9`!]; // global
19
20 // this is quite hackish, but fixes sample swapping issues
21 static int32_tcxx . . $ int32_t# getSampleSlotFromReadAddresscxx . . $ getSampleSlotFromReadAddress(59b63c69aae9a2d4).(const int8_tcxx . . $ int8_t# *sampleReadAddress)
22 {
23 ASSERTcxx . . $ `src/pt2_header.h:25:9`!(song != NULL);
24 const int8_tcxx . . $ int8_t# *sampleData = songcxx . . $ song.->sampleDatacxx . . $ module_t#sampleData.;
25 const int32_tcxx . . $ int32_t# sampleSlotSize = configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.;
26
27 if (sampleData == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) // shouldn't really happen, but just in case
28 return -1;
29
30 int32_tcxx . . $ int32_t# sampleSlot = 30; // start at last slot
31
32 const int8_tcxx . . $ int8_t# *sampleBaseAddress = &sampleData[sampleSlot * sampleSlotSize];
33 if (sampleReadAddress == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || sampleReadAddress >= sampleBaseAddress+sampleSlotSize)
34 return -1; // out of range
35
36 for (; sampleSlot >= 0; sampleSlot--)
37 {
38 if (sampleReadAddress >= sampleBaseAddress)
39 break;
40
41 sampleBaseAddress -= sampleSlotSize;
42 }
43
44 return sampleSlot; // 0..30, or -1 if out of range
45 }
46
47 int32_tcxx . . $ int32_t# getSampleReadPoscxx . . $ getSampleReadPos(7864480464b09eea).(int32_tcxx . . $ int32_t# ch) // used for the sampler screen
48 {
49 // cache some stuff
50 const scope_tcxx . . $ scope_t# *sc = &scopecxx . . $ scope.[ch];
51 const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! active = sc->activecxx . . $ scope_t#active.;
52 const int8_tcxx . . $ int8_t# *data = sc->datacxx . . $ scope_t#data.;
53 const int32_tcxx . . $ int32_t# pos = sc->poscxx . . $ scope_t#pos.;
54 const int32_tcxx . . $ int32_t# len = sc->lengthcxx . . $ scope_t#length.;
55
56 if (songcxx . . $ song. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || !active || data == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
57 return -1;
58
59 /* Because the scopes work like the Paula emulation, we have a DATA
60 ** and LENGTH variable, which are not static. This means that we have
61 ** to get creative to get the absolute sampling position.
62 */
63
64 int32_tcxx . . $ int32_t# sample = getSampleSlotFromReadAddresscxx . . $ getSampleSlotFromReadAddress(59b63c69aae9a2d4).(data);
65 if (sample != editorcxx . . $ editor..currSamplecxx . . $ editor_t#currSample.)
66 return -1; // sample is not the one we're seeing in the sampler screen
67
68 const moduleSample_tcxx . . $ moduleSample_t# *s = &songcxx . . $ song.->samplescxx . . $ module_t#samples.[sample];
69 const int8_tcxx . . $ int8_t# *sampleReadAddress = &data[pos];
70 const int8_tcxx . . $ int8_t# *sampleBaseAddress = &songcxx . . $ song.->sampleDatacxx . . $ module_t#sampleData.[s->offsetcxx . . $ moduleSample_t#offset.];
71 const int32_tcxx . . $ int32_t# realPos = (int32_tcxx . . $ int32_t#)(sampleReadAddress - sampleBaseAddress);
72
73 // return -1 if sample has no loop and read length is 2 (playing sample "loop" area)
74 const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! loopEnabled = (s->loopStartcxx . . $ moduleSample_t#loopStart. + s->loopLengthcxx . . $ moduleSample_t#loopLength.) > 2;
75 if (!loopEnabled && len == 2)
76 return -1;
77
78 if (realPos < 0 || realPos >= s->lengthcxx . . $ moduleSample_t#length.)
79 return -1;
80
81 return realPos;
82 }
83
84 void scopeSetPeriodcxx . . $ scopeSetPeriod(3e454b8ccf442868).(int32_tcxx . . $ int32_t# ch, int32_tcxx . . $ int32_t# period)
85 {
86 period &= 0xFFFF;
87
88 if (period < 113) // just in case, should already be clamped
89 period = 113;
90
91 scopecxx . . $ scope.[ch].dDeltacxx . . $ scope_t#dDelta. = (PAULA_PAL_CLKcxx . . $ `src/pt2_paula.h:14:9`! / (double)SCOPE_HZcxx . . $ `src/pt2_header.h:64:9`!) / period;
92 }
93
94 void scopeTriggercxx . . $ scopeTrigger(d4f767463ce0a6b3).(int32_tcxx . . $ int32_t# ch) // expects data & length variables to be set already
95 {
96 volatile scope_tcxx . . $ scope_t# *sc = &scopecxx . . $ scope.[ch];
97 scope_tcxx . . $ scope_t# tempState = *sc; // cache it
98
99 if (tempState.datacxx . . $ scope_t#data. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
100 tempState.datacxx . . $ scope_t#data. = paulaGetNullSamplePtrcxx . . $ paulaGetNullSamplePtr(d09b239063734497).();
101
102 if (tempState.lengthcxx . . $ scope_t#length. < 2)
103 tempState.lengthcxx . . $ scope_t#length. = 2; // for safety
104
105 tempState.dPhasecxx . . $ scope_t#dPhase. = 0.0;
106 tempState.poscxx . . $ scope_t#pos. = 0;
107 tempState.activecxx . . $ scope_t#active. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
108
109 /* Update live scope now.
110 ** In theory it -can- be written to in the middle of a cached read,
111 ** then the read thread writes its own non-updated cached copy back and
112 ** the trigger never happens. So far I have never seen it happen,
113 ** so it's probably very rare. Yes, this is not good coding...
114 */
115 *sc = tempState;
116 }
117
118 void updateScopescxx . . $ updateScopes(49f6e7a06ebc5aa8).(void)
119 {
120 scope_tcxx . . $ scope_t# tempState;
121
122 if (editorcxx . . $ editor..mod2WavOngoingcxx . . $ editor_t#mod2WavOngoing.)
123 return;
124
125 volatile scope_tcxx . . $ scope_t# *sc = scopecxx . . $ scope.;
126
127 scopesUpdatingFlagcxx . . $ scopesUpdatingFlag. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
128 for (int32_tcxx . . $ int32_t# i = 0; i < PAULA_VOICEScxx . . $ `src/pt2_paula.h:13:9`!; i++, sc++)
129 {
130 tempState = *sc; // cache it
131 if (!tempState.activecxx . . $ scope_t#active.)
132 continue; // scope is not active
133
134 tempState.dPhasecxx . . $ scope_t#dPhase. += tempState.dDeltacxx . . $ scope_t#dDelta.;
135
136 const int32_tcxx . . $ int32_t# wholeSamples = (int32_tcxx . . $ int32_t#)tempState.dPhasecxx . . $ scope_t#dPhase.;
137 tempState.dPhasecxx . . $ scope_t#dPhase. -= wholeSamples;
138 tempState.poscxx . . $ scope_t#pos. += wholeSamples;
139
140 if (tempState.poscxx . . $ scope_t#pos. >= tempState.lengthcxx . . $ scope_t#length.)
141 {
142 // sample reached end, simulate Paula register update (sample swapping)
143
144 /* Wrap pos around one time with current length, then set new length
145 ** and wrap around it (handles one-shot loops and sample swapping).
146 */
147 tempState.poscxx . . $ scope_t#pos. -= tempState.lengthcxx . . $ scope_t#length.;
148
149 tempState.lengthcxx . . $ scope_t#length. = tempState.newLengthcxx . . $ scope_t#newLength.;
150 if (tempState.poscxx . . $ scope_t#pos. >= tempState.lengthcxx . . $ scope_t#length. && tempState.lengthcxx . . $ scope_t#length. > 0)
151 tempState.poscxx . . $ scope_t#pos. %= tempState.lengthcxx . . $ scope_t#length.;
152
153 tempState.datacxx . . $ scope_t#data. = tempState.newDatacxx . . $ scope_t#newData.;
154 }
155
156 *sc = tempState; // update scope state
157 }
158 scopesUpdatingFlagcxx . . $ scopesUpdatingFlag. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
159 }
160
161 /* This routine gets the average sample amplitude through the running scope voices.
162 ** This gives a somewhat more stable result than getting the peak from the mixer,
163 ** and we don't care about including filters/BLEP in the peak calculation.
164 */
165 static void updateRealVuMeterscxx . . $ updateRealVuMeters(49f6e7a06ebc5aa8).(void)
166 {
167 scope_tcxx . . $ scope_t# tmpScope, *sc;
168
169 // sink VU-meters first
170 for (int32_tcxx . . $ int32_t# i = 0; i < PAULA_VOICEScxx . . $ `src/pt2_paula.h:13:9`!; i++)
171 {
172 editorcxx . . $ editor..realVuMeterVolumescxx . . $ editor_t#realVuMeterVolumes.[i] -= 4;
173 if (editorcxx . . $ editor..realVuMeterVolumescxx . . $ editor_t#realVuMeterVolumes.[i] < 0)
174 editorcxx . . $ editor..realVuMeterVolumescxx . . $ editor_t#realVuMeterVolumes.[i] = 0;
175 }
176
177 // get peak sample data from running scope voices
178 sc = scopecxx . . $ scope.;
179 for (int32_tcxx . . $ int32_t# i = 0; i < PAULA_VOICEScxx . . $ `src/pt2_paula.h:13:9`!; i++, sc++)
180 {
181 tmpScope = *sc; // cache it
182
183 if (!tmpScope.activecxx . . $ scope_t#active. || tmpScope.datacxx . . $ scope_t#data. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || tmpScope.volumecxx . . $ scope_t#volume. == 0 || tmpScope.lengthcxx . . $ scope_t#length. == 0)
184 continue;
185
186 // amount of integer samples getting skipped every frame (periods < 113 are clamped, this number can't get big)
187 uint32_tcxx . . $ uint32_t# samplesToScan = (uint32_tcxx . . $ uint32_t#)tmpScope.dDeltacxx . . $ scope_t#dDelta.;
188 if (samplesToScan == 0)
189 continue;
190
191 if (samplesToScan > 512)
192 samplesToScan = 512;
193
194 int32_tcxx . . $ int32_t# pos = tmpScope.poscxx . . $ scope_t#pos.;
195 int32_tcxx . . $ int32_t# length = tmpScope.lengthcxx . . $ scope_t#length.;
196 const int8_tcxx . . $ int8_t# *data = tmpScope.datacxx . . $ scope_t#data.;
197
198 uint32_tcxx . . $ uint32_t# runningAmplitude = 0;
199 for (uint32_tcxx . . $ uint32_t# x = 0; x < samplesToScan; x++)
200 {
201 int32_tcxx . . $ int32_t# amplitude = 0;
202 if (data != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
203 amplitude = data[pos] * tmpScope.volumecxx . . $ scope_t#volume.;
204
205 runningAmplitude += ABScxx . . $ `src/pt2_helpers.h:33:9`!(amplitude);
206
207 if (++pos >= length)
208 {
209 pos = 0;
210
211 /* Read cycle done, temporarily update the display data/length variables
212 ** before the scope thread does it.
213 */
214 data = tmpScope.newDatacxx . . $ scope_t#newData.;
215 length = tmpScope.newLengthcxx . . $ scope_t#newLength.;
216 }
217 }
218
219 runningAmplitude *= 96; // increase scale (for VU-meter height)
220 runningAmplitude /= samplesToScan; // get average
221 runningAmplitude = (runningAmplitude + 4096) >> (7+6); // normalize (rounded)
222
223 if (runningAmplitude > 48) // max VU-meter height
224 runningAmplitude = 48;
225
226 int8_tcxx . . $ int8_t# vuHeight = (int8_tcxx . . $ int8_t#)runningAmplitude;
227 if (vuHeight > editorcxx . . $ editor..realVuMeterVolumescxx . . $ editor_t#realVuMeterVolumes.[i])
228 editorcxx . . $ editor..realVuMeterVolumescxx . . $ editor_t#realVuMeterVolumes.[i] = vuHeight;
229 }
230 }
231
232 void drawScopescxx . . $ drawScopes(49f6e7a06ebc5aa8).(void)
233 {
234 volatile scope_tcxx . . $ scope_t# *sc = scopecxx . . $ scope.; // cache it
235 int32_tcxx . . $ int32_t# scopeX = 128;
236
237 const uint32_tcxx . . $ uint32_t# bgColor = videocxx . . $ video..palettecxx . . $ video_t#palette.[PAL_BACKGRDcxx . . $ PAL_BACKGRD.];
238 const uint32_tcxx . . $ uint32_t# fgColor = videocxx . . $ video..palettecxx . . $ video_t#palette.[PAL_QADSCPcxx . . $ PAL_QADSCP.];
239
240 scopesDisplayingFlagcxx . . $ scopesDisplayingFlag. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
241 for (int32_tcxx . . $ int32_t# i = 0; i < PAULA_VOICEScxx . . $ `src/pt2_paula.h:13:9`!; i++, sc++)
242 {
243 scope_tcxx . . $ scope_t# tmpScope = *sc; // cache it
244
245 // clear scope background
246 fillRectcxx . . $ fillRect(6fb2c7d311165c4f).(scopeX, 55, SCOPE_WIDTHcxx . . $ `src/pt2_header.h:77:9`!, SCOPE_HEIGHTcxx . . $ `src/pt2_header.h:78:9`!, bgColor);
247
248 // render scope
249 if (tmpScope.activecxx . . $ scope_t#active. && tmpScope.datacxx . . $ scope_t#data. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && tmpScope.volumecxx . . $ scope_t#volume. != 0 && tmpScope.lengthcxx . . $ scope_t#length. > 0)
250 {
251 // render scope data
252 int16_tcxx . . $ int16_t# scopeData;
253 int32_tcxx . . $ int32_t# pos = tmpScope.poscxx . . $ scope_t#pos.;
254 int32_tcxx . . $ int32_t# length = tmpScope.lengthcxx . . $ scope_t#length.;
255 const int8_tcxx . . $ int8_t# volume = -(tmpScope.volumecxx . . $ scope_t#volume. >> 1);
256 const int8_tcxx . . $ int8_t# *data = tmpScope.datacxx . . $ scope_t#data.;
257 uint32_tcxx . . $ uint32_t# *scopeDrawPtr = &videocxx . . $ video..frameBuffercxx . . $ video_t#frameBuffer.[(71 * SCREEN_Wcxx . . $ `src/pt2_header.h:41:9`!) + scopeX];
258
259 for (int32_tcxx . . $ int32_t# x = 0; x < SCOPE_WIDTHcxx . . $ `src/pt2_header.h:77:9`!; x++)
260 {
261 scopeData = 0;
262 if (data != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
263 scopeData = (data[pos] * volume) >> 8;
264
265 scopeDrawPtr[(scopeData * SCREEN_Wcxx . . $ `src/pt2_header.h:41:9`!) + x] = fgColor;
266
267 pos++;
268 if (pos >= length)
269 {
270 pos = 0;
271
272 // read cycle done, update the drawing data/length variables
273 length = tmpScope.newLengthcxx . . $ scope_t#newLength.;
274 data = tmpScope.newDatacxx . . $ scope_t#newData.;
275 }
276 }
277 }
278 else
279 {
280 // draw centered scope line
281 hLinecxx . . $ hLine(78a5fb7015678ec5).(scopeX, 71, SCOPE_WIDTHcxx . . $ `src/pt2_header.h:77:9`!, fgColor);
282 }
283
284 scopeX += SCOPE_WIDTHcxx . . $ `src/pt2_header.h:77:9`!+8;
285 }
286 scopesDisplayingFlagcxx . . $ scopesDisplayingFlag. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
287 }
288
289 static int32_tcxx . . $ int32_t# scopeThreadFunccxx . . $ scopeThreadFunc(dedf1e9b1c614a07).(void *ptr)
290 {
291 // this is confirmed to be needed for scope stability
292 SDL_SetThreadPrioritycxx . . $ SDL_SetThreadPriority(3fc85575c796fb27).(SDL_THREAD_PRIORITY_HIGHcxx . . $ SDL_THREAD_PRIORITY_HIGH.);
293
294 hpc_SetDurationInHzcxx . . $ hpc_SetDurationInHz(342ecb9d325f249a).(&scopeHpccxx . . $ scopeHpc., SCOPE_HZcxx . . $ `src/pt2_header.h:64:9`!);
295 hpc_ResetCounterscxx . . $ hpc_ResetCounters(efacbc4df292ea9a).(&scopeHpccxx . . $ scopeHpc.);
296
297 while (editorcxx . . $ editor..programRunningcxx . . $ editor_t#programRunning.)
298 {
299 if (configcxx . . $ config..realVuMeterscxx . . $ config_t#realVuMeters.)
300 updateRealVuMeterscxx . . $ updateRealVuMeters(49f6e7a06ebc5aa8).();
301
302 updateScopescxx . . $ updateScopes(49f6e7a06ebc5aa8).();
303
304 hpc_Waitcxx . . $ hpc_Wait(efacbc4df292ea9a).(&scopeHpccxx . . $ scopeHpc.);
305 }
306
307 (void)ptr;
308 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
309 }
310
311 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! initScopescxx . . $ initScopes(c9857a73d68e59ba).(void)
312 {
313 scopeThreadcxx . . $ scopeThread. = SDL_CreateThreadcxx . . $ SDL_CreateThread(6bda6645e1e04984).(scopeThreadFunccxx . . $ scopeThreadFunc(dedf1e9b1c614a07)., "scope thread", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
314 if (scopeThreadcxx . . $ scopeThread. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
315 {
316 showErrorMsgBoxcxx . . $ showErrorMsgBox(326aeb464d16410c).("Couldn't create scope thread!");
317 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
318 }
319
320 SDL_DetachThreadcxx . . $ SDL_DetachThread(892b99e67b469720).(scopeThreadcxx . . $ scopeThread.);
321 return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
322 }
323
324 void stopScopecxx . . $ stopScope(d4f767463ce0a6b3).(int32_tcxx . . $ int32_t# ch)
325 {
326 // wait for scopes to finish updating
327 while (scopesUpdatingFlagcxx . . $ scopesUpdatingFlag.);
328
329 scopecxx . . $ scope.[ch].activecxx . . $ scope_t#active. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
330
331 // wait for scope displaying to be done (safety)
332 while (scopesDisplayingFlagcxx . . $ scopesDisplayingFlag.);
333 }
334
335 void stopAllScopescxx . . $ stopAllScopes(49f6e7a06ebc5aa8).(void)
336 {
337 // wait for scopes to finish updating
338 while (scopesUpdatingFlagcxx . . $ scopesUpdatingFlag.);
339
340 for (int32_tcxx . . $ int32_t# i = 0; i < PAULA_VOICEScxx . . $ `src/pt2_paula.h:13:9`!; i++)
341 scopecxx . . $ scope.[i].activecxx . . $ scope_t#active. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
342
343 // wait for scope displaying to be done (safety)
344 while (scopesDisplayingFlagcxx . . $ scopesDisplayingFlag.);
345 }