// for finding memory leaks in debug mode with Visual Studio #if defined _DEBUG && defined _MSC_VER #include #endif #include #include #include #include #include #include "pt2_header.h" // PI #include "pt2_textout.h" #include "pt2_sampler.h" #include "pt2_visuals.h" #include "pt2_helpers.h" #include "pt2_bmp.h" #include "pt2_audio.h" #include "pt2_tables.h" #include "pt2_config.h" #include "pt2_sampling.h" #include "pt2_replayer.h" enum { SAMPLE_LEFT = 0, SAMPLE_RIGHT = 1, SAMPLE_MIX = 2 }; // ok settings based on computational time and audio quality #define SINC_TAPS 64 /* 2^n */ #define SINC_TAPS_BITS 6 /* log2(SINC_TAPS) */ #define SINC_OVERSAMPLING 256 /* 2^n */ #define SINC_OVERSAMPLING_BITS 8 /* log2(SINC_OVERSAMPLING) */ #define CENTER_TAP ((SINC_TAPS/2)-1) #define SMP_DELTA_FRAC_BITS 32 #define SMP_DELTA_FRAC_SCALE (1ULL << SMP_DELTA_FRAC_BITS) #define SMP_DELTA_FRAC_MASK (SMP_DELTA_FRAC_SCALE-1) #define INTRP_PHASE_SHIFT (SMP_DELTA_FRAC_BITS-SINC_OVERSAMPLING_BITS) #define INTRP_PHASE_SCALE (1L << INTRP_PHASE_SHIFT) #define INTRP_PHASE_MASK (INTRP_PHASE_SCALE-1) #define SAMPLE_PREVIEW_WITDH 194 #define SAMPLE_PREVIEW_HEIGHT 38 #define MAX_INPUT_DEVICES 99 #define VISIBLE_LIST_ENTRIES 4 #define SAMPLING_BUFFER_SIZE 1024 /* may change on audio input init */ static volatile bool callbackBusy, displayingBuffer, samplingEnded; static bool audioDevOpen; static char *audioInputDevs[MAX_INPUT_DEVICES]; static uint8_t samplingNote = 33, samplingFinetune = 4; // period 124, max safe period for PAL Paula static int16_t displayBuffer[SAMPLING_BUFFER_SIZE]; static int32_t samplingMode = SAMPLE_MIX, inputFrequency, roundedOutputFrequency; static int32_t numAudioInputDevs, audioInputDevListOffset, selectedDev; static int32_t bytesSampled, maxSamplingLength, inputBufferSize; static float *fSincLUT, *fSamplingBuffer, *fSamplingBufferOrig; static double dOutputFrequency, dResamplingRatio; static SDL_AudioDeviceID recordDev; static void listAudioDevices(void); // zeroth-order modified Bessel function of the first kind (series approximation) static inline double besselI0(double z) { double s = 1.0, ds = 1.0, d = 2.0; const double zz = z * z; do { ds *= zz / (d * d); s += ds; d += 2.0; } while (ds > s*(1E-12)); return s; } static inline double sinc(double x, double cutoff) { if (x == 0.0) { return cutoff; } else { x *= PI; return sin(x * cutoff) / x; } } static bool calcPolyphaseSincLUT(double sincCutoff) { fSincLUT = (float *)malloc(SINC_TAPS * (SINC_OVERSAMPLING+1) * sizeof (float)); if (fSincLUT == NULL) { showErrorMsgBox("Out of memory!"); return false; } const double kaiserBeta = 9.62046; // stopband attenuation of ~96dB const double besselI0BetaMul = 1.0 / besselI0(kaiserBeta); for (int32_t i = 0; i < SINC_TAPS * SINC_OVERSAMPLING; i++) { const double x = i * (1.0 / SINC_OVERSAMPLING); // Kaiser-Bessel window const double n = (x * (2.0 / SINC_TAPS)) - 1.0; const double window = besselI0(kaiserBeta * sqrt(1.0 - n * n)) * besselI0BetaMul; const double wsinc = sinc(x - (double)(SINC_TAPS / 2), sincCutoff) * window; // re-arrange for faster logic when in use const int32_t point = i >> SINC_OVERSAMPLING_BITS; const int32_t phase = i & (SINC_OVERSAMPLING-1); fSincLUT[(phase << SINC_TAPS_BITS) + ((SINC_TAPS-1) - point)] = (float)wsinc; } // store inverted wrap-around taps after end of LUT (for phase interpolation) float *fEnd = &fSincLUT[SINC_TAPS * SINC_OVERSAMPLING]; for (int32_t i = 0; i < SINC_TAPS; i++) fEnd[i] = fSincLUT[(SINC_TAPS-1) - i]; return true; } static void freePolyphaseSincLUT(void) { if (fSincLUT != NULL) { free(fSincLUT); fSincLUT = NULL; } } static void updateOutputFrequency(void) { if (samplingNote > 35) samplingNote = 35; int32_t period = periodTable[((samplingFinetune & 0xF) * 37) + samplingNote]; if (period < 113) // also happens in our "set period" Paula function period = 113; dOutputFrequency = (double)PAULA_PAL_CLK / period; roundedOutputFrequency = (int32_t)(dOutputFrequency + 0.5); // for display } static void samplingCallback(void *userdata, Uint8 *stream, int len) { callbackBusy = true; if (!displayingBuffer) { if (len > SAMPLING_BUFFER_SIZE) len = SAMPLING_BUFFER_SIZE; const int16_t *L = (int16_t *)stream; const int16_t *R = ((int16_t *)stream) + 1; int16_t *dst16 = displayBuffer; if (samplingMode == SAMPLE_LEFT) { for (int32_t i = 0; i < len; i++) dst16[i] = L[i << 1]; } else if (samplingMode == SAMPLE_RIGHT) { for (int32_t i = 0; i < len; i++) dst16[i] = R[i << 1]; } else { for (int32_t i = 0; i < len; i++) dst16[i] = (L[i << 1] + R[i << 1]) >> 1; } } if (audio.isSampling) { if (bytesSampled+len > maxSamplingLength) len = maxSamplingLength - bytesSampled; if (len > inputBufferSize) len = inputBufferSize; const int16_t *L = (int16_t *)stream; const int16_t *R = ((int16_t *)stream) + 1; float *fSmp = &fSamplingBuffer[bytesSampled]; if (samplingMode == SAMPLE_LEFT) { for (int32_t i = 0; i < len; i++) fSmp[i] = L[i << 1] * (1.0f / (32768.0f / 128.0f)); } else if (samplingMode == SAMPLE_RIGHT) { for (int32_t i = 0; i < len; i++) fSmp[i] = R[i << 1] * (1.0f / (32768.0f / 128.0f)); } else { for (int32_t i = 0; i < len; i++) fSmp[i] = (L[i << 1] + R[i << 1]) * (0.5f / (32768.0f / 128.0f)); } bytesSampled += len; if (bytesSampled >= maxSamplingLength) { audio.isSampling = true; samplingEnded = true; } } callbackBusy = false; (void)userdata; } static void stopInputAudio(void) { if (recordDev > 0) { SDL_CloseAudioDevice(recordDev); recordDev = 0; } callbackBusy = false; } static void startInputAudio(void) { SDL_AudioSpec want, have; if (recordDev > 0) stopInputAudio(); if (numAudioInputDevs == 0 || selectedDev >= numAudioInputDevs) { audioDevOpen = false; return; } ASSERT(roundedOutputFrequency > 0); memset(&want, 0, sizeof (SDL_AudioSpec)); want.freq = config.audioInputFrequency; want.format = AUDIO_S16; want.channels = 2; want.callback = samplingCallback; want.userdata = NULL; want.samples = SAMPLING_BUFFER_SIZE; recordDev = SDL_OpenAudioDevice(audioInputDevs[selectedDev], true, &want, &have, 0); audioDevOpen = (recordDev != 0); inputFrequency = have.freq; inputBufferSize = have.samples; SDL_PauseAudioDevice(recordDev, false); } static void selectAudioDevice(int32_t dev) { if (dev < 0) return; if (numAudioInputDevs == 0) { listAudioDevices(); return; } if (dev >= numAudioInputDevs) return; listAudioDevices(); stopInputAudio(); selectedDev = dev; listAudioDevices(); startInputAudio(); changeStatusText(ui.statusMessage); } void freeAudioDeviceList(void) { for (int32_t i = 0; i < numAudioInputDevs; i++) { if (audioInputDevs[i] != NULL) { free(audioInputDevs[i]); audioInputDevs[i] = NULL; } } } static void scanAudioDevices(void) { freeAudioDeviceList(); numAudioInputDevs = SDL_GetNumAudioDevices(true); if (numAudioInputDevs > MAX_INPUT_DEVICES) numAudioInputDevs = MAX_INPUT_DEVICES; for (int32_t i = 0; i < numAudioInputDevs; i++) { const char *deviceName = SDL_GetAudioDeviceName(i, true); if (deviceName == NULL) { numAudioInputDevs--; // hide device continue; } const uint32_t stringLen = (uint32_t)strlen(deviceName); audioInputDevs[i] = (char *)malloc(stringLen + 2); if (audioInputDevs[i] == NULL) break; if (stringLen > 0) strcpy(audioInputDevs[i], deviceName); audioInputDevs[i][stringLen+1] = '\0'; // UTF-8 needs double null termination (XXX: citation needed) } audioInputDevListOffset = 0; // reset scroll position if (selectedDev >= numAudioInputDevs) selectedDev = 0; } static void listAudioDevices(void) { fillRect(3, 219, 163, 33, PAL_BACKGRD); if (numAudioInputDevs == 0) { textOut(16, 219+13, "NO DEVICES FOUND!", video.palette[PAL_QADSCP]); return; } for (int32_t i = 0; i < VISIBLE_LIST_ENTRIES; i++) { const int32_t dev = audioInputDevListOffset+i; if (audioInputDevListOffset+i >= numAudioInputDevs) break; if (dev == selectedDev) fillRect(4, 219+1+(i*(FONT_CHAR_H+3)), 161, 8, video.palette[PAL_GENBKG2]); if (audioInputDevs[dev] != NULL) textOutTightN(2+2, 219+2+(i*(FONT_CHAR_H+3)), audioInputDevs[dev], 23, video.palette[PAL_QADSCP]); } } static void drawSamplingNote(void) { ASSERT(samplingNote < 36); const char *str = config.accidental ? noteNames2[2+samplingNote]: noteNames1[2+samplingNote]; textOutBg(262, 230, str, video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]); } static void drawSamplingFinetune(void) { textOutBg(254, 219, ftuneStrTab[samplingFinetune & 0xF], video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]); } static void drawSamplingFrequency(void) { char str[16]; sprintf(str, "%05dHZ", roundedOutputFrequency); const int32_t maxSafeFrequency = (int32_t)(PAL_PAULA_MAX_SAFE_HZ + 0.5); // rounded textOutBg(262, 208, str, roundedOutputFrequency <= maxSafeFrequency ? video.palette[PAL_GENTXT] : 0x8C0F0F, video.palette[PAL_GENBKG]); } static void drawSamplingModeCross(void) { // clear old crosses fillRect(4, 208, 6, 5, video.palette[PAL_GENBKG]); fillRect(51, 208, 6, 5, video.palette[PAL_GENBKG]); fillRect(105, 208, 6, 5, video.palette[PAL_GENBKG]); int16_t x; if (samplingMode == SAMPLE_LEFT) x = 3; else if (samplingMode == SAMPLE_RIGHT) x = 50; else x = 104; charOut(x, 208, 'X', video.palette[PAL_GENTXT]); } static void showCurrSample(void) { updateCurrSample(); // reset sampler screen attributes sampler.loopStartPos = 0; sampler.loopEndPos = 0; editor.markStartOfs = -1; editor.markEndOfs = -1; editor.samplePos = 0; hideSprite(SPRITE_LOOP_PIN_LEFT); hideSprite(SPRITE_LOOP_PIN_RIGHT); renderSampleData(); } void renderSamplingBox(void) { modStop(); editor.songPlaying = false; editor.playMode = PLAY_MODE_NORMAL; editor.currMode = MODE_IDLE; pointerSetMode(POINTER_MODE_IDLE, DO_CARRY); editor.sampleZero = false; editor.blockMarkFlag = false; // remove all open screens (except sampler) if (ui.diskOpScreenShown || ui.posEdScreenShown || ui.editOpScreenShown) { ui.diskOpScreenShown = false; ui.posEdScreenShown = false; ui.editOpScreenShown = false; displayMainScreen(); } blit32(0, 203, 320, 52, samplingBoxBMP); // render sample monitor blit32(120, 44, 200, 55, sampleMonitorBMP); memset(displayBuffer, 0, sizeof (displayBuffer)); hLine(123, 76, 194, video.palette[PAL_QADSCP]); // draw center line updateOutputFrequency(); drawSamplingNote(); drawSamplingFinetune(); drawSamplingFrequency(); drawSamplingModeCross(); showCurrSample(); changeStatusText("PLEASE WAIT..."); flipFrame(); hpc_ResetCounters(&video.vblankHpc); scanAudioDevices(); selectAudioDevice(selectedDev); setStatusMessage("ALL RIGHT", DO_CARRY); } static int32_t scrPos2SmpBufPos(int32_t x) // x = 0..SAMPLE_PREVIEW_WITDH { return (x * ((SAMPLING_BUFFER_SIZE << 16) / SAMPLE_PREVIEW_WITDH)) >> 16; } static uint8_t getDispBuffPeak(const int16_t *smpData, int32_t smpNum) { int32_t smpAbs, max = 0; for (int32_t i = 0; i < smpNum; i++) { const int32_t smp = smpData[i]; smpAbs = ABS(smp); if (smpAbs > max) max = smpAbs; } max = ((max * SAMPLE_PREVIEW_HEIGHT) + 32768) >> 16; if (max > (SAMPLE_PREVIEW_HEIGHT/2)-1) max = (SAMPLE_PREVIEW_HEIGHT/2)-1; return (uint8_t)max; } void writeSampleMonitorWaveform(void) // called every frame { if (!ui.samplingBoxShown || ui.askBoxShown) return; if (samplingEnded) { samplingEnded = false; stopSampling(); } // clear waveform background fillRect(123, 58, SAMPLE_PREVIEW_WITDH, SAMPLE_PREVIEW_HEIGHT, video.palette[PAL_BACKGRD]); if (!audioDevOpen) { textOutTight(136, 74, "CAN'T OPEN AUDIO DEVICE!", video.palette[PAL_QADSCP]); return; } uint32_t *centerPtr = &video.frameBuffer[(76 * SCREEN_W) + 123]; // hardcoded for a buffer size of 512 displayingBuffer = true; for (int32_t x = 0; x < SAMPLE_PREVIEW_WITDH; x++) { int32_t smpIdx = scrPos2SmpBufPos(x); int32_t smpNum = scrPos2SmpBufPos(x+1) - smpIdx; if (smpIdx+smpNum >= SAMPLING_BUFFER_SIZE) smpNum = SAMPLING_BUFFER_SIZE - smpIdx; const int32_t smpAbs = getDispBuffPeak(&displayBuffer[smpIdx], smpNum); if (smpAbs == 0) centerPtr[x] = video.palette[PAL_QADSCP]; else vLine(x + 123, 76 - smpAbs, (smpAbs * 2) + 1, video.palette[PAL_QADSCP]); } displayingBuffer = false; } void removeSamplingBox(void) { stopInputAudio(); freeAudioDeviceList(); ui.aboutScreenShown = false; editor.blockMarkFlag = false; displayMainScreen(); updateVisualizer(); // kludge // re-render sampler screen exitFromSam(); samplerScreen(); } static void startSampling(void) { if (!audioDevOpen) { displayErrorMsg("DEVICE ERROR !"); return; } ASSERT(roundedOutputFrequency > 0); dResamplingRatio = dOutputFrequency / inputFrequency; maxSamplingLength = (int32_t)ceil(config.maxSampleLength /dResamplingRatio) + 1; const int32_t allocLen = (SINC_TAPS/2) + maxSamplingLength + (SINC_TAPS/2); fSamplingBufferOrig = (float *)malloc(allocLen * sizeof (float)); if (fSamplingBufferOrig == NULL) { statusOutOfMemory(); return; } fSamplingBuffer = fSamplingBufferOrig + (SINC_TAPS/2); // allow negative look-up for sinc taps // clear tap area before sample memset(fSamplingBufferOrig, 0, (SINC_TAPS/2) * sizeof (float)); const double sincCutoff = MIN(1.0, dResamplingRatio); if (!calcPolyphaseSincLUT(sincCutoff)) { free(fSamplingBufferOrig); statusOutOfMemory(); return; } bytesSampled = 0; samplingEnded = false; audio.isSampling = true; turnOffVoices(); pointerSetMode(POINTER_MODE_RECORD, NO_CARRY); setStatusMessage("SAMPLING ...", NO_CARRY); } static float sincInterpolation(const float *fSmpData, const uint32_t frac) { const uint32_t lutPhase = frac >> INTRP_PHASE_SHIFT; const float fIntrpFrac = (frac & INTRP_PHASE_MASK) * (1.0f / INTRP_PHASE_SCALE); // it may look like we go out of bounds for fSinc_2, but we have an extra phase after LUT const float *fSinc_1 = fSincLUT + ( lutPhase << SINC_TAPS_BITS); const float *fSinc_2 = fSincLUT + ((lutPhase+1) << SINC_TAPS_BITS); float fSum = 0.0f; for (int32_t i = 0; i < SINC_TAPS; i++) { const float fSample = fSmpData[i]; // do linear interpolation between phases const float y1 = fSinc_1[i]; const float y2 = fSinc_2[i]; fSum += fSample * (y1 + ((y2 - y1) * fIntrpFrac)); } return fSum; } static int32_t resampleSamplingBuffer(void) { int32_t newSampleLength = (int32_t)(bytesSampled * dResamplingRatio) & ~1; if (newSampleLength > config.maxSampleLength) newSampleLength = config.maxSampleLength; float *fBuffer = (float *)malloc(newSampleLength * sizeof (float)); if (fBuffer == NULL) { statusOutOfMemory(); return -1; } // pre-center sample data pointer (left side is pre-cleared) const float *fSmpData = &fSamplingBuffer[-CENTER_TAP]; // set out-of-bounds sampling tap area for (int32_t i = 0; i < SINC_TAPS/2; i++) { fSamplingBuffer[i-CENTER_TAP] = fSamplingBuffer[0]; fSamplingBuffer[bytesSampled+i] = fSamplingBuffer[bytesSampled-1]; } const uint64_t delta = (uint64_t)round(SMP_DELTA_FRAC_SCALE / dResamplingRatio); uint64_t frac = 0; float fSmpPeak = 0.0f; for (int32_t i = 0; i < newSampleLength; i++) { float fSmp; if (i < 2) // clear first 2 samps. (to prevent "stuck beep"), do it here for norm. peak scan fSmp = 0.0f; else fSmp = sincInterpolation(fSmpData, (uint32_t)frac); fBuffer[i] = fSmp; // get peak (for normalization) if (fSmp < 0.0f) fSmp = -fSmp; if (fSmp > fSmpPeak) fSmpPeak = fSmp; // ------------------- frac += delta; fSmpData += (uint32_t)(frac >> SMP_DELTA_FRAC_BITS); frac &= SMP_DELTA_FRAC_MASK; } // normalize and quantize to 8-bit integer int8_t *output = &song->sampleData[song->samples[editor.currSample].offset]; if (fSmpPeak <= 0.25f) { // clear output sample if sampling peak was extremely low memset(output, 0, newSampleLength); } else { const float fAmpMul = INT8_MAX / fSmpPeak; for (int32_t i = 0; i < newSampleLength; i++) output[i] = (int8_t)roundf(fBuffer[i] * fAmpMul); } free(fBuffer); return newSampleLength; } void stopSampling(void) { while (callbackBusy); audio.isSampling = false; int32_t newLength = resampleSamplingBuffer(); if (fSamplingBufferOrig != NULL) { free(fSamplingBufferOrig); fSamplingBufferOrig = NULL; } freePolyphaseSincLUT(); if (newLength == -1) return; // out of memory moduleSample_t *s = &song->samples[editor.currSample]; s->length = newLength; s->fineTune = samplingFinetune; s->loopStart = 0; s->loopLength = 2; s->volume = 64; pointerSetMode(POINTER_MODE_IDLE, DO_CARRY); statusAllRight(); showCurrSample(); } static void scrollListUp(void) { if (numAudioInputDevs <= VISIBLE_LIST_ENTRIES) { audioInputDevListOffset = 0; return; } if (audioInputDevListOffset > 0) { audioInputDevListOffset--; listAudioDevices(); mouse.lastSamplingButton = 0; } } static void scrollListDown(void) { if (numAudioInputDevs <= VISIBLE_LIST_ENTRIES) { audioInputDevListOffset = 0; return; } if (audioInputDevListOffset < numAudioInputDevs-VISIBLE_LIST_ENTRIES) { audioInputDevListOffset++; listAudioDevices(); mouse.lastSamplingButton = 1; } } static void finetuneUp(void) { if ((int8_t)samplingFinetune < 7) { samplingFinetune++; updateOutputFrequency(); drawSamplingFinetune(); drawSamplingFrequency(); mouse.lastSamplingButton = 2; } } static void finetuneDown(void) { if ((int8_t)samplingFinetune > -8) { samplingFinetune--; updateOutputFrequency(); drawSamplingFinetune(); drawSamplingFrequency(); mouse.lastSamplingButton = 3; } } void samplingSampleNumUp(void) { if (editor.currSample < 30) { editor.currSample++; showCurrSample(); } } void samplingSampleNumDown(void) { if (editor.currSample > 0) { editor.currSample--; showCurrSample(); } } void handleSamplingBox(void) { if (ui.changingSamplingNote) { ui.changingSamplingNote = false; setPrevStatusMessage(); pointerSetPreviousMode(); drawSamplingNote(); return; } if (mouse.rightButtonPressed) { if (audio.isSampling) stopSampling(); else startSampling(); return; } if (!mouse.leftButtonPressed) return; mouse.lastSamplingButton = -1; mouse.repeatCounter = 0; if (audio.isSampling) { stopSampling(); return; } // check buttons const int32_t mx = mouse.x; const int32_t my = mouse.y; if (mx >= 182 && mx <= 243 && my >= 0 && my <= 10) // STOP (main UI) { turnOffVoices(); } if (mx >= 6 && mx <= 25 && my >= 124 && my <= 133) // EXIT (main UI) { ui.samplingBoxShown = false; removeSamplingBox(); exitFromSam(); } if (mx >= 98 && mx <= 108 && my >= 44 && my <= 54) // SAMPLE UP (main UI) { samplingSampleNumUp(); } else if (mx >= 109 && mx <= 119 && my >= 44 && my <= 54) // SAMPLE DOWN (main UI) { samplingSampleNumDown(); } else if (mx >= 143 && mx <= 176 && my >= 205 && my <= 215) // SCAN { if (audio.rescanAudioDevicesSupported) { scanAudioDevices(); listAudioDevices(); } else { displayErrorMsg("UNSUPPORTED !"); } } else if (mx >= 4 && mx <= 165 && my >= 220 && my <= 250) // DEVICE LIST { selectAudioDevice(audioInputDevListOffset + ((my - 220) >> 3)); } else if (mx >= 2 && mx <= 41 && my >= 206 && my <= 216) // LEFT { if (samplingMode != SAMPLE_LEFT) { samplingMode = SAMPLE_LEFT; drawSamplingModeCross(); } } else if (mx >= 49 && mx <= 95 && my >= 206 && my <= 216) // RIGHT { if (samplingMode != SAMPLE_RIGHT) { samplingMode = SAMPLE_RIGHT; drawSamplingModeCross(); } } else if (mx >= 103 && mx <= 135 && my >= 206 && my <= 216) // MIX { if (samplingMode != SAMPLE_MIX) { samplingMode = SAMPLE_MIX; drawSamplingModeCross(); } } else if (mx >= 188 && mx <= 237 && my >= 242 && my <= 252) // SAMPLE { startSampling(); } else if (mx >= 242 && mx <= 277 && my >= 242 && my <= 252) // NOTE { ui.changingSamplingNote = true; textOutBg(262, 230, "---", video.palette[PAL_GENTXT], video.palette[PAL_GENBKG]); setStatusMessage("SELECT NOTE", NO_CARRY); pointerSetMode(POINTER_MODE_MSG1, NO_CARRY); } else if (mx >= 282 && mx <= 317 && my >= 242 && my <= 252) // EXIT { ui.samplingBoxShown = false; removeSamplingBox(); } else if (mx >= 166 && mx <= 177 && my >= 218 && my <= 228) // SCROLL LIST UP { scrollListUp(); } else if (mx >= 166 && mx <= 177 && my >= 242 && my <= 252) // SCROLL LIST DOWN { scrollListDown(); } else if (mx >= 296 && mx <= 306 && my >= 217 && my <= 227) // FINETUNE UP { finetuneUp(); } else if (mx >= 307 && mx <= 317 && my >= 217 && my <= 227) // FINETUNE DOWN { finetuneDown(); } } void setSamplingNote(uint8_t note) // must be called from video thread! { if (note > 35) note = 35; samplingNote = note; samplingFinetune = 0; updateOutputFrequency(); drawSamplingNote(); drawSamplingFinetune(); drawSamplingFrequency(); } void handleRepeatedSamplingButtons(void) { if (!mouse.leftButtonPressed || mouse.lastSamplingButton == -1) return; switch (mouse.lastSamplingButton) { case 0: { if (mouse.repeatCounter++ >= 3) { mouse.repeatCounter = 0; scrollListUp(); } } break; case 1: { if (mouse.repeatCounter++ >= 3) { mouse.repeatCounter = 0; scrollListDown(); } } break; case 2: { if (mouse.repeatCounter++ >= 5) { mouse.repeatCounter = 0; finetuneUp(); } } break; case 3: { if (mouse.repeatCounter++ >= 5) { mouse.repeatCounter = 0; finetuneDown(); } } break; default: break; } }