UNPKG

webdaw-modules

Version:

a set of modules for building a web-based DAW

37 lines (32 loc) 994 B
export type AudioEvent = { audioBuffer: AudioBuffer; audioNode: AudioBufferSourceNode | null; gainNode: GainNode; pannerNode: PannerNode; // todo: more effects can be added such as reverb start: number; offset: number; duration: number; }; export const startAudioEvent = (audioContext: AudioContext, audioEvent: AudioEvent): AudioEvent => { // console.log("create audio node"); const audioNode = audioContext.createBufferSource(); audioNode.buffer = audioEvent.audioBuffer; audioNode .connect(audioEvent.gainNode) .connect(audioEvent.pannerNode) .connect(audioContext.destination); audioNode.start(audioEvent.start, audioEvent.offset, audioEvent.duration); return { ...audioEvent, audioNode, }; }; export const stopAudioEvent = (audioEvent: AudioEvent, stopParams?: any): AudioEvent => { // todo: add stop params such as when to stop and fade out audioEvent.audioNode?.stop(); return { ...audioEvent, audioNode: null, }; };