Lightweight sound synthesizer designed for real-time user interaction using the Web Audio API.
Install from npm:
npm install sw-synthimport {Synth, defaultParams} from 'sw-synth';
const context = new AudioContext({latencyHint: 'interactive'});
const synth = new Synth(context, context.destination);
synth.maxPolyphony = 6;
synth.voiceParams = defaultParams();
const noteOffs = new Map<string, () => void>();
window.addEventListener('keydown', (event: KeyboardEvent) => {
const pitch = parseInt(event.key, 36) % 24;
const frequency = 440 * 2 ** (pitch / 12);
const velocity = 0.2;
noteOffs.set(event.key, synth.noteOn(frequency, velocity));
});
window.addEventListener('keyup', (event: KeyboardEvent) => {
const noteOff = noteOffs.get(event.key);
if (noteOff) {
noteOff();
noteOffs.delete(event.key);
}
});The package exports four synth classes:
Synth: finite polyphony with standard Web AudioOscillatorNodevoices.UnisonSynth: finite polyphony with stacked detuned oscillator voices.AperiodicSynth: finite polyphony usingAperiodicWavefromaperiodic-oscillator.BufferSynth: finite polyphony driven by user-providedAudioBufferSourceNodefactories.
Voice parameter helpers are also exported:
defaultParams()defaultUnisonParams()
- Hosted docs: https://xenharmonic-devs.github.io/sw-synth
- Generate docs locally:
npm run docTypeDoc output is written to ./docs.