defineSound

Takes a sound definition and returns a function that plays it. Call the function to trigger the sound - each call returns a VoiceHandle for stopping mid-flight.

import { defineSound } from "@web-kits/audio";

const play = defineSound({
  source: { type: "sine", frequency: 440 },
  envelope: { decay: 0.1 },
  gain: 0.3,
});

const voice = play();
voice.stop(0.1);

Signature

function defineSound(definition: SoundDefinition): (opts?: PlayOptions) => VoiceHandle;

PlayOptions

Override volume, panning, or pitch at play time without changing the definition.

PropertyTypeDescription
volumenumberGain multiplier
pannumberStereo pan (-1 to 1)
detunenumberPitch offset (cents)
playbackRatenumberSpeed multiplier
velocitynumberIntensity (0–1)
play({ velocity: 0.5, pan: -0.3, detune: 100 });

VoiceHandle

MethodDescription
stop()Stop with a short default fade
stop(0.5)Fade out over 500ms