const log = require('./log'); /** * A symbol indicating all targets are to be effected. * @const {string} */ const ALL_TARGETS = '*'; class SoundBank { /** * A bank of sounds that can be played. * @constructor * @param {AudioEngine} audioEngine - related AudioEngine * @param {EffectChain} effectChainPrime - original EffectChain cloned for * playing sounds */ constructor (audioEngine, effectChainPrime) { /** * AudioEngine this SoundBank is related to. * @type {AudioEngine} */ this.audioEngine = audioEngine; /** * Map of ids to soundPlayers. * @type {object} */ this.soundPlayers = {}; /** * Map of targets by sound id. * @type {Map} */ this.playerTargets = new Map(); /** * Map of effect chains by sound id. * @type {Map { if (playerTarget === target) { this.getSoundEffects(key).setEffectsFromTarget(target); } }); } /** * Stop playback of sound by id if was lasted played by the target. * @param {Target} target - target to check if it last played the sound * @param {string} soundId - id of the sound to stop */ stop (target, soundId) { if (this.playerTargets.get(soundId) === target) { this.soundPlayers[soundId].stop(); } } /** * Stop all sounds for all targets or a specific target. * @param {Target|string} target - a symbol for all targets or the target * to stop sounds for */ stopAllSounds (target = ALL_TARGETS) { this.playerTargets.forEach((playerTarget, key) => { if (target === ALL_TARGETS || playerTarget === target) { this.getSoundPlayer(key).stop(); } }); } /** * Dispose of all EffectChains and SoundPlayers. */ dispose () { this.playerTargets.clear(); this.soundEffects.forEach(effects => effects.dispose()); this.soundEffects.clear(); for (const soundId in this.soundPlayers) { if (Object.prototype.hasOwnProperty.call(this.soundPlayers, soundId)) { this.soundPlayers[soundId].dispose(); } } this.soundPlayers = {}; } } module.exports = SoundBank;