Home / Trance from scratch

The synthesizer, part by part

LearningUpdated 2026-09-27

How the Trance Synth works (oscillators, detune, filter, envelopes) and what each slider does, before we make four sounds with it.

Goal

Understand every control on one synthesizer, because the bass, pad, pluck and lead are all this instrument with different settings.

The idea

Trance Synth is the chain from What is a sound? Waveforms, filters and envelopes, oscillators into a filter into a volume envelope, plus a second envelope that moves the filter, and eight voices so it can play chords.

PartSlidersJob
OscillatorsSaws per note, Detune spread, Stereo width, Sub sinethe raw sound
FilterFilter cutoff, Filter resonancecarves harmonics away
Filter envelopeFilter envelope amount, Filter envelope decaymoves the cutoff after each note starts
Volume envelopeAmp attack, decay, sustain, releaseloudness over time
OutputOutput (dB)overall level

The supersaw. Each note plays 1 to 7 saws. With more than one, they're pulled slightly out of tune with each other (Detune, in cents; 100 cents is a semitone). Saws at slightly different pitches drift in and out of step, so the sound shimmers and thickens. That's the Supersaw, the sound of trance. Detune spread sets how far apart the outer saws are; Stereo width fans them across the speakers.

Five saws, each a hair off. Their sum never settles into one shape: that movement is the shimmer.

The sub is a pure sine an octave below the note. Weight without harmonics, which is why the bass uses it.

A cleaner saw. A saw drawn as a naive ramp makes Aliasing: harmonics too high for the sample rate fold back down as wrong notes. The synth smooths the sharp edge with PolyBLEP, which cut those false frequencies by about 11 dB in testing.

The filter is a resonant Low-pass filter. The filter envelope pushes the cutoff up by a chosen number of octaves when a note starts, then lets it fall back. That falling sweep is the "pew" in a pluck.

Times mean "time to fall by 60 dB" everywhere in this instrument, so they're easy to compare. A ninth simultaneous note steals the quietest voice.

Show the full code: trance_synth.jsfx (184 lines)
desc:Trance Synth
// A complete subtractive synthesizer in one file:
//     oscillators (1-7 detuned saws + a sub sine)
//       -> resonant low-pass filter (with its own envelope)
//       -> volume envelope (attack / decay / sustain / release)
// 8-voice polyphonic. Use it for the bass, pad, pluck and lead.
// All "times" are the time the sound takes to fall by 60 dB.

slider1:5<1,7,1>Saws per note
slider2:18<0,60,0.5>Detune spread (cents)
slider3:0.4<0,1,0.01>Stereo width
slider4:0<0,1,0.01>Sub sine (1 octave down)
slider5:2500<30,18000,1:log=1200>Filter cutoff (Hz)
slider6:0.2<0,1,0.01>Filter resonance
slider7:0<0,6,0.05>Filter envelope amount (octaves)
slider8:200<5,3000,1:log=200>Filter envelope decay (ms)
slider9:3<0.5,4000,0.5:log=50>Amp attack (ms)
slider10:400<5,4000,1:log=300>Amp decay (ms)
slider11:0.7<0,1,0.01>Amp sustain
slider12:150<5,6000,1:log=250>Amp release (ms)
slider13:-9<-36,6,0.5>Output (dB)

in_pin:none
out_pin:Left
out_pin:Right

@init
// Runs once when the effect loads: set up variables and helper functions.
NV = 8;                       // number of voices
// --- memory map: one block of numbers per voice property ---
v_note = 0;   v_stage = 8;   v_env = 16;   v_fenv = 24;   v_vel = 32;
v_freq = 40;  v_l1 = 48;     v_l2 = 56;    v_r1 = 64;     v_r2 = 72;
v_ph = 96;    // 8 phases per voice (7 saws + sub) = 64 numbers
ev_off = 200; ev_kind = 216; ev_note = 232; ev_vel = 248;   // event queue (16 per block)
saw_ratio = 300; saw_pan = 310; saw_gl = 320; saw_gr = 330; // per-saw settings
// voice stage: 0 free, 1 attack, 2 decay/sustain, 3 release

// PolyBLEP: a tiny correction that removes most of the aliasing of a naive saw.
function polyblep(t, dt) (
  t < dt ? ( t /= dt; t + t - t * t - 1; )
  : t > 1 - dt ? ( t = (t - 1) / dt; t * t + t + t + 1; )
  : 0;
);

// Multiplier per sample that makes a level fall 60 dB in `ms` milliseconds.
function fall(ms) ( exp(-6.9 / (max(ms, 0.1) * 0.001 * srate)); );

@slider
// Runs whenever a slider moves: turn slider values into ready-to-use numbers.
nsaw = slider1;
saw_gain = 1 / sqrt(nsaw);
amp_inc = 1 / (slider9 * 0.001 * srate);
amp_dec = fall(slider10);
amp_rel = fall(slider12);
f_dec = fall(slider8);
res_k = 2 * (1 - slider6) + 0.1;         // filter damping: low = ringing, high = calm
outgain = 10 ^ (slider13 / 20);
si = 0;
loop(7,
  norm = nsaw > 1 ? (si - (nsaw - 1) / 2) / ((nsaw - 1) / 2) : 0;   // -1 .. +1 across the saws
  saw_ratio[si] = 2 ^ (slider2 * norm / 1200);                      // detune as a frequency ratio
  saw_pan[si] = slider3 * norm;
  saw_gl[si] = sqrt(0.5 * (1 - saw_pan[si]));                       // equal-power pan
  saw_gr[si] = sqrt(0.5 * (1 + saw_pan[si]));
  si += 1;
);

@block
// Runs before each chunk of audio: read the incoming MIDI notes here.
blk_pos = 0;
ev_n = 0;
ev_i = 0;
while (midirecv(offs, m1, m2, m3)) (
  st = m1 & 0xF0;
  ev_n < 16 && (st == 0x90 || st == 0x80) ? (
    ev_off[ev_n] = offs;
    ev_kind[ev_n] = (st == 0x90 && m3 > 0);
    ev_note[ev_n] = m2;
    ev_vel[ev_n] = m3 / 127;
    ev_n += 1;
  );
);

@sample
// Runs once for EVERY audio sample (44,100+ times a second): make the sound here.
// ---- handle notes that start on this exact sample ----
while (ev_i < ev_n && ev_off[ev_i] <= blk_pos) (
  n = ev_note[ev_i];
  ev_kind[ev_i] ? (
    // note on: take a free voice, or steal the quietest one
    vi = 0; best = 1000000; j = 0;
    loop(NV,
      score = v_stage[j] == 0 ? -1 : v_env[j];
      score < best ? ( best = score; vi = j; );
      j += 1;
    );
    v_note[vi] = n;
    v_freq[vi] = 440 * 2 ^ ((n - 69) / 12);
    v_vel[vi] = ev_vel[ev_i];
    v_stage[vi] = 1;
    v_env[vi] = 0;
    v_fenv[vi] = 1;
    v_l1[vi] = v_l2[vi] = v_r1[vi] = v_r2[vi] = 0;
    k = 0;
    loop(8, v_ph[vi * 8 + k] = (k == 0 ? 0.5 : rand(1)); k += 1; );
  ) : (
    // note off: release every voice that is playing this note
    j = 0;
    loop(NV,
      (v_stage[j] == 1 || v_stage[j] == 2) && v_note[j] == n ? ( v_stage[j] = 3; );
      j += 1;
    );
  );
  ev_i += 1;
);

// ---- synthesize every active voice ----
outL = outR = 0;
j = 0;
loop(NV,
  v_stage[j] ? (
    // volume envelope
    st = v_stage[j];
    e = v_env[j];
    st == 1 ? ( e += amp_inc; e >= 1 ? ( e = 1; v_stage[j] = 2; ); )
    : st == 2 ? ( e = slider11 + (e - slider11) * amp_dec; )
    : ( e *= amp_rel; e < 0.0001 ? ( e = 0; v_stage[j] = 0; ); );
    v_env[j] = e;
    v_fenv[j] *= f_dec;

    // oscillators
    dt0 = v_freq[j] / srate;
    l = r = 0;
    i = 0;
    loop(nsaw,
      dt = dt0 * saw_ratio[i];
      p = v_ph[j * 8 + i] + dt;
      p >= 1 ? p -= 1;
      v_ph[j * 8 + i] = p;
      s = 2 * p - 1 - polyblep(p, dt);
      l += s * saw_gl[i];
      r += s * saw_gr[i];
      i += 1;
    );
    l *= saw_gain; r *= saw_gain;
    slider4 > 0 ? (
      p = v_ph[j * 8 + 7] + dt0 * 0.5;
      p >= 1 ? p -= 1;
      v_ph[j * 8 + 7] = p;
      sub = sin(2 * $pi * p) * slider4;
      l += sub; r += sub;
    );

    // resonant low-pass filter (TPT state-variable), cutoff moved by its envelope
    fc = slider5 * 2 ^ (slider7 * v_fenv[j]);
    fc = min(max(fc, 20), srate * 0.45);
    g = tan($pi * fc / srate);
    a1 = 1 / (1 + g * (g + res_k));
    a2 = g * a1;
    a3 = g * a2;
    // left
    v3 = l - v_l2[j];
    v1 = a1 * v_l1[j] + a2 * v3;
    v2 = v_l2[j] + a2 * v_l1[j] + a3 * v3;
    v_l1[j] = 2 * v1 - v_l1[j];
    v_l2[j] = 2 * v2 - v_l2[j];
    l = v2;
    // right
    v3 = r - v_r2[j];
    v1 = a1 * v_r1[j] + a2 * v3;
    v2 = v_r2[j] + a2 * v_r1[j] + a3 * v3;
    v_r1[j] = 2 * v1 - v_r1[j];
    v_r2[j] = 2 * v2 - v_r2[j];
    r = v2;

    amp = e * v_vel[j];
    outL += l * amp;
    outR += r * amp;
  );
  j += 1;
);
spl0 = outL * outgain;
spl1 = outR * outgain;
blk_pos += 1;

Download trance_synth.jsfx

Build it

A scratch track Synth Test with Trance Synth and a one-bar item holding an Am chord (A3, C4, E4: notes 57, 60, 64). Loop it and move one slider at a time:

  1. Saws per note, 1 to 7. One is a plain saw, seven is a wall.
  2. Detune spread, 0 to 60 cents. At 0 it's just louder. At 15 to 25, the classic supersaw. At 60, out of tune.
  3. Filter cutoff, 18000 down to 300 Hz. Bright to muffled.
  4. Filter resonance up, with the cutoff around 1000 Hz. A whistle appears at the cutoff.
  5. Filter envelope amount 3 octaves, Filter envelope decay 200 ms. Every note starts bright and closes.
  6. Amp attack 800 ms: a pad. Then Amp sustain 0 with a short Amp decay: plucks.
  7. Sub sine 0.5: a new layer an octave down.

Listen for

Try this

What makes a supersaw wide and thick?

Several saws slightly detuned from each other and spread in stereo. The small pitch differences make it shimmer.

What does the filter envelope do?

Raises the cutoff at the start of each note and lets it fall back, so each note goes bright to dark.

What's aliasing, and how does this synth reduce it?

Harmonics too high for the sample rate folding back as false notes. PolyBLEP smooths the saw's edge to reduce it.

What's polyphony?

Playing several notes at once. This synth has eight voices.