Skip to content

Synthesizer

A synthesizer panel: knobs in four sections, an oscilloscope, a spectrum, a volume fader and a piano being played

A complete synthesizer in about 200 lines: examples/audio/synth.py.

Terminal window
python examples/audio/synth.py

Play it with the mouse on the piano or with the computer keys: A W S E D… are the white and black keys of one octave, and Z and X move the octave down and up. The three buttons at the top load presets (Bass, Lead, Pad).

The patch is a chain of engine modules:

MIDI in → 2 oscillators → mixer → filter → amplifier → distortion → chorus → delay → reverb → speakers
↑ ↑
└── envelope ┘

midi_in turns engine.note_on() into a pitch and a gate; the envelope opens the filter and the amplifier on every note. The panel keeps a dictionary of values and one function, put, sends each one to the module it controls:

def put(self, key, value):
self.values[key] = value
if key in ("cutoff", "resonance"):
self.engine.set(self.vcf, key, value)
elif key in ("attack", "decay", "sustain", "release"):
self.engine.set(self.env, key, value)
...

The view is made of knobs grouped in sections, a scope and a spectrum reading engine.scope() and engine.spectrum(), a VU meter, a fader for the volume and a piano whose note_on/note_off events go to the engine. pz.frames() redraws the window every frame so the displays move.