Menu with button support to switch wave tables

This commit is contained in:
Dejvino
2026-02-27 06:48:19 +01:00
parent 59b48c1ab3
commit 34d15a7f1c
4 changed files with 196 additions and 42 deletions
+26 -2
View File
@@ -45,14 +45,38 @@ void loopAudio() {
if (now - lastNoteChangeTime > 500) {
lastNoteChangeTime = now;
int noteIndex = random(0, SCALES[currentScaleIndex].numNotes);
currentFrequency = SCALES[currentScaleIndex].frequencies[noteIndex];
// Calculate frequency based on key, scale, and octave
const float baseFrequency = 261.63f; // C4
float keyFrequency = baseFrequency * pow(2.0f, currentKeyIndex / 12.0f);
int semitoneOffset = SCALES[currentScaleIndex].semitones[noteIndex];
currentFrequency = keyFrequency * pow(2.0f, semitoneOffset / 12.0f);
Serial.println("Playing note: " + String(currentFrequency) + " Hz");
}
// Generate the sine wave sample
int16_t sample;
double phaseIncrement = 2.0 * M_PI * currentFrequency / SAMPLE_RATE;
phase = fmod(phase + phaseIncrement, 2.0 * M_PI);
int16_t sample = static_cast<int16_t>(AMPLITUDE * sin(phase));
switch (currentWavetableIndex) {
case 0: // Sine
sample = static_cast<int16_t>(AMPLITUDE * sin(phase));
break;
case 1: // Square
sample = (phase < M_PI) ? AMPLITUDE : -AMPLITUDE;
break;
case 2: // Saw
sample = static_cast<int16_t>(AMPLITUDE * (1.0 - (phase / M_PI)));
break;
case 3: // Triangle
sample = static_cast<int16_t>(AMPLITUDE * (2.0 * fabs(phase / M_PI - 1.0) - 1.0));
break;
default:
sample = 0;
break;
}
// Write the same sample to both left and right channels (mono audio).
// This call is blocking and will wait until there is space in the DMA buffer.