| 1 | // used for syncing audio from Paula writes to tracker visuals |
| 2 | |
| 3 | #include <math.h> |
| 4 | #include <stdint.h> |
| 5 | #include <stdbool.h> |
| 6 | #include "pt2_audio.h" |
| 7 | #include "pt2_visuals_sync.h" |
| 8 | #include "pt2_scopes.h" |
| 9 | #include "pt2_visuals.h" |
| 10 | #include "pt2_tables.h" |
| 11 | |
| 12 | typedef struct syncVoice_t |
| 13 | { |
| 14 | const int8_t *newData, *data; |
| 15 | uint8_t flags, volume; |
| 16 | uint16_t period; |
| 17 | uint16_t newLength, length; |
| 18 | } syncVoice_t; |
| 19 | |
| 20 | static volatile bool chQueueClearing; |
| 21 | static uint32_t audLatencyPerfValInt; |
| 22 | static uint64_t audLatencyPerfValFrac; |
| 23 | static uint64_t tickTime64, tickTime64Frac; |
| 24 | static uint32_t tickTimeLenInt; |
| 25 | static uint64_t tickTimeLenFrac; |
| 26 | static syncVoice_t syncVoice[PAULA_VOICES]; |
| 27 | static chSyncData_t *chSyncEntry; |
| 28 | static chSync_t chSync; |
| 29 | |
| 30 | static void startDMA(int32_t ch) |
| 31 | { |
| 32 | syncVoice_t *sv = &syncVoice[ch]; |
| 33 | |
| 34 | if (editor.songPlaying) |
| 35 | { |
| 36 | sv->data = sv->newData; |
| 37 | sv->length = sv->newLength; |
| 38 | sv->flags |= TRIGGER_SCOPE; |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | scope_t *s = &scope[ch]; |
| 43 | s->data = sv->newData; |
| 44 | s->length = sv->newLength * 2; |
| 45 | scopeTrigger(ch); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static void stopDMA(int32_t ch) |
| 50 | { |
| 51 | if (editor.songPlaying) |
| 52 | syncVoice[ch].flags |= STOP_SCOPE; |
| 53 | else |
| 54 | scope[ch].active = false; |
| 55 | } |
| 56 | |
| 57 | void setVisualsDMACON(uint16_t bits) |
| 58 | { |
| 59 | if (bits & 0x8000) |
| 60 | { |
| 61 | // set |
| 62 | if (bits & 1) startDMA(0); |
| 63 | if (bits & 2) startDMA(1); |
| 64 | if (bits & 4) startDMA(2); |
| 65 | if (bits & 8) startDMA(3); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | // clear |
| 70 | if (bits & 1) stopDMA(0); |
| 71 | if (bits & 2) stopDMA(1); |
| 72 | if (bits & 4) stopDMA(2); |
| 73 | if (bits & 8) stopDMA(3); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void setVisualsVolume(int32_t ch, uint16_t vol) |
| 78 | { |
| 79 | int32_t realVol = vol & 127; |
| 80 | if (realVol > 64) |
| 81 | realVol = 64; |
| 82 | |
| 83 | if (editor.songPlaying) |
| 84 | { |
| 85 | syncVoice_t *sv = &syncVoice[ch]; |
| 86 | |
| 87 | sv->volume = (uint8_t)realVol; |
| 88 | sv->flags |= SET_SCOPE_VOLUME; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | scope[ch].volume = (uint8_t)realVol; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void setVisualsPeriod(int32_t ch, uint16_t period) |
| 97 | { |
| 98 | if (period == 0) |
| 99 | period = 65535; // On Amiga: period 0 = one full cycle with period 65536, then period 65535 for the rest |
| 100 | else if (period < 113) |
| 101 | period = 113; // close to what happens on real Amiga (and needed for BLEP synthesis) |
| 102 | |
| 103 | if (editor.songPlaying) |
| 104 | { |
| 105 | syncVoice_t *sv = &syncVoice[ch]; |
| 106 | |
| 107 | sv->period = period; |
| 108 | sv->flags |= SET_SCOPE_PERIOD; |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | scopeSetPeriod(ch, period); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void setVisualsLength(int32_t ch, uint16_t len) |
| 117 | { |
| 118 | syncVoice_t *sv = &syncVoice[ch]; |
| 119 | |
| 120 | sv->newLength = len; |
| 121 | |
| 122 | if (editor.songPlaying) |
| 123 | sv->flags |= SET_SCOPE_LENGTH; |
| 124 | else |
| 125 | scope[ch].newLength = len * 2; |
| 126 | } |
| 127 | |
| 128 | void setVisualsDataPtr(int32_t ch, const int8_t *src) |
| 129 | { |
| 130 | syncVoice_t *sv = &syncVoice[ch]; |
| 131 | |
| 132 | if (src == NULL) |
| 133 | src = paulaGetNullSamplePtr(); |
| 134 | |
| 135 | sv->newData = src; |
| 136 | |
| 137 | if (editor.songPlaying) |
| 138 | sv->flags |= SET_SCOPE_DATA; |
| 139 | else |
| 140 | scope[ch].newData = src; |
| 141 | } |
| 142 | |
| 143 | void calcAudioLatencyVars(int32_t audioBufferSize, int32_t audioFreq) |
| 144 | { |
| 145 | if (audioFreq == 0) |
| 146 | return; |
| 147 | |
| 148 | const double dAudioLatencyTime = (audioBufferSize / (double)audioFreq) * (double)hpcFreq.freq64; |
| 149 | double dAudioLatencyTimeInt, dAudioLatencyTimeFrac = modf(dAudioLatencyTime, &dAudioLatencyTimeInt); |
| 150 | |
| 151 | audLatencyPerfValInt = (uint32_t)dAudioLatencyTimeInt; |
| 152 | audLatencyPerfValFrac = (uint64_t)(dAudioLatencyTimeFrac * TICK_TIME_FRAC_SCALE); |
| 153 | } |
| 154 | |
| 155 | void setSyncTickTimeLen(uint32_t timeLenInt, uint64_t timeLenFrac) |
| 156 | { |
| 157 | tickTimeLenInt = timeLenInt; |
| 158 | tickTimeLenFrac = timeLenFrac; |
| 159 | } |
| 160 | |
| 161 | void resetChSyncQueue(void) |
| 162 | { |
| 163 | chSync.data[0].timestamp = 0; |
| 164 | chSync.writePos = 0; |
| 165 | chSync.readPos = 0; |
| 166 | } |
| 167 | |
| 168 | static int32_t chQueueReadSize(void) |
| 169 | { |
| 170 | while (chQueueClearing); |
| 171 | |
| 172 | if (chSync.writePos > chSync.readPos) |
| 173 | return chSync.writePos - chSync.readPos; |
| 174 | else if (chSync.writePos < chSync.readPos) |
| 175 | return chSync.writePos - chSync.readPos + SYNC_QUEUE_LEN + 1; |
| 176 | else |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int32_t chQueueWriteSize(void) |
| 181 | { |
| 182 | int32_t size; |
| 183 | |
| 184 | if (chSync.writePos > chSync.readPos) |
| 185 | { |
| 186 | size = chSync.readPos - chSync.writePos + SYNC_QUEUE_LEN; |
| 187 | } |
| 188 | else if (chSync.writePos < chSync.readPos) |
| 189 | { |
| 190 | chQueueClearing = true; |
| 191 | |
| 192 | /* Buffer is full, reset the read/write pos. This is actually really nasty since |
| 193 | ** read/write are two different threads, but because of timestamp validation it |
| 194 | ** shouldn't be that dangerous. |
| 195 | ** It will also create a small visual stutter while the buffer is getting filled, |
| 196 | ** though that is barely noticable on normal buffer sizes, and it takes a minute |
| 197 | ** or two at max BPM between each time (when queue size is default, 8191) |
| 198 | */ |
| 199 | chSync.data[0].timestamp = 0; |
| 200 | chSync.readPos = 0; |
| 201 | chSync.writePos = 0; |
| 202 | |
| 203 | size = SYNC_QUEUE_LEN; |
| 204 | |
| 205 | chQueueClearing = false; |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | size = SYNC_QUEUE_LEN; |
| 210 | } |
| 211 | |
| 212 | return size; |
| 213 | } |
| 214 | |
| 215 | bool chQueuePush(chSyncData_t t) |
| 216 | { |
| 217 | if (!chQueueWriteSize()) |
| 218 | return false; |
| 219 | |
| 220 | ASSERT(chSync.writePos <= SYNC_QUEUE_LEN); |
| 221 | chSync.data[chSync.writePos] = t; |
| 222 | chSync.writePos = (chSync.writePos + 1) & SYNC_QUEUE_LEN; |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | static bool chQueuePop(void) |
| 228 | { |
| 229 | if (!chQueueReadSize()) |
| 230 | return false; |
| 231 | |
| 232 | chSync.readPos = (chSync.readPos + 1) & SYNC_QUEUE_LEN; |
| 233 | ASSERT(chSync.readPos <= SYNC_QUEUE_LEN); |
| 234 | |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | static chSyncData_t *chQueuePeek(void) |
| 239 | { |
| 240 | if (!chQueueReadSize()) |
| 241 | return NULL; |
| 242 | |
| 243 | ASSERT(chSync.readPos <= SYNC_QUEUE_LEN); |
| 244 | return &chSync.data[chSync.readPos]; |
| 245 | } |
| 246 | |
| 247 | static uint64_t getChQueueTimestamp(void) |
| 248 | { |
| 249 | if (!chQueueReadSize()) |
| 250 | return 0; |
| 251 | |
| 252 | ASSERT(chSync.readPos <= SYNC_QUEUE_LEN); |
| 253 | return chSync.data[chSync.readPos].timestamp; |
| 254 | } |
| 255 | |
| 256 | void fillVisualsSyncBuffer(void) |
| 257 | { |
| 258 | chSyncData_t chSyncData; |
| 259 | |
| 260 | if (audio.resetSyncTickTimeFlag) |
| 261 | { |
| 262 | audio.resetSyncTickTimeFlag = false; |
| 263 | |
| 264 | tickTime64 = SDL_GetPerformanceCounter() + audLatencyPerfValInt; |
| 265 | tickTime64Frac = audLatencyPerfValFrac; |
| 266 | } |
| 267 | |
| 268 | if (song != NULL) |
| 269 | { |
| 270 | moduleChannel_t *ch = song->channels; |
| 271 | syncVoice_t *sv = syncVoice; |
| 272 | syncedChannel_t *sc = chSyncData.channels; |
| 273 | |
| 274 | for (int32_t i = 0; i < PAULA_VOICES; i++, ch++, sc++, sv++) |
| 275 | { |
| 276 | sc->flags = sv->flags | ch->syncFlags; |
| 277 | ch->syncFlags = sv->flags = 0; // clear sync flags |
| 278 | |
| 279 | sc->volume = sv->volume; |
| 280 | sc->period = sv->period; |
| 281 | sc->data = sv->data; |
| 282 | sc->length = sv->length; |
| 283 | sc->newData = sv->newData; |
| 284 | sc->newLength = sv->newLength; |
| 285 | sc->vuVolume = ch->syncVuVolume; |
| 286 | sc->analyzerVolume = ch->syncAnalyzerVolume; |
| 287 | sc->analyzerPeriod = ch->syncAnalyzerPeriod; |
| 288 | } |
| 289 | |
| 290 | chSyncData.timestamp = tickTime64; |
| 291 | chQueuePush(chSyncData); |
| 292 | } |
| 293 | |
| 294 | tickTime64 += tickTimeLenInt; |
| 295 | |
| 296 | tickTime64Frac += tickTimeLenFrac; |
| 297 | if (tickTime64Frac >= TICK_TIME_FRAC_SCALE) |
| 298 | { |
| 299 | tickTime64Frac &= TICK_TIME_FRAC_MASK; |
| 300 | tickTime64++; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | void updateChannelSyncBuffer(void) |
| 305 | { |
| 306 | uint8_t updateFlags[PAULA_VOICES]; |
| 307 | |
| 308 | chSyncEntry = NULL; |
| 309 | |
| 310 | *(uint32_t *)updateFlags = 0; // clear all channel update flags (this is needed) |
| 311 | |
| 312 | const uint64_t frameTime64 = SDL_GetPerformanceCounter(); |
| 313 | |
| 314 | // handle channel sync queue |
| 315 | |
| 316 | while (chQueueClearing); |
| 317 | while (chQueueReadSize() > 0) |
| 318 | { |
| 319 | if (frameTime64 < getChQueueTimestamp()) |
| 320 | break; // we have no more stuff to render for now |
| 321 | |
| 322 | chSyncEntry = chQueuePeek(); |
| 323 | if (chSyncEntry == NULL) |
| 324 | break; |
| 325 | |
| 326 | for (int32_t i = 0; i < PAULA_VOICES; i++) |
| 327 | updateFlags[i] |= chSyncEntry->channels[i].flags; // yes, OR the status |
| 328 | |
| 329 | if (!chQueuePop()) |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | /* Extra validation because of possible issues when the buffer is full |
| 334 | ** and positions are being reset, which is not entirely thread safe. |
| 335 | */ |
| 336 | if (chSyncEntry != NULL && chSyncEntry->timestamp == 0) |
| 337 | chSyncEntry = NULL; |
| 338 | |
| 339 | // do actual updates |
| 340 | if (chSyncEntry != NULL) |
| 341 | { |
| 342 | scope_t *s = scope; |
| 343 | syncedChannel_t *c = chSyncEntry->channels; |
| 344 | for (int32_t ch = 0; ch < PAULA_VOICES; ch++, s++, c++) |
| 345 | { |
| 346 | const uint8_t flags = updateFlags[ch]; |
| 347 | if (flags == 0) |
| 348 | continue; |
| 349 | |
| 350 | if (flags & SET_SCOPE_VOLUME) |
| 351 | scope[ch].volume = c->volume; |
| 352 | |
| 353 | if (flags & SET_SCOPE_PERIOD) |
| 354 | scopeSetPeriod(ch, c->period); |
| 355 | |
| 356 | // the following handling order is important, don't change it! |
| 357 | |
| 358 | if (flags & STOP_SCOPE) |
| 359 | scope[ch].active = false; |
| 360 | |
| 361 | if (flags & TRIGGER_SCOPE) |
| 362 | { |
| 363 | s->data = c->data; |
| 364 | s->length = c->length * 2; |
| 365 | scopeTrigger(ch); |
| 366 | } |
| 367 | |
| 368 | if (flags & SET_SCOPE_DATA) |
| 369 | scope[ch].newData = c->newData; |
| 370 | |
| 371 | if (flags & SET_SCOPE_LENGTH) |
| 372 | scope[ch].newLength = c->newLength * 2; |
| 373 | |
| 374 | // --------------------------------------------------------------- |
| 375 | |
| 376 | if (flags & UPDATE_SPECTRUM_ANALYZER) |
| 377 | updateSpectrumAnalyzer(c->analyzerVolume, c ->analyzerPeriod); |
| 378 | |
| 379 | if (flags & UPDATE_VUMETER) // for fake VU-meters only |
| 380 | { |
| 381 | if (c->vuVolume <= 64) |
| 382 | editor.vuMeterVolumes[ch] = vuMeterHeights[c->vuVolume]; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |