Lessons and the learning game

Build a synthesizer

A four-voice MIDI synthesizer on an Uno, built from one oscillator up.

The finished synth

Build a synthesizer runs every lesson on the same two boxes, the way studio gear is split:

  • The keyboard: an Arduino Uno with a 4x4 keypad, scanned by hand so several keys can be held, sending real MIDI at 31250 baud from pin 3 with SoftwareSerial.
  • The synth: a second Uno receiving MIDI on RX (pin 0) and making audio as 8-bit PWM at 62.5 kHz on pin 9.
  • The audio path: a 1 k resistor and 100 nF capacitor average the PWM into a wave, and 1 uF passes it to a piezo that shows the pitch it hears.
  • CUTOFF: a potentiometer on A0.

The catalog card names an ESP32 with a MAX98357A amplifier. The course builds on the Uno, whose timers make a PWM DAC that the simulator runs and the piezo hears.

How it works

Oscillators. Timer 2 interrupts 15,625 times a second. Each time, every voice adds its step to a 16-bit phase, and the phase's top byte reads a wavetable: sine from a table, saw, square and triangle straight from the phase. The frequency is step x 15625 / 65536, so middle C is a step of 1097.

Envelopes. Attack, decay, sustain and release, a state machine stepped 977 times a second, multiply every sample. Release is timed from full, so a note let go at 70 percent takes 70 percent of the time.

The filter. One line of fixed point, y += (x - y) x k / 256, with y in 8.8 so small steps are not lost. The cutoff is -15625 / 2 pi x ln(1 - k/256).

Polyphony. Four voices; a new note takes a free voice, and a fifth steals the oldest. The voices are summed at full level and clipped, a headroom choice the lesson explains. The interrupt takes about half of each 64 us sample.

MIDI in. Status bytes are 128 or more, data bytes under 128: note on is 0x90, note, velocity; note off is 0x80. Typed commands share the line, since text never sets the top bit.

Effects. Vibrato from a 5.5 Hz LFO on every voice's step, bitcrush by shifting away low bits, and presets: pluck, pad, bass and organ.

The course

  1. Oscillators: a phase accumulator, PWM, and four wavetables.
  2. Envelopes: ADSR, a pluck, a slow attack, and how release is timed.
  3. Filters: a one-pole low-pass in fixed point, on a knob.
  4. Polyphony: chords, voice stealing, the CPU budget and headroom.
  5. MIDI in: the bytes on the wire, note on and off, channels.
  6. Effects: vibrato, bitcrush, presets, and what an echo would cost.
  7. Project: a playable synth: play it, sweep it, and share a patch.

Every checked step is tested against the running circuit.