From 7cef4e60a848c62546a983d046e0a84c24ab2d44 Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Mon, 18 Jun 2018 12:10:14 -0400 Subject: [PATCH] pass main EffectChain to SoundBank from Engine.createBank --- src/AudioEngine.js | 21 ++++++++++----------- src/effects/EffectChain.js | 33 ++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/AudioEngine.js b/src/AudioEngine.js index 08e1b40..6781ef9 100644 --- a/src/AudioEngine.js +++ b/src/AudioEngine.js @@ -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); } } diff --git a/src/effects/EffectChain.js b/src/effects/EffectChain.js index fa874ef..e0b175f 100644 --- a/src/effects/EffectChain.js +++ b/src/effects/EffectChain.js @@ -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); }