| 1 | // for finding memory leaks in debug mode with Visual Studio |
| 2 | #if defined _DEBUG && defined _MSC_VER |
| 3 | #include <crtdbg.h> |
| 4 | #endif |
| 5 | |
| 6 | #include <stdint.h> |
| 7 | #include <stdbool.h> |
| 8 | #include <string.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/stat.h> |
| 12 | #ifdef _WIN32 |
| 13 | #include <direct.h> |
| 14 | #else |
| 15 | #include <unistd.h> |
| 16 | #endif |
| 17 | #include "pt2_helpers.h" |
| 18 | #include "pt2_textout.h" |
| 19 | #include "pt2_tables.h" |
| 20 | #include "pt2_diskop.h" |
| 21 | #include "pt2_sampler.h" |
| 22 | #include "pt2_visuals.h" |
| 23 | #include "pt2_keyboard.h" |
| 24 | #include "pt2_config.h" |
| 25 | #include "pt2_audio.h" |
| 26 | #include "pt2_chordmaker.h" |
| 27 | #include "pt2_edit.h" |
| 28 | #include "pt2_replayer.h" |
| 29 | #include "pt2_visuals_sync.h" |
| 30 | |
| 31 | static const int8_t scancode2NoteLo[52] = // "USB usage page standard" order |
| 32 | { |
| 33 | 7, 4, 3, 16, -1, 6, 8, 24, |
| 34 | 10, -1, 13, 11, 9, 26, 28, 12, |
| 35 | 17, 1, 19, 23, 5, 14, 2, 21, |
| 36 | 0, -1, 13, 15, -1, 18, 20, 22, |
| 37 | -1, 25, 27, -1, -1, -1, -1, -1, |
| 38 | -1, 30, 29, 31, 30, -1, 15, -1, |
| 39 | -1, 12, 14, 16 |
| 40 | }; |
| 41 | |
| 42 | static const int8_t scancode2NoteHi[52] = // "USB usage page standard" order |
| 43 | { |
| 44 | 19, 16, 15, 28, -1, 18, 20, -2, |
| 45 | 22, -1, 25, 23, 21, -2, -2, 24, |
| 46 | 29, 13, 31, 35, 17, 26, 14, 33, |
| 47 | 12, -1, 25, 27, -1, 30, 32, 34, |
| 48 | -1, -2, -2, -1, -1, -1, -1, -1, |
| 49 | -1, -2, -2, -2, -2, -1, 27, -1, |
| 50 | -1, 24, 26, 28 |
| 51 | }; |
| 52 | |
| 53 | static uint8_t quantizeCheck(uint8_t row) |
| 54 | { |
| 55 | ASSERT(song != NULL); |
| 56 | if (song == NULL) |
| 57 | return row; |
| 58 | |
| 59 | const uint8_t quantize = (uint8_t)config.quantizeValue; |
| 60 | |
| 61 | editor.didQuantize = false; |
| 62 | if (editor.currMode == MODE_RECORD) |
| 63 | { |
| 64 | if (quantize == 0) |
| 65 | { |
| 66 | return row; |
| 67 | } |
| 68 | else if (quantize == 1) |
| 69 | { |
| 70 | if (song->tick > song->speed>>1) |
| 71 | { |
| 72 | row = (row + 1) & 63; |
| 73 | editor.didQuantize = true; |
| 74 | } |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | uint8_t tempRow = ((((quantize >> 1) + row) & 63) / quantize) * quantize; |
| 79 | if (tempRow > row) |
| 80 | editor.didQuantize = true; |
| 81 | |
| 82 | return tempRow; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return row; |
| 87 | } |
| 88 | |
| 89 | static void jamAndPlaceSample(SDL_Scancode scancode, bool normalMode) |
| 90 | { |
| 91 | uint8_t chNum = cursor.channel; |
| 92 | ASSERT(chNum < PAULA_VOICES); |
| 93 | |
| 94 | moduleChannel_t *ch = &song->channels[chNum]; |
| 95 | note_t *note = &song->patterns[song->currPattern][(quantizeCheck(song->currRow) * PAULA_VOICES) + chNum]; |
| 96 | |
| 97 | int8_t noteVal = normalMode ? keyToNote(scancode) : pNoteTable[editor.currSample]; |
| 98 | if (noteVal >= 0) |
| 99 | { |
| 100 | moduleSample_t *s = &song->samples[editor.currSample]; |
| 101 | |
| 102 | int16_t tempPeriod = periodTable[((s->fineTune & 0xF) * 37) + noteVal]; |
| 103 | uint16_t cleanPeriod = periodTable[noteVal]; |
| 104 | |
| 105 | editor.currPlayNote = noteVal; |
| 106 | |
| 107 | // play current sample |
| 108 | |
| 109 | // don't play sample if we quantized to another row (will be played in modplayer instead) |
| 110 | if (editor.currMode != MODE_RECORD || !editor.didQuantize) |
| 111 | { |
| 112 | lockAudio(); |
| 113 | |
| 114 | ch->n_samplenum = editor.currSample; |
| 115 | ch->n_volume = s->volume; |
| 116 | ch->n_period = tempPeriod; |
| 117 | ch->n_start = &song->sampleData[s->offset]; |
| 118 | ch->n_length = (uint16_t)((s->loopStart > 0) ? (s->loopStart + s->loopLength) >> 1 : s->length >> 1); |
| 119 | ch->n_loopstart = &song->sampleData[s->offset + s->loopStart]; |
| 120 | ch->n_replen = (uint16_t)(s->loopLength >> 1); |
| 121 | |
| 122 | if (ch->n_length == 0) |
| 123 | ch->n_length = 1; |
| 124 | |
| 125 | const uint32_t voiceAddr = 0xDFF0A0 + (chNum * 16); |
| 126 | paulaWriteWord(voiceAddr + 8, ch->n_volume); |
| 127 | paulaWriteWord(voiceAddr + 6, ch->n_period); |
| 128 | paulaWritePtr(voiceAddr + 0, ch->n_start); |
| 129 | paulaWriteWord(voiceAddr + 4, ch->n_length); |
| 130 | |
| 131 | if (!editor.muted[chNum]) |
| 132 | paulaWriteWord(0xDFF096, 0x8000 | ch->n_dmabit); // voice DMA on |
| 133 | else |
| 134 | paulaWriteWord(0xDFF096, ch->n_dmabit); // voice DMA off |
| 135 | |
| 136 | // these take effect after the current DMA cycle is done |
| 137 | paulaWritePtr(voiceAddr + 0, ch->n_loopstart); |
| 138 | paulaWriteWord(voiceAddr + 4, ch->n_replen); |
| 139 | |
| 140 | // update tracker visuals |
| 141 | |
| 142 | setVisualsVolume(chNum, ch->n_volume); |
| 143 | setVisualsPeriod(chNum, ch->n_period); |
| 144 | setVisualsDataPtr(chNum, ch->n_start); |
| 145 | setVisualsLength(chNum, ch->n_length); |
| 146 | |
| 147 | if (!editor.muted[chNum]) |
| 148 | setVisualsDMACON(0x8000 | ch->n_dmabit); |
| 149 | else |
| 150 | setVisualsDMACON(ch->n_dmabit); |
| 151 | |
| 152 | setVisualsDataPtr(chNum, ch->n_loopstart); |
| 153 | setVisualsLength(chNum, ch->n_replen); |
| 154 | |
| 155 | unlockAudio(); |
| 156 | } |
| 157 | |
| 158 | // normalMode = normal keys, or else keypad keys (in jam mode) |
| 159 | if (normalMode || editor.pNoteFlag != 0) |
| 160 | { |
| 161 | if (normalMode || editor.pNoteFlag == 2) |
| 162 | { |
| 163 | // insert note and sample number |
| 164 | if (!ui.samplerScreenShown && (editor.currMode == MODE_EDIT || editor.currMode == MODE_RECORD)) |
| 165 | { |
| 166 | note->sample = editor.sampleZero ? 0 : (editor.currSample + 1); |
| 167 | note->period = cleanPeriod; |
| 168 | |
| 169 | if (editor.autoInsFlag) |
| 170 | { |
| 171 | note->command = editor.effectMacros[editor.autoInsSlot] >> 8; |
| 172 | note->param = editor.effectMacros[editor.autoInsSlot] & 0xFF; |
| 173 | } |
| 174 | |
| 175 | if (editor.currMode != MODE_RECORD) |
| 176 | modSetPos(DONT_SET_ORDER, (song->currRow + editor.editMoveAdd) & 63); |
| 177 | |
| 178 | updateWindowTitle(MOD_IS_MODIFIED); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (editor.multiFlag) |
| 183 | gotoNextMulti(); |
| 184 | } |
| 185 | |
| 186 | // PT quirk: spectrum analyzer is still handled here even if channel is muted |
| 187 | updateSpectrumAnalyzer(s->volume, tempPeriod); |
| 188 | } |
| 189 | else if (noteVal == -2) |
| 190 | { |
| 191 | // delete note and sample if illegal note (= -2, -1 = ignore) key was entered |
| 192 | |
| 193 | if (normalMode || editor.pNoteFlag == 2) |
| 194 | { |
| 195 | if (!ui.samplerScreenShown && (editor.currMode == MODE_EDIT || editor.currMode == MODE_RECORD)) |
| 196 | { |
| 197 | note->period = 0; |
| 198 | note->sample = 0; |
| 199 | |
| 200 | if (editor.currMode != MODE_RECORD) |
| 201 | modSetPos(DONT_SET_ORDER, (song->currRow + editor.editMoveAdd) & 63); |
| 202 | |
| 203 | updateWindowTitle(MOD_IS_MODIFIED); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void handleEditKeys(SDL_Scancode scancode, bool normalMode) |
| 210 | { |
| 211 | if (ui.editTextFlag) |
| 212 | return; |
| 213 | |
| 214 | if (ui.samplerScreenShown || (editor.currMode == MODE_IDLE || editor.currMode == MODE_PLAY)) |
| 215 | { |
| 216 | // at this point it will only jam, not place it |
| 217 | if (!keyb.leftAltPressed && !keyb.leftAmigaPressed && !keyb.leftCtrlPressed && !keyb.shiftPressed) |
| 218 | jamAndPlaceSample(scancode, normalMode); |
| 219 | |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | // handle modified (ALT/CTRL/SHIFT etc) keys for editing |
| 224 | if (editor.currMode == MODE_EDIT || editor.currMode == MODE_RECORD) |
| 225 | { |
| 226 | if (handleSpecialKeys(scancode)) |
| 227 | { |
| 228 | if (editor.currMode != MODE_RECORD) |
| 229 | modSetPos(DONT_SET_ORDER, (song->currRow + editor.editMoveAdd) & 63); |
| 230 | |
| 231 | return; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // are we editing a note, or other stuff? |
| 236 | if (cursor.mode != CURSOR_NOTE) |
| 237 | { |
| 238 | // if we held down any key modifier at this point, then do nothing |
| 239 | if (keyb.leftAltPressed || keyb.leftAmigaPressed || keyb.leftCtrlPressed || keyb.shiftPressed) |
| 240 | return; |
| 241 | |
| 242 | if (editor.currMode == MODE_EDIT || editor.currMode == MODE_RECORD) |
| 243 | { |
| 244 | int8_t hexKey, numberKey; |
| 245 | |
| 246 | if (scancode == SDL_SCANCODE_0) |
| 247 | numberKey = 0; |
| 248 | else if (scancode >= SDL_SCANCODE_1 && scancode <= SDL_SCANCODE_9) |
| 249 | numberKey = (int8_t)scancode - (SDL_SCANCODE_1-1); |
| 250 | else |
| 251 | numberKey = -1; |
| 252 | |
| 253 | if (scancode >= SDL_SCANCODE_A && scancode <= SDL_SCANCODE_F) |
| 254 | hexKey = 10 + ((int8_t)scancode - SDL_SCANCODE_A); |
| 255 | else |
| 256 | hexKey = -1; |
| 257 | |
| 258 | int8_t key = -1; |
| 259 | if (numberKey != -1) |
| 260 | { |
| 261 | if (key == -1) |
| 262 | key = 0; |
| 263 | |
| 264 | key += numberKey; |
| 265 | } |
| 266 | |
| 267 | if (hexKey != -1) |
| 268 | { |
| 269 | if (key == -1) |
| 270 | key = 0; |
| 271 | |
| 272 | key += hexKey; |
| 273 | } |
| 274 | |
| 275 | note_t *note = &song->patterns[song->currPattern][(song->currRow * PAULA_VOICES) + cursor.channel]; |
| 276 | switch (cursor.mode) |
| 277 | { |
| 278 | case CURSOR_SAMPLE1: |
| 279 | { |
| 280 | if (key != -1 && key < 2) |
| 281 | { |
| 282 | note->sample = (uint8_t)((note->sample & 0x0F) | (key << 4)); |
| 283 | |
| 284 | if (editor.currMode != MODE_RECORD) |
| 285 | modSetPos(DONT_SET_ORDER, (song->currRow + editor.editMoveAdd) & 63); |
| 286 | |
| 287 | updateWindowTitle(MOD_IS_MODIFIED); |
| 288 | } |
| 289 | } |
| 290 | break; |
| 291 | |
| 292 | case CURSOR_SAMPLE2: |
| 293 | { |
| 294 | if (key != -1 && key < 16) |
| 295 | { |
| 296 | note->sample = (uint8_t)((note->sample & 0xF0) | key); |
| 297 | |
| 298 | if (editor.currMode != MODE_RECORD) |
| 299 | modSetPos(DONT_SET_ORDER, (song->currRow + editor.editMoveAdd) & 63); |
| 300 | |
| 301 | updateWindowTitle(MOD_IS_MODIFIED); |
| 302 | } |
| 303 | } |
| 304 | break; |
| 305 | |
| 306 | case CURSOR_CMD: |
| 307 | { |
| 308 | if (key != -1 && key < 16) |
| 309 | { |
| 310 | note->command = (uint8_t)key; |
| 311 | |
| 312 | if (editor.currMode != MODE_RECORD) |
| 313 | modSetPos(DONT_SET_ORDER, (song->currRow + editor.editMoveAdd) & 63); |
| 314 | |
| 315 | updateWindowTitle(MOD_IS_MODIFIED); |
| 316 | } |
| 317 | } |
| 318 | break; |
| 319 | |
| 320 | case CURSOR_PARAM1: |
| 321 | { |
| 322 | if (key != -1 && key < 16) |
| 323 | { |
| 324 | note->param = (uint8_t)((note->param & 0x0F) | (key << 4)); |
| 325 | |
| 326 | if (editor.currMode != MODE_RECORD) |
| 327 | modSetPos(DONT_SET_ORDER, (song->currRow + editor.editMoveAdd) & 63); |
| 328 | |
| 329 | updateWindowTitle(MOD_IS_MODIFIED); |
| 330 | } |
| 331 | } |
| 332 | break; |
| 333 | |
| 334 | case CURSOR_PARAM2: |
| 335 | { |
| 336 | if (key != -1 && key < 16) |
| 337 | { |
| 338 | note->param = (uint8_t)((note->param & 0xF0) | key); |
| 339 | |
| 340 | if (editor.currMode != MODE_RECORD) |
| 341 | modSetPos(DONT_SET_ORDER, (song->currRow + editor.editMoveAdd) & 63); |
| 342 | |
| 343 | updateWindowTitle(MOD_IS_MODIFIED); |
| 344 | } |
| 345 | } |
| 346 | break; |
| 347 | |
| 348 | default: break; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | if (scancode == SDL_SCANCODE_DELETE) |
| 355 | { |
| 356 | if (editor.currMode == MODE_EDIT || editor.currMode == MODE_RECORD) |
| 357 | { |
| 358 | note_t *note = &song->patterns[song->currPattern][(song->currRow * PAULA_VOICES) + cursor.channel]; |
| 359 | |
| 360 | if (!keyb.leftAltPressed) |
| 361 | { |
| 362 | note->sample = 0; |
| 363 | note->period = 0; |
| 364 | } |
| 365 | |
| 366 | if (keyb.shiftPressed || keyb.leftAltPressed) |
| 367 | { |
| 368 | note->command = 0; |
| 369 | note->param = 0; |
| 370 | } |
| 371 | |
| 372 | if (editor.currMode != MODE_RECORD) |
| 373 | modSetPos(DONT_SET_ORDER, (song->currRow + editor.editMoveAdd) & 63); |
| 374 | |
| 375 | updateWindowTitle(MOD_IS_MODIFIED); |
| 376 | } |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | // if we held down any key modifier at this point, then do nothing |
| 381 | if (keyb.leftAltPressed || keyb.leftAmigaPressed || keyb.leftCtrlPressed || keyb.shiftPressed) |
| 382 | return; |
| 383 | |
| 384 | jamAndPlaceSample(scancode, normalMode); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | bool handleSpecialKeys(SDL_Scancode scancode) |
| 390 | { |
| 391 | if (!keyb.leftAltPressed) |
| 392 | return false; |
| 393 | |
| 394 | note_t *patt = song->patterns[song->currPattern]; |
| 395 | note_t *note = &patt[(song->currRow * PAULA_VOICES) + cursor.channel]; |
| 396 | note_t *prevNote = &patt[(((song->currRow - 1) & 63) * PAULA_VOICES) + cursor.channel]; |
| 397 | |
| 398 | if (scancode >= SDL_SCANCODE_1 && scancode <= SDL_SCANCODE_0) |
| 399 | { |
| 400 | // insert stored effect (buffer[0..8]) |
| 401 | note->command = editor.effectMacros[scancode - SDL_SCANCODE_1] >> 8; |
| 402 | note->param = editor.effectMacros[scancode - SDL_SCANCODE_1] & 0xFF; |
| 403 | |
| 404 | updateWindowTitle(MOD_IS_MODIFIED); |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | // copy command+effect from above into current command+effect |
| 409 | if (scancode == SDL_SCANCODE_BACKSLASH) |
| 410 | { |
| 411 | note->command = prevNote->command; |
| 412 | note->param = prevNote->param; |
| 413 | |
| 414 | updateWindowTitle(MOD_IS_MODIFIED); |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | // copy command+(effect + 1) from above into current command+effect |
| 419 | if (scancode == SDL_SCANCODE_EQUALS) |
| 420 | { |
| 421 | note->command = prevNote->command; |
| 422 | note->param = prevNote->param + 1; // wraps 0x00..0xFF |
| 423 | |
| 424 | updateWindowTitle(MOD_IS_MODIFIED); |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | // copy command+(effect - 1) from above into current command+effect |
| 429 | if (scancode == SDL_SCANCODE_MINUS) |
| 430 | { |
| 431 | note->command = prevNote->command; |
| 432 | note->param = prevNote->param - 1; // wraps 0x00..0xFF |
| 433 | |
| 434 | updateWindowTitle(MOD_IS_MODIFIED); |
| 435 | return true; |
| 436 | } |
| 437 | |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | void handleSampleJamming(SDL_Scancode scancode) // used for the sampling feature (in SAMPLER) |
| 442 | { |
| 443 | const int32_t chNum = cursor.channel; |
| 444 | |
| 445 | if (scancode == SDL_SCANCODE_NONUSBACKSLASH) |
| 446 | { |
| 447 | turnOffVoices(); // magic "kill all voices" button |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | int8_t noteVal = keyToNote(scancode); |
| 452 | if (noteVal < 0 || noteVal > 35) |
| 453 | return; |
| 454 | |
| 455 | moduleSample_t *s = &song->samples[editor.currSample]; |
| 456 | if (s->length <= 1) |
| 457 | return; |
| 458 | |
| 459 | moduleChannel_t *ch = &song->channels[chNum]; |
| 460 | |
| 461 | int8_t *n_start = &song->sampleData[s->offset]; |
| 462 | int8_t vol = 64; |
| 463 | uint16_t n_length = (uint16_t)(s->length >> 1); |
| 464 | uint16_t period = periodTable[((s->fineTune & 0xF) * 37) + noteVal]; |
| 465 | |
| 466 | lockAudio(); |
| 467 | |
| 468 | ch->n_samplenum = editor.currSample; // needed for sample playback/sampling line |
| 469 | |
| 470 | const uint32_t voiceAddr = 0xDFF0A0 + (chNum * 16); |
| 471 | paulaWriteWord(voiceAddr + 8, vol); |
| 472 | paulaWriteWord(voiceAddr + 6, period); |
| 473 | paulaWritePtr(voiceAddr + 0, n_start); |
| 474 | paulaWriteWord(voiceAddr + 4, n_length); |
| 475 | |
| 476 | if (!editor.muted[chNum]) |
| 477 | paulaWriteWord(0xDFF096, 0x8000 | ch->n_dmabit); // voice DMA on |
| 478 | else |
| 479 | paulaWriteWord(0xDFF096, ch->n_dmabit); // voice DMA off |
| 480 | |
| 481 | // these take effect after the current DMA cycle is done |
| 482 | paulaWritePtr(voiceAddr + 0, NULL); // data |
| 483 | paulaWriteWord(voiceAddr + 4, 1); // length |
| 484 | |
| 485 | // update tracker visuals |
| 486 | |
| 487 | setVisualsVolume(chNum, vol); |
| 488 | setVisualsPeriod(chNum, period); |
| 489 | setVisualsDataPtr(chNum, n_start); |
| 490 | setVisualsLength(chNum, n_length); |
| 491 | |
| 492 | if (!editor.muted[chNum]) |
| 493 | setVisualsDMACON(0x8000 | ch->n_dmabit); |
| 494 | else |
| 495 | setVisualsDMACON(ch->n_dmabit); |
| 496 | |
| 497 | setVisualsDataPtr(chNum, NULL); |
| 498 | setVisualsLength(chNum, 1); |
| 499 | |
| 500 | unlockAudio(); |
| 501 | } |
| 502 | |
| 503 | void saveUndo(void) |
| 504 | { |
| 505 | memcpy(editor.undoBuffer, song->patterns[song->currPattern], sizeof (note_t) * (PAULA_VOICES * MOD_ROWS)); |
| 506 | } |
| 507 | |
| 508 | void undoLastChange(void) |
| 509 | { |
| 510 | for (uint16_t i = 0; i < MOD_ROWS * PAULA_VOICES; i++) |
| 511 | { |
| 512 | note_t data = editor.undoBuffer[i]; |
| 513 | editor.undoBuffer[i] = song->patterns[song->currPattern][i]; |
| 514 | song->patterns[song->currPattern][i] = data; |
| 515 | } |
| 516 | |
| 517 | updateWindowTitle(MOD_IS_MODIFIED); |
| 518 | ui.updatePatternData = true; |
| 519 | } |
| 520 | |
| 521 | void copySampleTrack(void) |
| 522 | { |
| 523 | if (editor.trackPattFlag == 2) |
| 524 | { |
| 525 | // copy from one sample slot to another |
| 526 | |
| 527 | // never attempt to swap if from and/or to is 0 |
| 528 | if (editor.sampleFrom == 0 || editor.sampleTo == 0) |
| 529 | { |
| 530 | displayErrorMsg("FROM/TO = 0 !"); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | moduleSample_t *smpTo = &song->samples[editor.sampleTo - 1]; |
| 535 | moduleSample_t *smpFrom = &song->samples[editor.sampleFrom - 1]; |
| 536 | |
| 537 | turnOffVoices(); |
| 538 | |
| 539 | // copy |
| 540 | uint32_t tmpOffset = smpTo->offset; |
| 541 | *smpTo = *smpFrom; |
| 542 | smpTo->offset = tmpOffset; |
| 543 | |
| 544 | // update the copied sample's GUI text pointers |
| 545 | smpTo->volumeDisp = &smpTo->volume; |
| 546 | smpTo->lengthDisp = &smpTo->length; |
| 547 | smpTo->loopStartDisp = &smpTo->loopStart; |
| 548 | smpTo->loopLengthDisp = &smpTo->loopLength; |
| 549 | |
| 550 | // copy sample data |
| 551 | memcpy(&song->sampleData[smpTo->offset], &song->sampleData[smpFrom->offset], config.maxSampleLength); |
| 552 | |
| 553 | updateCurrSample(); |
| 554 | ui.updateSongSize = true; |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | // copy sample number in track/pattern |
| 559 | if (editor.trackPattFlag == 0) |
| 560 | { |
| 561 | for (int32_t i = 0; i < MOD_ROWS; i++) |
| 562 | { |
| 563 | note_t *noteSrc = &song->patterns[song->currPattern][(i * PAULA_VOICES) + cursor.channel]; |
| 564 | if (noteSrc->sample == editor.sampleFrom) |
| 565 | noteSrc->sample = editor.sampleTo; |
| 566 | } |
| 567 | } |
| 568 | else |
| 569 | { |
| 570 | for (int32_t i = 0; i < PAULA_VOICES; i++) |
| 571 | { |
| 572 | for (int32_t j = 0; j < MOD_ROWS; j++) |
| 573 | { |
| 574 | note_t *noteSrc = &song->patterns[song->currPattern][(j * PAULA_VOICES) + i]; |
| 575 | if (noteSrc->sample == editor.sampleFrom) |
| 576 | noteSrc->sample = editor.sampleTo; |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | ui.updatePatternData = true; |
| 582 | } |
| 583 | |
| 584 | editor.samplePos = 0; |
| 585 | updateSamplePos(); |
| 586 | |
| 587 | updateWindowTitle(MOD_IS_MODIFIED); |
| 588 | } |
| 589 | |
| 590 | void exchSampleTrack(void) |
| 591 | { |
| 592 | if (editor.trackPattFlag == 2) |
| 593 | { |
| 594 | // exchange sample slots |
| 595 | |
| 596 | // never attempt to swap if from and/or to is 0 |
| 597 | if (editor.sampleFrom == 0 || editor.sampleTo == 0) |
| 598 | { |
| 599 | displayErrorMsg("FROM/TO = 0 !"); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | moduleSample_t *smpTo = &song->samples[editor.sampleTo-1]; |
| 604 | moduleSample_t *smpFrom = &song->samples[editor.sampleFrom-1]; |
| 605 | |
| 606 | turnOffVoices(); |
| 607 | |
| 608 | // swap offsets first so that the next swap will leave offsets intact |
| 609 | uint32_t tmpOffset = smpFrom->offset; |
| 610 | smpFrom->offset = smpTo->offset; |
| 611 | smpTo->offset = tmpOffset; |
| 612 | |
| 613 | // swap sample (now offsets are left as before) |
| 614 | moduleSample_t smpTmp = *smpFrom; |
| 615 | *smpFrom = *smpTo; |
| 616 | *smpTo = smpTmp; |
| 617 | |
| 618 | // update the swapped sample's GUI text pointers |
| 619 | smpFrom->volumeDisp = &smpFrom->volume; |
| 620 | smpFrom->lengthDisp = &smpFrom->length; |
| 621 | smpFrom->loopStartDisp = &smpFrom->loopStart; |
| 622 | smpFrom->loopLengthDisp = &smpFrom->loopLength; |
| 623 | smpTo->volumeDisp = &smpTo->volume; |
| 624 | smpTo->lengthDisp = &smpTo->length; |
| 625 | smpTo->loopStartDisp = &smpTo->loopStart; |
| 626 | smpTo->loopLengthDisp = &smpTo->loopLength; |
| 627 | |
| 628 | // swap sample data |
| 629 | for (int32_t i = 0; i < config.maxSampleLength; i++) |
| 630 | { |
| 631 | int8_t smp = song->sampleData[smpFrom->offset+i]; |
| 632 | song->sampleData[smpFrom->offset+i] = song->sampleData[smpTo->offset+i]; |
| 633 | song->sampleData[smpTo->offset+i] = smp; |
| 634 | } |
| 635 | |
| 636 | editor.sampleZero = false; |
| 637 | |
| 638 | updateCurrSample(); |
| 639 | } |
| 640 | else |
| 641 | { |
| 642 | // exchange sample number in track/pattern |
| 643 | if (editor.trackPattFlag == 0) |
| 644 | { |
| 645 | for (int32_t i = 0; i < MOD_ROWS; i++) |
| 646 | { |
| 647 | note_t *noteSrc = &song->patterns[song->currPattern][(i * PAULA_VOICES) + cursor.channel]; |
| 648 | |
| 649 | if (noteSrc->sample == editor.sampleFrom) noteSrc->sample = editor.sampleTo; |
| 650 | else if (noteSrc->sample == editor.sampleTo) noteSrc->sample = editor.sampleFrom; |
| 651 | } |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | for (int32_t i = 0; i < PAULA_VOICES; i++) |
| 656 | { |
| 657 | for (int32_t j = 0; j < MOD_ROWS; j++) |
| 658 | { |
| 659 | note_t *noteSrc = &song->patterns[song->currPattern][(j * PAULA_VOICES) + i]; |
| 660 | |
| 661 | if (noteSrc->sample == editor.sampleFrom) noteSrc->sample = editor.sampleTo; |
| 662 | else if (noteSrc->sample == editor.sampleTo) noteSrc->sample = editor.sampleFrom; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | ui.updatePatternData = true; |
| 668 | } |
| 669 | |
| 670 | editor.samplePos = 0; |
| 671 | updateSamplePos(); |
| 672 | |
| 673 | updateWindowTitle(MOD_IS_MODIFIED); |
| 674 | } |
| 675 | |
| 676 | void delSampleTrack(void) |
| 677 | { |
| 678 | saveUndo(); |
| 679 | |
| 680 | if (editor.trackPattFlag == 0) |
| 681 | { |
| 682 | for (int32_t i = 0; i < MOD_ROWS; i++) |
| 683 | { |
| 684 | note_t *noteSrc = &song->patterns[song->currPattern][(i * PAULA_VOICES) + cursor.channel]; |
| 685 | if (noteSrc->sample == editor.currSample+1) |
| 686 | { |
| 687 | noteSrc->period = 0; |
| 688 | noteSrc->sample = 0; |
| 689 | noteSrc->command = 0; |
| 690 | noteSrc->param = 0; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | else |
| 695 | { |
| 696 | for (int32_t i = 0; i < PAULA_VOICES; i++) |
| 697 | { |
| 698 | for (int32_t j = 0; j < MOD_ROWS; j++) |
| 699 | { |
| 700 | note_t *noteSrc = &song->patterns[song->currPattern][(j * PAULA_VOICES) + i]; |
| 701 | if (noteSrc->sample == editor.currSample+1) |
| 702 | { |
| 703 | noteSrc->period = 0; |
| 704 | noteSrc->sample = 0; |
| 705 | noteSrc->command = 0; |
| 706 | noteSrc->param = 0; |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | updateWindowTitle(MOD_IS_MODIFIED); |
| 713 | ui.updatePatternData = true; |
| 714 | } |
| 715 | |
| 716 | void trackNoteUp(bool sampleAllFlag, uint8_t from, uint8_t to) |
| 717 | { |
| 718 | const uint8_t transposeSample = editor.sampleZero ? 0 : editor.currSample + 1; |
| 719 | |
| 720 | if (from > to) |
| 721 | { |
| 722 | uint8_t old = from; |
| 723 | from = to; |
| 724 | to = old; |
| 725 | } |
| 726 | |
| 727 | saveUndo(); |
| 728 | for (int32_t i = from; i <= to; i++) |
| 729 | { |
| 730 | note_t *noteSrc = &song->patterns[song->currPattern][(i * PAULA_VOICES) + cursor.channel]; |
| 731 | |
| 732 | if (!sampleAllFlag && noteSrc->sample != transposeSample) |
| 733 | continue; |
| 734 | |
| 735 | if (noteSrc->period) |
| 736 | { |
| 737 | // period -> note |
| 738 | int32_t j; |
| 739 | for (j = 0; j < 36; j++) |
| 740 | { |
| 741 | if (noteSrc->period >= periodTable[j]) |
| 742 | break; |
| 743 | } |
| 744 | |
| 745 | bool noteDeleted = false; |
| 746 | if (++j > 35) |
| 747 | { |
| 748 | j = 35; |
| 749 | |
| 750 | if (config.transDel) |
| 751 | { |
| 752 | noteSrc->period = 0; |
| 753 | noteSrc->sample = 0; |
| 754 | |
| 755 | noteDeleted = true; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | if (!noteDeleted) |
| 760 | noteSrc->period = periodTable[j]; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | updateWindowTitle(MOD_IS_MODIFIED); |
| 765 | ui.updatePatternData = true; |
| 766 | } |
| 767 | |
| 768 | void trackNoteDown(bool sampleAllFlag, uint8_t from, uint8_t to) |
| 769 | { |
| 770 | const uint8_t transposeSample = editor.sampleZero ? 0 : editor.currSample + 1; |
| 771 | |
| 772 | if (from > to) |
| 773 | { |
| 774 | uint8_t old = from; |
| 775 | from = to; |
| 776 | to = old; |
| 777 | } |
| 778 | |
| 779 | saveUndo(); |
| 780 | for (int32_t i = from; i <= to; i++) |
| 781 | { |
| 782 | note_t *noteSrc = &song->patterns[song->currPattern][(i * PAULA_VOICES) + cursor.channel]; |
| 783 | |
| 784 | if (!sampleAllFlag && noteSrc->sample != transposeSample) |
| 785 | continue; |
| 786 | |
| 787 | if (noteSrc->period) |
| 788 | { |
| 789 | // period -> note |
| 790 | int32_t j; |
| 791 | for (j = 0; j < 36; j++) |
| 792 | { |
| 793 | if (noteSrc->period >= periodTable[j]) |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | bool noteDeleted = false; |
| 798 | if (--j < 0) |
| 799 | { |
| 800 | j = 0; |
| 801 | |
| 802 | if (config.transDel) |
| 803 | { |
| 804 | noteSrc->period = 0; |
| 805 | noteSrc->sample = 0; |
| 806 | |
| 807 | noteDeleted = true; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | if (!noteDeleted) |
| 812 | noteSrc->period = periodTable[j]; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | updateWindowTitle(MOD_IS_MODIFIED); |
| 817 | ui.updatePatternData = true; |
| 818 | } |
| 819 | |
| 820 | void trackOctaUp(bool sampleAllFlag, uint8_t from, uint8_t to) |
| 821 | { |
| 822 | const uint8_t transposeSample = editor.sampleZero ? 0 : editor.currSample + 1; |
| 823 | |
| 824 | if (from > to) |
| 825 | { |
| 826 | uint8_t old = from; |
| 827 | from = to; |
| 828 | to = old; |
| 829 | } |
| 830 | |
| 831 | bool noteChanged = false; |
| 832 | |
| 833 | saveUndo(); |
| 834 | for (int32_t i = from; i <= to; i++) |
| 835 | { |
| 836 | note_t *noteSrc = &song->patterns[song->currPattern][(i * PAULA_VOICES) + cursor.channel]; |
| 837 | |
| 838 | if (!sampleAllFlag && noteSrc->sample != transposeSample) |
| 839 | continue; |
| 840 | |
| 841 | if (noteSrc->period) |
| 842 | { |
| 843 | uint16_t oldPeriod = noteSrc->period; |
| 844 | |
| 845 | // period -> note |
| 846 | int32_t j; |
| 847 | for (j = 0; j < 36; j++) |
| 848 | { |
| 849 | if (noteSrc->period >= periodTable[j]) |
| 850 | break; |
| 851 | } |
| 852 | |
| 853 | bool noteDeleted = false; |
| 854 | if (j+12 > 35 && config.transDel) |
| 855 | { |
| 856 | noteSrc->period = 0; |
| 857 | noteSrc->sample = 0; |
| 858 | |
| 859 | noteDeleted = true; |
| 860 | } |
| 861 | |
| 862 | if (j <= 23) |
| 863 | j += 12; |
| 864 | |
| 865 | if (!noteDeleted) |
| 866 | noteSrc->period = periodTable[j]; |
| 867 | |
| 868 | if (noteSrc->period != oldPeriod) |
| 869 | noteChanged = true; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | if (noteChanged) |
| 874 | { |
| 875 | updateWindowTitle(MOD_IS_MODIFIED); |
| 876 | ui.updatePatternData = true; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | void trackOctaDown(bool sampleAllFlag, uint8_t from, uint8_t to) |
| 881 | { |
| 882 | const uint8_t transposeSample = editor.sampleZero ? 0 : editor.currSample + 1; |
| 883 | |
| 884 | if (from > to) |
| 885 | { |
| 886 | uint8_t old = from; |
| 887 | from = to; |
| 888 | to = old; |
| 889 | } |
| 890 | |
| 891 | saveUndo(); |
| 892 | for (int32_t i = from; i <= to; i++) |
| 893 | { |
| 894 | note_t *noteSrc = &song->patterns[song->currPattern][(i * PAULA_VOICES) + cursor.channel]; |
| 895 | |
| 896 | if (!sampleAllFlag && noteSrc->sample != transposeSample) |
| 897 | continue; |
| 898 | |
| 899 | if (noteSrc->period) |
| 900 | { |
| 901 | // period -> note |
| 902 | int32_t j; |
| 903 | for (j = 0; j < 36; j++) |
| 904 | { |
| 905 | if (noteSrc->period >= periodTable[j]) |
| 906 | break; |
| 907 | } |
| 908 | |
| 909 | bool noteDeleted = false; |
| 910 | if (j-12 < 0 && config.transDel) |
| 911 | { |
| 912 | noteSrc->period = 0; |
| 913 | noteSrc->sample = 0; |
| 914 | |
| 915 | noteDeleted = true; |
| 916 | } |
| 917 | |
| 918 | if (j >= 12) |
| 919 | j -= 12; |
| 920 | |
| 921 | if (!noteDeleted) |
| 922 | noteSrc->period = periodTable[j]; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | updateWindowTitle(MOD_IS_MODIFIED); |
| 927 | ui.updatePatternData = true; |
| 928 | } |
| 929 | |
| 930 | void pattNoteUp(bool sampleAllFlag) |
| 931 | { |
| 932 | const uint8_t transposeSample = editor.sampleZero ? 0 : editor.currSample + 1; |
| 933 | |
| 934 | saveUndo(); |
| 935 | for (int32_t i = 0; i < PAULA_VOICES; i++) |
| 936 | { |
| 937 | for (int32_t j = 0; j < MOD_ROWS; j++) |
| 938 | { |
| 939 | note_t *noteSrc = &song->patterns[song->currPattern][(j * PAULA_VOICES) + i]; |
| 940 | |
| 941 | if (!sampleAllFlag && noteSrc->sample != transposeSample) |
| 942 | continue; |
| 943 | |
| 944 | if (noteSrc->period) |
| 945 | { |
| 946 | // period -> note |
| 947 | int32_t k; |
| 948 | for (k = 0; k < 36; k++) |
| 949 | { |
| 950 | if (noteSrc->period >= periodTable[k]) |
| 951 | break; |
| 952 | } |
| 953 | |
| 954 | bool noteDeleted = false; |
| 955 | if (++k > 35) |
| 956 | { |
| 957 | k = 35; |
| 958 | |
| 959 | if (config.transDel) |
| 960 | { |
| 961 | noteSrc->period = 0; |
| 962 | noteSrc->sample = 0; |
| 963 | |
| 964 | noteDeleted = true; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | if (!noteDeleted) |
| 969 | noteSrc->period = periodTable[k]; |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | updateWindowTitle(MOD_IS_MODIFIED); |
| 975 | ui.updatePatternData = true; |
| 976 | } |
| 977 | |
| 978 | void pattNoteDown(bool sampleAllFlag) |
| 979 | { |
| 980 | const uint8_t transposeSample = editor.sampleZero ? 0 : editor.currSample + 1; |
| 981 | |
| 982 | saveUndo(); |
| 983 | for (int32_t i = 0; i < PAULA_VOICES; i++) |
| 984 | { |
| 985 | for (int32_t j = 0; j < MOD_ROWS; j++) |
| 986 | { |
| 987 | note_t *noteSrc = &song->patterns[song->currPattern][(j * PAULA_VOICES) + i]; |
| 988 | |
| 989 | if (!sampleAllFlag && noteSrc->sample != transposeSample) |
| 990 | continue; |
| 991 | |
| 992 | if (noteSrc->period) |
| 993 | { |
| 994 | // period -> note |
| 995 | int32_t k; |
| 996 | for (k = 0; k < 36; k++) |
| 997 | { |
| 998 | if (noteSrc->period >= periodTable[k]) |
| 999 | break; |
| 1000 | } |
| 1001 | |
| 1002 | bool noteDeleted = false; |
| 1003 | if (--k < 0) |
| 1004 | { |
| 1005 | k = 0; |
| 1006 | |
| 1007 | if (config.transDel) |
| 1008 | { |
| 1009 | noteSrc->period = 0; |
| 1010 | noteSrc->sample = 0; |
| 1011 | |
| 1012 | noteDeleted = true; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | if (!noteDeleted) |
| 1017 | noteSrc->period = periodTable[k]; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | updateWindowTitle(MOD_IS_MODIFIED); |
| 1023 | ui.updatePatternData = true; |
| 1024 | } |
| 1025 | |
| 1026 | void pattOctaUp(bool sampleAllFlag) |
| 1027 | { |
| 1028 | const uint8_t transposeSample = editor.sampleZero ? 0 : editor.currSample + 1; |
| 1029 | |
| 1030 | saveUndo(); |
| 1031 | for (int32_t i = 0; i < PAULA_VOICES; i++) |
| 1032 | { |
| 1033 | for (int32_t j = 0; j < MOD_ROWS; j++) |
| 1034 | { |
| 1035 | note_t *noteSrc = &song->patterns[song->currPattern][(j * PAULA_VOICES) + i]; |
| 1036 | |
| 1037 | if (!sampleAllFlag && noteSrc->sample != transposeSample) |
| 1038 | continue; |
| 1039 | |
| 1040 | if (noteSrc->period) |
| 1041 | { |
| 1042 | // period -> note |
| 1043 | int32_t k; |
| 1044 | for (k = 0; k < 36; k++) |
| 1045 | { |
| 1046 | if (noteSrc->period >= periodTable[k]) |
| 1047 | break; |
| 1048 | } |
| 1049 | |
| 1050 | bool noteDeleted = false; |
| 1051 | if (k+12 > 35 && config.transDel) |
| 1052 | { |
| 1053 | noteSrc->period = 0; |
| 1054 | noteSrc->sample = 0; |
| 1055 | |
| 1056 | noteDeleted = true; |
| 1057 | } |
| 1058 | |
| 1059 | if (k <= 23) |
| 1060 | k += 12; |
| 1061 | |
| 1062 | if (!noteDeleted) |
| 1063 | noteSrc->period = periodTable[k]; |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | updateWindowTitle(MOD_IS_MODIFIED); |
| 1069 | ui.updatePatternData = true; |
| 1070 | } |
| 1071 | |
| 1072 | void pattOctaDown(bool sampleAllFlag) |
| 1073 | { |
| 1074 | const uint8_t transposeSample = editor.sampleZero ? 0 : editor.currSample + 1; |
| 1075 | |
| 1076 | saveUndo(); |
| 1077 | for (int32_t i = 0; i < PAULA_VOICES; i++) |
| 1078 | { |
| 1079 | for (int32_t j = 0; j < MOD_ROWS; j++) |
| 1080 | { |
| 1081 | note_t *noteSrc = &song->patterns[song->currPattern][(j * PAULA_VOICES) + i]; |
| 1082 | |
| 1083 | if (!sampleAllFlag && noteSrc->sample != transposeSample) |
| 1084 | continue; |
| 1085 | |
| 1086 | if (noteSrc->period) |
| 1087 | { |
| 1088 | // period -> note |
| 1089 | int32_t k; |
| 1090 | for (k = 0; k < 36; k++) |
| 1091 | { |
| 1092 | if (noteSrc->period >= periodTable[k]) |
| 1093 | break; |
| 1094 | } |
| 1095 | |
| 1096 | bool noteDeleted = false; |
| 1097 | if (k-12 < 0 && config.transDel) |
| 1098 | { |
| 1099 | noteSrc->period = 0; |
| 1100 | noteSrc->sample = 0; |
| 1101 | |
| 1102 | noteDeleted = true; |
| 1103 | } |
| 1104 | |
| 1105 | if (k >= 12) |
| 1106 | k -= 12; |
| 1107 | |
| 1108 | if (!noteDeleted) |
| 1109 | noteSrc->period = periodTable[k]; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | updateWindowTitle(MOD_IS_MODIFIED); |
| 1115 | ui.updatePatternData = true; |
| 1116 | } |
| 1117 | |
| 1118 | int8_t keyToNote(SDL_Scancode scancode) |
| 1119 | { |
| 1120 | int8_t note; |
| 1121 | |
| 1122 | if (scancode < SDL_SCANCODE_B || scancode > SDL_SCANCODE_SLASH) |
| 1123 | return -1; // not a note key |
| 1124 | |
| 1125 | int32_t lookUpKey = (int32_t)scancode - SDL_SCANCODE_B; |
| 1126 | if (lookUpKey < 0 || lookUpKey >= 52) |
| 1127 | return -1; // just in case |
| 1128 | |
| 1129 | if (editor.keyOctave == OCTAVE_LOW) |
| 1130 | note = scancode2NoteLo[lookUpKey]; |
| 1131 | else |
| 1132 | note = scancode2NoteHi[lookUpKey]; |
| 1133 | |
| 1134 | return note; |
| 1135 | } |