newcat / baklavajs

Graph / node editor in the browser using VueJS
http://baklava.tech
MIT License
1.57k stars 116 forks source link

How to use baklavajs with Web Audio? #321

Closed Hazmatron7 closed 11 months ago

Hazmatron7 commented 1 year ago

I recently discovered this amazing library and I have a fun idea that I am working on. I want to know how to use this library with Web Audio API to do things such as connecting to audio nodes and audio params and setting audio param values etc.

Not sure how labels work but this is supposed to be under the Q&A label

yojeek commented 11 months ago

Basically to work with WebAudio you need to pass instance of AudioContext to all your audio nodes.

Good way to do it is to use some callback on beforeAddNode event, for example :

    const audioContext = new AudioContext();

    editor.graphEvents.beforeAddNode.subscribe(tkGraphEventsBeforeAddNode, (node) => {
      console.log(`Adding node ${node.type}`);
      if (node instanceof AudioContextNode) {
        node.setAudioContext(audioContext);
      }
    });

And abstract AudioConextNode can be defined as following :

/**
 * A node that requires an AudioContext to work.
 */
export default abstract class AudioContextNode<Inputs, Outputs> extends Node<Inputs, Outputs> {
    protected audioContext: AudioContext;

    setAudioContext(audioContext: AudioContext) {
        this.audioContext = audioContext;
        this.onAudioContextSet();
    }

    onAudioContextSet() {
        // to be implemented by subclasses
        console.warn("AudioContextNode.onAudioContextSet not implemented");
    }
}