mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2025-01-03 11:35:49 -05:00
fixup! extend existing effects from Effect
This commit is contained in:
parent
61e54b2457
commit
5b8f0fce65
4 changed files with 77 additions and 37 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
} else {
|
||||
|
||||
if (this.lastEffect === null) {
|
||||
this.audioPlayer.connect(this.inputNode);
|
||||
this.audioPlayer.connect(nextNode);
|
||||
} 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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue