Synths and their settings

A synth is an instrument made of settings. makeSynth gives you one. Start from a ready-made synth, change one, or make one from nothing.

const bass = makeSynth("bass")                            // a ready-made synth
const soft = makeSynth("lead", { cutoff: 900 })           // a ready-made one, changed
const mine = makeSynth({ wave: "square", attack: 0.2 })   // one from nothing

The ready-made synths

KindSynths
Instrumentskeys (the one playNote uses), bass, lead, pad, pluck, organ, strings, bell, flute
Drumskick, snare, hat
Chip soundschip, chipLead, chipBass, chipNoise, chipKick, chipSnare, chipHat

In Python the chip sounds are chip_lead, chip_bass and so on. All of them are settings of the same engine, not recordings, so every one can be changed. Every sound, synth and setting shows what each is made of, and synthNames() lists them (synth_names()).

Playing a synth

const lead = makeSynth("lead")

function onMouseDown() {
  lead.play("E4", 0.5)                 // a note, for half a second
  lead.play(["C4", "E4", "G4"], 1)     // three notes at once: a chord
  lead.play("Cm7", 1, { after: 1 })    // a chord by its name, a second later
}
JavaScriptPythonWhat it does
lead.play("E4", 0.5)lead.play("E4", 0.5)A note for half a second. A list of notes, or a chord's name, is a chord. With no note at all it plays the synth's own (a drum's)
lead.play("E4", 0.5, 0.8)lead.play("E4", 0.5, 0.8)The third value is how loud, from 0 to 1
lead.play("E4", { length: 1, volume: 0.5, pan: -1, after: 2 })lead.play("E4", length=1, volume=0.5, pan=-1, after=2)The same things by name, and when
lead.start("E4")lead.start("E4")A note that stays until it is stopped. See below
lead.stop(), lead.stop("E4")lead.stop(), lead.stop("E4")Lets go of every note of the synth, or of one
lead.playNotes("C4 E4 G4", 0.25)lead.play_notes("C4 E4 G4", 0.25)A tune as text on this synth
lead.note("C4", "quarter")lead.note("C4", "quarter")The next note of this synth's line. Also chord and rest
lead.arp("Cm7"), lead.startArp("Am")lead.arp("Cm7"), lead.start_arp("Am")A chord as an arpeggio
lead.remove()lead.remove()Finished with it

A note that stays

start gives back the note, which sounds until its stop(). While it sounds it can be moved:

const lead = makeSynth("lead")
let held = null

function onMouseDown() {
  held = lead.start("E4")
  held.slideTo("G4", 0.5)        // slide up to G over half a second
}

function onMouseUp() {
  if (held) held.stop()
}

A note that is sounding also has pitch, volume and pan to set: held.pitch = "A4", held.volume = 0.5, held.pan = -1. held.vibrato() starts it singing, and held.vibrato(false) stops that.

Bending a note

A note's pitch can move, on any synth and on a sample:

const lead = makeSynth("lead")
let turn = 0

function onMouseDown() {
  turn += 1
  if (turn % 5 === 1) lead.play("A4", 1, { bend: "scoop" })           // up into the note from just below, as a singer starts one
  if (turn % 5 === 2) lead.play("C5", 1, { glide: "sixteenth" })      // slides to C5 from the note this synth played last
  if (turn % 5 === 3) lead.play("E5", 2, { vibrato: true })           // begins straight, then starts to sing
  if (turn % 5 === 4) lead.play("A3", 1, { bend: "fall" })            // droops away at its end
  if (turn % 5 === 0) lead.play("C4", 1, { slide: 12 })               // an octave up, at once: a laser
}
WordWhat it does
glideThe note starts at the pitch of the synth's last note and slides to its own: true (a sixteenth), a note length, or seconds
bend"scoop" (from 2 semitones below, over a sixteenth), "fall" (5 down over an eighth, ending with the note), "dip" (a semitone down and back), or { to: 2, over: "eighth" }: how far in semitones, or to a note by name, and how long. { shape: "scoop", to: -5 } is a scoop of another size
slideA bend by a number of semitones over a sixteenth, or { to, over }
vibratotrue, or { depth: 0.3, rate: 6, delay: "eighth" }: semitones each way, times a second, and how long the note sounds first

In Python the ones in braces are dictionaries: bend={"to": 2, "over": "eighth"}.

They are for one note at a time, because a chord's notes would all be moved to one pitch. They work in play, start and playNote. A tune from playNotes does not take them.

A key at an exact time

For a sequencer of your own that looks ahead, a key can be pressed and let go at a time on the sound clock, and it happens on the exact sample of that time:

const lead = makeSynth("lead")

function onMouseDown() {
  const at = soundTime() + 0.25
  lead.noteOn("C4", 0.8, { at })               // the key goes down a quarter of a second from now
  lead.noteOff("C4", { at: at + 0.5 })         // and comes up half a second after that
}

lead.play("C4", { at }) does the same for a note with a length. A key held with noteOn is let go by its note, with noteOff("C4"): it is not a thing to keep, as the note from start is.

Settings

Every setting is a plain property, and reads back what you set. A note that is already sounding changes too.

lead.cutoff = 800
lead.echo = 0.4
console.log(lead.cutoff)                        // 800

lead.set({ cutoff: 4000, resonance: 0.4 })      // several at once
lead.set({ cutoff: 4000 }, { glide: 2 })        // slide there over two seconds

Sliding to a setting over time is glide in both languages: { glide: 2 } in JavaScript and glide=2 in Python. (slide is an older word for the same, and still works.)

These are the ones you will use first. Where two spellings are shown, the first is JavaScript's and the second is Python's.

SettingWhat it is
waveThe raw sound: "sine" (pure), "triangle" (soft), "saw" (bright), "square" (hollow), or "pulse" with a width from 0.02 to 0.98
volume, panHow loud the synth is (1 is normal), and left (-1) to right (1)
attack, decay, sustain, releaseThe shape of a note's loudness: seconds to reach full, seconds to fall to the sustain level (0 to 1), which holds while the note lasts, and seconds to die away afterwards. A sustain of 0 makes a pluck or a drum
cutoff, resonanceThe filter: cutoff is how bright (20 to 20000), and resonance adds a whistle at the cutoff (0 to 1)
filter"lowpass" (takes away the top; what cutoff does unless you say), "highpass", "bandpass", "notch", "off"
sweep, sweepAttack, sweepDecay, sweepSustain (sweep_attack …)Each note opens the filter by sweep semitones and lets it close again: the "wow" of a synth bass
octave, pitch, tuneMove every note by octaves, by semitones, or by hundredths of a semitone
glideSeconds to slide from one note to the next
mode"poly" (many notes at once), "mono" (one at a time), "legato"
voices, detune, spreadSeveral copies of each note, a little out of tune and spread left to right: a thick sound
wave2, level2, pitch2, detune2A second wave mixed in, at its own pitch
sub, noiseA wave an octave below, and hiss (each 0 to 1)
vibrato, vibratoRate, vibratoDelay (vibrato_rate, vibrato_delay)A wobble of pitch: how far (semitones), how fast, and how long before it begins
tremolo, wobble, wobbleRate (wobble_rate)A wobble of loudness, and of the filter
pwm, pwmRate (pwm_rate)The pulse wave's width moving in and out: how far (0 to 1) and how fast, in Hz. It is the thick, shifting sound of a "pulse" or "square" wave, and does nothing to the other waves. pwmRate and wobbleRate are one speed under two names
pitchSweep, pitchSweepDecay (pitch_sweep, pitch_sweep_decay)Each note begins that many semitones off and falls into place: a drum's thump, a laser
echo, reverb, chorusHow much of the synth goes to each effect (0 to 1)
reverbType, reverbSize, reverbDecay (reverb_type …)What kind of space the reverb is ("room", "hall", "plate", "cathedral", "spring"), how big, and how many seconds it rings. Every synth shares them: see The kinds of reverb
distortion, crushGrit (0 to 1), and the bits each sample keeps (16 is clean, 4 is an old games console)
voiceA whole voice changer at once: "robot", "chipmunk", "monster" and the rest of the voices
sampleA recording for its wave: see A synth made from a sample

There are more. Every sound, synth and setting lists them all with their ranges, and settingNames() gives the list to a program (setting_names()).

Try it: three sounds from one synth

const synth = makeSynth({ wave: "saw" })
const sounds = [
  { cutoff: 400, resonance: 0.5, attack: 0.005, decay: 0.2, sustain: 0, release: 0.2 },   // a pluck
  { cutoff: 1500, resonance: 0.1, attack: 0.6, decay: 0.3, sustain: 0.8, release: 1.2 },  // a slow pad
  { cutoff: 6000, resonance: 0.2, attack: 0.01, decay: 0.1, sustain: 0.7, release: 0.1 }, // a bright lead
]
let turn = 0

function onMouseDown() {
  synth.set(sounds[turn % 3])
  synth.play(["C4", "E4", "G4"], 0.8)
  turn += 1
}

function draw() {
  background("midnightblue")
  text("Click: a pluck, a pad, a lead", width / 2, height / 2, "white", 24, "centre")
}

The same wave is three instruments. Only the shape of the loudness and the brightness changed.

Change it by hand

To change a synth while it plays, put a dial on it. The dial takes its range from the synth, and turning it sets it.

const lead = makeSynth("lead")

dial("cutoff", lead, "cutoff")
scope(lead)
keyboard(lead, { octave: 4 })

See Dials, sliders and a keyboard.

What a synth is doing

For your own drawing, a synth says what it is doing now:

CallWhat it gives
lead.samples()The wave it is making now: numbers from -1 to 1
lead.level()How loud it is: left and right, each from 0 to 1
lead.notes()The notes it is sounding

soundOutput() has the same for all the sound together (sound_output()), the microphone has them for your voice, and a song has them a channel at a time.

const lead = makeSynth("lead")

function onMouseDown() {
  lead.play("C4", 2)
}

function draw() {
  background("midnightblue")
  const loud = lead.level()[0]                  // the left side, from 0 to 1
  circle(width / 2, height / 2, 40 + loud * 200, "gold")
}

In Python the sound is made in the page and not in your program, so what is read back is a frame late: the first call asks the page to start saying, and gives an empty wave, zeros and no notes. A scope, a meter or a keyboard tied to the synth is drawn by the page itself and is not late at all: scope(lead), meter(sound_output()).

In both languages lead.describe("cutoff") gives a setting's range, unit and kind, and mainSynth() is the synth that playNote plays (main_synth()).

How many

A program can have 32 synths at once. remove() gives a place back. 64 notes can sound at once; past that the oldest note makes way.