mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 05:53:43 -05:00
pass main EffectChain to SoundBank from Engine.createBank
This commit is contained in:
parent
5177dd5c85
commit
7cef4e60a8
2 changed files with 32 additions and 22 deletions
|
@ -9,6 +9,7 @@ const AudioPlayer = require('./AudioPlayer');
|
|||
const Loudness = require('./Loudness');
|
||||
const SoundPlayer = require('./GreenPlayer');
|
||||
|
||||
const EffectChain = require('./effects/EffectChain');
|
||||
const PanEffect = require('./effects/PanEffect');
|
||||
const PitchEffect = require('./effects/PitchEffect');
|
||||
const VolumeEffect = require('./effects/VolumeEffect');
|
||||
|
@ -198,27 +199,23 @@ class AudioEngine {
|
|||
|
||||
/**
|
||||
* Retrieve the audio buffer as held in memory for a given sound id.
|
||||
* @param {!string} soundId - the id of the sound buffer to get
|
||||
* @return {AudioBuffer} the buffer corresponding to the given sound id.
|
||||
* @todo remove this
|
||||
*/
|
||||
getSoundBuffer (soundId) {
|
||||
getSoundBuffer () {
|
||||
// todo: Deprecate audioBuffers. If something wants to hold onto the
|
||||
// buffer, it should. Otherwise buffers need to be able to release their
|
||||
// decoded memory to avoid running out of memory which is possible with
|
||||
// enough large audio buffers as they are full 16bit pcm waveforms for
|
||||
// each audio channel.
|
||||
return this.audioBuffers[soundId];
|
||||
log.warn('The getSoundBuffer function is no longer available. Use soundBank.getSoundPlayer().buffer.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or update the in-memory audio buffer to a new one by soundId.
|
||||
* @param {!string} soundId - the id of the sound buffer to update.
|
||||
* @param {AudioBuffer} newBuffer - the new buffer to swap in.
|
||||
* @return {string} The uid of the sound that was updated or added
|
||||
* @todo remove this
|
||||
*/
|
||||
updateSoundBuffer (soundId, newBuffer) {
|
||||
this.audioBuffers[soundId] = newBuffer;
|
||||
return soundId;
|
||||
updateSoundBuffer () {
|
||||
log.warn('The updateSoundBuffer function is no longer available. Use soundBank.getSoundPlayer().buffer.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -256,7 +253,9 @@ class AudioEngine {
|
|||
|
||||
|
||||
createBank () {
|
||||
return new SoundBank(this);
|
||||
const effects = new EffectChain(this, this.effects);
|
||||
effects.connect(this);
|
||||
return new SoundBank(this, effects);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,22 +6,30 @@ class EffectChain {
|
|||
|
||||
this.effects = effects;
|
||||
|
||||
this.lastEffect = null;
|
||||
// Effects are instantiate in reverse so that the first refers to the
|
||||
// second, the second refers to the third, etc and the last refers to
|
||||
// null.
|
||||
let lastEffect = null;
|
||||
this._effects = effects
|
||||
.reverse()
|
||||
.map(Effect => {
|
||||
const effect = new Effect(audioEngine, this, lastEffect);
|
||||
this[effect.name] = effect;
|
||||
lastEffect = effect;
|
||||
return effect;
|
||||
})
|
||||
.reverse();
|
||||
|
||||
this._effects = effects.map(Effect => {
|
||||
const effect = new Effect(audioEngine, this, this.lastEffect);
|
||||
this[effect.name] = effect;
|
||||
this.lastEffect = effect;
|
||||
return effect;
|
||||
});
|
||||
this.firstEffect = this._effects[0];
|
||||
this.lastEffect = this._effects[this._effects.length - 1];
|
||||
|
||||
this._soundPlayers = new Set();
|
||||
}
|
||||
|
||||
clone () {
|
||||
const chain = new EffectChain(this.audioEngine, this.effects);
|
||||
if (this.target === target) {
|
||||
chain.connect(target);
|
||||
if (this.target) {
|
||||
chain.connect(this.target);
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
@ -46,17 +54,20 @@ class EffectChain {
|
|||
* @param {object} target - target whose node to should be connected
|
||||
*/
|
||||
connect (target) {
|
||||
const {lastEffect} = this;
|
||||
const {firstEffect, lastEffect} = this;
|
||||
|
||||
if (target === lastEffect) {
|
||||
this.inputNode.disconnect();
|
||||
this.inputNode.connect(lastEffect.getInputNode());
|
||||
|
||||
return;
|
||||
} else if (target === firstEffect) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.target = target;
|
||||
|
||||
this._effects[0].connect(target);
|
||||
firstEffect.connect(target);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue