fixup! extend existing effects from Effect

This commit is contained in:
Michael "Z" Goddard 2018-06-05 13:59:53 -04:00
parent 61e54b2457
commit 5b8f0fce65
No known key found for this signature in database
GPG key ID: 762CD40DD5349872
4 changed files with 77 additions and 37 deletions

View file

@ -86,6 +86,14 @@ class AudioEngine {
return 0.001; 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. * Decode a sound, decompressing it into audio samples.
* Store a reference to it the sound in the audioBuffers dictionary, indexed by soundId * Store a reference to it the sound in the audioBuffers dictionary, indexed by soundId

View file

@ -20,7 +20,7 @@ class Effect {
this.inputNode = null; this.inputNode = null;
this.outputNode = 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. * Should the effect be connected to the audio graph?
* The pitch effect is always neutral. Instead of affecting the graph it * The pitch effect is an example that does not need to be patched in.
* affects the player directly. * Instead of affecting the graph it affects the player directly.
* @return {boolean} is the effect affecting the graph? * @return {boolean} is the effect affecting the graph?
*/ */
get isNeutral () { get _isPatch () {
return !this.initialized; 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. // 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. // Call the internal implementation per this Effect.
this._set(value); this._set(value);
// Connect or disconnect from the graph if this now applies or no longer // Connect or disconnect from the graph if this now applies or no longer
// applies an effect. // applies an effect.
if (this.isNeutral !== isNeutral && this.targetNode !== null) { if (this._isPatch !== _isPatch && this.targetNode !== null) {
this.connect(this.targetNode); this.connect(this.targetNode);
} }
} }
@ -97,28 +108,27 @@ class Effect {
/** /**
* Connnect this effect's output to another audio node * 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) { connect (target) {
this.targetNode = node; this.target = target;
if (node === null) { if (target === null) {
return; return;
} }
if (this.isNeutral) { const targetNode = target.getInputNode();
if (this.lastEffect === null) {
this.audioPlayer.connect(node); let nextNode = targetNode;
} else { if (this._isPatch) {
this.lastEffect.connect(node); nextNode = this.inputNode;
} this.outputNode.connect(targetNode);
}
if (this.lastEffect === null) {
this.audioPlayer.connect(nextNode);
} else { } else {
if (this.lastEffect === null) { this.lastEffect.connect(nextNode);
this.audioPlayer.connect(this.inputNode);
} else {
this.lastEffect.connect(this.inputNode);
}
this.outputNode.connect(node);
} }
} }
@ -128,7 +138,7 @@ class Effect {
dispose () { dispose () {
this.inputNode = null; this.inputNode = null;
this.outputNode = null; this.outputNode = null;
this.targetNode = null; this.target = null;
this.initialized = false; this.initialized = false;
} }

View file

@ -20,10 +20,20 @@ class PanEffect extends Effect {
this.channelMerger = null; 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 () { initialize () {
const audioContext = this.audioEngine.audioContext; const audioContext = this.audioEngine.audioContext;
@ -42,9 +52,9 @@ class PanEffect extends Effect {
} }
/** /**
* Set the effect value * Set the effect value
* @param {number} value - the new value to set the effect to * @param {number} value - the new value to set the effect to
*/ */
_set (value) { _set (value) {
this.value = value; this.value = value;
@ -74,7 +84,7 @@ class PanEffect extends Effect {
this.rightGain = null; this.rightGain = null;
this.channelMerger = null; this.channelMerger = null;
this.outputNode = null; this.outputNode = null;
this.targetNode = null; this.target = null;
this.initialized = false; this.initialized = false;
} }

View file

@ -36,15 +36,27 @@ class PitchEffect extends Effect {
} }
/** /**
* Does the effect currently affect the player's graph. * Should the effect be connected to the audio graph?
* The pitch effect is always neutral. Instead of affecting the graph it * @return {boolean} is the effect affecting the graph?
* affects the player directly.
* @returns {boolean} is the effect affecting the graph?
*/ */
get isNeutral () { get _isPatch () {
return true; 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 () { initialize () {
this.initialized = true; this.initialized = true;
} }