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 )
| Argument | Type | Default | |
|---|---|---|---|
note | any | required | A 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 )
| Argument | Type | Default | |
|---|---|---|---|
shape | string | required | sine, 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. |
freq | double | required | The frequency in Hz, see audioFreq() for note names. |
seconds | double | required | The length in seconds. |
sampleRate | integer | 44100 | The sample rate in Hz. Defaults to 44100. |
harmonics | integer | 16 | For saw, square and triangle: the most harmonics to add, fewer are used near the Nyquist frequency. Fewer harmonics sound duller. Defaults to 16. |
partials | array | optional | For additive: an array of [ frequency multiple, amplitude ] pairs, for example [ [ 1, 1.0 ], [ 2, 0.5 ] ]. Amplitudes are used as given. |
width | double | 0.5 | For 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. |
phase | double | 0 | Where 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 )
| Argument | Type | Default | |
|---|---|---|---|
startHz | double | required | The frequency at the start, in Hz. |
endHz | double | required | The frequency it settles towards, in Hz. |
seconds | double | required | The length in seconds. |
speed | double | 28 | How fast it settles, in 1/seconds. Higher is quicker. Defaults to 28. |
sampleRate | integer | 44100 | The 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" )
| Argument | Type | Default | |
|---|---|---|---|
seconds | double | required | The length in seconds. |
sampleRate | integer | 44100 | The sample rate in Hz. Defaults to 44100. |
seed | long | 1 | Seed for the random generator. The same seed always gives the same noise. Defaults to 1. |
color | string | "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 )
| Argument | Type | Default | |
|---|---|---|---|
audio | any | required | The audio buffer. |
attack | double | 0.005 | Seconds to rise from silence to full volume in a straight line. 0 starts at full volume. Defaults to 0.005. |
decay | double | 0 | The 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 )
| Argument | Type | Default | |
|---|---|---|---|
type | string | required | kick, snare, hat, openhat, clap, tom, crash or riser. |
sampleRate | integer | 44100 | The sample rate in Hz. Defaults to 44100. |
seed | long | 1 | Seed for the noise, the same seed always gives the same hit. Defaults to 1. |
pitch | double | 1 | Multiplier for the tuned part of kick, snare and tom. 1 is the default tuning, 0.8 is lower. |
decay | double | 1 | Multiplier for how long the hit rings. 1 is the default, 2 is twice as long. |
level | double | 1 | The peak level of the result, 0 to 1. Defaults to 1. |
seconds | double | 1 | The 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 )
| Argument | Type | Default | |
|---|---|---|---|
audio | any | required | The audio buffer. |
type | string | required | lowpass, highpass, bandpass or notch. |
cutoff | double | required | The corner or center frequency in Hz. |
q | double | 0.7071 | Resonance. 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 )
| Argument | Type | Default | |
|---|---|---|---|
audio | any | required | The 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. |
rootNote | any | "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. |
loop | boolean | false | True to repeat a loop of the recording while a note is held longer than the recording. Defaults to false. |
loopStart | double | 0 | Where the loop starts in the recording, in seconds. Defaults to 0. |
loopEnd | double | 0 | Where the loop ends in the recording, in seconds. 0, the default, is the end of the recording. |
crossfade | double | 0.02 | Seconds 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 )
| Argument | Type | Default | |
|---|---|---|---|
sampler | any | required | The instrument from audioSampler(). |
note | any | required | A 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. |
seconds | double | 0 | How long the note is held. 0, the default, plays the recording through. The note lasts a little longer than this, for the release. |
velocity | double | 1 | How hard the note is played, 0 to 1, which is its loudness. Defaults to 1. |
attack | double | 0.003 | Seconds of fade in, which keeps a note from clicking. Defaults to 0.003. |
release | double | 0.05 | Seconds 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.