Synthesis

Make sound from nothing: oscillators, noise, envelopes, sweeps and drums.

On this page

Make sound from nothing: oscillators, noise, envelopes, sweeps and drums.

audioFreq

Convert a note name or MIDI number to a frequency in Hz

audioFreq( note )
ArgumentTypeDefault
noteanyrequiredA note name such as C4, F#3 or Bb2 (C4 is middle C), or a MIDI note number where 69 is A4. Fractions detune, 60.1 is ten cents above C4.

Returns The frequency in Hz with A4 tuned to 440.

audioOsc

Generate a tone with an oscillator

audioOsc( shape, freq, seconds, sampleRate=44100, harmonics=16, partials, width=0.5, phase=0 )
ArgumentTypeDefault
shapestringrequiredsine, saw, square, triangle, pulse or additive. Saw, square and triangle are band limited so they do not alias. Pulse is the naive digital kind used by chiptune hardware.
freqdoublerequiredThe frequency in Hz, see audioFreq() for note names.
secondsdoublerequiredThe length in seconds.
sampleRateinteger44100The sample rate in Hz. Defaults to 44100.
harmonicsinteger16For saw, square and triangle: the most harmonics to add, fewer are used near the Nyquist frequency. Fewer harmonics sound duller. Defaults to 16.
partialsarrayoptionalFor additive: an array of [ frequency multiple, amplitude ] pairs, for example [ [ 1, 1.0 ], [ 2, 0.5 ] ]. Amplitudes are used as given.
widthdouble0.5For pulse: the fraction of the cycle spent high, 0.5 is a square. NES style duties are 0.125, 0.25, 0.5 and 0.75. Defaults to 0.5.
phasedouble0Where in the cycle to start, 0 to 1. Defaults to 0.

Returns A mono audio buffer with a peak of about 1.

audioSweep

Generate a sine that glides from one pitch to another

audioSweep( startHz, endHz, seconds, speed=28, sampleRate=44100 )
ArgumentTypeDefault
startHzdoublerequiredThe frequency at the start, in Hz.
endHzdoublerequiredThe frequency it settles towards, in Hz.
secondsdoublerequiredThe length in seconds.
speeddouble28How fast it settles, in 1/seconds. Higher is quicker. Defaults to 28.
sampleRateinteger44100The sample rate in Hz. Defaults to 44100.

Returns A mono audio buffer. Shape it with audioEnvelope() to make a kick or a laser.

audioNoise

Generate repeatable noise

audioNoise( seconds, sampleRate=44100, seed=1, color="white" )
ArgumentTypeDefault
secondsdoublerequiredThe length in seconds.
sampleRateinteger44100The sample rate in Hz. Defaults to 44100.
seedlong1Seed for the random generator. The same seed always gives the same noise. Defaults to 1.
colorstring"white"white (flat), pink (softer highs), brown (rumble) or blue (hissy highs). Defaults to white.

Returns A mono audio buffer at an RMS level of 0.25.

audioEnvelope

Shape the volume of a sound with an attack and an exponential decay

audioEnvelope( audio, attack=0.005, decay=0 )
ArgumentTypeDefault
audioanyrequiredThe audio buffer.
attackdouble0.005Seconds to rise from silence to full volume in a straight line. 0 starts at full volume. Defaults to 0.005.
decaydouble0The decay time constant in seconds, the level falls to 37% after one decay time. 0 means no decay. Defaults to 0.

Returns A new audio buffer.

audioDrum

Synthesise a drum hit

audioDrum( type, sampleRate=44100, seed=1, pitch=1, decay=1, level=1, seconds=1 )
ArgumentTypeDefault
typestringrequiredkick, snare, hat, openhat, clap, tom, crash or riser.
sampleRateinteger44100The sample rate in Hz. Defaults to 44100.
seedlong1Seed for the noise, the same seed always gives the same hit. Defaults to 1.
pitchdouble1Multiplier for the tuned part of kick, snare and tom. 1 is the default tuning, 0.8 is lower.
decaydouble1Multiplier for how long the hit rings. 1 is the default, 2 is twice as long.
leveldouble1The peak level of the result, 0 to 1. Defaults to 1.
secondsdouble1The length of a riser. Ignored by the other types.

Returns A mono audio buffer.

audioFilter

Filter an audio buffer with a low-pass, high-pass, band-pass or notch filter

audioFilter( audio, type, cutoff, q=0.7071 )
ArgumentTypeDefault
audioanyrequiredThe audio buffer.
typestringrequiredlowpass, highpass, bandpass or notch.
cutoffdoublerequiredThe corner or center frequency in Hz.
qdouble0.7071Resonance. 0.7071 is flat, higher peaks at the cutoff. For bandpass and notch it sets how narrow they are. Defaults to 0.7071.

Returns A new audio buffer.

Edit this page Download Markdown Last updated Oct 16, 2026, 11:59:46 AM Report an issue