From d5d279b05c5cf206db066ec397ed7522dc9dc474 Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Fri, 19 Sep 2025 08:34:00 +0300 Subject: [PATCH 1/4] Narrow down the node type in `AudioGraph` --- src/audio/resources/audiograph.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/audio/resources/audiograph.js b/src/audio/resources/audiograph.js index 0d51e8f4..5bc07f25 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() @@ -52,7 +52,7 @@ export class AudioGraph { } /** - * @param {AudioNode} node + * @param {AudioGraphNode} node */ add(node) { return this.graph.addNode(node) @@ -65,7 +65,9 @@ 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) @@ -86,4 +88,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 From 733b9b7b0bf5f504be395914f115e78fb57bbd04 Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Fri, 19 Sep 2025 08:34:00 +0300 Subject: [PATCH 2/4] Add `AudioGraph.get` --- src/audio/resources/audiograph.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/audio/resources/audiograph.js b/src/audio/resources/audiograph.js index 5bc07f25..099e84c4 100644 --- a/src/audio/resources/audiograph.js +++ b/src/audio/resources/audiograph.js @@ -74,6 +74,14 @@ export class AudioGraph { } } + /** + * @param {NodeId} id + * @returns {AudioGraphNode} + */ + get(id) { + return this.graph.getNode(id)?.weight + } + /* * disconnect(from, to) { From 77c2ed41f9e18bb7791b0a989f2b586934a98ced Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Fri, 19 Sep 2025 08:34:00 +0300 Subject: [PATCH 3/4] Add `AudioGraph.update` --- src/audio/resources/audiograph.js | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/audio/resources/audiograph.js b/src/audio/resources/audiograph.js index 099e84c4..9a530629 100644 --- a/src/audio/resources/audiograph.js +++ b/src/audio/resources/audiograph.js @@ -82,6 +82,40 @@ export class AudioGraph { 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) { From 3cdc5cb908230b97d0c1a94844c18bc0814f84ff Mon Sep 17 00:00:00 2001 From: waynemwashuma <94756970+waynemwashuma@users.noreply.github.com> Date: Fri, 19 Sep 2025 08:34:00 +0300 Subject: [PATCH 4/4] Format files --- src/audio/resources/audiograph.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/audio/resources/audiograph.js b/src/audio/resources/audiograph.js index 9a530629..f945f1da 100644 --- a/src/audio/resources/audiograph.js +++ b/src/audio/resources/audiograph.js @@ -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,7 +50,7 @@ export class AudioGraph { getContext() { return this.context } - + /** * @param {AudioGraphNode} node */ @@ -69,7 +69,6 @@ export class AudioGraph { this.graph.addEdge(from, to) if (node1 && node2) { - this.graph.addEdge(from, to) node1.connect(node2) } }