Skip to content

Instruments

Instruments are sound sources that can be triggered with notes. All instruments share a common interface for triggering attacks and releases.

Synth

A basic synthesizer with an oscillator and an ADSR envelope.

1let synth = Synth.make()
2let synth2 = Synth.makeWithOptions({
3 oscillator: {"type": "square"},
4 envelope: {attack: 0.1, decay: 0.2, sustain: 0.5, release: 0.8},
5 volume: -6.0,
6})
7
8synth->Synth.asAudioNode->AudioNode.toDestination
9
10// Trigger a note
11synth->Synth.triggerAttack("C4")
12synth->Synth.triggerRelease()
13
14// Trigger attack and release in one call
15synth->Synth.triggerAttackRelease("C4", "8n")
16
17// With time and velocity
18synth->Synth.triggerAttackReleaseAtVel("E4", "4n", ~velocity=0.8)
19
20// Properties
21synth->Synth.volume // Param.t
22synth->Synth.frequency // Param.t
23synth->Synth.detune // Param.t
24
25// Lifecycle
26synth->Synth.dispose()
27synth->Synth.sync()
28synth->Synth.unsync()

FMSynth

A frequency modulation synthesizer. Same trigger interface as Synth, plus modulation controls.

1let fm = FMSynth.make()
2let fm2 = FMSynth.makeWithOptions({
3 modulationIndex: 10.0,
4 harmonicity: 3.0,
5})
6
7fm->FMSynth.asAudioNode->AudioNode.toDestination
8fm->FMSynth.triggerAttackRelease("A3", "2n")
9
10// FM-specific properties
11fm->FMSynth.harmonicity // Param.t
12fm->FMSynth.modulationIndex // Param.t

AMSynth

An amplitude modulation synthesizer.

1let am = AMSynth.make()
2am->AMSynth.asAudioNode->AudioNode.toDestination
3am->AMSynth.triggerAttackRelease("G3", "4n")
4
5am->AMSynth.harmonicity // Param.t

MonoSynth

A monophonic synthesizer with one oscillator, a filter, and two envelopes.

1let mono = MonoSynth.make()
2let mono2 = MonoSynth.makeWithOptions({
3 oscillator: {"type": "sawtooth"},
4 filterEnvelope: {
5 attack: 0.01,
6 decay: 0.3,
7 sustain: 0.2,
8 release: 0.5,
9 baseFrequency: 200.0,
10 octaves: 4.0,
11 },
12})
13
14mono->MonoSynth.asAudioNode->AudioNode.toDestination
15mono->MonoSynth.triggerAttackRelease("D3", "8n")

PolySynth

A polyphonic synthesizer that can play multiple notes simultaneously.

1let poly = PolySynth.make()
2poly->PolySynth.asAudioNode->AudioNode.toDestination
3
4// Play a chord
5poly->PolySynth.triggerAttack(["C4", "E4", "G4"])
6poly->PolySynth.triggerRelease(["C4", "E4", "G4"])
7
8poly->PolySynth.triggerAttackRelease(["C4", "E4", "G4"], "2n")
9
10// Set max polyphony
11poly->PolySynth.maxPolyphony // int
12poly->PolySynth.releaseAll()
Was this page helpful?