| 1 | #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" |
| 8 | #include "pt2_visuals.h" |
| 9 | #include "pt2_scopes.h" |
| 10 | #include "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 bool scopesUpdatingFlag, scopesDisplayingFlag; |
| 15 | static hpc_t scopeHpc; |
| 16 | static SDL_Thread *scopeThread; |
| 17 | |
| 18 | scope_t scope[PAULA_VOICES]; // global |
| 19 | |
| 20 | // this is quite hackish, but fixes sample swapping issues |
| 21 | static int32_t getSampleSlotFromReadAddress(const int8_t *sampleReadAddress) |
| 22 | { |
| 23 | ASSERT(song != NULL); |
| 24 | const int8_t *sampleData = song->sampleData; |
| 25 | const int32_t sampleSlotSize = config.maxSampleLength; |
| 26 | |
| 27 | if (sampleData == NULL) // shouldn't really happen, but just in case |
| 28 | return -1; |
| 29 | |
| 30 | int32_t sampleSlot = 30; // start at last slot |
| 31 | |
| 32 | const int8_t *sampleBaseAddress = &sampleData[sampleSlot * sampleSlotSize]; |
| 33 | if (sampleReadAddress == NULL || 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_t getSampleReadPos(int32_t ch) // used for the sampler screen |
| 48 | { |
| 49 | // cache some stuff |
| 50 | const scope_t *sc = &scope[ch]; |
| 51 | const bool active = sc->active; |
| 52 | const int8_t *data = sc->data; |
| 53 | const int32_t pos = sc->pos; |
| 54 | const int32_t len = sc->length; |
| 55 | |
| 56 | if (song == NULL || !active || data == NULL) |
| 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_t sample = getSampleSlotFromReadAddress(data); |
| 65 | if (sample != editor.currSample) |
| 66 | return -1; // sample is not the one we're seeing in the sampler screen |
| 67 | |
| 68 | const moduleSample_t *s = &song->samples[sample]; |
| 69 | const int8_t *sampleReadAddress = &data[pos]; |
| 70 | const int8_t *sampleBaseAddress = &song->sampleData[s->offset]; |
| 71 | const int32_t realPos = (int32_t)(sampleReadAddress - sampleBaseAddress); |
| 72 | |
| 73 | // return -1 if sample has no loop and read length is 2 (playing sample "loop" area) |
| 74 | const bool loopEnabled = (s->loopStart + s->loopLength) > 2; |
| 75 | if (!loopEnabled && len == 2) |
| 76 | return -1; |
| 77 | |
| 78 | if (realPos < 0 || realPos >= s->length) |
| 79 | return -1; |
| 80 | |
| 81 | return realPos; |
| 82 | } |
| 83 | |
| 84 | void scopeSetPeriod(int32_t ch, int32_t period) |
| 85 | { |
| 86 | period &= 0xFFFF; |
| 87 | |
| 88 | if (period < 113) // just in case, should already be clamped |
| 89 | period = 113; |
| 90 | |
| 91 | scope[ch].dDelta = (PAULA_PAL_CLK / (double)SCOPE_HZ) / period; |
| 92 | } |
| 93 | |
| 94 | void scopeTrigger(int32_t ch) // expects data & length variables to be set already |
| 95 | { |
| 96 | volatile scope_t *sc = &scope[ch]; |
| 97 | scope_t tempState = *sc; // cache it |
| 98 | |
| 99 | if (tempState.data == NULL) |
| 100 | tempState.data = paulaGetNullSamplePtr(); |
| 101 | |
| 102 | if (tempState.length < 2) |
| 103 | tempState.length = 2; // for safety |
| 104 | |
| 105 | tempState.dPhase = 0.0; |
| 106 | tempState.pos = 0; |
| 107 | tempState.active = true; |
| 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 updateScopes(void) |
| 119 | { |
| 120 | scope_t tempState; |
| 121 | |
| 122 | if (editor.mod2WavOngoing) |
| 123 | return; |
| 124 | |
| 125 | volatile scope_t *sc = scope; |
| 126 | |
| 127 | scopesUpdatingFlag = true; |
| 128 | for (int32_t i = 0; i < PAULA_VOICES; i++, sc++) |
| 129 | { |
| 130 | tempState = *sc; // cache it |
| 131 | if (!tempState.active) |
| 132 | continue; // scope is not active |
| 133 | |
| 134 | tempState.dPhase += tempState.dDelta; |
| 135 | |
| 136 | const int32_t wholeSamples = (int32_t)tempState.dPhase; |
| 137 | tempState.dPhase -= wholeSamples; |
| 138 | tempState.pos += wholeSamples; |
| 139 | |
| 140 | if (tempState.pos >= tempState.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.pos -= tempState.length; |
| 148 | |
| 149 | tempState.length = tempState.newLength; |
| 150 | if (tempState.pos >= tempState.length && tempState.length > 0) |
| 151 | tempState.pos %= tempState.length; |
| 152 | |
| 153 | tempState.data = tempState.newData; |
| 154 | } |
| 155 | |
| 156 | *sc = tempState; // update scope state |
| 157 | } |
| 158 | scopesUpdatingFlag = false; |
| 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 updateRealVuMeters(void) |
| 166 | { |
| 167 | scope_t tmpScope, *sc; |
| 168 | |
| 169 | // sink VU-meters first |
| 170 | for (int32_t i = 0; i < PAULA_VOICES; i++) |
| 171 | { |
| 172 | editor.realVuMeterVolumes[i] -= 4; |
| 173 | if (editor.realVuMeterVolumes[i] < 0) |
| 174 | editor.realVuMeterVolumes[i] = 0; |
| 175 | } |
| 176 | |
| 177 | // get peak sample data from running scope voices |
| 178 | sc = scope; |
| 179 | for (int32_t i = 0; i < PAULA_VOICES; i++, sc++) |
| 180 | { |
| 181 | tmpScope = *sc; // cache it |
| 182 | |
| 183 | if (!tmpScope.active || tmpScope.data == NULL || tmpScope.volume == 0 || tmpScope.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_t samplesToScan = (uint32_t)tmpScope.dDelta; |
| 188 | if (samplesToScan == 0) |
| 189 | continue; |
| 190 | |
| 191 | if (samplesToScan > 512) |
| 192 | samplesToScan = 512; |
| 193 | |
| 194 | int32_t pos = tmpScope.pos; |
| 195 | int32_t length = tmpScope.length; |
| 196 | const int8_t *data = tmpScope.data; |
| 197 | |
| 198 | uint32_t runningAmplitude = 0; |
| 199 | for (uint32_t x = 0; x < samplesToScan; x++) |
| 200 | { |
| 201 | int32_t amplitude = 0; |
| 202 | if (data != NULL) |
| 203 | amplitude = data[pos] * tmpScope.volume; |
| 204 | |
| 205 | runningAmplitude += ABS(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.newData; |
| 215 | length = tmpScope.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_t vuHeight = (int8_t)runningAmplitude; |
| 227 | if (vuHeight > editor.realVuMeterVolumes[i]) |
| 228 | editor.realVuMeterVolumes[i] = vuHeight; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void drawScopes(void) |
| 233 | { |
| 234 | volatile scope_t *sc = scope; // cache it |
| 235 | int32_t scopeX = 128; |
| 236 | |
| 237 | const uint32_t bgColor = video.palette[PAL_BACKGRD]; |
| 238 | const uint32_t fgColor = video.palette[PAL_QADSCP]; |
| 239 | |
| 240 | scopesDisplayingFlag = true; |
| 241 | for (int32_t i = 0; i < PAULA_VOICES; i++, sc++) |
| 242 | { |
| 243 | scope_t tmpScope = *sc; // cache it |
| 244 | |
| 245 | // clear scope background |
| 246 | fillRect(scopeX, 55, SCOPE_WIDTH, SCOPE_HEIGHT, bgColor); |
| 247 | |
| 248 | // render scope |
| 249 | if (tmpScope.active && tmpScope.data != NULL && tmpScope.volume != 0 && tmpScope.length > 0) |
| 250 | { |
| 251 | // render scope data |
| 252 | int16_t scopeData; |
| 253 | int32_t pos = tmpScope.pos; |
| 254 | int32_t length = tmpScope.length; |
| 255 | const int8_t volume = -(tmpScope.volume >> 1); |
| 256 | const int8_t *data = tmpScope.data; |
| 257 | uint32_t *scopeDrawPtr = &video.frameBuffer[(71 * SCREEN_W) + scopeX]; |
| 258 | |
| 259 | for (int32_t x = 0; x < SCOPE_WIDTH; x++) |
| 260 | { |
| 261 | scopeData = 0; |
| 262 | if (data != NULL) |
| 263 | scopeData = (data[pos] * volume) >> 8; |
| 264 | |
| 265 | scopeDrawPtr[(scopeData * SCREEN_W) + 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.newLength; |
| 274 | data = tmpScope.newData; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | // draw centered scope line |
| 281 | hLine(scopeX, 71, SCOPE_WIDTH, fgColor); |
| 282 | } |
| 283 | |
| 284 | scopeX += SCOPE_WIDTH+8; |
| 285 | } |
| 286 | scopesDisplayingFlag = false; |
| 287 | } |
| 288 | |
| 289 | static int32_t scopeThreadFunc(void *ptr) |
| 290 | { |
| 291 | // this is confirmed to be needed for scope stability |
| 292 | SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); |
| 293 | |
| 294 | hpc_SetDurationInHz(&scopeHpc, SCOPE_HZ); |
| 295 | hpc_ResetCounters(&scopeHpc); |
| 296 | |
| 297 | while (editor.programRunning) |
| 298 | { |
| 299 | if (config.realVuMeters) |
| 300 | updateRealVuMeters(); |
| 301 | |
| 302 | updateScopes(); |
| 303 | |
| 304 | hpc_Wait(&scopeHpc); |
| 305 | } |
| 306 | |
| 307 | (void)ptr; |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | bool initScopes(void) |
| 312 | { |
| 313 | scopeThread = SDL_CreateThread(scopeThreadFunc, "scope thread", NULL); |
| 314 | if (scopeThread == NULL) |
| 315 | { |
| 316 | showErrorMsgBox("Couldn't create scope thread!"); |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | SDL_DetachThread(scopeThread); |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | void stopScope(int32_t ch) |
| 325 | { |
| 326 | // wait for scopes to finish updating |
| 327 | while (scopesUpdatingFlag); |
| 328 | |
| 329 | scope[ch].active = false; |
| 330 | |
| 331 | // wait for scope displaying to be done (safety) |
| 332 | while (scopesDisplayingFlag); |
| 333 | } |
| 334 | |
| 335 | void stopAllScopes(void) |
| 336 | { |
| 337 | // wait for scopes to finish updating |
| 338 | while (scopesUpdatingFlag); |
| 339 | |
| 340 | for (int32_t i = 0; i < PAULA_VOICES; i++) |
| 341 | scope[i].active = false; |
| 342 | |
| 343 | // wait for scope displaying to be done (safety) |
| 344 | while (scopesDisplayingFlag); |
| 345 | } |