UNPKG

audio-manager

Version:

Play sounds using Web Audio, fallback to HTML5 Audio

70 lines (64 loc) 3.42 kB
//▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ /** Set of sound played in sequence each times it triggers * used for animation sfx * @author Cedric Stoquer * * @param {String} id - sound ground id * @param {number[]} soundIds - array of sound ids * @param {number[]} volumes - array of volumes * @param {number[]} pitches - array of pitches */ function SoundGroup(id, soundIds, volumes, pitches, muted) { this.id = id; this.soundIds = soundIds; this.volumes = volumes || []; this.pitches = pitches || []; this.soundIndex = 0; this.volIndex = 0; this.pitchIndex = 0; this.poolRef = null; this._ready = false; if (this.volumes.length === 0) this.volumes.push(1.0); if (this.pitches.length === 0) this.pitches.push(0.0); if (!muted) this._createSounds(); } module.exports = SoundGroup; //▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ /** Create and load all sound used in group */ SoundGroup.prototype._createSounds = function () { var soundIds = this.soundIds; for (var i = 0; i < soundIds.length; i++) { this.audioManager.loadSound(soundIds[i]); } this._ready = true; }; //▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ /** Play sound group. * * @param {number} [volume] - optional volume * @param {number} [pan] - optional panoramic * @param {number} [pitch] - optional pitch value in semi-tone (available only if using webAudio) */ SoundGroup.prototype.play = function (volume, pan, pitch) { if (this.soundIds.length === 0) return; if (!this._ready) this._createSounds(); var soundId = this.soundIds[this.soundIndex++]; var sound = this.audioManager.getSound(soundId); if (!sound) return console.warn('[Sound Group: ' + this.id + '] sound id ' + soundId + ' cannot be played.'); volume = volume || 1.0; pitch = pitch || 0.0; volume *= this.volumes[this.volIndex++]; pitch += this.pitches[this.pitchIndex++]; sound.play(volume, pan, pitch); if (this.soundIndex >= this.soundIds.length) { this.soundIndex = 0; } if (this.volIndex >= this.volumes.length) { this.volIndex = 0; } if (this.pitchIndex >= this.pitches.length) { this.pitchIndex = 0; } }; //▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ /** Check that all sounds in group are correctly created */ SoundGroup.prototype.verifySounds = function () { for (var i = 0; i < this.soundIds.length; i++) { var soundId = this.soundIds[i]; this.audioManager.createSound(soundId); } };