Synthesizer you can play (ESP32, FabGL sound, PAM8403)
Play it with your computer keyboard: six voices, four waveforms, envelope and a resonant filter, through a real amplifier to the speaker.
- SSD1306 OLED
How it works
An ESP32 computes the sound sample by sample, 12 000 times a second: six voices, each a sawtooth, square, triangle or sine wave with an attack-decay-sustain-release envelope, summed into a resonant low-pass filter. FabGL's SoundGenerator hands the samples by DMA through I2S0 to the ESP32's 8-bit DAC on GPIO25; from there a volume potentiometer, a PAM8403 class-D amplifier board (24 dB, bridge output) and a 57 mm speaker. Turn on the sound (speaker button) and play with the keys A W S E D F T G Y H U J K (C4 to C5, like a piano: the black keys on the upper row). The five potentiometers shape the sound while you play: waveform, cutoff, resonance, attack, release; the OLED shows the settings, the notes held and the output wave. Put the oscilloscope on DAC and on the speaker to see the samples and what the amplifier makes of them. The sketch (synth) is in the code view: change the sound engine and compile.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | A#4 2A1 GNDA2 GNDA4 2B4 2C#4 2C4 2C5 2D#4 2D4 2DS1 GNDE4 2F#4 2F4 2G#4 2G4 2RV1 1RV2 1RV3 1RV4 1RV5 1RV6 1 |
| 3V3 | A1 3V3DS1 VCCR1 1R2 1RV1 2RV2 2RV3 2RV4 2RV5 2 |
| V5 | A1 5VA2 +5V |
| VOL | A2 L inA2 R inRV6 W |
| DAC | A1 GPIO25RV6 2 |
| KEY_A4 | A1 GPIO23A4 1 |
| KEY_AS4 | A#4 1A1 GPIO26 |
| KEY_B4 | A1 GPIO27B4 1 |
| KEY_C4 | A1 GPIO4C4 1 |
| KEY_C5 | A1 GPIO33C5 1 |
| KEY_CS4 | A1 GPIO5C#4 1 |
| KEY_D4 | A1 GPIO13D4 1 |
| KEY_DS4 | A1 GPIO14D#4 1 |
| KEY_E4 | A1 GPIO15E4 1 |
| KEY_F4 | A1 GPIO16F4 1 |
| KEY_FS4 | A1 GPIO17F#4 1 |
| KEY_G4 | A1 GPIO18G4 1 |
| KEY_GS4 | A1 GPIO19G#4 1 |
| POT_ATT | A1 GPIO36/VPRV4 W |
| POT_CUT | A1 GPIO34RV2 W |
| POT_REL | A1 GPIO39/VNRV5 W |
| POT_RES | A1 GPIO35RV3 W |
| POT_WAVE | A1 GPIO32RV1 W |
| SCL | A1 GPIO22DS1 SCLR1 2 |
| SDA | A1 GPIO21DS1 SDAR2 2 |
| SPK_N | A2 L−LS1 − |
| SPK_P | A2 L+LS1 + |
- A#4
- Push button
- C#4
- Push button
- D#4
- Push button
- F#4
- Push button
- G#4
- Push button
- LS1
- Speaker
- R1
- Resistor
- R2
- Resistor
Parts
Firmware
sketch.ino · 183 lines · the first 120 here, all of it in the editor
// ESP32 synthesizer. 13 keys from C4 to C5 (buttons to GND on GPIO 4, 5, 13, 14, 15, 16, 17, 18,
// 19, 23, 26, 27, 33), six voices, four waveforms, an ADSR envelope and a resonant low-pass filter,
// computed sample by sample (12000 per second) inside FabGL's SoundGenerator. FabGL sends the samples
// by DMA through I2S0 to the 8-bit DAC on GPIO25: from there a volume potentiometer, a PAM8403
// amplifier and a speaker. Five potentiometers set the sound: waveform (GPIO32), cutoff (34),
// resonance (35), attack (36) and release (39). The OLED (I2C GPIO21 / 22) shows the settings,
// the notes held and the output wave.
#include "fabgl.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "driver/adc.h" // the legacy ADC API: FabGL's sound uses the legacy DAC driver, and ESP-IDF
// stops at boot when the legacy and the new driver (analogRead) meet
const int KEY_PINS[13] = { 4, 5, 13, 14, 15, 16, 17, 18, 19, 23, 26, 27, 33 };
const char *NOTE[13] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B", "C" };
const char *WAVE_NAME[4] = { "Saw", "Square", "Triangle", "Sine" };
#define POT_WAVE ADC1_CHANNEL_4 // GPIO32
#define POT_CUTOFF ADC1_CHANNEL_6 // GPIO34
#define POT_RESO ADC1_CHANNEL_7 // GPIO35
#define POT_ATTACK ADC1_CHANNEL_0 // GPIO36
#define POT_RELEASE ADC1_CHANNEL_3 // GPIO39
#define RATE 12000 // samples per second: the filter's cutoff stays below 2.4 kHz
#define VOICES 6
struct Voice {
uint32_t phase, inc;
int32_t env; // envelope 0 .. 32767
uint8_t stage; // 0 off, 1 attack, 2 decay, 3 sustain, 4 release
int8_t key;
};
int16_t SINE[256];
// the sound engine: one FabGL waveform generator that mixes all voices and filters the sum
class SynthEngine : public WaveformGenerator {
public:
Voice v[VOICES];
volatile int wave = 0;
volatile int32_t att = 300, dec = 4, sus = 22000, rel = 10; // envelope steps per sample
volatile int32_t f = 8000, q = 30000; // filter coefficients (Q15)
int32_t low = 0, band = 0;
int8_t scope[128];
volatile uint8_t scopePos = 0;
uint8_t scopeDiv = 0;
void setFrequency(int) override { }
int getSample() override {
int32_t mix = 0;
for (int i = 0; i < VOICES; i++) {
Voice &x = v[i];
if (!x.stage) continue;
switch (x.stage) {
case 1: x.env += att; if (x.env >= 32767) { x.env = 32767; x.stage = 2; } break;
case 2: x.env -= dec; if (x.env <= sus) { x.env = sus; x.stage = 3; } break;
case 4: x.env -= rel; if (x.env <= 0) { x.env = 0; x.stage = 0; continue; } break;
}
x.phase += x.inc;
int32_t p = x.phase >> 20, s; // phase 0 .. 4095
switch (wave) {
case 0: s = p - 2048; break; // saw
case 1: s = p < 2048 ? 2047 : -2048; break; // square
case 2: s = p < 2048 ? p * 2 - 2048 : 6143 - p * 2; break; // triangle
default: s = SINE[p >> 4]; break; // sine
}
mix += (s * x.env) >> 15;
}
// state-variable low-pass filter (Chamberlin)
int32_t in = mix >> 1;
low += (f * band) >> 15;
int32_t high = in - low - ((q * band) >> 15);
band += (f * high) >> 15;
if (low > 30000) low = 30000; else if (low < -30000) low = -30000;
if (band > 30000) band = 30000; else if (band < -30000) band = -30000;
int32_t out = (low * 13) >> 9;
if (out > 127) out = 127; else if (out < -127) out = -127;
if ((scopeDiv++ & 3) == 0) scope[scopePos++ & 127] = out;
return out;
}
void noteOn(int key) {
float hz = 261.63f * powf(2.0f, key / 12.0f);
int best = 0;
for (int i = 0; i < VOICES; i++) { // a free voice, else the quietest
if (!v[i].stage) { best = i; break; }
if (v[i].env < v[best].env) best = i;
}
Voice &x = v[best];
x.inc = (uint32_t)(hz * 4294967296.0f / RATE);
x.key = key;
x.stage = 1;
}
void noteOff(int key) {
for (int i = 0; i < VOICES; i++) if (v[i].stage && v[i].stage != 4 && v[i].key == key) v[i].stage = 4;
}
};
fabgl::SoundGenerator sound(RATE);
SynthEngine synth;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
bool held[13];
float cutoffHz, resoQ, attackMs, releaseMs;
// a potentiometer as 0 .. 1
float pot(adc1_channel_t ch) { return adc1_get_raw(ch) / 4095.0f; }
void readControls() {
synth.wave = min(3, (int)(pot(POT_WAVE) * 4));
cutoffHz = 80.0f * powf(30.0f, pot(POT_CUTOFF)); // 80 Hz .. 2.4 kHz
synth.f = (int32_t)(2.0f * sinf(3.14159f * cutoffHz / RATE) * 32768);
resoQ = 0.7f + pot(POT_RESO) * 7.3f; // Q 0.7 .. 8
synth.q = (int32_t)(32768 / resoQ);
attackMs = 2.0f * powf(500.0f, pot(POT_ATTACK)); // 2 ms .. 1 s
synth.att = max(1, (int)(32767 / (attackMs * RATE / 1000)));
releaseMs = 10.0f * powf(200.0f, pot(POT_RELEASE)); // 10 ms .. 2 s
synth.rel = max(1, (int)(32767 / (releaseMs * RATE / 1000)));
}
Open in the simulator