| 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 <stdio.h> |
| 7 | #include <stdint.h> |
| 8 | #include <stdbool.h> |
| 9 | #include <string.h> |
| 10 | #include <math.h> |
| 11 | #include "pt2_header.h" // PI |
| 12 | #include "pt2_textout.h" |
| 13 | #include "pt2_sampler.h" |
| 14 | #include "pt2_visuals.h" |
| 15 | #include "pt2_helpers.h" |
| 16 | #include "pt2_bmp.h" |
| 17 | #include "pt2_audio.h" |
| 18 | #include "pt2_tables.h" |
| 19 | #include "pt2_config.h" |
| 20 | #include "pt2_sampling.h" |
| 21 | #include "pt2_replayer.h" |
| 22 | |
| 23 | enum |
| 24 | { |
| 25 | SAMPLE_LEFT = 0, |
| 26 | SAMPLE_RIGHT = 1, |
| 27 | SAMPLE_MIX = 2 |
| 28 | }; |
| 29 | |
| 30 | // ok settings based on computational time and audio quality |
| 31 | #define SINC_TAPS 64 /* 2^n */ |
| 32 | #define SINC_TAPS_BITS 6 /* log2(SINC_TAPS) */ |
| 33 | #define SINC_OVERSAMPLING 256 /* 2^n */ |
| 34 | #define SINC_OVERSAMPLING_BITS 8 /* log2(SINC_OVERSAMPLING) */ |
| 35 | |
| 36 | #define CENTER_TAP ((SINC_TAPS/2)-1) |
| 37 | #define SMP_DELTA_FRAC_BITS 32 |
| 38 | #define SMP_DELTA_FRAC_SCALE (1ULL << SMP_DELTA_FRAC_BITS) |
| 39 | #define SMP_DELTA_FRAC_MASK (SMP_DELTA_FRAC_SCALE-1) |
| 40 | #define INTRP_PHASE_SHIFT (SMP_DELTA_FRAC_BITS-SINC_OVERSAMPLING_BITS) |
| 41 | #define INTRP_PHASE_SCALE (1L << INTRP_PHASE_SHIFT) |
| 42 | #define INTRP_PHASE_MASK (INTRP_PHASE_SCALE-1) |
| 43 | #define SAMPLE_PREVIEW_WITDH 194 |
| 44 | #define SAMPLE_PREVIEW_HEIGHT 38 |
| 45 | #define MAX_INPUT_DEVICES 99 |
| 46 | #define VISIBLE_LIST_ENTRIES 4 |
| 47 | #define SAMPLING_BUFFER_SIZE 1024 /* may change on audio input init */ |
| 48 | |
| 49 | static volatile bool callbackBusy, displayingBuffer, samplingEnded; |
| 50 | static bool audioDevOpen; |
| 51 | static char *audioInputDevs[MAX_INPUT_DEVICES]; |
| 52 | static uint8_t samplingNote = 33, samplingFinetune = 4; // period 124, max safe period for PAL Paula |
| 53 | static int16_t displayBuffer[SAMPLING_BUFFER_SIZE]; |
| 54 | static int32_t samplingMode = SAMPLE_MIX, inputFrequency, roundedOutputFrequency; |
| 55 | static int32_t numAudioInputDevs, audioInputDevListOffset, selectedDev; |
| 56 | static int32_t bytesSampled, maxSamplingLength, inputBufferSize; |
| 57 | static float *fSincLUT, *fSamplingBuffer, *fSamplingBufferOrig; |
| 58 | static double dOutputFrequency, dResamplingRatio; |
| 59 | static SDL_AudioDeviceID recordDev; |
| 60 | |
| 61 | static void listAudioDevices(void); |
| 62 | |
| 63 | // zeroth-order modified Bessel function of the first kind (series approximation) |
| 64 | static inline double besselI0(double z) |
| 65 | { |
| 66 | double s = 1.0, ds = 1.0, d = 2.0; |
| 67 | const double zz = z * z; |
| 68 | |
| 69 | do |
| 70 | { |
| 71 | ds *= zz / (d * d); |
| 72 | s += ds; |
| 73 | d += 2.0; |
| 74 | } |
| 75 | while (ds > s*(1E-12)); |
| 76 | |
| 77 | return s; |
| 78 | } |
| 79 | |
| 80 | static inline double sinc(double x, double cutoff) |
| 81 | { |
| 82 | if (x == 0.0) |
| 83 | { |
| 84 | return cutoff; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | x *= PI; |
| 89 | return sin(x * cutoff) / x; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static bool calcPolyphaseSincLUT(double sincCutoff) |
| 94 | { |
| 95 | fSincLUT = (float *)malloc(SINC_TAPS * (SINC_OVERSAMPLING+1) * sizeof (float)); |
| 96 | if (fSincLUT == NULL) |
| 97 | { |
| 98 | showErrorMsgBox("Out of memory!"); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | const double kaiserBeta = 9.62046; // stopband attenuation of ~96dB |
| 103 | const double besselI0BetaMul = 1.0 / besselI0(kaiserBeta); |
| 104 | |
| 105 | for (int32_t i = 0; i < SINC_TAPS * SINC_OVERSAMPLING; i++) |
| 106 | { |
| 107 | const double x = i * (1.0 / SINC_OVERSAMPLING); |
| 108 | |
| 109 | // Kaiser-Bessel window |
| 110 | const double n = (x * (2.0 / SINC_TAPS)) - 1.0; |
| 111 | const double window = besselI0(kaiserBeta * sqrt(1.0 - n * n)) * besselI0BetaMul; |
| 112 | |
| 113 | const double wsinc = sinc(x - (double)(SINC_TAPS / 2), sincCutoff) * window; |
| 114 | |
| 115 | // re-arrange for faster logic when in use |
| 116 | const int32_t point = i >> SINC_OVERSAMPLING_BITS; |
| 117 | const int32_t phase = i & (SINC_OVERSAMPLING-1); |
| 118 | fSincLUT[(phase << SINC_TAPS_BITS) + ((SINC_TAPS-1) - point)] = (float)wsinc; |
| 119 | } |
| 120 | |
| 121 | // store inverted wrap-around taps after end of LUT (for phase interpolation) |
| 122 | float *fEnd = &fSincLUT[SINC_TAPS * SINC_OVERSAMPLING]; |
| 123 | for (int32_t i = 0; i < SINC_TAPS; i++) |
| 124 | fEnd[i] = fSincLUT[(SINC_TAPS-1) - i]; |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | static void freePolyphaseSincLUT(void) |
| 130 | { |
| 131 | if (fSincLUT != NULL) |
| 132 | { |
| 133 | free(fSincLUT); |
| 134 | fSincLUT = NULL; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static void updateOutputFrequency(void) |
| 139 | { |
| 140 | if (samplingNote > 35) |
| 141 | samplingNote = 35; |
| 142 | |
| 143 | int32_t period = periodTable[((samplingFinetune & 0xF) * 37) + samplingNote]; |
| 144 | if (period < 113) // also happens in our "set period" Paula function |
| 145 | period = 113; |
| 146 | |
| 147 | dOutputFrequency = (double)PAULA_PAL_CLK / period; |
| 148 | roundedOutputFrequency = (int32_t)(dOutputFrequency + 0.5); // for display |
| 149 | } |
| 150 | |
| 151 | static void samplingCallback(void *userdata, Uint8 *stream, int len) |
| 152 | { |
| 153 | callbackBusy = true; |
| 154 | |
| 155 | if (!displayingBuffer) |
| 156 | { |
| 157 | if (len > SAMPLING_BUFFER_SIZE) |
| 158 | len = SAMPLING_BUFFER_SIZE; |
| 159 | |
| 160 | const int16_t *L = (int16_t *)stream; |
| 161 | const int16_t *R = ((int16_t *)stream) + 1; |
| 162 | |
| 163 | int16_t *dst16 = displayBuffer; |
| 164 | |
| 165 | if (samplingMode == SAMPLE_LEFT) |
| 166 | { |
| 167 | for (int32_t i = 0; i < len; i++) |
| 168 | dst16[i] = L[i << 1]; |
| 169 | } |
| 170 | else if (samplingMode == SAMPLE_RIGHT) |
| 171 | { |
| 172 | for (int32_t i = 0; i < len; i++) |
| 173 | dst16[i] = R[i << 1]; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | for (int32_t i = 0; i < len; i++) |
| 178 | dst16[i] = (L[i << 1] + R[i << 1]) >> 1; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (audio.isSampling) |
| 183 | { |
| 184 | if (bytesSampled+len > maxSamplingLength) |
| 185 | len = maxSamplingLength - bytesSampled; |
| 186 | |
| 187 | if (len > inputBufferSize) |
| 188 | len = inputBufferSize; |
| 189 | |
| 190 | const int16_t *L = (int16_t *)stream; |
| 191 | const int16_t *R = ((int16_t *)stream) + 1; |
| 192 | |
| 193 | float *fSmp = &fSamplingBuffer[bytesSampled]; |
| 194 | |
| 195 | if (samplingMode == SAMPLE_LEFT) |
| 196 | { |
| 197 | for (int32_t i = 0; i < len; i++) |
| 198 | fSmp[i] = L[i << 1] * (1.0f / (32768.0f / 128.0f)); |
| 199 | } |
| 200 | else if (samplingMode == SAMPLE_RIGHT) |
| 201 | { |
| 202 | for (int32_t i = 0; i < len; i++) |
| 203 | fSmp[i] = R[i << 1] * (1.0f / (32768.0f / 128.0f)); |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | for (int32_t i = 0; i < len; i++) |
| 208 | fSmp[i] = (L[i << 1] + R[i << 1]) * (0.5f / (32768.0f / 128.0f)); |
| 209 | } |
| 210 | |
| 211 | bytesSampled += len; |
| 212 | if (bytesSampled >= maxSamplingLength) |
| 213 | { |
| 214 | audio.isSampling = true; |
| 215 | samplingEnded = true; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | callbackBusy = false; |
| 220 | (void)userdata; |
| 221 | } |
| 222 | |
| 223 | static void stopInputAudio(void) |
| 224 | { |
| 225 | if (recordDev > 0) |
| 226 | { |
| 227 | SDL_CloseAudioDevice(recordDev); |
| 228 | recordDev = 0; |
| 229 | } |
| 230 | callbackBusy = false; |
| 231 | } |
| 232 | |
| 233 | static void startInputAudio(void) |
| 234 | { |
| 235 | SDL_AudioSpec want, have; |
| 236 | |
| 237 | if (recordDev > 0) |
| 238 | stopInputAudio(); |
| 239 | |
| 240 | if (numAudioInputDevs == 0 || selectedDev >= numAudioInputDevs) |
| 241 | { |
| 242 | audioDevOpen = false; |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | ASSERT(roundedOutputFrequency > 0); |
| 247 | |
| 248 | memset(&want, 0, sizeof (SDL_AudioSpec)); |
| 249 | want.freq = config.audioInputFrequency; |
| 250 | want.format = AUDIO_S16; |
| 251 | want.channels = 2; |
| 252 | want.callback = samplingCallback; |
| 253 | want.userdata = NULL; |
| 254 | want.samples = SAMPLING_BUFFER_SIZE; |
| 255 | |
| 256 | recordDev = SDL_OpenAudioDevice(audioInputDevs[selectedDev], true, &want, &have, 0); |
| 257 | audioDevOpen = (recordDev != 0); |
| 258 | |
| 259 | inputFrequency = have.freq; |
| 260 | inputBufferSize = have.samples; |
| 261 | |
| 262 | SDL_PauseAudioDevice(recordDev, false); |
| 263 | } |
| 264 | |
| 265 | static void selectAudioDevice(int32_t dev) |
| 266 | { |
| 267 | if (dev < 0) |
| 268 | return; |
| 269 | |
| 270 | if (numAudioInputDevs == 0) |
| 271 | { |
| 272 | listAudioDevices(); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | if (dev >= numAudioInputDevs) |
| 277 | return; |
| 278 | |
| 279 | listAudioDevices(); |
| 280 | |
| 281 | stopInputAudio(); |
| 282 | selectedDev = dev; |
| 283 | listAudioDevices(); |
| 284 | startInputAudio(); |
| 285 | |
| 286 | changeStatusText(ui.statusMessage); |
| 287 | } |
| 288 | |
| 289 | void freeAudioDeviceList(void) |
| 290 | { |
| 291 | for (int32_t i = 0; i < numAudioInputDevs; i++) |
| 292 | { |
| 293 | if (audioInputDevs[i] != NULL) |
| 294 | { |
| 295 | free(audioInputDevs[i]); |
| 296 | audioInputDevs[i] = NULL; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | static void scanAudioDevices(void) |
| 302 | { |
| 303 | freeAudioDeviceList(); |
| 304 | |
| 305 | numAudioInputDevs = SDL_GetNumAudioDevices(true); |
| 306 | if (numAudioInputDevs > MAX_INPUT_DEVICES) |
| 307 | numAudioInputDevs = MAX_INPUT_DEVICES; |
| 308 | |
| 309 | for (int32_t i = 0; i < numAudioInputDevs; i++) |
| 310 | { |
| 311 | const char *deviceName = SDL_GetAudioDeviceName(i, true); |
| 312 | if (deviceName == NULL) |
| 313 | { |
| 314 | numAudioInputDevs--; // hide device |
| 315 | continue; |
| 316 | } |
| 317 | |
| 318 | const uint32_t stringLen = (uint32_t)strlen(deviceName); |
| 319 | |
| 320 | audioInputDevs[i] = (char *)malloc(stringLen + 2); |
| 321 | if (audioInputDevs[i] == NULL) |
| 322 | break; |
| 323 | |
| 324 | if (stringLen > 0) |
| 325 | strcpy(audioInputDevs[i], deviceName); |
| 326 | |
| 327 | audioInputDevs[i][stringLen+1] = '\0'; // UTF-8 needs double null termination (XXX: citation needed) |
| 328 | } |
| 329 | |
| 330 | audioInputDevListOffset = 0; // reset scroll position |
| 331 | |
| 332 | if (selectedDev >= numAudioInputDevs) |
| 333 | selectedDev = 0; |
| 334 | } |
| 335 | |
| 336 | static void listAudioDevices(void) |
| 337 | { |
| 338 | fillRect(3, 219, 163, 33, PAL_BACKGRD); |
| 339 | |
| 340 | if (numAudioInputDevs == 0) |
| 341 | { |
| 342 | textOut(16, 219+13, "NO DEVICES FOUND!", video.palette[PAL_QADSCP]); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | for (int32_t i = 0; i < VISIBLE_LIST_ENTRIES; i++) |
| 347 | { |
| 348 | const int32_t dev = audioInputDevListOffset+i; |
| 349 | if (audioInputDevListOffset+i >= numAudioInputDevs) |
| 350 | break; |
| 351 | |
| 352 | if (dev == selectedDev) |
| 353 | fillRect(4, 219+1+(i*(FONT_CHAR_H+3)), 161, 8, video.palette[PAL_GENBKG2]); |
| 354 | |
| 355 | if (audioInputDevs[dev] != NULL) |
| 356 | textOutTightN(2+2, 219+2+(i*(FONT_CHAR_H+3)), audioInputDevs[dev], 23, video.palette[PAL_QADSCP]); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | static void drawSamplingNote(void) |
| 361 | { |
| 362 | ASSERT(samplingNote < 36); |
| 363 | const char *str = config.accidental ? noteNames2[2+samplingNote]: noteNames1[2+samplingNote]; |
| 364 | textOutBg(262, 230, str, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]); |
| 365 | } |
| 366 | |
| 367 | static void drawSamplingFinetune(void) |
| 368 | { |
| 369 | textOutBg(254, 219, ftuneStrTab[samplingFinetune & 0xF], video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]); |
| 370 | } |
| 371 | |
| 372 | static void drawSamplingFrequency(void) |
| 373 | { |
| 374 | char str[16]; |
| 375 | sprintf(str, "%05dHZ", roundedOutputFrequency); |
| 376 | |
| 377 | const int32_t maxSafeFrequency = (int32_t)(PAL_PAULA_MAX_SAFE_HZ + 0.5); // rounded |
| 378 | textOutBg(262, 208, str, roundedOutputFrequency <= maxSafeFrequency ? video.palette[PAL_GENTXT] : 0x8C0F0F, video.palette[PAL_GENBKG]); |
| 379 | } |
| 380 | |
| 381 | static void drawSamplingModeCross(void) |
| 382 | { |
| 383 | // clear old crosses |
| 384 | fillRect(4, 208, 6, 5, video.palette[PAL_GENBKG]); |
| 385 | fillRect(51, 208, 6, 5, video.palette[PAL_GENBKG]); |
| 386 | fillRect(105, 208, 6, 5, video.palette[PAL_GENBKG]); |
| 387 | |
| 388 | int16_t x; |
| 389 | if (samplingMode == SAMPLE_LEFT) |
| 390 | x = 3; |
| 391 | else if (samplingMode == SAMPLE_RIGHT) |
| 392 | x = 50; |
| 393 | else |
| 394 | x = 104; |
| 395 | |
| 396 | charOut(x, 208, 'X', video.palette[PAL_GENTXT]); |
| 397 | } |
| 398 | |
| 399 | static void showCurrSample(void) |
| 400 | { |
| 401 | updateCurrSample(); |
| 402 | |
| 403 | // reset sampler screen attributes |
| 404 | sampler.loopStartPos = 0; |
| 405 | sampler.loopEndPos = 0; |
| 406 | editor.markStartOfs = -1; |
| 407 | editor.markEndOfs = -1; |
| 408 | editor.samplePos = 0; |
| 409 | hideSprite(SPRITE_LOOP_PIN_LEFT); |
| 410 | hideSprite(SPRITE_LOOP_PIN_RIGHT); |
| 411 | |
| 412 | renderSampleData(); |
| 413 | } |
| 414 | |
| 415 | void renderSamplingBox(void) |
| 416 | { |
| 417 | modStop(); |
| 418 | editor.songPlaying = false; |
| 419 | editor.playMode = PLAY_MODE_NORMAL; |
| 420 | editor.currMode = MODE_IDLE; |
| 421 | pointerSetMode(POINTER_MODE_IDLE, DO_CARRY); |
| 422 | |
| 423 | editor.sampleZero = false; |
| 424 | editor.blockMarkFlag = false; |
| 425 | |
| 426 | // remove all open screens (except sampler) |
| 427 | if (ui.diskOpScreenShown || ui.posEdScreenShown || ui.editOpScreenShown) |
| 428 | { |
| 429 | ui.diskOpScreenShown = false; |
| 430 | ui.posEdScreenShown = false; |
| 431 | ui.editOpScreenShown = false; |
| 432 | |
| 433 | displayMainScreen(); |
| 434 | } |
| 435 | |
| 436 | blit32(0, 203, 320, 52, samplingBoxBMP); |
| 437 | |
| 438 | // render sample monitor |
| 439 | blit32(120, 44, 200, 55, sampleMonitorBMP); |
| 440 | memset(displayBuffer, 0, sizeof (displayBuffer)); |
| 441 | hLine(123, 76, 194, video.palette[PAL_QADSCP]); // draw center line |
| 442 | |
| 443 | updateOutputFrequency(); |
| 444 | drawSamplingNote(); |
| 445 | drawSamplingFinetune(); |
| 446 | drawSamplingFrequency(); |
| 447 | drawSamplingModeCross(); |
| 448 | showCurrSample(); |
| 449 | |
| 450 | changeStatusText("PLEASE WAIT..."); |
| 451 | flipFrame(); |
| 452 | hpc_ResetCounters(&video.vblankHpc); |
| 453 | scanAudioDevices(); |
| 454 | selectAudioDevice(selectedDev); |
| 455 | setStatusMessage("ALL RIGHT", DO_CARRY); |
| 456 | } |
| 457 | |
| 458 | static int32_t scrPos2SmpBufPos(int32_t x) // x = 0..SAMPLE_PREVIEW_WITDH |
| 459 | { |
| 460 | return (x * ((SAMPLING_BUFFER_SIZE << 16) / SAMPLE_PREVIEW_WITDH)) >> 16; |
| 461 | } |
| 462 | |
| 463 | static uint8_t getDispBuffPeak(const int16_t *smpData, int32_t smpNum) |
| 464 | { |
| 465 | int32_t smpAbs, max = 0; |
| 466 | for (int32_t i = 0; i < smpNum; i++) |
| 467 | { |
| 468 | const int32_t smp = smpData[i]; |
| 469 | |
| 470 | smpAbs = ABS(smp); |
| 471 | if (smpAbs > max) |
| 472 | max = smpAbs; |
| 473 | } |
| 474 | |
| 475 | max = ((max * SAMPLE_PREVIEW_HEIGHT) + 32768) >> 16; |
| 476 | if (max > (SAMPLE_PREVIEW_HEIGHT/2)-1) |
| 477 | max = (SAMPLE_PREVIEW_HEIGHT/2)-1; |
| 478 | |
| 479 | return (uint8_t)max; |
| 480 | } |
| 481 | |
| 482 | void writeSampleMonitorWaveform(void) // called every frame |
| 483 | { |
| 484 | if (!ui.samplingBoxShown || ui.askBoxShown) |
| 485 | return; |
| 486 | |
| 487 | if (samplingEnded) |
| 488 | { |
| 489 | samplingEnded = false; |
| 490 | stopSampling(); |
| 491 | } |
| 492 | |
| 493 | // clear waveform background |
| 494 | fillRect(123, 58, SAMPLE_PREVIEW_WITDH, SAMPLE_PREVIEW_HEIGHT, video.palette[PAL_BACKGRD]); |
| 495 | |
| 496 | if (!audioDevOpen) |
| 497 | { |
| 498 | textOutTight(136, 74, "CAN'T OPEN AUDIO DEVICE!", video.palette[PAL_QADSCP]); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | uint32_t *centerPtr = &video.frameBuffer[(76 * SCREEN_W) + 123]; |
| 503 | |
| 504 | // hardcoded for a buffer size of 512 |
| 505 | displayingBuffer = true; |
| 506 | for (int32_t x = 0; x < SAMPLE_PREVIEW_WITDH; x++) |
| 507 | { |
| 508 | int32_t smpIdx = scrPos2SmpBufPos(x); |
| 509 | int32_t smpNum = scrPos2SmpBufPos(x+1) - smpIdx; |
| 510 | |
| 511 | if (smpIdx+smpNum >= SAMPLING_BUFFER_SIZE) |
| 512 | smpNum = SAMPLING_BUFFER_SIZE - smpIdx; |
| 513 | |
| 514 | const int32_t smpAbs = getDispBuffPeak(&displayBuffer[smpIdx], smpNum); |
| 515 | if (smpAbs == 0) |
| 516 | centerPtr[x] = video.palette[PAL_QADSCP]; |
| 517 | else |
| 518 | vLine(x + 123, 76 - smpAbs, (smpAbs * 2) + 1, video.palette[PAL_QADSCP]); |
| 519 | } |
| 520 | displayingBuffer = false; |
| 521 | } |
| 522 | |
| 523 | void removeSamplingBox(void) |
| 524 | { |
| 525 | stopInputAudio(); |
| 526 | freeAudioDeviceList(); |
| 527 | |
| 528 | ui.aboutScreenShown = false; |
| 529 | editor.blockMarkFlag = false; |
| 530 | displayMainScreen(); |
| 531 | updateVisualizer(); // kludge |
| 532 | |
| 533 | // re-render sampler screen |
| 534 | exitFromSam(); |
| 535 | samplerScreen(); |
| 536 | } |
| 537 | |
| 538 | static void startSampling(void) |
| 539 | { |
| 540 | if (!audioDevOpen) |
| 541 | { |
| 542 | displayErrorMsg("DEVICE ERROR !"); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | ASSERT(roundedOutputFrequency > 0); |
| 547 | |
| 548 | dResamplingRatio = dOutputFrequency / inputFrequency; |
| 549 | maxSamplingLength = (int32_t)ceil(config.maxSampleLength /dResamplingRatio) + 1; |
| 550 | |
| 551 | const int32_t allocLen = (SINC_TAPS/2) + maxSamplingLength + (SINC_TAPS/2); |
| 552 | |
| 553 | fSamplingBufferOrig = (float *)malloc(allocLen * sizeof (float)); |
| 554 | if (fSamplingBufferOrig == NULL) |
| 555 | { |
| 556 | statusOutOfMemory(); |
| 557 | return; |
| 558 | } |
| 559 | fSamplingBuffer = fSamplingBufferOrig + (SINC_TAPS/2); // allow negative look-up for sinc taps |
| 560 | |
| 561 | // clear tap area before sample |
| 562 | memset(fSamplingBufferOrig, 0, (SINC_TAPS/2) * sizeof (float)); |
| 563 | |
| 564 | const double sincCutoff = MIN(1.0, dResamplingRatio); |
| 565 | if (!calcPolyphaseSincLUT(sincCutoff)) |
| 566 | { |
| 567 | free(fSamplingBufferOrig); |
| 568 | statusOutOfMemory(); |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | bytesSampled = 0; |
| 573 | samplingEnded = false; |
| 574 | audio.isSampling = true; |
| 575 | |
| 576 | turnOffVoices(); |
| 577 | |
| 578 | pointerSetMode(POINTER_MODE_RECORD, NO_CARRY); |
| 579 | setStatusMessage("SAMPLING ...", NO_CARRY); |
| 580 | } |
| 581 | |
| 582 | static float sincInterpolation(const float *fSmpData, const uint32_t frac) |
| 583 | { |
| 584 | const uint32_t lutPhase = frac >> INTRP_PHASE_SHIFT; |
| 585 | const float fIntrpFrac = (frac & INTRP_PHASE_MASK) * (1.0f / INTRP_PHASE_SCALE); |
| 586 | |
| 587 | // it may look like we go out of bounds for fSinc_2, but we have an extra phase after LUT |
| 588 | const float *fSinc_1 = fSincLUT + ( lutPhase << SINC_TAPS_BITS); |
| 589 | const float *fSinc_2 = fSincLUT + ((lutPhase+1) << SINC_TAPS_BITS); |
| 590 | |
| 591 | float fSum = 0.0f; |
| 592 | for (int32_t i = 0; i < SINC_TAPS; i++) |
| 593 | { |
| 594 | const float fSample = fSmpData[i]; |
| 595 | |
| 596 | // do linear interpolation between phases |
| 597 | const float y1 = fSinc_1[i]; |
| 598 | const float y2 = fSinc_2[i]; |
| 599 | fSum += fSample * (y1 + ((y2 - y1) * fIntrpFrac)); |
| 600 | } |
| 601 | |
| 602 | return fSum; |
| 603 | } |
| 604 | |
| 605 | static int32_t resampleSamplingBuffer(void) |
| 606 | { |
| 607 | int32_t newSampleLength = (int32_t)(bytesSampled * dResamplingRatio) & ~1; |
| 608 | if (newSampleLength > config.maxSampleLength) |
| 609 | newSampleLength = config.maxSampleLength; |
| 610 | |
| 611 | float *fBuffer = (float *)malloc(newSampleLength * sizeof (float)); |
| 612 | if (fBuffer == NULL) |
| 613 | { |
| 614 | statusOutOfMemory(); |
| 615 | return -1; |
| 616 | } |
| 617 | |
| 618 | // pre-center sample data pointer (left side is pre-cleared) |
| 619 | const float *fSmpData = &fSamplingBuffer[-CENTER_TAP]; |
| 620 | |
| 621 | // set out-of-bounds sampling tap area |
| 622 | for (int32_t i = 0; i < SINC_TAPS/2; i++) |
| 623 | { |
| 624 | fSamplingBuffer[i-CENTER_TAP] = fSamplingBuffer[0]; |
| 625 | fSamplingBuffer[bytesSampled+i] = fSamplingBuffer[bytesSampled-1]; |
| 626 | } |
| 627 | |
| 628 | const uint64_t delta = (uint64_t)round(SMP_DELTA_FRAC_SCALE / dResamplingRatio); |
| 629 | uint64_t frac = 0; |
| 630 | |
| 631 | float fSmpPeak = 0.0f; |
| 632 | for (int32_t i = 0; i < newSampleLength; i++) |
| 633 | { |
| 634 | float fSmp; |
| 635 | |
| 636 | if (i < 2) // clear first 2 samps. (to prevent "stuck beep"), do it here for norm. peak scan |
| 637 | fSmp = 0.0f; |
| 638 | else |
| 639 | fSmp = sincInterpolation(fSmpData, (uint32_t)frac); |
| 640 | |
| 641 | fBuffer[i] = fSmp; |
| 642 | |
| 643 | // get peak (for normalization) |
| 644 | if (fSmp < 0.0f) |
| 645 | fSmp = -fSmp; |
| 646 | |
| 647 | if (fSmp > fSmpPeak) |
| 648 | fSmpPeak = fSmp; |
| 649 | // ------------------- |
| 650 | |
| 651 | frac += delta; |
| 652 | fSmpData += (uint32_t)(frac >> SMP_DELTA_FRAC_BITS); |
| 653 | frac &= SMP_DELTA_FRAC_MASK; |
| 654 | } |
| 655 | |
| 656 | // normalize and quantize to 8-bit integer |
| 657 | |
| 658 | int8_t *output = &song->sampleData[song->samples[editor.currSample].offset]; |
| 659 | if (fSmpPeak <= 0.25f) |
| 660 | { |
| 661 | // clear output sample if sampling peak was extremely low |
| 662 | memset(output, 0, newSampleLength); |
| 663 | } |
| 664 | else |
| 665 | { |
| 666 | const float fAmpMul = INT8_MAX / fSmpPeak; |
| 667 | for (int32_t i = 0; i < newSampleLength; i++) |
| 668 | output[i] = (int8_t)roundf(fBuffer[i] * fAmpMul); |
| 669 | } |
| 670 | |
| 671 | free(fBuffer); |
| 672 | return newSampleLength; |
| 673 | } |
| 674 | |
| 675 | void stopSampling(void) |
| 676 | { |
| 677 | while (callbackBusy); |
| 678 | audio.isSampling = false; |
| 679 | |
| 680 | int32_t newLength = resampleSamplingBuffer(); |
| 681 | |
| 682 | if (fSamplingBufferOrig != NULL) |
| 683 | { |
| 684 | free(fSamplingBufferOrig); |
| 685 | fSamplingBufferOrig = NULL; |
| 686 | } |
| 687 | |
| 688 | freePolyphaseSincLUT(); |
| 689 | |
| 690 | if (newLength == -1) |
| 691 | return; // out of memory |
| 692 | |
| 693 | moduleSample_t *s = &song->samples[editor.currSample]; |
| 694 | s->length = newLength; |
| 695 | s->fineTune = samplingFinetune; |
| 696 | s->loopStart = 0; |
| 697 | s->loopLength = 2; |
| 698 | s->volume = 64; |
| 699 | |
| 700 | pointerSetMode(POINTER_MODE_IDLE, DO_CARRY); |
| 701 | statusAllRight(); |
| 702 | |
| 703 | showCurrSample(); |
| 704 | } |
| 705 | |
| 706 | static void scrollListUp(void) |
| 707 | { |
| 708 | if (numAudioInputDevs <= VISIBLE_LIST_ENTRIES) |
| 709 | { |
| 710 | audioInputDevListOffset = 0; |
| 711 | return; |
| 712 | } |
| 713 | |
| 714 | if (audioInputDevListOffset > 0) |
| 715 | { |
| 716 | audioInputDevListOffset--; |
| 717 | listAudioDevices(); |
| 718 | mouse.lastSamplingButton = 0; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | static void scrollListDown(void) |
| 723 | { |
| 724 | if (numAudioInputDevs <= VISIBLE_LIST_ENTRIES) |
| 725 | { |
| 726 | audioInputDevListOffset = 0; |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | if (audioInputDevListOffset < numAudioInputDevs-VISIBLE_LIST_ENTRIES) |
| 731 | { |
| 732 | audioInputDevListOffset++; |
| 733 | listAudioDevices(); |
| 734 | mouse.lastSamplingButton = 1; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | static void finetuneUp(void) |
| 739 | { |
| 740 | if ((int8_t)samplingFinetune < 7) |
| 741 | { |
| 742 | samplingFinetune++; |
| 743 | updateOutputFrequency(); |
| 744 | drawSamplingFinetune(); |
| 745 | drawSamplingFrequency(); |
| 746 | mouse.lastSamplingButton = 2; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | static void finetuneDown(void) |
| 751 | { |
| 752 | if ((int8_t)samplingFinetune > -8) |
| 753 | { |
| 754 | samplingFinetune--; |
| 755 | updateOutputFrequency(); |
| 756 | drawSamplingFinetune(); |
| 757 | drawSamplingFrequency(); |
| 758 | mouse.lastSamplingButton = 3; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | void samplingSampleNumUp(void) |
| 763 | { |
| 764 | if (editor.currSample < 30) |
| 765 | { |
| 766 | editor.currSample++; |
| 767 | showCurrSample(); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | void samplingSampleNumDown(void) |
| 772 | { |
| 773 | if (editor.currSample > 0) |
| 774 | { |
| 775 | editor.currSample--; |
| 776 | showCurrSample(); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | void handleSamplingBox(void) |
| 781 | { |
| 782 | if (ui.changingSamplingNote) |
| 783 | { |
| 784 | ui.changingSamplingNote = false; |
| 785 | setPrevStatusMessage(); |
| 786 | pointerSetPreviousMode(); |
| 787 | drawSamplingNote(); |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | if (mouse.rightButtonPressed) |
| 792 | { |
| 793 | if (audio.isSampling) |
| 794 | stopSampling(); |
| 795 | else |
| 796 | startSampling(); |
| 797 | |
| 798 | return; |
| 799 | } |
| 800 | |
| 801 | if (!mouse.leftButtonPressed) |
| 802 | return; |
| 803 | |
| 804 | mouse.lastSamplingButton = -1; |
| 805 | mouse.repeatCounter = 0; |
| 806 | |
| 807 | if (audio.isSampling) |
| 808 | { |
| 809 | stopSampling(); |
| 810 | return; |
| 811 | } |
| 812 | |
| 813 | // check buttons |
| 814 | const int32_t mx = mouse.x; |
| 815 | const int32_t my = mouse.y; |
| 816 | |
| 817 | if (mx >= 182 && mx <= 243 && my >= 0 && my <= 10) // STOP (main UI) |
| 818 | { |
| 819 | turnOffVoices(); |
| 820 | } |
| 821 | |
| 822 | if (mx >= 6 && mx <= 25 && my >= 124 && my <= 133) // EXIT (main UI) |
| 823 | { |
| 824 | ui.samplingBoxShown = false; |
| 825 | removeSamplingBox(); |
| 826 | exitFromSam(); |
| 827 | } |
| 828 | |
| 829 | if (mx >= 98 && mx <= 108 && my >= 44 && my <= 54) // SAMPLE UP (main UI) |
| 830 | { |
| 831 | samplingSampleNumUp(); |
| 832 | } |
| 833 | |
| 834 | else if (mx >= 109 && mx <= 119 && my >= 44 && my <= 54) // SAMPLE DOWN (main UI) |
| 835 | { |
| 836 | samplingSampleNumDown(); |
| 837 | } |
| 838 | |
| 839 | else if (mx >= 143 && mx <= 176 && my >= 205 && my <= 215) // SCAN |
| 840 | { |
| 841 | if (audio.rescanAudioDevicesSupported) |
| 842 | { |
| 843 | scanAudioDevices(); |
| 844 | listAudioDevices(); |
| 845 | } |
| 846 | else |
| 847 | { |
| 848 | displayErrorMsg("UNSUPPORTED !"); |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | else if (mx >= 4 && mx <= 165 && my >= 220 && my <= 250) // DEVICE LIST |
| 853 | { |
| 854 | selectAudioDevice(audioInputDevListOffset + ((my - 220) >> 3)); |
| 855 | } |
| 856 | |
| 857 | else if (mx >= 2 && mx <= 41 && my >= 206 && my <= 216) // LEFT |
| 858 | { |
| 859 | if (samplingMode != SAMPLE_LEFT) |
| 860 | { |
| 861 | samplingMode = SAMPLE_LEFT; |
| 862 | drawSamplingModeCross(); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | else if (mx >= 49 && mx <= 95 && my >= 206 && my <= 216) // RIGHT |
| 867 | { |
| 868 | if (samplingMode != SAMPLE_RIGHT) |
| 869 | { |
| 870 | samplingMode = SAMPLE_RIGHT; |
| 871 | drawSamplingModeCross(); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | else if (mx >= 103 && mx <= 135 && my >= 206 && my <= 216) // MIX |
| 876 | { |
| 877 | if (samplingMode != SAMPLE_MIX) |
| 878 | { |
| 879 | samplingMode = SAMPLE_MIX; |
| 880 | drawSamplingModeCross(); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | else if (mx >= 188 && mx <= 237 && my >= 242 && my <= 252) // SAMPLE |
| 885 | { |
| 886 | startSampling(); |
| 887 | } |
| 888 | |
| 889 | else if (mx >= 242 && mx <= 277 && my >= 242 && my <= 252) // NOTE |
| 890 | { |
| 891 | ui.changingSamplingNote = true; |
| 892 | textOutBg(262, 230, "---", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]); |
| 893 | setStatusMessage("SELECT NOTE", NO_CARRY); |
| 894 | pointerSetMode(POINTER_MODE_MSG1, NO_CARRY); |
| 895 | } |
| 896 | |
| 897 | else if (mx >= 282 && mx <= 317 && my >= 242 && my <= 252) // EXIT |
| 898 | { |
| 899 | ui.samplingBoxShown = false; |
| 900 | removeSamplingBox(); |
| 901 | } |
| 902 | |
| 903 | else if (mx >= 166 && mx <= 177 && my >= 218 && my <= 228) // SCROLL LIST UP |
| 904 | { |
| 905 | scrollListUp(); |
| 906 | } |
| 907 | |
| 908 | else if (mx >= 166 && mx <= 177 && my >= 242 && my <= 252) // SCROLL LIST DOWN |
| 909 | { |
| 910 | scrollListDown(); |
| 911 | } |
| 912 | |
| 913 | else if (mx >= 296 && mx <= 306 && my >= 217 && my <= 227) // FINETUNE UP |
| 914 | { |
| 915 | finetuneUp(); |
| 916 | } |
| 917 | |
| 918 | else if (mx >= 307 && mx <= 317 && my >= 217 && my <= 227) // FINETUNE DOWN |
| 919 | { |
| 920 | finetuneDown(); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | void setSamplingNote(uint8_t note) // must be called from video thread! |
| 925 | { |
| 926 | if (note > 35) |
| 927 | note = 35; |
| 928 | |
| 929 | samplingNote = note; |
| 930 | samplingFinetune = 0; |
| 931 | updateOutputFrequency(); |
| 932 | |
| 933 | drawSamplingNote(); |
| 934 | drawSamplingFinetune(); |
| 935 | drawSamplingFrequency(); |
| 936 | } |
| 937 | |
| 938 | void handleRepeatedSamplingButtons(void) |
| 939 | { |
| 940 | if (!mouse.leftButtonPressed || mouse.lastSamplingButton == -1) |
| 941 | return; |
| 942 | |
| 943 | switch (mouse.lastSamplingButton) |
| 944 | { |
| 945 | case 0: |
| 946 | { |
| 947 | if (mouse.repeatCounter++ >= 3) |
| 948 | { |
| 949 | mouse.repeatCounter = 0; |
| 950 | scrollListUp(); |
| 951 | } |
| 952 | } |
| 953 | break; |
| 954 | |
| 955 | case 1: |
| 956 | { |
| 957 | if (mouse.repeatCounter++ >= 3) |
| 958 | { |
| 959 | mouse.repeatCounter = 0; |
| 960 | scrollListDown(); |
| 961 | } |
| 962 | } |
| 963 | break; |
| 964 | |
| 965 | case 2: |
| 966 | { |
| 967 | if (mouse.repeatCounter++ >= 5) |
| 968 | { |
| 969 | mouse.repeatCounter = 0; |
| 970 | finetuneUp(); |
| 971 | } |
| 972 | } |
| 973 | break; |
| 974 | |
| 975 | case 3: |
| 976 | { |
| 977 | if (mouse.repeatCounter++ >= 5) |
| 978 | { |
| 979 | mouse.repeatCounter = 0; |
| 980 | finetuneDown(); |
| 981 | } |
| 982 | } |
| 983 | break; |
| 984 | |
| 985 | default: break; |
| 986 | } |
| 987 | } |