From 5b8f0fce65a5efefde7081d3586f2215e9ceb1c7 Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Tue, 5 Jun 2018 13:59:53 -0400 Subject: [PATCH] fixup! extend existing effects from Effect --- src/AudioEngine.js | 8 +++++ src/effects/Effect.js | 60 ++++++++++++++++++++++---------------- src/effects/PanEffect.js | 22 ++++++++++---- src/effects/PitchEffect.js | 24 +++++++++++---- 4 files changed, 77 insertions(+), 37 deletions(-) diff --git a/src/AudioEngine.js b/src/AudioEngine.js index e021306..f76d150 100644 --- a/src/AudioEngine.js +++ b/src/AudioEngine.js @@ -86,6 +86,14 @@ class AudioEngine { return 0.001; } + /** + * Get the input node. + * @return {AudioNode} - audio node that is the input for this effect + */ + getInputNode () { + return this.inputNode; + } + /** * Decode a sound, decompressing it into audio samples. * Store a reference to it the sound in the audioBuffers dictionary, indexed by soundId diff --git a/src/effects/Effect.js b/src/effects/Effect.js index fa8e9dd..3cc4b26 100644 --- a/src/effects/Effect.js +++ b/src/effects/Effect.js @@ -20,7 +20,7 @@ class Effect { this.inputNode = null; this.outputNode = null; - this.targetNode = null; + this.target = null; } /** @@ -32,13 +32,24 @@ class Effect { } /** - * Does the effect currently affect the player's graph. - * The pitch effect is always neutral. Instead of affecting the graph it - * affects the player directly. + * Should the effect be connected to the audio graph? + * The pitch effect is an example that does not need to be patched in. + * Instead of affecting the graph it affects the player directly. * @return {boolean} is the effect affecting the graph? */ - get isNeutral () { - return !this.initialized; + get _isPatch () { + return this.initialized; + } + + /** + * Get the input node. + * @return {AudioNode} - audio node that is the input for this effect + */ + getInputNode () { + if (this.initialized) { + return this.inputNode; + } + return this.target.getInputNode(); } /** @@ -71,14 +82,14 @@ class Effect { } // Store whether the graph should currently affected by this effect. - const isNeutral = this.isNeutral; + const _isPatch = this._isPatch; // Call the internal implementation per this Effect. this._set(value); // Connect or disconnect from the graph if this now applies or no longer // applies an effect. - if (this.isNeutral !== isNeutral && this.targetNode !== null) { + if (this._isPatch !== _isPatch && this.targetNode !== null) { this.connect(this.targetNode); } } @@ -97,28 +108,27 @@ class Effect { /** * Connnect this effect's output to another audio node - * @param {AudioNode} node - the node to connect to + * @param {object} target - target whose node to should be connected */ - connect (node) { - this.targetNode = node; + connect (target) { + this.target = target; - if (node === null) { + if (target === null) { return; } - if (this.isNeutral) { - if (this.lastEffect === null) { - this.audioPlayer.connect(node); - } else { - this.lastEffect.connect(node); - } + const targetNode = target.getInputNode(); + + let nextNode = targetNode; + if (this._isPatch) { + nextNode = this.inputNode; + this.outputNode.connect(targetNode); + } + + if (this.lastEffect === null) { + this.audioPlayer.connect(nextNode); } else { - if (this.lastEffect === null) { - this.audioPlayer.connect(this.inputNode); - } else { - this.lastEffect.connect(this.inputNode); - } - this.outputNode.connect(node); + this.lastEffect.connect(nextNode); } } @@ -128,7 +138,7 @@ class Effect { dispose () { this.inputNode = null; this.outputNode = null; - this.targetNode = null; + this.target = null; this.initialized = false; } diff --git a/src/effects/PanEffect.js b/src/effects/PanEffect.js index 7fc2113..58ccf10 100644 --- a/src/effects/PanEffect.js +++ b/src/effects/PanEffect.js @@ -20,10 +20,20 @@ class PanEffect extends Effect { this.channelMerger = null; } - get isNeutral () { - return !this.initialized || this.value === 0; + /** + * Should the effect be connected to the audio graph? + * @return {boolean} is the effect affecting the graph? + */ + get _isPatch () { + return this.initialized && this.value !== 0; } + /** + * Initialize the Effect. + * Effects start out uninitialized. Then initialize when they are first set + * with some value. + * @throws {Error} throws when left unimplemented + */ initialize () { const audioContext = this.audioEngine.audioContext; @@ -42,9 +52,9 @@ class PanEffect extends Effect { } /** - * Set the effect value - * @param {number} value - the new value to set the effect to - */ + * Set the effect value + * @param {number} value - the new value to set the effect to + */ _set (value) { this.value = value; @@ -74,7 +84,7 @@ class PanEffect extends Effect { this.rightGain = null; this.channelMerger = null; this.outputNode = null; - this.targetNode = null; + this.target = null; this.initialized = false; } diff --git a/src/effects/PitchEffect.js b/src/effects/PitchEffect.js index 24e5383..2f71dd8 100644 --- a/src/effects/PitchEffect.js +++ b/src/effects/PitchEffect.js @@ -36,15 +36,27 @@ class PitchEffect extends Effect { } /** - * Does the effect currently affect the player's graph. - * The pitch effect is always neutral. Instead of affecting the graph it - * affects the player directly. - * @returns {boolean} is the effect affecting the graph? + * Should the effect be connected to the audio graph? + * @return {boolean} is the effect affecting the graph? */ - get isNeutral () { - return true; + get _isPatch () { + return false; } + /** + * Get the input node. + * @return {AudioNode} - audio node that is the input for this effect + */ + getInputNode () { + return this.target.getInputNode(); + } + + /** + * Initialize the Effect. + * Effects start out uninitialized. Then initialize when they are first set + * with some value. + * @throws {Error} throws when left unimplemented + */ initialize () { this.initialized = true; }