pass main EffectChain to SoundBank from Engine.createBank

This commit is contained in:
Michael "Z" Goddard 2018-06-18 12:10:14 -04:00 committed by Corey Frang
parent 5177dd5c85
commit 7cef4e60a8
2 changed files with 32 additions and 22 deletions

View file

@ -9,6 +9,7 @@ const AudioPlayer = require('./AudioPlayer');
const Loudness = require('./Loudness'); const Loudness = require('./Loudness');
const SoundPlayer = require('./GreenPlayer'); const SoundPlayer = require('./GreenPlayer');
const EffectChain = require('./effects/EffectChain');
const PanEffect = require('./effects/PanEffect'); const PanEffect = require('./effects/PanEffect');
const PitchEffect = require('./effects/PitchEffect'); const PitchEffect = require('./effects/PitchEffect');
const VolumeEffect = require('./effects/VolumeEffect'); const VolumeEffect = require('./effects/VolumeEffect');
@ -198,27 +199,23 @@ class AudioEngine {
/** /**
* Retrieve the audio buffer as held in memory for a given sound id. * Retrieve the audio buffer as held in memory for a given sound id.
* @param {!string} soundId - the id of the sound buffer to get * @todo remove this
* @return {AudioBuffer} the buffer corresponding to the given sound id.
*/ */
getSoundBuffer (soundId) { getSoundBuffer () {
// todo: Deprecate audioBuffers. If something wants to hold onto the // todo: Deprecate audioBuffers. If something wants to hold onto the
// buffer, it should. Otherwise buffers need to be able to release their // buffer, it should. Otherwise buffers need to be able to release their
// decoded memory to avoid running out of memory which is possible with // decoded memory to avoid running out of memory which is possible with
// enough large audio buffers as they are full 16bit pcm waveforms for // enough large audio buffers as they are full 16bit pcm waveforms for
// each audio channel. // 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. * 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. * @todo remove this
* @param {AudioBuffer} newBuffer - the new buffer to swap in.
* @return {string} The uid of the sound that was updated or added
*/ */
updateSoundBuffer (soundId, newBuffer) { updateSoundBuffer () {
this.audioBuffers[soundId] = newBuffer; log.warn('The updateSoundBuffer function is no longer available. Use soundBank.getSoundPlayer().buffer.');
return soundId;
} }
/** /**
@ -256,7 +253,9 @@ class AudioEngine {
createBank () { createBank () {
return new SoundBank(this); const effects = new EffectChain(this, this.effects);
effects.connect(this);
return new SoundBank(this, effects);
} }
} }

View file

@ -6,22 +6,30 @@ class EffectChain {
this.effects = effects; 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 => { this.firstEffect = this._effects[0];
const effect = new Effect(audioEngine, this, this.lastEffect); this.lastEffect = this._effects[this._effects.length - 1];
this[effect.name] = effect;
this.lastEffect = effect;
return effect;
});
this._soundPlayers = new Set(); this._soundPlayers = new Set();
} }
clone () { clone () {
const chain = new EffectChain(this.audioEngine, this.effects); const chain = new EffectChain(this.audioEngine, this.effects);
if (this.target === target) { if (this.target) {
chain.connect(target); chain.connect(this.target);
} }
return chain; return chain;
} }
@ -46,17 +54,20 @@ class EffectChain {
* @param {object} target - target whose node to should be connected * @param {object} target - target whose node to should be connected
*/ */
connect (target) { connect (target) {
const {lastEffect} = this; const {firstEffect, lastEffect} = this;
if (target === lastEffect) { if (target === lastEffect) {
this.inputNode.disconnect(); this.inputNode.disconnect();
this.inputNode.connect(lastEffect.getInputNode()); this.inputNode.connect(lastEffect.getInputNode());
return;
} else if (target === firstEffect) {
return; return;
} }
this.target = target; this.target = target;
this._effects[0].connect(target); firstEffect.connect(target);
} }