Synth Keyboard
An interactive polyphonic synthesizer with ADSR envelope control. Play multiple notes simultaneously using your mouse, touch, or computer keyboard.
ADSR envelopes shape a note's character.
A
W
S
E
D
F
T
G
Y
H
U
J
K
A–K trigger notes · black keys W E T Y U
How It Works
The synth keyboard creates a separate oscillator for each note you play, enabling polyphonic playback. Each oscillator uses the selected waveform type and ADSR envelope settings.
When you press a key:
- A new oscillator is created with the note's frequency
- The ADSR envelope shapes the sound over time
- The oscillator plays until you release the key
- On release, the envelope's release phase fades out the sound
ADSR Envelope
The envelope controls how a sound evolves over time with four parameters:
- Attack: Time to reach full volume after note starts (0-2 seconds)
- Decay: Time to fall from peak to sustain level (0-2 seconds)
- Sustain: Volume level while holding the key (0-1, where 1 is full volume)
- Release: Time to fade to silence after releasing the key (0-3 seconds)
Try the preset buttons to hear different envelope shapes:
- Piano: Quick attack, medium decay, low sustain - mimics acoustic piano
- Pad: Slow attack, high sustain - smooth, ambient sound
- Pluck: Instant attack, no sustain - percussive, plucked sound
- Lead: Quick attack, high sustain - typical synthesizer lead
Code Example
typescript
import { createOscillator, frequencyMap } from 'ez-web-audio'
// Create oscillator with ADSR envelope
const synth = await createOscillator({
frequency: frequencyMap.A4, // 440 Hz
type: 'triangle',
envelope: {
attack: 0.01, // 10ms to full volume
decay: 0.3, // 300ms decay to sustain
sustain: 0.4, // Hold at 40% volume
release: 0.5 // 500ms fade out
}
})
synth.changeGainTo(0.3)
synth.playFor(0.5) // Hold A4 for half a second, then release it.Polyphonic Playback
Call prepareKeys() from an Enable Audio button. Once it resolves, wire the returned functions to key presses, key releases, and the keyboard’s teardown. Each pitch has its own oscillator so releasing C leaves E ringing.
typescript
import { createOscillator } from 'ez-web-audio'
export async function prepareKeys() {
const notes = ['C4', 'E4', 'G4'] as const
const keys = await Promise.all(notes.map(async note => [
note,
await createOscillator({
note,
type: 'triangle',
gain: 0.2,
envelope: { attack: 0.01, decay: 0.1, sustain: 0.5, release: 0.2 },
}),
] as const))
const voices = new Map(keys)
type Note = typeof notes[number]
return {
press: (note: Note) => voices.get(note)?.play(),
release: (note: Note) => voices.get(note)?.stop(),
dispose: () => voices.forEach(voice => voice.dispose()),
}
}API Used
createOscillator(options)- Creates a synthesizer oscillatorfrequencyMap- Note name to frequency lookup (e.g.,frequencyMap['A4']= 440)envelope- ADSR envelope configuration for sound shaping- Oscillator types:
sine,square,sawtooth,triangle
Next Steps
- Explore the Oscillator API for more synthesis options
- Try other interactive examples
- Learn about core concepts in the guide