Skip to content

Commit

Permalink
fix(CustomVirtualAudioNode): do not pointlessly disconnect parent nod…
Browse files Browse the repository at this point in the history
…es when a child node needs to be replaced

Bug introduced in 06bc0fb, should fix #317
  • Loading branch information
benji6 committed Jul 22, 2021
1 parent 5aaf5a2 commit 5bbe2a9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
9 changes: 2 additions & 7 deletions src/VirtualAudioNodes/CustomVirtualAudioNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ export default class CustomVirtualAudioNode extends VirtualAudioNodeBase {
}

public update(
params: IVirtualAudioNodeParams = {},
_params: IVirtualAudioNodeParams | null | undefined,
audioContext: AudioContext
): this {
const params = _params ?? {};
const audioGraphParamsFactoryValues = values(this.node(params));
const keys = Object.keys(this.virtualNodes);

Expand All @@ -84,7 +85,6 @@ export default class CustomVirtualAudioNode extends VirtualAudioNodeBase {

if (virtualAudioNode.cannotUpdateInPlace(newVirtualAudioNode)) {
virtualAudioNode.disconnectAndDestroy();
this.disconnectParents(virtualAudioNode);
this.virtualNodes[key] = newVirtualAudioNode.initialize(audioContext);
continue;
}
Expand All @@ -93,7 +93,6 @@ export default class CustomVirtualAudioNode extends VirtualAudioNodeBase {

if (!equals(newVirtualAudioNode.output, virtualAudioNode.output)) {
virtualAudioNode.disconnect();
this.disconnectParents(virtualAudioNode);
virtualAudioNode.output = newVirtualAudioNode.output;
}
}
Expand All @@ -102,8 +101,4 @@ export default class CustomVirtualAudioNode extends VirtualAudioNodeBase {
this.params = params;
return this;
}

private disconnectParents(vNode: VirtualAudioNode): void {
for (const node of values(this.virtualNodes)) node.disconnect(vNode);
}
}
3 changes: 2 additions & 1 deletion src/VirtualAudioNodes/StandardVirtualAudioNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export default class StandardVirtualAudioNode extends VirtualAudioNodeBase {
return this;
}

public update(params: IVirtualAudioNodeParams = {}): this {
public update(_params: IVirtualAudioNodeParams | null | undefined): this {
const params = _params ?? {};
const audioNode: IAudioNodePropertyLookup = this.audioNode;
for (const key of Object.keys(params)) {
if (constructorParamsKeys.indexOf(key) !== -1) continue;
Expand Down
30 changes: 30 additions & 0 deletions test/customNodes/__snapshots__/expectedBehaviour.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -891,3 +891,33 @@ Object {
"name": "AudioDestinationNode",
}
`;

exports[`custom nodes updating startTime of nested custom nodes 1`] = `
Object {
"inputs": Array [
Object {
"gain": Object {
"inputs": Array [],
"value": 1,
},
"inputs": Array [
Object {
"detune": Object {
"inputs": Array [],
"value": 0,
},
"frequency": Object {
"inputs": Array [],
"value": 440,
},
"inputs": Array [],
"name": "OscillatorNode",
"type": "sine",
},
],
"name": "GainNode",
},
],
"name": "AudioDestinationNode",
}
`;
15 changes: 15 additions & 0 deletions test/customNodes/expectedBehaviour.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import createVirtualAudioGraph, * as V from "../../src";
import { oscillator } from "../../src";
import gainWithNoParams from "../utils/gainWithNoParams";
import pingPongDelay from "../utils/pingPongDelay";
import sineOsc from "../utils/sineOsc";
Expand Down Expand Up @@ -210,4 +211,18 @@ describe("custom nodes", () => {
});
expect(audioContext.toJSON()).toEqual(expectedData);
});

test("updating startTime of nested custom nodes", () => {
const voice = V.createNode(({ startTime }) => ({
0: gainWithNoParams("output"),
1: oscillator(0, { startTime, stopTime: startTime + 1 }),
}));

virtualAudioGraph.update({ 0: voice("output", { startTime: 1 }) });
const initialSnapshot = audioContext.toJSON();
expect(initialSnapshot).toMatchSnapshot();

virtualAudioGraph.update({ 0: voice("output", { startTime: 2 }) });
expect(initialSnapshot).toEqual(audioContext.toJSON());
});
});

0 comments on commit 5bbe2a9

Please sign in to comment.