Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 56 additions & 9 deletions src/audio/resources/audiograph.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class AudioGraph {
context

/**
* @type {GraphList<AudioNode,undefined>}
* @type {GraphList<AudioGraphNode,undefined>}
*/
graph = new GraphList()

Expand All @@ -26,19 +26,19 @@ 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)

/**
*
*/
function resumeAudio() {
const ctx = context

ctx.resume()

if (ctx.state === 'running') {
removeEventListener('pointerdown', resumeAudio)
}
Expand All @@ -50,9 +50,9 @@ export class AudioGraph {
getContext() {
return this.context
}

/**
* @param {AudioNode} node
* @param {AudioGraphNode} node
*/
add(node) {
return this.graph.addNode(node)
Expand All @@ -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) {
Expand All @@ -86,4 +129,8 @@ export class AudioGraph {

remove(id) {}
*/
}
}

/**
* @typedef {(AudioBufferSourceNode | undefined) | (OscillatorNode | undefined) | Exclude<AudioNode,AudioBufferSourceNode | OscillatorNode>} AudioGraphNode
*/