mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 14:02:29 -05:00
streamline connecting Effects
- Add sanity checks to connect
This commit is contained in:
parent
e40e6e0b1b
commit
841b029d7f
3 changed files with 25 additions and 7 deletions
|
@ -111,26 +111,36 @@ class Effect {
|
|||
* @param {object} target - target whose node to should be connected
|
||||
*/
|
||||
connect (target) {
|
||||
this.target = target;
|
||||
|
||||
if (target === null) {
|
||||
return;
|
||||
throw new Error('target may not be null');
|
||||
}
|
||||
|
||||
const checkForCircularReference = subtarget => {
|
||||
if (subtarget) {
|
||||
if (subtarget === this) {
|
||||
return true;
|
||||
}
|
||||
return checkForCircularReference(subtarget.target);
|
||||
}
|
||||
};
|
||||
if (checkForCircularReference(target)) {
|
||||
throw new Error('Effect cannot connect to itself');
|
||||
}
|
||||
|
||||
this.target = target;
|
||||
|
||||
if (this.outputNode !== null) {
|
||||
this.outputNode.disconnect();
|
||||
}
|
||||
|
||||
let nextTarget = target;
|
||||
if (this._isPatch) {
|
||||
nextTarget = this;
|
||||
this.outputNode.connect(target.getInputNode());
|
||||
}
|
||||
|
||||
if (this.lastEffect === null) {
|
||||
this.audioPlayer.connect(nextTarget);
|
||||
this.audioPlayer.connect(this);
|
||||
} else {
|
||||
this.lastEffect.connect(nextTarget);
|
||||
this.lastEffect.connect(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -66,6 +66,10 @@ class PanEffect extends Effect {
|
|||
* Clean up and disconnect audio nodes.
|
||||
*/
|
||||
dispose () {
|
||||
if (!this.initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.inputNode.disconnect();
|
||||
this.leftGain.disconnect();
|
||||
this.rightGain.disconnect();
|
||||
|
|
|
@ -41,6 +41,10 @@ class VolumeEffect extends Effect {
|
|||
* Clean up and disconnect audio nodes.
|
||||
*/
|
||||
dispose () {
|
||||
if (!this.initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.outputNode.disconnect();
|
||||
|
||||
this.inputNode = null;
|
||||
|
|
Loading…
Reference in a new issue