sound.synth

index · sound

Overview

A tone synthesizer for game sounds. A tone is one oscillator (sine, square, saw, triangle or noise) shaped by a linear ADSR envelope. The C core (buffer:osc, buffer:fade) renders the samples; this module joins the envelope segments. The result is a sound.buffer for mixer:play.

Functions

NameSignature
note_freqnote_freq(name) -> hz
tonetone(opts) -> buf, err

note_freq(name) -> hz

Convert a note name to a frequency

name is a letter, an optional # or b, and an octave: "A4", "C#5", "Eb3". A4 is 440 Hz, in equal temperament. Returns nil for a name it cannot read.

tone(opts) -> buf, err

Generate a tone buffer

opts fielddefaultdescription
freq440frequency, Hz
notenilnote name such as "A4" or "C#5"; used when freq is absent
dur0.25duration, seconds
rate48000sample rate, Hz
channels2channel count
wave"sine""sine", "square", "saw", "triangle", or "noise"
amp0.5amplitude, 0..1
attack0.005ADSR attack, seconds
decay0.0ADSR decay, seconds
sustain1.0ADSR sustain level, 0..1
release0.02ADSR release, seconds
seednilnoise only: a 32-bit seed that makes the tone the same every call; without it the noise draws the process-wide stream, so two tones differ

Returns nil and an error for an unreadable note or an unknown wave.