| 1 | // C port of ProTracker 2.3D's replayer (with some modifications, but still accurate) |
| 2 | |
| 3 | // for finding memory leaks in debug mode with Visual Studio |
| 4 | #if defined _DEBUG && defined _MSC_VER |
| 5 | #include <crtdbg.h> |
| 6 | #endif |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | #include <stdbool.h> |
| 10 | #include <string.h> |
| 11 | #include "pt2_audio.h" |
| 12 | #include "pt2_helpers.h" |
| 13 | #include "pt2_tables.h" |
| 14 | #include "pt2_config.h" |
| 15 | #include "pt2_visuals.h" |
| 16 | #include "pt2_scopes.h" |
| 17 | #include "pt2_paula.h" |
| 18 | #include "pt2_visuals_sync.h" |
| 19 | #include "pt2_posed.h" |
| 20 | |
| 21 | static bool posJumpAssert, pBreakFlag, modRenderDone; |
| 22 | static bool doStopSong; // from F00 (Set Speed) |
| 23 | static int8_t pBreakPosition, oldRow, modPattern; |
| 24 | static uint8_t pattDelTime, lowMask = 0xFF, pattDelTime2; |
| 25 | static int16_t modPos, oldPattern, oldPos; |
| 26 | static uint16_t DMACONtemp; |
| 27 | static int32_t modBPM, oldBPM, oldSpeed, ciaSetBPM; |
| 28 | |
| 29 | static const uint8_t funkTable[16] = // EFx (FunkRepeat/InvertLoop) |
| 30 | { |
| 31 | 0x00, 0x05, 0x06, 0x07, 0x08, 0x0A, 0x0B, 0x0D, |
| 32 | 0x10, 0x13, 0x16, 0x1A, 0x20, 0x2B, 0x40, 0x80 |
| 33 | }; |
| 34 | |
| 35 | void gotoNextMulti(void) |
| 36 | { |
| 37 | cursor.channel = (editor.multiModeNext[cursor.channel] - 1) & 3; |
| 38 | cursor.pos = cursor.channel * 6; |
| 39 | updateCursorPos(); |
| 40 | } |
| 41 | |
| 42 | double ciaBpm2Hz(int32_t bpm) |
| 43 | { |
| 44 | if (bpm == 0) |
| 45 | return 0.0; |
| 46 | |
| 47 | const uint32_t ciaPeriod = 1773447 / bpm; // yes, PT truncates here |
| 48 | return (double)CIA_PAL_CLK / (ciaPeriod+1); // +1, CIA triggers on underflow |
| 49 | } |
| 50 | |
| 51 | void updatePaulaLoops(void) // used after manipulating sample loop points while Paula is live |
| 52 | { |
| 53 | moduleSample_t *s = &song->samples[editor.currSample]; |
| 54 | |
| 55 | const bool audioWasntLocked = !audio.locked; |
| 56 | if (audioWasntLocked) |
| 57 | lockAudio(); |
| 58 | |
| 59 | moduleChannel_t *ch = song->channels; |
| 60 | for (uint32_t i = 0; i < PAULA_VOICES; i++, ch++) |
| 61 | { |
| 62 | if (ch->n_samplenum == editor.currSample && ch->n_start != NULL) |
| 63 | { |
| 64 | const uint32_t voiceAddr = 0xDFF0A0 + (i * 16); |
| 65 | |
| 66 | // update replayer vars |
| 67 | ch->n_loopstart = ch->n_start + s->loopStart; |
| 68 | ch->n_replen = (uint16_t)(s->loopLength >> 1); |
| 69 | ch->n_wavestart = ch->n_loopstart; |
| 70 | |
| 71 | // set Paula DAT and LEN (for next cycle) |
| 72 | paulaWritePtr(voiceAddr + 0, ch->n_loopstart); |
| 73 | paulaWriteWord(voiceAddr + 4, ch->n_replen); |
| 74 | |
| 75 | // ... and update quadrascope as well |
| 76 | setVisualsDataPtr(i, ch->n_loopstart); |
| 77 | setVisualsLength(i, ch->n_replen); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (audioWasntLocked) |
| 82 | unlockAudio(); |
| 83 | } |
| 84 | |
| 85 | void turnOffVoices(void) |
| 86 | { |
| 87 | const bool audioWasntLocked = !audio.locked; |
| 88 | if (audioWasntLocked) |
| 89 | lockAudio(); |
| 90 | |
| 91 | paulaWriteWord(0xDFF096, 0x000F); // turn off all voice DMAs |
| 92 | setVisualsDMACON(0x000F); |
| 93 | |
| 94 | // clear all volumes |
| 95 | for (int32_t i = 0; i < PAULA_VOICES; i++) |
| 96 | { |
| 97 | const uint32_t voiceAddr = 0xDFF0A0 + (i * 16); |
| 98 | paulaWriteWord(voiceAddr + 8, 0); |
| 99 | |
| 100 | setVisualsVolume(i, 0); |
| 101 | } |
| 102 | |
| 103 | resetAudioDither(); |
| 104 | |
| 105 | editor.tuningToneFlag = false; |
| 106 | |
| 107 | if (audioWasntLocked) |
| 108 | unlockAudio(); |
| 109 | } |
| 110 | |
| 111 | void initializeModuleChannels(module_t *m) |
| 112 | { |
| 113 | ASSERT(m != NULL); |
| 114 | |
| 115 | memset(m->channels, 0, sizeof (m->channels)); |
| 116 | |
| 117 | moduleChannel_t *ch = m->channels; |
| 118 | for (uint8_t i = 0; i < PAULA_VOICES; i++, ch++) |
| 119 | { |
| 120 | ch->n_chanindex = i; |
| 121 | ch->n_dmabit = 1 << i; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void setReplayerPosToTrackerPos(void) |
| 126 | { |
| 127 | if (song == NULL) |
| 128 | return; |
| 129 | |
| 130 | modPattern = (int8_t)song->currPattern; |
| 131 | modPos = song->currPos; |
| 132 | song->row = song->currRow; |
| 133 | song->tick = 0; |
| 134 | } |
| 135 | |
| 136 | module_t *createEmptyMod(void) |
| 137 | { |
| 138 | module_t *m = (module_t *)calloc(1, sizeof (module_t)); |
| 139 | if (m == NULL) |
| 140 | goto error; |
| 141 | |
| 142 | // allocate memory for all sample data blocks (+ 2 extra, for quirk + safety) |
| 143 | const size_t allocLen = (MOD_SAMPLES + 2) * config.maxSampleLength; |
| 144 | |
| 145 | m->sampleData = (int8_t *)calloc(allocLen, 1); |
| 146 | if (m->sampleData == NULL) |
| 147 | goto error; |
| 148 | |
| 149 | for (int32_t i = 0; i < MAX_PATTERNS; i++) |
| 150 | { |
| 151 | m->patterns[i] = (note_t *)calloc(1, MOD_ROWS * sizeof (note_t) * PAULA_VOICES); |
| 152 | if (m->patterns[i] == NULL) |
| 153 | goto error; |
| 154 | } |
| 155 | |
| 156 | m->header.songLength = 1; |
| 157 | |
| 158 | moduleSample_t *s = m->samples; |
| 159 | for (int32_t i = 0; i < MOD_SAMPLES; i++, s++) |
| 160 | { |
| 161 | // setup GUI text pointers |
| 162 | s->volumeDisp = &s->volume; |
| 163 | s->lengthDisp = &s->length; |
| 164 | s->loopStartDisp = &s->loopStart; |
| 165 | s->loopLengthDisp = &s->loopLength; |
| 166 | |
| 167 | s->loopLength = 2; |
| 168 | |
| 169 | // sample data offsets (sample data = one huge buffer to rule them all) |
| 170 | s->offset = config.maxSampleLength * i; |
| 171 | } |
| 172 | |
| 173 | initializeModuleChannels(m); |
| 174 | |
| 175 | return m; |
| 176 | |
| 177 | error: |
| 178 | if (m != NULL) |
| 179 | { |
| 180 | for (int32_t i = 0; i < MAX_PATTERNS; i++) |
| 181 | { |
| 182 | if (m->patterns[i] != NULL) |
| 183 | free(m->patterns[i]); |
| 184 | } |
| 185 | |
| 186 | if (m->sampleData != NULL) |
| 187 | free(m->sampleData); |
| 188 | |
| 189 | free(m); |
| 190 | } |
| 191 | |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | void modSetSpeed(int32_t speed) |
| 196 | { |
| 197 | song->currSpeed = song->speed = speed; |
| 198 | song->tick = 0; |
| 199 | } |
| 200 | |
| 201 | void doStopIt(bool resetPlayMode) |
| 202 | { |
| 203 | const bool audioWasntLocked = !audio.locked; |
| 204 | if (audioWasntLocked) |
| 205 | lockAudio(); |
| 206 | |
| 207 | editor.songPlaying = false; |
| 208 | |
| 209 | pattDelTime = pattDelTime2 = 0; |
| 210 | |
| 211 | if (resetPlayMode) |
| 212 | { |
| 213 | editor.playMode = PLAY_MODE_NORMAL; |
| 214 | editor.currMode = MODE_IDLE; |
| 215 | |
| 216 | if (editor.stepPlayLastMode != MODE_IDLE) |
| 217 | pointerSetModeThreadSafe(POINTER_MODE_IDLE, true); |
| 218 | } |
| 219 | |
| 220 | if (song != NULL) |
| 221 | { |
| 222 | moduleChannel_t *ch = song->channels; |
| 223 | for (int32_t i = 0; i < PAULA_VOICES; i++, ch++) |
| 224 | { |
| 225 | ch->n_wavecontrol = 0; |
| 226 | ch->n_glissfunk = 0; |
| 227 | ch->n_finetune = 0; |
| 228 | ch->n_loopcount = 0; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | doStopSong = false; // just in case this flag was stuck from command F00 (stop song) |
| 233 | |
| 234 | if (audioWasntLocked) |
| 235 | unlockAudio(); |
| 236 | } |
| 237 | |
| 238 | void setPattern(int16_t pattern) |
| 239 | { |
| 240 | if (pattern > MAX_PATTERNS-1) |
| 241 | pattern = MAX_PATTERNS-1; |
| 242 | |
| 243 | song->currPattern = modPattern = (int8_t)pattern; |
| 244 | } |
| 245 | |
| 246 | void storeTempVariables(void) // this one is accessed in other files, so non-static |
| 247 | { |
| 248 | oldBPM = song->currBPM; |
| 249 | oldRow = song->currRow; |
| 250 | oldPos = song->currPos; |
| 251 | oldSpeed = song->currSpeed; |
| 252 | oldPattern = song->currPattern; |
| 253 | } |
| 254 | |
| 255 | static void setVUMeterHeight(moduleChannel_t *ch) |
| 256 | { |
| 257 | if (editor.muted[ch->n_chanindex]) |
| 258 | return; |
| 259 | |
| 260 | uint8_t vol = ch->n_volume; |
| 261 | if ((ch->n_cmd & 0xF00) == 0xC00) // handle Cxx effect |
| 262 | vol = ch->n_cmd & 0xFF; |
| 263 | |
| 264 | if (vol > 64) |
| 265 | vol = 64; |
| 266 | |
| 267 | if (!editor.songPlaying) |
| 268 | { |
| 269 | editor.vuMeterVolumes[ch->n_chanindex] = vuMeterHeights[vol]; |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | ch->syncVuVolume = vol; |
| 274 | ch->syncFlags |= UPDATE_VUMETER; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | static void updateFunk(moduleChannel_t *ch) |
| 279 | { |
| 280 | const int8_t funkSpeed = ch->n_glissfunk >> 4; |
| 281 | if (funkSpeed == 0) |
| 282 | return; |
| 283 | |
| 284 | ch->n_funkoffset += funkTable[funkSpeed]; |
| 285 | if (ch->n_funkoffset >= 128) |
| 286 | { |
| 287 | ch->n_funkoffset = 0; |
| 288 | |
| 289 | if (ch->n_loopstart != NULL && ch->n_wavestart != NULL) // ProTracker bugfix |
| 290 | { |
| 291 | if (++ch->n_wavestart >= ch->n_loopstart + (ch->n_replen << 1)) |
| 292 | ch->n_wavestart = ch->n_loopstart; |
| 293 | |
| 294 | *ch->n_wavestart = -1 - *ch->n_wavestart; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | static void setGlissControl(moduleChannel_t *ch) |
| 300 | { |
| 301 | ch->n_glissfunk = (ch->n_glissfunk & 0xF0) | (ch->n_cmd & 0x0F); |
| 302 | } |
| 303 | |
| 304 | static void setVibratoControl(moduleChannel_t *ch) |
| 305 | { |
| 306 | ch->n_wavecontrol = (ch->n_wavecontrol & 0xF0) | (ch->n_cmd & 0x0F); |
| 307 | } |
| 308 | |
| 309 | static void setFineTune(moduleChannel_t *ch) |
| 310 | { |
| 311 | ch->n_finetune = ch->n_cmd & 0xF; |
| 312 | } |
| 313 | |
| 314 | static void jumpLoop(moduleChannel_t *ch) |
| 315 | { |
| 316 | if (song->tick != 0) |
| 317 | return; |
| 318 | |
| 319 | if ((ch->n_cmd & 0xF) == 0) |
| 320 | { |
| 321 | ch->n_pattpos = song->row; |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | if (ch->n_loopcount == 0) |
| 326 | ch->n_loopcount = ch->n_cmd & 0xF; |
| 327 | else if (--ch->n_loopcount == 0) |
| 328 | return; |
| 329 | |
| 330 | pBreakPosition = ch->n_pattpos; |
| 331 | pBreakFlag = true; |
| 332 | |
| 333 | // stuff used for MOD2WAV to determine if the song has reached its end |
| 334 | if (editor.mod2WavOngoing) |
| 335 | { |
| 336 | for (int32_t tempParam = pBreakPosition; tempParam <= song->row; tempParam++) |
| 337 | editor.rowVisitTable[(modPos * MOD_ROWS) + tempParam] = false; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | static void setTremoloControl(moduleChannel_t *ch) |
| 343 | { |
| 344 | ch->n_wavecontrol = ((ch->n_cmd & 0xF) << 4) | (ch->n_wavecontrol & 0xF); |
| 345 | } |
| 346 | |
| 347 | /* This is the least used (and least known) ProTracker effect there is. It's not even |
| 348 | ** documented in the official ProTracker help text... |
| 349 | ** |
| 350 | ** When E8x is seen in a .mod, it is in >95% of cases used for demo effect syncing for |
| 351 | ** demo .mod players, so it is disabled by default (as this effect trashes sample data |
| 352 | * and is almost NEVER used as Karplus-Strong intentionally). |
| 353 | ** It can be turned on through ENABLE_E8X in protracker.ini if you so desire. |
| 354 | */ |
| 355 | static void karplusStrong(moduleChannel_t *ch) // E8x |
| 356 | { |
| 357 | if (!config.enableE8xEffect) |
| 358 | return; |
| 359 | |
| 360 | if (ch->n_loopstart == NULL) |
| 361 | return; // ProTracker bugfix |
| 362 | |
| 363 | // yes, this means that it's buggy for >64kB loops! |
| 364 | uint16_t end = (uint16_t)((ch->n_replen * 2) - 2); |
| 365 | |
| 366 | int8_t *loopStartPtr = ch->n_loopstart; |
| 367 | for (int32_t i = 0; i <= end; i++) |
| 368 | loopStartPtr[i] = (loopStartPtr[i+0] + loopStartPtr[i+1]) >> 1; |
| 369 | |
| 370 | loopStartPtr[end+1] = (loopStartPtr[end+1] + loopStartPtr[0]) >> 1; |
| 371 | } |
| 372 | |
| 373 | static void doRetrg(moduleChannel_t *ch) |
| 374 | { |
| 375 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 376 | |
| 377 | // voice DMA off |
| 378 | paulaWriteWord(0xDFF096, ch->n_dmabit); |
| 379 | |
| 380 | // set voice data ptr, data length and period |
| 381 | paulaWritePtr(voiceAddr + 0, ch->n_start); // n_start is increased on 9xx |
| 382 | paulaWriteWord(voiceAddr + 4, ch->n_length); |
| 383 | paulaWriteWord(voiceAddr + 6, ch->n_period); |
| 384 | |
| 385 | // voice DMA on |
| 386 | paulaWriteWord(0xDFF096, 0x8000 | ch->n_dmabit); |
| 387 | |
| 388 | // set new data ptr and data length (these take effect after the current DMA cycle is done) |
| 389 | paulaWritePtr(voiceAddr + 0, ch->n_loopstart); |
| 390 | paulaWriteWord(voiceAddr + 4, ch->n_replen); |
| 391 | |
| 392 | // update tracker visuals |
| 393 | setVisualsDMACON(ch->n_dmabit); |
| 394 | setVisualsDataPtr(ch->n_chanindex, ch->n_start); |
| 395 | setVisualsLength(ch->n_chanindex, ch->n_length); |
| 396 | setVisualsPeriod(ch->n_chanindex, ch->n_period); |
| 397 | setVisualsDMACON(0x8000 | ch->n_dmabit); |
| 398 | setVisualsDataPtr(ch->n_chanindex, ch->n_loopstart); |
| 399 | setVisualsLength(ch->n_chanindex, ch->n_replen); |
| 400 | |
| 401 | // set spectrum analyzer state for this channel |
| 402 | ch->syncAnalyzerVolume = ch->n_volume; |
| 403 | ch->syncAnalyzerPeriod = ch->n_period; |
| 404 | ch->syncFlags |= UPDATE_SPECTRUM_ANALYZER; |
| 405 | |
| 406 | setVUMeterHeight(ch); |
| 407 | } |
| 408 | |
| 409 | static void retrigNote(moduleChannel_t *ch) |
| 410 | { |
| 411 | if ((ch->n_cmd & 0xF) > 0) |
| 412 | { |
| 413 | if (song->tick == 0 && (ch->n_note & 0xFFF) > 0) |
| 414 | return; |
| 415 | |
| 416 | if (song->tick % (ch->n_cmd & 0xF) == 0) |
| 417 | doRetrg(ch); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | static void volumeSlide(moduleChannel_t *ch) |
| 422 | { |
| 423 | uint8_t param = ch->n_cmd & 0xFF; |
| 424 | if ((param & 0xF0) == 0) |
| 425 | { |
| 426 | ch->n_volume -= param & 0x0F; |
| 427 | if (ch->n_volume < 0) |
| 428 | ch->n_volume = 0; |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | ch->n_volume += param >> 4; |
| 433 | if (ch->n_volume > 64) |
| 434 | ch->n_volume = 64; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | static void volumeFineUp(moduleChannel_t *ch) |
| 439 | { |
| 440 | if (song->tick == 0) |
| 441 | { |
| 442 | ch->n_volume += ch->n_cmd & 0xF; |
| 443 | if (ch->n_volume > 64) |
| 444 | ch->n_volume = 64; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | static void volumeFineDown(moduleChannel_t *ch) |
| 449 | { |
| 450 | if (song->tick == 0) |
| 451 | { |
| 452 | ch->n_volume -= ch->n_cmd & 0xF; |
| 453 | if (ch->n_volume < 0) |
| 454 | ch->n_volume = 0; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | static void noteCut(moduleChannel_t *ch) |
| 459 | { |
| 460 | if (song->tick == (ch->n_cmd & 0xF)) |
| 461 | ch->n_volume = 0; |
| 462 | } |
| 463 | |
| 464 | static void noteDelay(moduleChannel_t *ch) |
| 465 | { |
| 466 | if (song->tick == (ch->n_cmd & 0xF) && (ch->n_note & 0xFFF) > 0) |
| 467 | doRetrg(ch); |
| 468 | } |
| 469 | |
| 470 | static void patternDelay(moduleChannel_t *ch) |
| 471 | { |
| 472 | if (song->tick == 0 && pattDelTime2 == 0) |
| 473 | pattDelTime = (ch->n_cmd & 0xF) + 1; |
| 474 | } |
| 475 | |
| 476 | static void funkIt(moduleChannel_t *ch) |
| 477 | { |
| 478 | if (song->tick == 0) |
| 479 | { |
| 480 | ch->n_glissfunk = ((ch->n_cmd & 0xF) << 4) | (ch->n_glissfunk & 0xF); |
| 481 | |
| 482 | if ((ch->n_glissfunk & 0xF0) > 0) |
| 483 | updateFunk(ch); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | static void positionJump(moduleChannel_t *ch) |
| 488 | { |
| 489 | // original PT doesn't do this check, but we have to |
| 490 | if (editor.playMode != PLAY_MODE_PATTERN || (editor.currMode == MODE_RECORD && editor.recordMode != RECORD_PATT)) |
| 491 | modPos = (ch->n_cmd & 0xFF) - 1; // B00 results in -1, but it safely jumps to order 0 |
| 492 | |
| 493 | pBreakPosition = 0; |
| 494 | posJumpAssert = true; |
| 495 | } |
| 496 | |
| 497 | static void volumeChange(moduleChannel_t *ch) |
| 498 | { |
| 499 | ch->n_volume = ch->n_cmd & 0xFF; |
| 500 | if ((uint8_t)ch->n_volume > 64) |
| 501 | ch->n_volume = 64; |
| 502 | } |
| 503 | |
| 504 | static void patternBreak(moduleChannel_t *ch) |
| 505 | { |
| 506 | pBreakPosition = (((ch->n_cmd & 0xF0) >> 4) * 10) + (ch->n_cmd & 0x0F); |
| 507 | if ((uint8_t)pBreakPosition > 63) |
| 508 | pBreakPosition = 0; |
| 509 | |
| 510 | posJumpAssert = true; |
| 511 | } |
| 512 | |
| 513 | static void setSpeed(moduleChannel_t *ch) |
| 514 | { |
| 515 | if ((ch->n_cmd & 0xFF) > 0) |
| 516 | { |
| 517 | if (editor.timingMode == TEMPO_MODE_VBLANK || (ch->n_cmd & 0xFF) < 32) |
| 518 | modSetSpeed(ch->n_cmd & 0xFF); |
| 519 | else |
| 520 | ciaSetBPM = ch->n_cmd & 0xFF; // the CIA chip doesn't use its new timer value until the next interrupt, so change it later |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | // F00 - stop song |
| 525 | doStopSong = true; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | static void arpeggio(moduleChannel_t *ch) |
| 530 | { |
| 531 | int32_t arpNote; |
| 532 | |
| 533 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 534 | |
| 535 | int32_t arpTick = song->tick % 3; // 0, 1, 2 |
| 536 | if (arpTick == 1) |
| 537 | { |
| 538 | arpNote = ch->n_cmd >> 4; |
| 539 | } |
| 540 | else if (arpTick == 2) |
| 541 | { |
| 542 | arpNote = ch->n_cmd & 0xF; |
| 543 | } |
| 544 | else // arpTick 0 |
| 545 | { |
| 546 | // set voice period |
| 547 | paulaWriteWord(voiceAddr + 6, ch->n_period); |
| 548 | |
| 549 | setVisualsPeriod(ch->n_chanindex, ch->n_period); |
| 550 | |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | /* 8bitbubsy: If the finetune is -1, this can overflow up to |
| 555 | ** 15 words outside of the table. The table is padded with |
| 556 | ** the correct overflow values to allow this to safely happen |
| 557 | ** and sound correct at the same time. |
| 558 | */ |
| 559 | const int16_t *periods = &periodTable[ch->n_finetune * 37]; |
| 560 | for (int32_t baseNote = 0; baseNote < 37; baseNote++) |
| 561 | { |
| 562 | if (ch->n_period >= periods[baseNote]) |
| 563 | { |
| 564 | // set voice period |
| 565 | paulaWriteWord(voiceAddr + 6, periods[baseNote+arpNote]); |
| 566 | |
| 567 | setVisualsPeriod(ch->n_chanindex, periods[baseNote+arpNote]); |
| 568 | |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | static void portaUp(moduleChannel_t *ch) |
| 575 | { |
| 576 | ch->n_period -= (ch->n_cmd & 0xFF) & lowMask; |
| 577 | lowMask = 0xFF; |
| 578 | |
| 579 | if ((ch->n_period & 0xFFF) < 113) // PT BUG: sign removed before comparison, underflow not clamped! |
| 580 | ch->n_period = (ch->n_period & 0xF000) | 113; |
| 581 | |
| 582 | // set voice period |
| 583 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 584 | paulaWriteWord(voiceAddr + 6, ch->n_period & 0xFFF); |
| 585 | |
| 586 | setVisualsPeriod(ch->n_chanindex, ch->n_period & 0xFFF); |
| 587 | } |
| 588 | |
| 589 | static void portaDown(moduleChannel_t *ch) |
| 590 | { |
| 591 | ch->n_period += (ch->n_cmd & 0xFF) & lowMask; |
| 592 | lowMask = 0xFF; |
| 593 | |
| 594 | if ((ch->n_period & 0xFFF) > 856) |
| 595 | ch->n_period = (ch->n_period & 0xF000) | 856; |
| 596 | |
| 597 | // set voice period |
| 598 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 599 | paulaWriteWord(voiceAddr + 6, ch->n_period & 0xFFF); |
| 600 | |
| 601 | setVisualsPeriod(ch->n_chanindex, ch->n_period & 0xFFF); |
| 602 | } |
| 603 | |
| 604 | static void filterOnOff(moduleChannel_t *ch) |
| 605 | { |
| 606 | if (song->tick == 0) // added this (just pointless to call this during all ticks!) |
| 607 | { |
| 608 | const bool filterOn = (ch->n_cmd & 1) ^ 1; |
| 609 | |
| 610 | // set "LED" filter |
| 611 | paulaWriteByte(0xBFE001, filterOn << 1); |
| 612 | audio.ledFilterEnabled = filterOn; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | static void finePortaUp(moduleChannel_t *ch) |
| 617 | { |
| 618 | if (song->tick == 0) |
| 619 | { |
| 620 | lowMask = 0xF; |
| 621 | portaUp(ch); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | static void finePortaDown(moduleChannel_t *ch) |
| 626 | { |
| 627 | if (song->tick == 0) |
| 628 | { |
| 629 | lowMask = 0xF; |
| 630 | portaDown(ch); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | static void setTonePorta(moduleChannel_t *ch) |
| 635 | { |
| 636 | uint16_t note = ch->n_note & 0xFFF; |
| 637 | const int16_t *portaPointer = &periodTable[ch->n_finetune * 37]; |
| 638 | |
| 639 | int32_t i = 0; |
| 640 | while (true) |
| 641 | { |
| 642 | // portaPointer[36] = 0, so i=36 is safe |
| 643 | if (note >= portaPointer[i]) |
| 644 | break; |
| 645 | |
| 646 | if (++i >= 37) |
| 647 | { |
| 648 | i = 35; |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | if ((ch->n_finetune & 8) && i > 0) |
| 654 | i--; |
| 655 | |
| 656 | ch->n_wantedperiod = portaPointer[i]; |
| 657 | ch->n_toneportdirec = 0; |
| 658 | |
| 659 | if (ch->n_period == ch->n_wantedperiod) ch->n_wantedperiod = 0; |
| 660 | else if (ch->n_period > ch->n_wantedperiod) ch->n_toneportdirec = 1; |
| 661 | } |
| 662 | |
| 663 | static void tonePortNoChange(moduleChannel_t *ch) |
| 664 | { |
| 665 | if (ch->n_wantedperiod <= 0) |
| 666 | return; |
| 667 | |
| 668 | if (ch->n_toneportdirec > 0) |
| 669 | { |
| 670 | ch->n_period -= ch->n_toneportspeed; |
| 671 | if (ch->n_period <= ch->n_wantedperiod) |
| 672 | { |
| 673 | ch->n_period = ch->n_wantedperiod; |
| 674 | ch->n_wantedperiod = 0; |
| 675 | } |
| 676 | } |
| 677 | else |
| 678 | { |
| 679 | ch->n_period += ch->n_toneportspeed; |
| 680 | if (ch->n_period >= ch->n_wantedperiod) |
| 681 | { |
| 682 | ch->n_period = ch->n_wantedperiod; |
| 683 | ch->n_wantedperiod = 0; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 688 | |
| 689 | if ((ch->n_glissfunk & 0xF) == 0) |
| 690 | { |
| 691 | // set voice period |
| 692 | paulaWriteWord(voiceAddr + 6, ch->n_period); |
| 693 | |
| 694 | setVisualsPeriod(ch->n_chanindex, ch->n_period); |
| 695 | } |
| 696 | else |
| 697 | { |
| 698 | const int16_t *portaPointer = &periodTable[ch->n_finetune * 37]; |
| 699 | |
| 700 | int32_t i = 0; |
| 701 | while (true) |
| 702 | { |
| 703 | // portaPointer[36] = 0, so i=36 is safe |
| 704 | if (ch->n_period >= portaPointer[i]) |
| 705 | break; |
| 706 | |
| 707 | if (++i >= 37) |
| 708 | { |
| 709 | i = 35; |
| 710 | break; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | // set voice period |
| 715 | paulaWriteWord(voiceAddr + 6, portaPointer[i]); |
| 716 | |
| 717 | setVisualsPeriod(ch->n_chanindex, portaPointer[i]); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | static void tonePortamento(moduleChannel_t *ch) |
| 722 | { |
| 723 | if ((ch->n_cmd & 0xFF) > 0) |
| 724 | { |
| 725 | ch->n_toneportspeed = ch->n_cmd & 0xFF; |
| 726 | ch->n_cmd &= 0xFF00; |
| 727 | } |
| 728 | |
| 729 | tonePortNoChange(ch); |
| 730 | } |
| 731 | |
| 732 | static void vibrato2(moduleChannel_t *ch) |
| 733 | { |
| 734 | uint16_t vibratoData; |
| 735 | |
| 736 | const uint8_t vibratoPos = (ch->n_vibratopos >> 2) & 0x1F; |
| 737 | const uint8_t vibratoType = ch->n_wavecontrol & 3; |
| 738 | |
| 739 | if (vibratoType == 0) // sine |
| 740 | { |
| 741 | vibratoData = vibratoTable[vibratoPos]; |
| 742 | } |
| 743 | else if (vibratoType == 1) // ramp |
| 744 | { |
| 745 | if (ch->n_vibratopos < 128) |
| 746 | vibratoData = vibratoPos << 3; |
| 747 | else |
| 748 | vibratoData = 255 - (vibratoPos << 3); |
| 749 | } |
| 750 | else // square |
| 751 | { |
| 752 | vibratoData = 255; |
| 753 | } |
| 754 | |
| 755 | vibratoData = (vibratoData * (ch->n_vibratocmd & 0xF)) >> 7; |
| 756 | |
| 757 | if (ch->n_vibratopos < 128) |
| 758 | vibratoData = ch->n_period + vibratoData; |
| 759 | else |
| 760 | vibratoData = ch->n_period - vibratoData; |
| 761 | |
| 762 | // set voice period |
| 763 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 764 | paulaWriteWord(voiceAddr + 6, vibratoData); |
| 765 | |
| 766 | setVisualsPeriod(ch->n_chanindex, vibratoData); |
| 767 | |
| 768 | ch->n_vibratopos += (ch->n_vibratocmd >> 2) & 0x3C; |
| 769 | } |
| 770 | |
| 771 | static void vibrato(moduleChannel_t *ch) |
| 772 | { |
| 773 | if ((ch->n_cmd & 0x0F) > 0) |
| 774 | ch->n_vibratocmd = (ch->n_vibratocmd & 0xF0) | (ch->n_cmd & 0x0F); |
| 775 | |
| 776 | if ((ch->n_cmd & 0xF0) > 0) |
| 777 | ch->n_vibratocmd = (ch->n_cmd & 0xF0) | (ch->n_vibratocmd & 0x0F); |
| 778 | |
| 779 | vibrato2(ch); |
| 780 | } |
| 781 | |
| 782 | static void tonePlusVolSlide(moduleChannel_t *ch) |
| 783 | { |
| 784 | tonePortNoChange(ch); |
| 785 | volumeSlide(ch); |
| 786 | } |
| 787 | |
| 788 | static void vibratoPlusVolSlide(moduleChannel_t *ch) |
| 789 | { |
| 790 | vibrato2(ch); |
| 791 | volumeSlide(ch); |
| 792 | } |
| 793 | |
| 794 | static void tremolo(moduleChannel_t *ch) |
| 795 | { |
| 796 | int16_t tremoloData; |
| 797 | |
| 798 | if ((ch->n_cmd & 0x0F) > 0) |
| 799 | ch->n_tremolocmd = (ch->n_tremolocmd & 0xF0) | (ch->n_cmd & 0x0F); |
| 800 | |
| 801 | if ((ch->n_cmd & 0xF0) > 0) |
| 802 | ch->n_tremolocmd = (ch->n_cmd & 0xF0) | (ch->n_tremolocmd & 0x0F); |
| 803 | |
| 804 | const uint8_t tremoloPos = (ch->n_tremolopos >> 2) & 0x1F; |
| 805 | const uint8_t tremoloType = (ch->n_wavecontrol >> 4) & 3; |
| 806 | |
| 807 | if (tremoloType == 0) // sine |
| 808 | { |
| 809 | tremoloData = vibratoTable[tremoloPos]; |
| 810 | } |
| 811 | else if (tremoloType == 1) // ramp |
| 812 | { |
| 813 | if (ch->n_vibratopos < 128) // PT bug, should've been ch->n_tremolopos |
| 814 | tremoloData = tremoloPos << 3; |
| 815 | else |
| 816 | tremoloData = 255 - (tremoloPos << 3); |
| 817 | } |
| 818 | else // square |
| 819 | { |
| 820 | tremoloData = 255; |
| 821 | } |
| 822 | |
| 823 | tremoloData = ((uint16_t)tremoloData * (ch->n_tremolocmd & 0xF)) >> 6; |
| 824 | |
| 825 | if (ch->n_tremolopos < 128) |
| 826 | { |
| 827 | tremoloData = ch->n_volume + tremoloData; |
| 828 | if (tremoloData > 64) |
| 829 | tremoloData = 64; |
| 830 | } |
| 831 | else |
| 832 | { |
| 833 | tremoloData = ch->n_volume - tremoloData; |
| 834 | if (tremoloData < 0) |
| 835 | tremoloData = 0; |
| 836 | } |
| 837 | |
| 838 | // set voice volume |
| 839 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 840 | paulaWriteWord(voiceAddr + 8, tremoloData); |
| 841 | |
| 842 | setVisualsVolume(ch->n_chanindex, tremoloData); |
| 843 | |
| 844 | ch->n_tremolopos += (ch->n_tremolocmd >> 2) & 0x3C; |
| 845 | } |
| 846 | |
| 847 | static void sampleOffset(moduleChannel_t *ch) |
| 848 | { |
| 849 | if ((ch->n_cmd & 0xFF) > 0) |
| 850 | ch->n_sampleoffset = ch->n_cmd & 0xFF; |
| 851 | |
| 852 | const uint16_t newOffset = ch->n_sampleoffset << 7; |
| 853 | if (newOffset < ch->n_length) |
| 854 | { |
| 855 | ch->n_length -= newOffset; |
| 856 | ch->n_start += newOffset << 1; |
| 857 | } |
| 858 | else |
| 859 | { |
| 860 | ch->n_length = 1; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | static void E_Commands(moduleChannel_t *ch) |
| 865 | { |
| 866 | const uint8_t ecmd = (ch->n_cmd & 0x00F0) >> 4; |
| 867 | switch (ecmd) |
| 868 | { |
| 869 | case 0x0: filterOnOff(ch); return; |
| 870 | case 0x1: finePortaUp(ch); return; |
| 871 | case 0x2: finePortaDown(ch); return; |
| 872 | case 0x3: setGlissControl(ch); return; |
| 873 | case 0x4: setVibratoControl(ch); return; |
| 874 | case 0x5: setFineTune(ch); return; |
| 875 | case 0x6: jumpLoop(ch); return; |
| 876 | case 0x7: setTremoloControl(ch); return; |
| 877 | case 0x8: karplusStrong(ch); return; |
| 878 | case 0xE: patternDelay(ch); return; |
| 879 | default: break; |
| 880 | } |
| 881 | |
| 882 | if (editor.muted[ch->n_chanindex]) |
| 883 | return; |
| 884 | |
| 885 | switch (ecmd) |
| 886 | { |
| 887 | case 0x9: retrigNote(ch); return; |
| 888 | case 0xA: volumeFineUp(ch); return; |
| 889 | case 0xB: volumeFineDown(ch); return; |
| 890 | case 0xC: noteCut(ch); return; |
| 891 | case 0xD: noteDelay(ch); return; |
| 892 | case 0xF: funkIt(ch); return; |
| 893 | default: break; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | static void checkMoreEffects(moduleChannel_t *ch) |
| 898 | { |
| 899 | const uint8_t cmd = (ch->n_cmd & 0x0F00) >> 8; |
| 900 | switch (cmd) |
| 901 | { |
| 902 | case 0x9: sampleOffset(ch); return; // note the returns here, not breaks! |
| 903 | case 0xB: positionJump(ch); return; |
| 904 | case 0xD: patternBreak(ch); return; |
| 905 | case 0xE: E_Commands(ch); return; |
| 906 | case 0xF: setSpeed(ch); return; |
| 907 | default: break; |
| 908 | } |
| 909 | |
| 910 | if (editor.muted[ch->n_chanindex]) |
| 911 | return; |
| 912 | |
| 913 | if (cmd == 0xC) |
| 914 | { |
| 915 | volumeChange(ch); |
| 916 | return; |
| 917 | } |
| 918 | |
| 919 | // set voice period |
| 920 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 921 | paulaWriteWord(voiceAddr + 6, ch->n_period); |
| 922 | |
| 923 | setVisualsPeriod(ch->n_chanindex, ch->n_period); |
| 924 | } |
| 925 | |
| 926 | static void chkefx2(moduleChannel_t *ch) |
| 927 | { |
| 928 | updateFunk(ch); |
| 929 | |
| 930 | if ((ch->n_cmd & 0xFFF) == 0) |
| 931 | return; |
| 932 | |
| 933 | const uint8_t cmd = (ch->n_cmd & 0x0F00) >> 8; |
| 934 | switch (cmd) |
| 935 | { |
| 936 | case 0x0: arpeggio(ch); return; // note the returns here, not breaks! |
| 937 | case 0x1: portaUp(ch); return; |
| 938 | case 0x2: portaDown(ch); return; |
| 939 | case 0x3: tonePortamento(ch); return; |
| 940 | case 0x4: vibrato(ch); return; |
| 941 | case 0x5: tonePlusVolSlide(ch); return; |
| 942 | case 0x6: vibratoPlusVolSlide(ch); return; |
| 943 | case 0xE: E_Commands(ch); return; |
| 944 | default: break; |
| 945 | } |
| 946 | |
| 947 | // set voice period |
| 948 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 949 | paulaWriteWord(voiceAddr + 6, ch->n_period); |
| 950 | |
| 951 | setVisualsPeriod(ch->n_chanindex, ch->n_period); |
| 952 | |
| 953 | if (cmd == 0x7) |
| 954 | tremolo(ch); |
| 955 | else if (cmd == 0xA) |
| 956 | volumeSlide(ch); |
| 957 | } |
| 958 | |
| 959 | static void checkEffects(moduleChannel_t *ch) |
| 960 | { |
| 961 | if (editor.muted[ch->n_chanindex]) |
| 962 | return; |
| 963 | |
| 964 | chkefx2(ch); |
| 965 | |
| 966 | /* This is not very clear in the original PT replayer code, |
| 967 | ** but the tremolo effect skips chkefx2()'s return address |
| 968 | ** in the stack so that it jumps to checkEffects()'s return |
| 969 | ** address instead of ending up here. In other words, volume |
| 970 | ** is not updated here after tremolo (it's done inside the |
| 971 | ** tremolo routine itself). |
| 972 | */ |
| 973 | const uint8_t cmd = (ch->n_cmd & 0x0F00) >> 8; |
| 974 | if (cmd != 0x7) |
| 975 | { |
| 976 | // set voice volume |
| 977 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 978 | paulaWriteWord(voiceAddr + 8, ch->n_volume); |
| 979 | |
| 980 | setVisualsVolume(ch->n_chanindex, ch->n_volume); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | static void setPeriod(moduleChannel_t *ch) |
| 985 | { |
| 986 | int32_t i; |
| 987 | |
| 988 | uint16_t note = ch->n_note & 0xFFF; |
| 989 | for (i = 0; i < 37; i++) |
| 990 | { |
| 991 | // periodTable[36] = 0, so i=36 is safe |
| 992 | if (note >= periodTable[i]) |
| 993 | break; |
| 994 | } |
| 995 | |
| 996 | // yes it's safe if i=37 because of zero-padding |
| 997 | ch->n_period = periodTable[(ch->n_finetune * 37) + i]; |
| 998 | |
| 999 | if ((ch->n_cmd & 0xFF0) != 0xED0) // no note delay |
| 1000 | { |
| 1001 | // voice DMA off (turned on in setDMA() later) |
| 1002 | paulaWriteWord(0xDFF096, ch->n_dmabit); |
| 1003 | |
| 1004 | if ((ch->n_wavecontrol & 0x04) == 0) ch->n_vibratopos = 0; |
| 1005 | if ((ch->n_wavecontrol & 0x40) == 0) ch->n_tremolopos = 0; |
| 1006 | |
| 1007 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 1008 | |
| 1009 | // set voice data length and data ptr |
| 1010 | paulaWriteWord(voiceAddr + 4, ch->n_length); |
| 1011 | paulaWritePtr(voiceAddr + 0, ch->n_start); |
| 1012 | |
| 1013 | if (ch->n_start == NULL) |
| 1014 | { |
| 1015 | ch->n_loopstart = NULL; |
| 1016 | paulaWriteWord(voiceAddr + 4, 1); // length |
| 1017 | ch->n_replen = 1; |
| 1018 | } |
| 1019 | |
| 1020 | // set voice period |
| 1021 | paulaWriteWord(voiceAddr + 6, ch->n_period); |
| 1022 | |
| 1023 | DMACONtemp |= ch->n_dmabit; |
| 1024 | |
| 1025 | // update tracker visuals |
| 1026 | |
| 1027 | setVisualsDMACON(ch->n_dmabit); |
| 1028 | |
| 1029 | setVisualsLength(ch->n_chanindex, ch->n_length); |
| 1030 | setVisualsDataPtr(ch->n_chanindex, ch->n_start); |
| 1031 | |
| 1032 | if (ch->n_start == NULL) |
| 1033 | setVisualsLength(ch->n_chanindex, 1); |
| 1034 | |
| 1035 | setVisualsPeriod(ch->n_chanindex, ch->n_period); |
| 1036 | |
| 1037 | // set spectrum analyzer state for this channel |
| 1038 | if (!editor.muted[ch->n_chanindex]) |
| 1039 | { |
| 1040 | ch->syncAnalyzerVolume = ch->n_volume; |
| 1041 | ch->syncAnalyzerPeriod = ch->n_period; |
| 1042 | ch->syncFlags |= UPDATE_SPECTRUM_ANALYZER; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | checkMoreEffects(ch); |
| 1047 | } |
| 1048 | |
| 1049 | static void checkMetronome(moduleChannel_t *ch, note_t *note) |
| 1050 | { |
| 1051 | if (editor.metroFlag && editor.metroChannel > 0) |
| 1052 | { |
| 1053 | if (ch->n_chanindex == (uint32_t)editor.metroChannel-1 && (song->row % editor.metroSpeed) == 0) |
| 1054 | { |
| 1055 | note->sample = 31; |
| 1056 | note->period = (((song->row / editor.metroSpeed) % editor.metroSpeed) == 0) ? 160 : 214; |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | static void playVoice(moduleChannel_t *ch) |
| 1062 | { |
| 1063 | if (ch->n_note == 0 && ch->n_cmd == 0) // test period, command and command parameter |
| 1064 | { |
| 1065 | // set voice period |
| 1066 | const uint32_t voiceAddr = 0xDFF0A0 + (ch->n_chanindex * 16); |
| 1067 | paulaWriteWord(voiceAddr + 6, ch->n_period); |
| 1068 | |
| 1069 | setVisualsPeriod(ch->n_chanindex, ch->n_period); |
| 1070 | } |
| 1071 | |
| 1072 | note_t note = song->patterns[modPattern][(song->row * PAULA_VOICES) + ch->n_chanindex]; |
| 1073 | |
| 1074 | checkMetronome(ch, ¬e); |
| 1075 | |
| 1076 | ch->n_note = note.period; |
| 1077 | ch->n_cmd = (note.command << 8) | note.param; |
| 1078 | |
| 1079 | if (note.sample >= 1 && note.sample <= 31) // SAFETY BUG FIX: don't handle sample-numbers >31 |
| 1080 | { |
| 1081 | ch->n_samplenum = note.sample - 1; |
| 1082 | moduleSample_t *s = &song->samples[ch->n_samplenum]; |
| 1083 | |
| 1084 | ch->n_start = &song->sampleData[s->offset]; |
| 1085 | ch->n_finetune = s->fineTune & 0xF; |
| 1086 | ch->n_volume = s->volume; |
| 1087 | ch->n_length = (uint16_t)(s->length >> 1); |
| 1088 | ch->n_replen = (uint16_t)(s->loopLength >> 1); |
| 1089 | |
| 1090 | const uint16_t repeat = (uint16_t)(s->loopStart >> 1); |
| 1091 | if (repeat > 0) |
| 1092 | { |
| 1093 | ch->n_loopstart = ch->n_start + (repeat << 1); |
| 1094 | ch->n_wavestart = ch->n_loopstart; |
| 1095 | ch->n_length = repeat + ch->n_replen; |
| 1096 | } |
| 1097 | else |
| 1098 | { |
| 1099 | ch->n_loopstart = ch->n_start; |
| 1100 | ch->n_wavestart = ch->n_start; |
| 1101 | } |
| 1102 | |
| 1103 | // non-PT2 requirement (set safe sample space for uninitialized voices - f.ex. "the ultimate beeper.mod") |
| 1104 | if (ch->n_length == 0) |
| 1105 | ch->n_loopstart = ch->n_wavestart = paulaGetNullSamplePtr(); |
| 1106 | } |
| 1107 | |
| 1108 | if ((ch->n_note & 0xFFF) > 0) |
| 1109 | { |
| 1110 | if ((ch->n_cmd & 0xFF0) == 0xE50) // set finetune |
| 1111 | { |
| 1112 | setFineTune(ch); |
| 1113 | setPeriod(ch); |
| 1114 | } |
| 1115 | else |
| 1116 | { |
| 1117 | uint8_t cmd = (ch->n_cmd & 0x0F00) >> 8; |
| 1118 | if (cmd == 3 || cmd == 5) |
| 1119 | { |
| 1120 | setVUMeterHeight(ch); |
| 1121 | setTonePorta(ch); |
| 1122 | checkMoreEffects(ch); |
| 1123 | } |
| 1124 | else if (cmd == 9) |
| 1125 | { |
| 1126 | checkMoreEffects(ch); |
| 1127 | setPeriod(ch); |
| 1128 | } |
| 1129 | else |
| 1130 | { |
| 1131 | setPeriod(ch); |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | else |
| 1136 | { |
| 1137 | checkMoreEffects(ch); |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | static void updateUIPositions(void) |
| 1142 | { |
| 1143 | if (editor.mod2WavOngoing || editor.pat2SmpOngoing) |
| 1144 | return; // don't update UI under MOD2WAV/PAT2SMP rendering |
| 1145 | |
| 1146 | song->currRow = song->row; |
| 1147 | song->currPos = modPos; |
| 1148 | song->currPattern = modPattern; |
| 1149 | |
| 1150 | uint16_t *currPatPtr = &song->header.patternTable[modPos]; |
| 1151 | editor.currPatternDisp = currPatPtr; |
| 1152 | editor.currPosEdPattDisp = currPatPtr; |
| 1153 | editor.currPatternDisp = currPatPtr; |
| 1154 | editor.currPosEdPattDisp = currPatPtr; |
| 1155 | |
| 1156 | ui.updateSongPos = true; |
| 1157 | ui.updateSongPattern = true; |
| 1158 | ui.updateCurrPattText = true; |
| 1159 | ui.updatePatternData = true; |
| 1160 | |
| 1161 | if (ui.posEdScreenShown) |
| 1162 | ui.updatePosEd = true; |
| 1163 | } |
| 1164 | |
| 1165 | static void nextPosition(void) |
| 1166 | { |
| 1167 | if (editor.pat2SmpOngoing) |
| 1168 | modRenderDone = true; |
| 1169 | |
| 1170 | song->row = pBreakPosition; |
| 1171 | pBreakPosition = 0; |
| 1172 | posJumpAssert = false; |
| 1173 | |
| 1174 | if (editor.playMode != PLAY_MODE_PATTERN || |
| 1175 | (editor.currMode == MODE_RECORD && editor.recordMode != RECORD_PATT)) |
| 1176 | { |
| 1177 | if (editor.stepPlayEnabled) |
| 1178 | { |
| 1179 | if (config.keepEditModeAfterStepPlay && editor.stepPlayLastMode == MODE_EDIT) |
| 1180 | { |
| 1181 | doStopIt(false); |
| 1182 | |
| 1183 | editor.playMode = PLAY_MODE_NORMAL; |
| 1184 | editor.currMode = MODE_EDIT; |
| 1185 | pointerSetModeThreadSafe(POINTER_MODE_EDIT, true); |
| 1186 | } |
| 1187 | else |
| 1188 | { |
| 1189 | doStopIt(true); |
| 1190 | } |
| 1191 | |
| 1192 | editor.stepPlayEnabled = false; |
| 1193 | editor.stepPlayBackwards = false; |
| 1194 | |
| 1195 | if (editor.stepPlayLastMode == MODE_EDIT || editor.stepPlayLastMode == MODE_IDLE) |
| 1196 | { |
| 1197 | song->row &= 63; |
| 1198 | song->currRow = song->row; |
| 1199 | } |
| 1200 | else |
| 1201 | { |
| 1202 | // if we were playing, set replayer row to tracker row (stay in sync) |
| 1203 | song->currRow &= 63; |
| 1204 | song->row = song->currRow; |
| 1205 | } |
| 1206 | |
| 1207 | return; |
| 1208 | } |
| 1209 | |
| 1210 | modPos = (modPos + 1) & 127; |
| 1211 | if (modPos >= song->header.songLength) |
| 1212 | { |
| 1213 | modPos = 0; |
| 1214 | |
| 1215 | if (config.compoMode) // stop song for music competitions playing |
| 1216 | { |
| 1217 | doStopIt(true); |
| 1218 | turnOffVoices(); |
| 1219 | |
| 1220 | modPos = 0; |
| 1221 | modPattern = (int8_t)song->header.patternTable[modPos]; |
| 1222 | song->row = 0; |
| 1223 | |
| 1224 | updateUIPositions(); |
| 1225 | } |
| 1226 | |
| 1227 | if (editor.mod2WavOngoing) |
| 1228 | modRenderDone = true; |
| 1229 | } |
| 1230 | |
| 1231 | modPattern = (int8_t)song->header.patternTable[modPos]; |
| 1232 | if (modPattern > MAX_PATTERNS-1) |
| 1233 | modPattern = MAX_PATTERNS-1; |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | static void increasePlaybackTimer(void) |
| 1238 | { |
| 1239 | // (the timer is not counting in "play pattern" mode) |
| 1240 | if (editor.playMode != PLAY_MODE_PATTERN && modBPM >= MIN_BPM && modBPM <= MAX_BPM) |
| 1241 | { |
| 1242 | if (editor.timingMode == TEMPO_MODE_CIA) |
| 1243 | editor.playbackSecondsFrac += tickDuration31fp[modBPM-MIN_BPM]; |
| 1244 | else |
| 1245 | editor.playbackSecondsFrac += tickDuration31fp[(MAX_BPM-MIN_BPM)+1]; // vblank tempo mode |
| 1246 | |
| 1247 | if (editor.playbackSecondsFrac > INT32_MAX) |
| 1248 | { |
| 1249 | editor.playbackSecondsFrac &= INT32_MAX; |
| 1250 | |
| 1251 | if (editor.playbackSeconds >= (99*60)+59) // wrap around 99:59 -> 00:00 |
| 1252 | editor.playbackSeconds = 0; |
| 1253 | else |
| 1254 | editor.playbackSeconds++; |
| 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | static void setCurrRowToVisited(void) // for MOD2WAV |
| 1260 | { |
| 1261 | if (editor.mod2WavOngoing) |
| 1262 | editor.rowVisitTable[(modPos * MOD_ROWS) + song->row] = true; |
| 1263 | } |
| 1264 | |
| 1265 | static bool renderEndCheck(void) // for MOD2WAV/PAT2SMP |
| 1266 | { |
| 1267 | if (!editor.mod2WavOngoing && !editor.pat2SmpOngoing) |
| 1268 | return true; // we're not doing MOD2WAV/PAT2SMP |
| 1269 | |
| 1270 | bool noPatternDelay = (pattDelTime2 == 0); |
| 1271 | if (noPatternDelay) |
| 1272 | { |
| 1273 | if (editor.pat2SmpOngoing) |
| 1274 | { |
| 1275 | if (modRenderDone) |
| 1276 | return false; // we're done rendering |
| 1277 | } |
| 1278 | |
| 1279 | if (editor.mod2WavOngoing && song->tick == song->speed-1) |
| 1280 | { |
| 1281 | bool rowVisited = editor.rowVisitTable[(modPos * MOD_ROWS) + song->row]; |
| 1282 | if (rowVisited || modRenderDone) |
| 1283 | { |
| 1284 | modRenderDone = false; |
| 1285 | return false; // we're done rendering |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | return true; |
| 1291 | } |
| 1292 | |
| 1293 | static void setDMA(void) |
| 1294 | { |
| 1295 | if (editor.muted[0]) DMACONtemp &= ~1; |
| 1296 | if (editor.muted[1]) DMACONtemp &= ~2; |
| 1297 | if (editor.muted[2]) DMACONtemp &= ~4; |
| 1298 | if (editor.muted[3]) DMACONtemp &= ~8; |
| 1299 | |
| 1300 | // start DMAs for selected voices |
| 1301 | paulaWriteWord(0xDFF096, 0x8000 | DMACONtemp); |
| 1302 | |
| 1303 | setVisualsDMACON(0x8000 | DMACONtemp); |
| 1304 | |
| 1305 | moduleChannel_t *ch = song->channels; |
| 1306 | for (int32_t i = 0; i < PAULA_VOICES; i++, ch++) |
| 1307 | { |
| 1308 | if (DMACONtemp & ch->n_dmabit) // handle visuals on sample trigger |
| 1309 | setVUMeterHeight(ch); |
| 1310 | |
| 1311 | // set new voice data ptr and length (these take effect after the current DMA cycle is done) |
| 1312 | const uint32_t voiceAddr = 0xDFF0A0 + (i * 16); |
| 1313 | paulaWritePtr(voiceAddr + 0, ch->n_loopstart); |
| 1314 | paulaWriteWord(voiceAddr + 4, ch->n_replen); |
| 1315 | |
| 1316 | setVisualsDataPtr(i, ch->n_loopstart); |
| 1317 | setVisualsLength(i, ch->n_replen); |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | void modSetTempo(int32_t bpm, bool doLockAudio) |
| 1322 | { |
| 1323 | if (bpm < MIN_BPM || bpm > MAX_BPM) |
| 1324 | return; |
| 1325 | |
| 1326 | const bool audioWasntLocked = !audio.locked; |
| 1327 | if (doLockAudio && audioWasntLocked) |
| 1328 | lockAudio(); |
| 1329 | |
| 1330 | modBPM = bpm; |
| 1331 | if (!editor.pat2SmpOngoing && !editor.mod2WavOngoing) |
| 1332 | { |
| 1333 | song->currBPM = bpm; |
| 1334 | ui.updateSongBPM = true; |
| 1335 | } |
| 1336 | |
| 1337 | const int32_t i = bpm - MIN_BPM; // 32..255 -> 0..223 |
| 1338 | |
| 1339 | audio.samplesPerTickInt = audio.samplesPerTickIntTab[i]; |
| 1340 | audio.samplesPerTickFrac = audio.samplesPerTickFracTab[i]; |
| 1341 | |
| 1342 | // calculate tick time length for audio/video sync timestamp (visualizers) |
| 1343 | if (!editor.pat2SmpOngoing && !editor.mod2WavOngoing) |
| 1344 | setSyncTickTimeLen(audio.tickTimeIntTab[i], audio.tickTimeFracTab[i]); |
| 1345 | |
| 1346 | if (doLockAudio && audioWasntLocked) |
| 1347 | unlockAudio(); |
| 1348 | } |
| 1349 | |
| 1350 | bool tickReplayer(void) |
| 1351 | { |
| 1352 | // quirk: CIA BPM changes are delayed by one tick in PT, so handle previous tick's BPM change now |
| 1353 | if (ciaSetBPM != -1) |
| 1354 | { |
| 1355 | const int32_t newBPM = ciaSetBPM; |
| 1356 | modSetTempo(newBPM, false); |
| 1357 | ciaSetBPM = -1; |
| 1358 | } |
| 1359 | |
| 1360 | increasePlaybackTimer(); |
| 1361 | |
| 1362 | if (!editor.stepPlayEnabled) |
| 1363 | song->tick++; |
| 1364 | |
| 1365 | bool readNewNote = false; |
| 1366 | if ((uint32_t)song->tick >= (uint32_t)song->speed) |
| 1367 | { |
| 1368 | song->tick = 0; |
| 1369 | readNewNote = true; |
| 1370 | } |
| 1371 | |
| 1372 | if (readNewNote || editor.stepPlayEnabled) // tick 0 |
| 1373 | { |
| 1374 | if (pattDelTime2 == 0) // no pattern delay, time to read note data |
| 1375 | { |
| 1376 | DMACONtemp = 0; // reset Paula DMA trigger states |
| 1377 | |
| 1378 | setCurrRowToVisited(); // for MOD2WAV |
| 1379 | updateUIPositions(); // update current song positions in UI |
| 1380 | |
| 1381 | // read note data and trigger voices |
| 1382 | moduleChannel_t *ch = song->channels; |
| 1383 | for (int32_t i = 0; i < PAULA_VOICES; i++, ch++) |
| 1384 | { |
| 1385 | playVoice(ch); |
| 1386 | |
| 1387 | // set voice volume |
| 1388 | const uint32_t voiceAddr = 0xDFF0A0 + (i * 16); |
| 1389 | paulaWriteWord(voiceAddr + 8, ch->n_volume); |
| 1390 | |
| 1391 | setVisualsVolume(i, ch->n_volume); |
| 1392 | } |
| 1393 | |
| 1394 | setDMA(); |
| 1395 | } |
| 1396 | else // pattern delay is on-going |
| 1397 | { |
| 1398 | moduleChannel_t *ch = song->channels; |
| 1399 | for (int32_t i = 0; i < PAULA_VOICES; i++, ch++) |
| 1400 | checkEffects(ch); |
| 1401 | } |
| 1402 | |
| 1403 | // increase row |
| 1404 | if (!editor.stepPlayBackwards) |
| 1405 | { |
| 1406 | song->row++; |
| 1407 | song->rowsCounter++; // for MOD2WAV's progress bar |
| 1408 | } |
| 1409 | |
| 1410 | if (pattDelTime > 0) |
| 1411 | { |
| 1412 | pattDelTime2 = pattDelTime; |
| 1413 | pattDelTime = 0; |
| 1414 | } |
| 1415 | |
| 1416 | // undo row increase if pattern delay is on-going |
| 1417 | if (pattDelTime2 > 0) |
| 1418 | { |
| 1419 | pattDelTime2--; |
| 1420 | if (pattDelTime2 > 0) |
| 1421 | { |
| 1422 | song->row--; |
| 1423 | song->rowsCounter--; // for MOD2WAV's progress bar |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | if (pBreakFlag) |
| 1428 | { |
| 1429 | song->row = pBreakPosition; |
| 1430 | pBreakPosition = 0; |
| 1431 | pBreakFlag = false; |
| 1432 | } |
| 1433 | |
| 1434 | // step-play handling |
| 1435 | if (editor.stepPlayEnabled) |
| 1436 | { |
| 1437 | if (config.keepEditModeAfterStepPlay && editor.stepPlayLastMode == MODE_EDIT) |
| 1438 | { |
| 1439 | doStopIt(false); |
| 1440 | |
| 1441 | editor.playMode = PLAY_MODE_NORMAL; |
| 1442 | editor.currMode = MODE_EDIT; |
| 1443 | pointerSetModeThreadSafe(POINTER_MODE_EDIT, true); |
| 1444 | } |
| 1445 | else |
| 1446 | { |
| 1447 | doStopIt(true); |
| 1448 | } |
| 1449 | |
| 1450 | if (editor.stepPlayLastMode == MODE_EDIT || editor.stepPlayLastMode == MODE_IDLE) |
| 1451 | { |
| 1452 | song->row &= 63; |
| 1453 | song->currRow = song->row; |
| 1454 | } |
| 1455 | else |
| 1456 | { |
| 1457 | // if we were playing, set replayer row to tracker row (stay in sync) |
| 1458 | song->currRow &= 63; |
| 1459 | song->row = song->currRow; |
| 1460 | } |
| 1461 | |
| 1462 | editor.stepPlayEnabled = false; |
| 1463 | editor.stepPlayBackwards = false; |
| 1464 | |
| 1465 | ui.updatePatternData = true; |
| 1466 | return true; |
| 1467 | } |
| 1468 | |
| 1469 | if (song->row >= MOD_ROWS || posJumpAssert) |
| 1470 | nextPosition(); |
| 1471 | |
| 1472 | // for pattern block mark feature |
| 1473 | if (editor.blockMarkFlag) |
| 1474 | ui.updateStatusText = true; |
| 1475 | } |
| 1476 | else // tick > 0 (handle effects) |
| 1477 | { |
| 1478 | moduleChannel_t *ch = song->channels; |
| 1479 | for (int32_t i = 0; i < PAULA_VOICES; i++, ch++) |
| 1480 | checkEffects(ch); |
| 1481 | |
| 1482 | if (posJumpAssert) |
| 1483 | nextPosition(); |
| 1484 | } |
| 1485 | |
| 1486 | // command F00 = stop song, do it here (so that the scopes are updated properly) |
| 1487 | if (doStopSong) |
| 1488 | { |
| 1489 | doStopSong = false; |
| 1490 | |
| 1491 | editor.songPlaying = false; |
| 1492 | |
| 1493 | editor.playMode = PLAY_MODE_NORMAL; |
| 1494 | editor.currMode = MODE_IDLE; |
| 1495 | pointerSetModeThreadSafe(POINTER_MODE_IDLE, true); |
| 1496 | } |
| 1497 | |
| 1498 | return renderEndCheck(); // MOD2WAV/PAT2SMP listens to the return value (true = not done yet) |
| 1499 | } |
| 1500 | |
| 1501 | void modSetPattern(uint8_t pattern) |
| 1502 | { |
| 1503 | modPattern = pattern; |
| 1504 | song->currPattern = modPattern; |
| 1505 | ui.updateCurrPattText = true; |
| 1506 | } |
| 1507 | |
| 1508 | void updateNewPos(void) // for after having edited the "POS" digits only |
| 1509 | { |
| 1510 | modPos = song->currPos; |
| 1511 | editor.currPatternDisp = &song->header.patternTable[song->currPos]; |
| 1512 | ui.updateSongPos = true; |
| 1513 | ui.updateSongPattern = true; |
| 1514 | } |
| 1515 | |
| 1516 | void modSetPos(int16_t pos, int16_t row) |
| 1517 | { |
| 1518 | /* |
| 1519 | const bool audioWasntLocked = !audio.locked; |
| 1520 | if (audioWasntLocked) |
| 1521 | lockAudio(); |
| 1522 | */ |
| 1523 | |
| 1524 | if (row != -1) |
| 1525 | { |
| 1526 | row = CLAMP(row, 0, 63); |
| 1527 | |
| 1528 | song->tick = 0; |
| 1529 | song->row = (int8_t)row; |
| 1530 | song->currRow = (int8_t)row; |
| 1531 | } |
| 1532 | |
| 1533 | if (pos != -1) |
| 1534 | { |
| 1535 | if (pos >= 0) |
| 1536 | { |
| 1537 | song->currPos = modPos = pos; |
| 1538 | ui.updateSongPos = true; |
| 1539 | |
| 1540 | if (editor.currMode == MODE_PLAY && editor.playMode == PLAY_MODE_NORMAL) |
| 1541 | { |
| 1542 | modPattern = (int8_t)song->header.patternTable[pos]; |
| 1543 | if (modPattern > MAX_PATTERNS-1) |
| 1544 | modPattern = MAX_PATTERNS-1; |
| 1545 | |
| 1546 | song->currPattern = modPattern; |
| 1547 | ui.updateCurrPattText = true; |
| 1548 | } |
| 1549 | |
| 1550 | ui.updateSongPattern = true; |
| 1551 | editor.currPatternDisp = &song->header.patternTable[modPos]; |
| 1552 | |
| 1553 | int16_t posEdPos = song->currPos; |
| 1554 | if (posEdPos > song->header.songLength-1) |
| 1555 | posEdPos = song->header.songLength-1; |
| 1556 | |
| 1557 | editor.currPosEdPattDisp = &song->header.patternTable[posEdPos]; |
| 1558 | |
| 1559 | if (ui.posEdScreenShown) |
| 1560 | ui.updatePosEd = true; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | if (audioWasntLocked) |
| 1566 | unlockAudio(); |
| 1567 | */ |
| 1568 | |
| 1569 | ui.updatePatternData = true; |
| 1570 | |
| 1571 | if (editor.blockMarkFlag) |
| 1572 | ui.updateStatusText = true; |
| 1573 | } |
| 1574 | |
| 1575 | void modStop(void) |
| 1576 | { |
| 1577 | editor.songPlaying = false; |
| 1578 | turnOffVoices(); |
| 1579 | |
| 1580 | if (song != NULL) |
| 1581 | { |
| 1582 | moduleChannel_t *ch = song->channels; |
| 1583 | for (int32_t i = 0; i < PAULA_VOICES; i++, ch++) |
| 1584 | { |
| 1585 | ch->n_wavecontrol = 0; |
| 1586 | ch->n_glissfunk = 0; |
| 1587 | ch->n_finetune = 0; |
| 1588 | ch->n_loopcount = 0; |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | pBreakFlag = false; |
| 1593 | pattDelTime = 0; |
| 1594 | pattDelTime2 = 0; |
| 1595 | pBreakPosition = 0; |
| 1596 | posJumpAssert = false; |
| 1597 | modRenderDone = true; |
| 1598 | |
| 1599 | /* The replayer is one tick ahead (unfortunately), so if the user was to stop the mod at the previous tick |
| 1600 | ** before a position jump (pattern loop, pattern break, position jump, row 63->0 transition, etc), |
| 1601 | ** it would be possible for the replayer to be at another order/pattern than the tracker. |
| 1602 | ** Let's set the replayer state to the tracker state on mod stop, to fix possible confusion. |
| 1603 | */ |
| 1604 | setReplayerPosToTrackerPos(); |
| 1605 | |
| 1606 | doStopSong = false; // just in case this flag was stuck from command F00 (stop song) |
| 1607 | } |
| 1608 | |
| 1609 | void playPattern(int8_t startRow) |
| 1610 | { |
| 1611 | if (!editor.stepPlayEnabled) |
| 1612 | pointerSetMode(POINTER_MODE_PLAY, DO_CARRY); |
| 1613 | |
| 1614 | const bool audioWasntLocked = !audio.locked; |
| 1615 | if (audioWasntLocked) |
| 1616 | lockAudio(); |
| 1617 | |
| 1618 | audio.tickSampleCounter = 0; // zero tick sample counter so that it will instantly initiate a tick |
| 1619 | audio.tickSampleCounterFrac = 0; |
| 1620 | |
| 1621 | song->currRow = song->row = startRow & 63; |
| 1622 | |
| 1623 | if (!editor.stepPlayEnabled) |
| 1624 | song->tick = song->speed-1; |
| 1625 | else |
| 1626 | song->tick = 0; |
| 1627 | |
| 1628 | ciaSetBPM = -1; // fix possibly stuck "set BPM" flag |
| 1629 | |
| 1630 | if (!editor.stepPlayEnabled) |
| 1631 | { |
| 1632 | editor.playMode = PLAY_MODE_PATTERN; |
| 1633 | editor.currMode = MODE_PLAY; |
| 1634 | } |
| 1635 | |
| 1636 | editor.didQuantize = false; |
| 1637 | editor.songPlaying = true; |
| 1638 | |
| 1639 | if (audioWasntLocked) |
| 1640 | unlockAudio(); |
| 1641 | } |
| 1642 | |
| 1643 | void incPatt(void) |
| 1644 | { |
| 1645 | modPattern++; |
| 1646 | if (modPattern > MAX_PATTERNS-1) |
| 1647 | modPattern = 0; |
| 1648 | |
| 1649 | song->currPattern = modPattern; |
| 1650 | |
| 1651 | ui.updatePatternData = true; |
| 1652 | ui.updateCurrPattText = true; |
| 1653 | } |
| 1654 | |
| 1655 | void decPatt(void) |
| 1656 | { |
| 1657 | modPattern--; |
| 1658 | if (modPattern < 0) |
| 1659 | modPattern = MAX_PATTERNS - 1; |
| 1660 | |
| 1661 | song->currPattern = modPattern; |
| 1662 | |
| 1663 | ui.updatePatternData = true; |
| 1664 | ui.updateCurrPattText = true; |
| 1665 | } |
| 1666 | |
| 1667 | void modPlay(int16_t patt, int16_t pos, int8_t row) |
| 1668 | { |
| 1669 | const bool audioWasntLocked = !audio.locked; |
| 1670 | if (audioWasntLocked) |
| 1671 | lockAudio(); |
| 1672 | |
| 1673 | doStopIt(false); |
| 1674 | turnOffVoices(); |
| 1675 | |
| 1676 | if (row != -1) |
| 1677 | { |
| 1678 | if (row >= 0 && row <= 63) |
| 1679 | { |
| 1680 | song->row = row; |
| 1681 | song->currRow = row; |
| 1682 | } |
| 1683 | } |
| 1684 | else |
| 1685 | { |
| 1686 | song->row = 0; |
| 1687 | song->currRow = 0; |
| 1688 | } |
| 1689 | |
| 1690 | if (editor.playMode != PLAY_MODE_PATTERN) |
| 1691 | { |
| 1692 | if (modPos >= song->header.songLength) |
| 1693 | song->currPos = modPos = 0; |
| 1694 | |
| 1695 | if (pos >= 0 && pos < song->header.songLength) |
| 1696 | song->currPos = modPos = pos; |
| 1697 | |
| 1698 | if (pos >= song->header.songLength) |
| 1699 | song->currPos = modPos = 0; |
| 1700 | } |
| 1701 | |
| 1702 | if (patt >= 0 && patt <= MAX_PATTERNS-1) |
| 1703 | song->currPattern = modPattern = (int8_t)patt; |
| 1704 | else |
| 1705 | song->currPattern = modPattern = (int8_t)song->header.patternTable[modPos]; |
| 1706 | |
| 1707 | editor.currPatternDisp = &song->header.patternTable[modPos]; |
| 1708 | editor.currPosEdPattDisp = &song->header.patternTable[modPos]; |
| 1709 | |
| 1710 | song->tick = song->speed-1; |
| 1711 | ciaSetBPM = -1; // fix possibly stuck "set BPM" flag |
| 1712 | |
| 1713 | modRenderDone = false; |
| 1714 | editor.songPlaying = true; |
| 1715 | editor.didQuantize = false; |
| 1716 | |
| 1717 | // don't reset playback counter in "play/rec pattern" mode |
| 1718 | if (editor.playMode != PLAY_MODE_PATTERN) |
| 1719 | editor.playbackSeconds = editor.playbackSecondsFrac = 0; |
| 1720 | |
| 1721 | audio.tickSampleCounter = 0; // zero tick sample counter so that it will instantly initiate a tick |
| 1722 | audio.tickSampleCounterFrac = 0; |
| 1723 | |
| 1724 | if (audioWasntLocked) |
| 1725 | unlockAudio(); |
| 1726 | |
| 1727 | if (!editor.pat2SmpOngoing && !editor.mod2WavOngoing) |
| 1728 | { |
| 1729 | ui.updateSongPos = true; |
| 1730 | ui.updatePatternData = true; |
| 1731 | ui.updateSongPattern = true; |
| 1732 | ui.updateCurrPattText = true; |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | void clearSong(void) |
| 1737 | { |
| 1738 | ASSERT(song != NULL); |
| 1739 | if (song == NULL) |
| 1740 | return; |
| 1741 | |
| 1742 | memset(song->header.patternTable, 0, sizeof (song->header.patternTable)); |
| 1743 | memset(song->header.name, 0, sizeof (song->header.name)); |
| 1744 | |
| 1745 | posEdClearNames(); |
| 1746 | |
| 1747 | editor.muted[0] = false; |
| 1748 | editor.muted[1] = false; |
| 1749 | editor.muted[2] = false; |
| 1750 | editor.muted[3] = false; |
| 1751 | |
| 1752 | editor.f6Pos = 0; |
| 1753 | editor.f7Pos = 16; |
| 1754 | editor.f8Pos = 32; |
| 1755 | editor.f9Pos = 48; |
| 1756 | editor.f10Pos = 63; |
| 1757 | |
| 1758 | editor.playbackSeconds = editor.playbackSecondsFrac = 0; |
| 1759 | editor.metroFlag = false; |
| 1760 | editor.currSample = 0; |
| 1761 | editor.editMoveAdd = 1; |
| 1762 | editor.blockMarkFlag = false; |
| 1763 | editor.swapChannelFlag = false; |
| 1764 | |
| 1765 | song->header.songLength = 1; |
| 1766 | |
| 1767 | for (int32_t i = 0; i < MAX_PATTERNS; i++) |
| 1768 | memset(song->patterns[i], 0, (MOD_ROWS * PAULA_VOICES) * sizeof (note_t)); |
| 1769 | |
| 1770 | moduleChannel_t *ch = song->channels; |
| 1771 | for (int32_t i = 0; i < PAULA_VOICES; i++, ch++) |
| 1772 | { |
| 1773 | ch->n_wavecontrol = 0; |
| 1774 | ch->n_glissfunk = 0; |
| 1775 | ch->n_finetune = 0; |
| 1776 | ch->n_loopcount = 0; |
| 1777 | } |
| 1778 | |
| 1779 | modSetPos(0, 0); // this also refreshes pattern data |
| 1780 | |
| 1781 | song->currPos = 0; |
| 1782 | song->currPattern = 0; |
| 1783 | editor.currPatternDisp = &song->header.patternTable[0]; |
| 1784 | editor.currPosEdPattDisp = &song->header.patternTable[0]; |
| 1785 | |
| 1786 | modSetTempo(editor.initialTempo, true); |
| 1787 | modSetSpeed(editor.initialSpeed); |
| 1788 | |
| 1789 | // disable the LED filter after clearing the song (real PT2 doesn't do this) |
| 1790 | setLEDFilter(false); |
| 1791 | |
| 1792 | updateCurrSample(); |
| 1793 | |
| 1794 | ui.updateSongSize = true; |
| 1795 | ui.updateSongLength = true; |
| 1796 | ui.updateSongName = true; |
| 1797 | renderMuteButtons(); |
| 1798 | } |
| 1799 | |
| 1800 | void clearSamples(void) |
| 1801 | { |
| 1802 | ASSERT(song != NULL); |
| 1803 | if (song == NULL) |
| 1804 | return; |
| 1805 | |
| 1806 | moduleSample_t *s = song->samples; |
| 1807 | for (int32_t i = 0; i < MOD_SAMPLES; i++, s++) |
| 1808 | { |
| 1809 | s->fineTune = 0; |
| 1810 | s->length = 0; |
| 1811 | s->loopLength = 2; |
| 1812 | s->loopStart = 0; |
| 1813 | s->volume = 0; |
| 1814 | |
| 1815 | memset(s->text, 0, sizeof (s->text)); |
| 1816 | } |
| 1817 | |
| 1818 | memset(song->sampleData, 0, (MOD_SAMPLES + 1) * config.maxSampleLength); |
| 1819 | |
| 1820 | editor.currSample = 0; |
| 1821 | editor.hiLowInstr = 0; |
| 1822 | editor.sampleZero = false; |
| 1823 | editor.blockMarkFlag = false; |
| 1824 | editor.samplePos = 0; |
| 1825 | |
| 1826 | updateCurrSample(); |
| 1827 | } |
| 1828 | |
| 1829 | void modFree(void) |
| 1830 | { |
| 1831 | if (song == NULL) |
| 1832 | return; // not allocated |
| 1833 | |
| 1834 | const bool audioWasntLocked = !audio.locked; |
| 1835 | if (audioWasntLocked) |
| 1836 | lockAudio(); |
| 1837 | |
| 1838 | turnOffVoices(); |
| 1839 | |
| 1840 | for (int32_t i = 0; i < MAX_PATTERNS; i++) |
| 1841 | { |
| 1842 | if (song->patterns[i] != NULL) |
| 1843 | free(song->patterns[i]); |
| 1844 | } |
| 1845 | |
| 1846 | if (song->sampleData != NULL) |
| 1847 | free(song->sampleData); |
| 1848 | |
| 1849 | free(song); |
| 1850 | song = NULL; |
| 1851 | |
| 1852 | if (audioWasntLocked) |
| 1853 | unlockAudio(); |
| 1854 | } |
| 1855 | |
| 1856 | void restartSong(void) // for the beginning of MOD2WAV/PAT2SMP |
| 1857 | { |
| 1858 | if (editor.songPlaying) |
| 1859 | modStop(); |
| 1860 | |
| 1861 | editor.playMode = PLAY_MODE_NORMAL; |
| 1862 | editor.blockMarkFlag = false; |
| 1863 | |
| 1864 | song->row = 0; |
| 1865 | song->currRow = 0; |
| 1866 | song->rowsCounter = 0; |
| 1867 | |
| 1868 | memset(editor.rowVisitTable, 0, 128 * MOD_ROWS); // for MOD2WAV |
| 1869 | |
| 1870 | if (editor.pat2SmpOngoing) |
| 1871 | { |
| 1872 | modSetSpeed(song->currSpeed); |
| 1873 | modSetTempo(song->currBPM, true); |
| 1874 | modPlay(DONT_SET_PATTERN, DONT_SET_ORDER, 0); |
| 1875 | } |
| 1876 | else |
| 1877 | { |
| 1878 | song->currSpeed = 6; |
| 1879 | song->currBPM = 125; |
| 1880 | modSetSpeed(song->currSpeed); |
| 1881 | modSetTempo(song->currBPM, true); |
| 1882 | |
| 1883 | modPlay(DONT_SET_PATTERN, 0, 0); |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | // this function is meant for the end of MOD2WAV/PAT2SMP |
| 1888 | void resetSong(void) // only call this after storeTempVariables() has been called! |
| 1889 | { |
| 1890 | modStop(); |
| 1891 | |
| 1892 | editor.songPlaying = false; |
| 1893 | editor.playMode = PLAY_MODE_NORMAL; |
| 1894 | editor.currMode = MODE_IDLE; |
| 1895 | |
| 1896 | turnOffVoices(); |
| 1897 | |
| 1898 | memset((int8_t *)editor.vuMeterVolumes, 0, sizeof (editor.vuMeterVolumes)); |
| 1899 | memset((int8_t *)editor.realVuMeterVolumes, 0, sizeof (editor.realVuMeterVolumes)); |
| 1900 | memset((int8_t *)editor.spectrumVolumes, 0, sizeof (editor.spectrumVolumes)); |
| 1901 | |
| 1902 | initializeModuleChannels(song); |
| 1903 | |
| 1904 | modPos = oldPos; |
| 1905 | modPattern = (int8_t)oldPattern; |
| 1906 | |
| 1907 | song->row = oldRow; |
| 1908 | song->currRow = oldRow; |
| 1909 | song->currBPM = oldBPM; |
| 1910 | song->currPos = oldPos; |
| 1911 | song->currPattern = oldPattern; |
| 1912 | |
| 1913 | editor.currPosDisp = &song->currPos; |
| 1914 | editor.currEditPatternDisp = &song->currPattern; |
| 1915 | editor.currPatternDisp = &song->header.patternTable[song->currPos]; |
| 1916 | editor.currPosEdPattDisp = &song->header.patternTable[song->currPos]; |
| 1917 | |
| 1918 | modSetSpeed(oldSpeed); |
| 1919 | modSetTempo(oldBPM, true); |
| 1920 | |
| 1921 | doStopIt(true); |
| 1922 | |
| 1923 | song->tick = 0; |
| 1924 | modRenderDone = false; |
| 1925 | } |