diff --git a/src/audio/resources/audiograph.js b/src/audio/resources/audiograph.js index 0d51e8f4..f945f1da 100644 --- a/src/audio/resources/audiograph.js +++ b/src/audio/resources/audiograph.js @@ -10,7 +10,7 @@ export class AudioGraph { context /** - * @type {GraphList} + * @type {GraphList} */ graph = new GraphList() @@ -26,11 +26,11 @@ export class AudioGraph { constructor(options) { const context = new AudioContext(options) const rootid = this.add(context.destination) - + this.context = context this.root = rootid addEventListener('pointerdown', resumeAudio) - + /** * */ @@ -38,7 +38,7 @@ export class AudioGraph { const ctx = context ctx.resume() - + if (ctx.state === 'running') { removeEventListener('pointerdown', resumeAudio) } @@ -50,9 +50,9 @@ export class AudioGraph { getContext() { return this.context } - + /** - * @param {AudioNode} node + * @param {AudioGraphNode} node */ add(node) { return this.graph.addNode(node) @@ -65,13 +65,56 @@ export class AudioGraph { connect(from, to) { const node1 = this.graph.getNodeWeight(from) const node2 = this.graph.getNodeWeight(to) - + + this.graph.addEdge(from, to) + if (node1 && node2) { - this.graph.addEdge(from, to) node1.connect(node2) } } + /** + * @param {NodeId} id + * @returns {AudioGraphNode} + */ + get(id) { + return this.graph.getNode(id)?.weight + } + + /** + * @param {NodeId} id + * @param {AudioGraphNode} value + * @returns {void} + */ + update(id, value) { + const node = this.graph.getNode(id) + + if (!node) return + + const previousValue = node.weight + + if (previousValue) { + if( + previousValue instanceof OscillatorNode || + previousValue instanceof ConstantSourceNode || + previousValue instanceof AudioBufferSourceNode || + previousValue instanceof AudioScheduledSourceNode + ) previousValue.stop() + + previousValue.disconnect() + } + + if (value) { + for (const neighbour of this.graph.getNeighbours(id)) { + const node = this.graph.getNodeWeight(neighbour) + + if (node) value.connect(node) + } + } + + node.weight = value + } + /* * disconnect(from, to) { @@ -86,4 +129,8 @@ export class AudioGraph { remove(id) {} */ -} \ No newline at end of file +} + +/** + * @typedef {(AudioBufferSourceNode | undefined) | (OscillatorNode | undefined) | Exclude} AudioGraphNode + */ \ No newline at end of file