Menu with button support to switch wave tables
This commit is contained in:
+26
-2
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user