Synthesis

Make sound from nothing: oscillators, noise, envelopes, sweeps and drums, and instruments made from your own recordings.

On this page

Make sound from nothing: oscillators, noise, envelopes, sweeps and drums, and instruments made from your own recordings.

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.

audioSampler

Makes an instrument out of a recording, such as a piano note, a vocal "ah", a guitar pluck, a drum hit or a synth pad. Ask the instrument for notes with audioNote(): it plays the recording faster for a higher note and slower for a lower one, like a tape keyboard, so the length changes with the pitch. Give several recordings of different pitches and the nearest one is used for each note, so a note is never shifted far and keeps its character.

A recording is as long as it is. For a note that is held longer, turn on loop: the loop (the whole recording, or the part from loopStart to loopEnd) is repeated until the note ends, with a short crossfade that hides the join. Choose a steady part of a pad, a string or an organ for the loop.

audioSampler( audio, rootNote="C4", loop=false, loopStart=0, loopEnd=0, crossfade=0.02 )
ArgumentTypeDefault
audioanyrequiredThe recording, or a struct of recordings by the note they sound at, such as { C3: low, C4: middle, C5: high }. All must have the same channels and sample rate.
rootNoteany"C4"The note the recording sounds at, a name such as C4 (middle C) or a MIDI number where 69 is A4. Fractions detune, 60.1 is ten cents above C4. Ignored when audio is a struct. Defaults to C4.
loopbooleanfalseTrue to repeat a loop of the recording while a note is held longer than the recording. Defaults to false.
loopStartdouble0Where the loop starts in the recording, in seconds. Defaults to 0.
loopEnddouble0Where the loop ends in the recording, in seconds. 0, the default, is the end of the recording.
crossfadedouble0.02Seconds of overlap that make the loop seamless. Defaults to 0.02.

Returns A sampler, to give to audioNote().

audioNote

Plays one note, or a chord, of an instrument made with audioSampler(), and gives it back as audio to place on a timeline with audioAdd(). Notes are played by playing the recording faster or slower, so a higher note is also shorter unless the sampler loops.

audioNote( sampler, note, seconds=0, velocity=1, attack=0.003, release=0.05 )
ArgumentTypeDefault
sampleranyrequiredThe instrument from audioSampler().
noteanyrequiredA note name such as C4, F#3 or Bb2 (C4 is middle C), a MIDI number where 69 is A4, or an array of them for a chord. Fractions detune.
secondsdouble0How long the note is held. 0, the default, plays the recording through. The note lasts a little longer than this, for the release.
velocitydouble1How hard the note is played, 0 to 1, which is its loudness. Defaults to 1.
attackdouble0.003Seconds of fade in, which keeps a note from clicking. Defaults to 0.003.
releasedouble0.05Seconds the note fades out after it was held. Ignored when seconds is 0. Defaults to 0.05.

Returns An audio buffer. A chord is the notes added together, so give each a lower velocity when they would be too loud.

Edit this page Download Markdown Last updated Nov 26, 2026, 11:36:03 AM Report an issue