# Synth and music


A [`Sound`](/docs/sound/) does not need a file. Its `gen` can be any procedure that fills a buffer with samples, and `fx` comes with two layers of them: five finished one-shots for cuts and clicks, and a small studio (a synth, drums, buses, effects) to write a whole score with. Everything here is arithmetic on a `[]fx.Sample`, so unlike files it also runs in the browser: every player on this page renders its sound with the code shown under it.

```odin
Sample    :: [2]f32                    // left, right
Generator :: #type proc(out: []Sample) // fills out with sound (stereo at RATE = 48000)
```

## One-shots

```odin
whoosh   :: proc(out: []Sample)
boom     :: proc(out: []Sample)
riser    :: proc(out: []Sample)
click    :: proc(out: []Sample)
sub_drop :: proc(out: []Sample)
```

Each is a `Generator`, ready for `Sound.gen`, and each fills the whole buffer it is given, so its shape follows the sound's `dur`: a `whoosh` with `dur = 0.4` is a quick swish, with `dur = 1.5` a slow pass.

```odin
SOUNDS := [?]fx.Sound {
	{name = "whoosh", gen = fx.whoosh, at = 4.8, dur = 0.7},
	{name = "boom", gen = fx.boom, at = 5.5, dur = 2, volume = 0.5},
}
```

They are loud (peaks around 0.8 of full scale): set a `volume`.

`whoosh` is filtered noise that swells and falls while its band sweeps up and the sound crosses from left to right: something passing by. Put its middle (the loudest point is 62% of the way through) on a whip pan or a fast move.

```odin
music_whoosh :: proc(out: []fx.Sample) {
	fx.whoosh(out)
}
```

`boom` is a low thud falling to 38 Hz with a noisy crack on top: a hit for cuts and titles. The thud takes about a second to die away, so give it 1.5 to 2 seconds.

```odin
music_boom :: proc(out: []fx.Sample) {
	fx.boom(out)
}
```

`riser` is noise and a tone climbing for the whole of its length and ending at full height, with nothing after it. It is half of a pair: place whatever it leads to exactly at its end. Because generators are ordinary procedures, you can also call them on parts of a buffer, as here.

```odin
music_riser :: proc(out: []fx.Sample) {
	drop := 2 * fx.RATE // the hit lands 2 s in
	fx.riser(out[:drop])
	fx.boom(out[drop:])
}
```

`click` is a short tick for cursor clicks and taps. It is over in about 40 ms, whatever the `dur`.

```odin
music_click :: proc(out: []fx.Sample) {
	fx.click(out)
}
```

`sub_drop` is an 808-style sub bass falling from 100 Hz to about 33 Hz while it fades to nothing at the end of the buffer. Small speakers will not play it.

```odin
music_sub_drop :: proc(out: []fx.Sample) {
	fx.sub_drop(out)
}
```

## The studio

For a score, the pieces are smaller. A buffer of samples is a bus. `play` adds a synth note to a bus, the drum procedures add hits, the effects change a bus in place, `send` adds one bus to another, and at the end one bus is what the generator hands over:

```odin
song :: proc(out: []fx.Sample) {
	pads := make([]fx.Sample, len(out)); defer delete(pads)
	fx.play(pads, PAD, at = 0, dur = 4, midi = 50)
	fx.kick(out, 0); fx.kick(out, 0.5)
	fx.duck(pads, {0, 0.5}, 0.7, 0.3)
	fx.send(out, pads, 1)
	fx.limit(out)
}
SOUNDS := [?]fx.Sound{{name = "music", gen = song, dur = 60}}
```

Three rules hold everywhere. Times are seconds from the start of the buffer. Notes and hits are added to what is already there, so order does not matter and anything can be layered. And what would run past the end of the buffer is cut off, so leave room for the last note's release.

Nothing is random that is not seeded: the same code renders the same samples on every machine, every time. It is also quick. A 60-second piece with a dozen buses renders in about two seconds, once, when the mix is first needed.

## Notes

```odin
note_hz :: proc(midi: f32) -> f32
play    :: proc(out: []Sample, p: Patch, at, dur, midi: f32, vel: f32 = 1)
```

Pitches are MIDI note numbers: 69 is the A above middle C (440 Hz), one more is a semitone up, twelve more an octave. Fractions are fine. `note_hz` converts one to a frequency.

| Note | C2 | A2 | C3 | A3 | C4 (middle C) | A4 | C5 |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Number | 36 | 45 | 48 | 57 | 60 | 69 | 72 |
| Hz | 65.4 | 110 | 130.8 | 220 | 261.6 | 440 | 523.3 |

`play` adds one note to `out`: it starts at second `at`, is held for `dur` and then rings out over the patch's `release`. `vel` is how hard the key is hit, 0..1: it scales the note's loudness and how far the filter envelope opens.

```odin
music_note :: proc(out: []fx.Sample) {
	patch := fx.Patch{wave = .Saw, attack = 0.01, decay = 0.3, sustain = 0.5, release = 0.4, cutoff = 1800, gain = 0.3}
	fx.play(out, patch, at = 0.1, dur = 0.8, midi = 57) // A3: held for 0.8 s, then 0.4 s of release
}
```

## Patches

A `Patch` describes the instrument: oscillators, into a low-pass filter with its own envelope, into drive, into the amplifier envelope. Most fields can stay at zero, so a patch only lists what it wants. The one thing every patch needs is an envelope: with `sustain = 0` and no `decay`, a note is over after a millisecond.

```odin
Wave :: enum {Saw, Square, Sine, Triangle}

Patch :: struct {
	wave:      Wave,
	voices:    int, // unison voices, 0 = 1
	detune:    f32, // cents between the outermost unison voices
	width:     f32, // 0..1 how far the unison voices are spread left and right
	sub:       f32, // level of a sine one octave below
	noise:     f32, // level of white noise
	attack:    f32, // seconds
	decay:     f32,
	sustain:   f32, // 0..1
	release:   f32,
	cutoff:    f32, // low-pass in Hz, 0 = open
	env:       f32, // Hz the filter envelope adds at its peak
	env_decay: f32, // seconds until the filter envelope has fallen
	track:     f32, // 0..1 how much the cutoff follows the note's pitch
	q:         f32, // resonance: 0 = 0.7 (none), 4 = whistling
	drive:     f32, // saturation, 0 = clean
	vibrato:   f32, // cents
	vib_hz:    f32, // 0 = 5.5
	vib_delay: f32, // seconds before the vibrato has come in
	bend:      f32, // starts this many semitones off and glides to the note
	bend_time: f32,
	pan:       f32, // -1..1
	gain:      f32, // 0 = 1
}
```

| Fields | What they do |
| --- | --- |
| `wave` | The oscillator's shape. `.Saw` is bright and the usual start for pads, basses and leads, `.Square` hollow, `.Triangle` soft, `.Sine` pure. |
| `voices`, `detune`, `width` | Unison: up to 9 copies of the oscillator, tuned evenly across `detune` cents and placed alternately left and right, `width` wide. This is what makes one note sound wide. More voices are not louder, and cost time in proportion. |
| `sub`, `noise` | A sine an octave below and white noise, mixed in at these levels before the filter. |
| `attack`, `decay`, `sustain`, `release` | The loudness of the note over time: up to full in `attack` seconds, down to the `sustain` level over about `decay` seconds, held there until `dur` is over, then out to silence in `release` seconds. `sustain = 0` with a short `decay` is a pluck. |
| `cutoff`, `q` | The low-pass filter (12 dB per octave). `cutoff = 0` leaves the filter out, and `env`, `track` and `q` with it. `q` adds a peak at the cutoff. |
| `env`, `env_decay` | On every note the cutoff starts `env` Hz higher and falls back over `env_decay` seconds: the bite at the start of a pluck or a bass. A softer `vel` opens it less. |
| `track` | Lets the cutoff follow the pitch so high notes are not duller than low ones. At 1 it follows fully; `cutoff` is then what middle C gets. |
| `drive` | Saturation after the filter: thicker and grittier, with the peaks held down. |
| `vibrato`, `vib_hz`, `vib_delay` | A pitch wobble of this many cents, 5.5 times a second unless `vib_hz` says otherwise. With `vib_delay` it fades in around that many seconds into the note (from half of it to one and a half times), so only held notes get it. |
| `bend`, `bend_time` | The note starts `bend` semitones off and glides to its pitch in `bend_time` seconds. Both must be set. `bend = -12` over seconds is a rising siren; `-0.5` over 50 ms a scooped attack. |
| `pan`, `gain` | Where the note sits, -1 left to 1 right, and its level. Patches are loud at `gain = 1`: 0.2 to 0.3 is usual for something that plays chords. |

A chord is `play` once per note. Seven detuned saws through a low-pass, with a slow attack and a long release, is the classic pad:

```odin
music_pad :: proc(out: []fx.Sample) {
	pad := fx.Patch {
		wave = .Saw, voices = 7, detune = 30, width = 0.9, // seven saws 30 cents apart, spread left and right
		attack = 0.4, decay = 1.2, sustain = 0.8, release = 0.9,
		cutoff = 2600, gain = 0.2,
	}
	// D3 F3 A3 C4 E4: D minor 9
	for midi in ([?]f32{50, 53, 57, 60, 64}) do fx.play(out, pad, at = 0.1, dur = 2.8, midi = midi)
}
```

## A score is a list

`play` takes one note, so a melody is a loop over a list of them. Keep that list as data, in musical units, and convert on the way in:

```odin
Music_Note :: struct {
	at, dur, midi, vel: f32, // start and length in sixteenths
}

MUSIC_STEP :: f32(0.125) // a sixteenth at 120 bpm, in seconds

MUSIC_MELODY := [?]Music_Note {
	{0, 3, 69, 1}, {3, 3, 74, 0.9}, {6, 2, 72, 0.8}, {8, 4, 69, 0.9}, {12, 2, 65, 0.8}, {14, 2, 67, 0.8},
	{16, 6, 69, 1}, {22, 2, 67, 0.8}, {24, 4, 65, 0.9}, {28, 2, 62, 0.8},
}
```

This patch uses most of the rest of the struct: a filter envelope, key tracking, delayed vibrato, a small bend into every note.

```odin
music_melody :: proc(out: []fx.Sample) {
	lead := fx.Patch {
		wave = .Saw, voices = 3, detune = 16, width = 0.6, sub = 0.25,
		attack = 0.012, decay = 0.35, sustain = 0.7, release = 0.22,
		cutoff = 2000, env = 3000, env_decay = 0.3, track = 0.5, // the filter opens on every note
		vibrato = 14, vib_delay = 0.22, // long notes start to sing
		bend = -0.5, bend_time = 0.05, // every note is reached from a quarter tone below
		gain = 0.3,
	}
	for n in MUSIC_MELODY {
		fx.play(out, lead, n.at * MUSIC_STEP, n.dur * MUSIC_STEP * 0.92, n.midi, n.vel)
	}
}
```

The list is the reason to write music this way. The pictures can read it too, and then they hit exactly what the music hits, with nothing to detect and nothing to keep in sync by hand. Below, a scene draws the same `MUSIC_MELODY`: where each note is, whether it sounds now, and a ring from [`fx.hit`](/docs/edit/#beat-and-pulse) when it starts. (The players on this page are separate, so this one is silent; in a video both run on one timeline.)

```odin
music_roll :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	x0, w := f32(260), f32(1400)
	for n in MUSIC_MELODY {
		t0, t1 := n.at * MUSIC_STEP, (n.at + n.dur) * MUSIC_STEP // the same numbers fx.play was given
		y := 760 - (n.midi - 62) * 34
		r := fx.Rect{x0 + w * t0 / 4, y, w * (t1 - t0) / 4 - 8, 26}
		sounding := s.lt >= t0 && s.lt < t1
		fx.rrect(r, 8, sounding ? fx.theme.na : (s.lt >= t1 ? fx.theme.nm : fx.theme.ns))
		k := fx.hit(s.lt, t0, 0.4) // 1 when the note starts, falling to 0
		if k > 0 do fx.ring({r.x + 13, y + 13}, 20 + 70 * (1 - k), fx.alpha(fx.theme.na, k), 4)
	}
	fx.vline(x0 + w * s.lt / 4, 240, 600, fx.theme.nt, 3)
	fx.label("for n in MUSIC_MELODY", {960, 950}, fx.mono(34), fx.theme.nm, {0.5, 0.5})
}
```

`projects/fable` is built on this. Its `music.odin` generates every note and hit of 32 bars into lists once (`ev()`), the score is rendered from them, and the scenes read them: `fx.pulse(ev().kicks[:], t)` drives the lens and the punch-ins, a piano roll draws `ev().lead`, and `marks = ev().kicks[:]` puts the kicks on the editor's ruler.

## Drums

```odin
kick  :: proc(out: []Sample, at: f32, vel: f32 = 1, hz: f32 = 47, punch: f32 = 150, length: f32 = 0.26, click: f32 = 0.5)
clap  :: proc(out: []Sample, at: f32, vel: f32 = 1, length: f32 = 0.16)
snare :: proc(out: []Sample, at: f32, vel: f32 = 1, hz: f32 = 196, length: f32 = 0.13)
hat   :: proc(out: []Sample, at: f32, vel: f32 = 1, length: f32 = 0.035, pan: f32 = 0)
crash :: proc(out: []Sample, at: f32, vel: f32 = 1, length: f32 = 1.6)

impact      :: proc(out: []Sample, at: f32, vel: f32 = 1, length: f32 = 1.4)
sweep_noise :: proc(out: []Sample, at, dur: f32, lo: f32 = 300, hi: f32 = 9000, up := true, vel: f32 = 1, q: f32 = 2.2)
```

Each adds one hit to `out` at second `at`; `vel` is its loudness. `length` is in seconds and sets how quickly the hit dies away; what it writes is two to three times that long.

| Procedure | Sound | Arguments |
| --- | --- | --- |
| `kick` | A sine falling quickly onto its note, with a click. | `hz`: the note it lands on. `punch`: how far above it the fall starts, in Hz (the first milliseconds start higher still). `click`: level of the tick at the front; 0 for a soft, round kick. |
| `clap` | Three quick bursts of noise and a short tail. | |
| `snare` | A short tone under a burst of bright noise. | `hz`: the tone. |
| `hat` | A hi-hat. | `length`: 0.03 closed, 0.2 open. `pan`: -1..1. |
| `crash` | A cymbal: a long wash of bright noise. | |
| `impact` | A deep, distorted hit with a long tail, for drops and title cards. | |
| `sweep_noise` | Noise in a band that moves from `lo` to `hi` Hz over `dur` while it swells: a riser into a drop. With `up = false` it runs from `hi` down to `lo` and dies away: the lifter after one. | `q`: how narrow the band is. |

A beat is a loop over the beats of the bar:

```odin
music_drums :: proc(out: []fx.Sample) {
	BEAT :: 0.5 // 120 bpm
	for b in 0 ..< 8 {
		t := f32(b) * BEAT
		fx.kick(out, t)
		if b % 2 == 1 do fx.clap(out, t, 0.8) // on two and four
		fx.hat(out, t + BEAT / 2, 0.8, 0.05, pan = 0.3) // the off-beat, a little longer
		fx.hat(out, t + BEAT / 4, 0.3, pan = -0.25)
		fx.hat(out, t + BEAT * 3 / 4, 0.4, pan = -0.25)
	}
	fx.crash(out, 0, 0.6)
	// a fill on the last beat, getting louder
	for i in 0 ..< 4 do fx.snare(out, 3.5 + f32(i) * BEAT / 4, 0.5 + 0.12 * f32(i))
	fx.limit(out)
}
```

Anything these do not cover you can write yourself: a hit is a loop that adds samples into `out`. `projects/fable/music.odin` has a short one, `tick`: a key being pressed.

## Buses and effects

```odin
send         :: proc(dst, src: []Sample, gain: f32 = 1)
automate     :: proc(buf: []Sample, gain: proc(t: f32) -> f32)
duck         :: proc(buf: []Sample, hits: []f32, depth: f32 = 0.7, release: f32 = 0.3, attack: f32 = 0.006)
sweep_filter :: proc(buf: []Sample, cutoff: proc(t: f32) -> f32, q: f32 = 0.8, high := false)
reverb       :: proc(buf: []Sample, room: f32 = 0.82, damp: f32 = 0.35, width: f32 = 1, pre: f32 = 0.012)
echo         :: proc(buf: []Sample, time: f32, feedback: f32 = 0.45, ping_pong := true, lowpass: f32 = 4500)
limit        :: proc(buf: []Sample, ceiling: f32 = 0.79, gain: f32 = 1, release: f32 = 0.12)
```

Give each group of instruments its own buffer, so it can be treated on its own, and add the buffers up at the end. `send(dst, src, gain)` adds `src` to `dst`: it is the fader of the mixing desk, and also how a copy of a bus gets to an effect.

| Procedure | What it does to the bus |
| --- | --- |
| `automate` | Multiplies it by `gain(t)`, `t` in seconds from the start of the buffer: fades, a part stepping back while another plays. |
| `duck` | Side-chain pumping. At every time in `hits` (seconds, ascending) the bus dips by `depth` (0..1, 1 = to silence) and comes back over `release` seconds, as if a compressor listened to the kick. `attack` is the few milliseconds the dip takes to open, just ahead of the hit. |
| `sweep_filter` | A low-pass over the whole bus whose cutoff is `cutoff(t)` in Hz; with `high = true`, a high-pass. A constant function makes it a fixed filter. |
| `reverb` | Replaces the bus by its reverberation (Freeverb). `room` 0..1 is how long the tail is, `damp` 0..1 how dull it gets, `width` its stereo width, `pre` a delay in seconds before it starts. |
| `echo` | Replaces the bus by its echoes: the first `time` seconds later, each next one `feedback` times as loud and duller (`lowpass`, Hz). `ping_pong` throws them from side to side. |
| `limit` | A look-ahead limiter: applies `gain`, then turns the bus down just before every peak so nothing exceeds `ceiling`, recovering over `release` seconds. |

`reverb` and `echo` leave only the effect in the bus, not the original. So they work on a copy: send the dry bus to a second one, run the effect there, and add both to the output.

```odin
music_space :: proc(out: []fx.Sample) {
	pluck := fx.Patch {
		wave = .Saw, voices = 2, detune = 12, width = 0.5, attack = 0.002, decay = 0.13, sustain = 0, release = 0.12,
		cutoff = 420, env = 5200, env_decay = 0.12, track = 0.5, q = 1.9, gain = 0.3,
	}
	dry, echoes, wet := make([]fx.Sample, len(out)), make([]fx.Sample, len(out)), make([]fx.Sample, len(out))
	defer {delete(dry); delete(echoes); delete(wet)}
	notes := [?]f32{62, 65, 69, 72, 76, 72, 69, 65}
	for i in 0 ..< 16 do fx.play(dry, pluck, f32(i) * 0.125, 0.1, notes[i % 8], i % 4 == 0 ? 1 : 0.7)

	fx.send(echoes, dry)
	fx.echo(echoes, 0.375, 0.5) // every three sixteenths, thrown from side to side
	fx.send(wet, dry, 0.5)
	fx.send(wet, echoes, 0.5) // the echoes go into the room too
	fx.reverb(wet, room = 0.85, damp = 0.4)

	fx.send(out, dry)
	fx.send(out, echoes, 0.6)
	fx.send(out, wet, 0.5)
	fx.automate(out, proc(t: f32) -> f32 {return fx.clamp01((5 - t) / 1.5)}) // fade the tail out
}
```

`sweep_filter` and `sweep_noise` are the two halves of a build: the music opens up while noise rises into the hit.

```odin
music_build :: proc(out: []fx.Sample) {
	DROP :: 2.5
	pad := fx.Patch{wave = .Saw, voices = 7, detune = 30, width = 0.9, attack = 0.3, sustain = 1, release = 0.05, gain = 0.2}
	pads := make([]fx.Sample, len(out))
	defer delete(pads)
	for midi in ([?]f32{50, 57, 62, 65}) do fx.play(pads, pad, 0, DROP - 0.05, midi)
	fx.sweep_filter(pads, proc(t: f32) -> f32 {
		return 200 * math.pow(40, fx.clamp01(t / DROP)) // 200 Hz opening to 8 kHz
	}, 0.9)
	fx.send(out, pads)
	fx.sweep_noise(out, 0, DROP, 300, 9000, vel = 0.6) // noise rising into the hit
	fx.impact(out, DROP, 0.7, length = 0.9)
	fx.sweep_noise(out, DROP, 1.5, 300, 8000, up = false, vel = 0.5) // and falling away after it
	fx.limit(out)
}
```

`duck` is what makes a pad breathe with the beat. It takes the same list of times that places the kicks, so the dip is exactly on them.

```odin
MUSIC_KICKS := [?]f32{0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5} // four on the floor, two bars at 120 bpm
```

```odin
music_beat :: proc(out: []fx.Sample) {
	pad := fx.Patch{wave = .Saw, voices = 7, detune = 30, width = 0.9, attack = 0.05, sustain = 0.9, release = 0.3, cutoff = 3000, gain = 0.2}
	bass := fx.Patch {
		wave = .Saw, sub = 1, attack = 0.004, decay = 0.1, sustain = 0.6, release = 0.05,
		cutoff = 200, env = 1000, env_decay = 0.11, q = 1.3, drive = 0.35, gain = 0.5,
	}
	pads, low := make([]fx.Sample, len(out)), make([]fx.Sample, len(out))
	defer {delete(pads); delete(low)}
	for bar in 0 ..< 2 {
		t0 := f32(bar) * 2
		chord := bar == 0 ? [4]f32{50, 53, 57, 60} : [4]f32{46, 53, 57, 62} // Dm7, then B♭maj7
		for midi in chord do fx.play(pads, pad, t0, 1.7, midi)
		for b in 0 ..< 4 do fx.play(low, bass, t0 + f32(b) * 0.5 + 0.25, 0.2, chord[0] - 12) // between the kicks
	}
	for t in MUSIC_KICKS {
		fx.kick(out, t)
		fx.hat(out, t + 0.25, 0.7, 0.05)
	}
	fx.duck(pads, MUSIC_KICKS[:], 0.7, 0.3) // the pump: the pad dips on every kick and swells back
	fx.duck(low, MUSIC_KICKS[:], 0.85, 0.2)
	fx.send(out, pads)
	fx.send(out, low)
	fx.limit(out)
}
```

End a score with `limit`. A sum of buses easily peaks above 1, and the [mix's own limiter](/docs/sound/#the-mix) starts bending at 0.8; `limit`'s default ceiling of 0.79 stays just under that, so a limited score passes through the mix unchanged, and its `gain` argument is the master volume: `fx.limit(out, 0.78, 0.72)` in `fable`.

## Pictures that follow the sound

```odin
levels   :: proc(buf: []Sample, allocator := context.allocator) -> []f32
level_at :: proc(lv: []f32, t: f32) -> f32
```

Lists of notes tell a picture when things happen. For how loud things are, measure the sound itself: `levels` is the loudness (RMS) of a bus for every 10 ms, and `level_at` reads that curve at a time in seconds, interpolated. Take the levels of the whole mix, or of single buses before they are added up: `fable` keeps one for kick and bass together and one for the lead.

```odin
music_levels :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	lv := music_beat_levels() // fx.levels(mix) of the beat above, rendered once and kept
	// the whole curve, a bar every 40 ms, lit up to the playhead
	for i in 0 ..< 100 {
		t := f32(i) * 0.04
		h := 10 + 400 * fx.level_at(lv, t)
		fx.rrect({262 + f32(i) * 14, 880 - h / 2, 8, h}, 4, t <= s.lt ? fx.theme.na : fx.theme.ns)
	}
	// the loudness now sets the size; the kick list sets the ring
	c := fx.Vec2{960, 380}
	fx.circle(c, 40 + 280 * fx.level_at(lv, s.lt), fx.theme.nt)
	k := fx.pulse(MUSIC_KICKS[:], s.lt, 0.4) // 1 on every kick, falling to 0
	fx.ring(c, 250 + 70 * (1 - k), fx.alpha(fx.theme.na, k), 6)
}
```

The curve belongs to a rendered piece, so render the piece once and keep both. This is the shape of `fable`'s `music.odin`:

```odin
Song :: struct {
	mix:   []fx.Sample,
	level: []f32, // of the whole mix
	low:   []f32, // of kick and bass
}

@(private = "file")
rendered: Song
@(private = "file")
rendered_once: sync.Once

// The piece as samples, rendered once per process.
song :: proc() -> ^Song {
	sync.once_do(&rendered_once, proc() {rendered = render_song()})
	return &rendered
}

// The Sound's generator: hands the mix to the renderer.
music :: proc(out: []fx.Sample) {copy(out, song().mix)}

SOUNDS := [?]fx.Sound{{name = "score", gen = music, dur = LENGTH}}
```

`render_song` builds the buses, calls `fx.levels` on the ones the pictures want and returns everything. A scene then asks `fx.level_at(song().low, s.t)`. The `sync.Once` matters: frames are drawn on many threads, and all of them may ask first.

## Filters and oscillators

```odin
Svf :: struct { … }

svf_set :: proc(f: ^Svf, hz: f32, q: f32 = 0.7071)
svf_low :: proc(f: ^Svf, x: Sample) -> Sample
svf_run :: proc(f: ^Svf, x: Sample) -> (low, band, high: Sample)

osc :: proc(w: Wave, phase, dt: f32) -> f32
```

For instruments and effects of your own, the parts the synth is made of are public. `Svf` is a stereo state-variable filter: `svf_set` gives it a cutoff (clamped to 16 Hz..21.6 kHz) and a resonance, `svf_low` filters one sample, `svf_run` returns the low-pass, band-pass and high-pass outputs at once. It stays stable when its cutoff is changed quickly, which the [`Biquad`](/docs/sound/#filters) does not, so use it for anything that sweeps. A zero `Svf` lets nothing through: set it before running it.

`osc` is one sample of a wave at `phase` (0..1), where `dt` is how far the phase moves per sample (`hz / fx.RATE`). Saw and square are band-limited, so high notes do not alias.

## Reference

| Name | What it is |
| --- | --- |
| `whoosh`, `boom`, `riser`, `click`, `sub_drop` | One-shot generators for `Sound.gen`. |
| `note_hz(midi)` | Frequency of a MIDI note number. |
| `Wave` | `.Saw`, `.Square`, `.Sine`, `.Triangle`. |
| `Patch` | A synth sound: oscillators, filter, drive, envelopes. |
| `play(out, p, at, dur, midi, vel)` | Adds one note. |
| `kick`, `clap`, `snare`, `hat`, `crash` | Add one drum hit at `at`. |
| `impact(out, at, vel, length)` | Adds a deep hit with a long tail. |
| `sweep_noise(out, at, dur, lo, hi, up, vel, q)` | Adds rising or falling noise. |
| `send(dst, src, gain)` | Adds one bus to another. |
| `automate(buf, gain)` | Multiplies a bus by a function of time. |
| `duck(buf, hits, depth, release, attack)` | Dips a bus at every hit. |
| `sweep_filter(buf, cutoff, q, high)` | Filters a bus with a moving cutoff. |
| `reverb(buf, room, damp, width, pre)` | Replaces a bus by its reverberation. |
| `echo(buf, time, feedback, ping_pong, lowpass)` | Replaces a bus by its echoes. |
| `limit(buf, ceiling, gain, release)` | Holds a bus's peaks under a ceiling. |
| `levels(buf, allocator)`, `level_at(lv, t)` | A bus's loudness every 10 ms, and its value at a time. |
| `Svf`, `svf_set`, `svf_low`, `svf_run` | A state-variable filter that can sweep. |
| `osc(w, phase, dt)` | One sample of a band-limited wave. |
