UNPKG

sono

Version:

A simple yet powerful JavaScript library for working with Web Audio

241 lines (200 loc) 5.24 kB
'use strict'; exports.__esModule = true; exports.default = Group; var _effects = require('./effects'); var _effects2 = _interopRequireDefault(_effects); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function Group(context, destination) { var sounds = []; var effects = new _effects2.default(context); var gain = context.createGain(); var preMuteVolume = 1; var group = null; if (context) { effects.setSource(gain); effects.setDestination(destination || context.destination); } /* * Add / remove */ function find(soundOrId, callback) { var found = void 0; if (!soundOrId && soundOrId !== 0) { return found; } sounds.some(function (sound) { if (sound === soundOrId || sound.id === soundOrId) { found = sound; return true; } return false; }); if (found && callback) { return callback(found); } return found; } function remove(soundOrId) { find(soundOrId, function (sound) { return sounds.splice(sounds.indexOf(sound), 1); }); return group; } function add(sound) { sound.gain.disconnect(); sound.gain.connect(gain); sounds.push(sound); sound.once('destroy', remove); return group; } /* * Controls */ function play(delay, offset) { sounds.forEach(function (sound) { return sound.play(delay, offset); }); return group; } function pause() { sounds.forEach(function (sound) { if (sound.playing) { sound.pause(); } }); return group; } function resume() { sounds.forEach(function (sound) { if (sound.paused) { sound.play(); } }); return group; } function stop() { sounds.forEach(function (sound) { return sound.stop(); }); return group; } function seek(percent) { sounds.forEach(function (sound) { return sound.seek(percent); }); return group; } function mute() { preMuteVolume = group.volume; group.volume = 0; return group; } function unMute() { group.volume = preMuteVolume || 1; return group; } function setVolume(value) { group.volume = value; return group; } function fade(volume, duration) { if (context) { var param = gain.gain; var time = context.currentTime; param.cancelScheduledValues(time); param.setValueAtTime(param.value, time); // param.setValueAtTime(volume, time + duration); param.linearRampToValueAtTime(volume, time + duration); // param.setTargetAtTime(volume, time, duration); // param.exponentialRampToValueAtTime(Math.max(volume, 0.0001), time + duration); } else { sounds.forEach(function (sound) { return sound.fade(volume, duration); }); } return group; } /* * Load */ function load() { sounds.forEach(function (sound) { return sound.load(); }); } /* * Unload */ function unload() { sounds.forEach(function (sound) { return sound.unload(); }); } /* * Destroy */ function destroy() { while (sounds.length) { sounds.pop().destroy(); } } /* * Api */ group = { add: add, find: find, remove: remove, play: play, pause: pause, resume: resume, stop: stop, seek: seek, setVolume: setVolume, mute: mute, unMute: unMute, fade: fade, load: load, unload: unload, destroy: destroy, gain: gain, get effects() { return effects._nodes; }, set effects(value) { effects.removeAll().add(value); }, get fx() { return this.effects; }, set fx(value) { this.effects = value; }, get sounds() { return sounds; }, get volume() { return gain.gain.value; }, set volume(value) { if (isNaN(value)) { return; } value = Math.min(Math.max(value, 0), 1); if (context) { gain.gain.cancelScheduledValues(context.currentTime); gain.gain.value = value; gain.gain.setValueAtTime(value, context.currentTime); } else { gain.gain.value = value; } sounds.forEach(function (sound) { if (!sound.context) { sound.groupVolume = value; } }); } }; return group; } Group.Effects = _effects2.default;