Reorganized file structure to allow building Arduino project

This commit is contained in:
Dejvino
2026-02-28 21:49:17 +01:00
parent ef69701878
commit aaeaa9986e
10 changed files with 391 additions and 224 deletions
+18 -25
View File
@@ -1,7 +1,9 @@
#include <mutex>
#include "AudioThread.h"
#include "SharedState.h"
#include <I2S.h>
#include <math.h>
#include "synth_engine.h"
// I2S Pin definitions
// You may need to change these to match your hardware setup (e.g., for a specific DAC).
@@ -16,6 +18,8 @@ const int16_t AMPLITUDE = 16383; // Use a lower amplitude to avoid clipping (max
// Create an I2S output object
I2S i2s(OUTPUT);
extern SynthEngine* globalSynth;
// --- Synthesizer State ---
float currentFrequency = 440.0f;
double phase = 0.0;
@@ -36,6 +40,9 @@ void setupAudio() {
// Seed the random number generator from an unconnected analog pin
randomSeed(analogRead(A0));
// Initialize the portable synth engine
globalSynth = new SynthEngine(SAMPLE_RATE);
}
void loopAudio() {
@@ -52,34 +59,20 @@ void loopAudio() {
int semitoneOffset = SCALES[currentScaleIndex].semitones[noteIndex];
currentFrequency = keyFrequency * pow(2.0f, semitoneOffset / 12.0f);
if (globalSynth) {
globalSynth->setFrequency(currentFrequency);
globalSynth->setGate(true); // Trigger envelope
}
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);
// Process a small batch of samples
int16_t samples[32];
if (globalSynth) globalSynth->process(samples, 32);
else memset(samples, 0, sizeof(samples));
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;
for (int i = 0; i < 32; ++i) {
i2s.write(samples[i]);
i2s.write(samples[i]);
}
// 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.
i2s.write(sample);
i2s.write(sample);
}