Commit d5b11369 by oriol

attempt to fix sono

parent 085de654
......@@ -72,7 +72,7 @@
/*! all exports used */
/***/ (function(module, exports) {
eval("throw new Error(\"Module parse failed: Unexpected token (3:0)\\nYou may need an appropriate loader to handle this file type.\\n| \\\"use strict\\\";\\n| import soundObject\\n| import sono\\n| var strings=new Strings()\\n| var tick=null;\");//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMC5qcyIsInNvdXJjZXMiOltdLCJtYXBwaW5ncyI6IiIsInNvdXJjZVJvb3QiOiIifQ==\n//# sourceURL=webpack-internal:///0\n");
eval("throw new Error(\"Module parse failed: Unexpected token (2:8)\\nYou may need an appropriate loader to handle this file type.\\n| \\\"use strict\\\";\\n| import {*} from ./www/js/sono/core;\\n| var strings=new Strings();\\n| var tick=null;\");//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMC5qcyIsInNvdXJjZXMiOltdLCJtYXBwaW5ncyI6IiIsInNvdXJjZVJvb3QiOiIifQ==\n//# sourceURL=webpack-internal:///0\n");
/***/ })
/******/ ]);
\ No newline at end of file
"use strict";
import soundObject
import sono
var strings=new Strings()
import {*} from ./www/js/sono/core;
var strings=new Strings();
var tick=null;
var sKill;
var lang="english";
......
This source diff could not be displayed because it is too large. You can view the blob instead.
import dummy from './utils/dummy';
import FakeContext from './utils/fake-context';
import iOS from './utils/iOS';
const desiredSampleRate = 44100;
const Ctx = window.AudioContext || window.webkitAudioContext || FakeContext;
let context = new Ctx();
if (!context) {
context = new FakeContext();
}
// Check if hack is necessary. Only occurs in iOS6+ devices
// and only when you first boot the iPhone, or play a audio/video
// with a different sample rate
// https://github.com/Jam3/ios-safe-audio-context/blob/master/index.js
if (iOS && context.sampleRate !== desiredSampleRate) {
dummy(context);
context.close(); // dispose old context
context = new Ctx();
}
// Handles bug in Safari 9 OSX where AudioContext instance starts in 'suspended' state
if (context.state === 'suspended' && typeof context.resume === 'function') {
window.setTimeout(() => context.resume(), 1000);
}
export default context;
export default class Effects {
constructor(context) {
this.context = context;
this._destination = null;
this._source = null;
this._nodes = [];
this._nodes.has = node => this.has(node);
this._nodes.add = node => this.add(node);
this._nodes.remove = node => this.remove(node);
this._nodes.toggle = (node, force) => this.toggle(node, force);
this._nodes.removeAll = () => this.removeAll();
Object.keys(Effects.prototype).forEach(key => {
if (!this._nodes.hasOwnProperty(key) && typeof Effects.prototype[key] === 'function') {
this._nodes[key] = this[key].bind(this);
}
});
}
setSource(node) {
this._source = node;
this._updateConnections();
return node;
}
setDestination(node) {
this._connectToDestination(node);
return node;
}
has(node) {
if (!node) {
return false;
}
return this._nodes.indexOf(node) > -1;
}
add(node) {
if (!node) {
return null;
}
if (this.has(node)) {
return node;
}
if (Array.isArray(node)) {
let n;
for (let i = 0; i < node.length; i++) {
n = this.add(node[i]);
}
return n;
}
this._nodes.push(node);
this._updateConnections();
return node;
}
remove(node) {
if (!node) {
return null;
}
if (!this.has(node)) {
return node;
}
const l = this._nodes.length;
for (let i = 0; i < l; i++) {
if (node === this._nodes[i]) {
this._nodes.splice(i, 1);
break;
}
}
node.disconnect();
this._updateConnections();
return node;
}
toggle(node, force) {
force = !!force;
const hasNode = this.has(node);
if (arguments.length > 1 && hasNode === force) {
return this;
}
if (hasNode) {
this.remove(node);
} else {
this.add(node);
}
return this;
}
removeAll() {
while (this._nodes.length) {
const node = this._nodes.pop();
node.disconnect();
}
this._updateConnections();
return this;
}
destroy() {
this.removeAll();
this.context = null;
this._destination = null;
if (this._source) {
this._source.disconnect();
}
this._source = null;
}
_connect(a, b) {
a.disconnect();
// console.log('> connect output', (a.name || a.constructor.name), 'to input', (b.name || b.constructor.name));
a.connect(b._in || b);
}
_connectToDestination(node) {
const lastNode = this._nodes[this._nodes.length - 1] || this._source;
if (lastNode) {
this._connect(lastNode, node);
}
this._destination = node;
}
_updateConnections() {
if (!this._source) {
return;
}
// console.log('updateConnections');
let node,
prev;
for (let i = 0; i < this._nodes.length; i++) {
node = this._nodes[i];
prev = i === 0 ? this._source : this._nodes[i - 1];
this._connect(prev, node);
}
if (this._destination) {
this._connectToDestination(this._destination);
}
}
}
import Effects from './effects';
export default function Group(context, destination) {
const sounds = [];
const effects = new Effects(context);
const gain = context.createGain();
let preMuteVolume = 1;
let group = null;
if (context) {
effects.setSource(gain);
effects.setDestination(destination || context.destination);
}
/*
* Add / remove
*/
function find(soundOrId, callback) {
let found;
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, (sound) => 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((sound) => sound.play(delay, offset));
return group;
}
function pause() {
sounds.forEach((sound) => {
if (sound.playing) {
sound.pause();
}
});
return group;
}
function resume() {
sounds.forEach((sound) => {
if (sound.paused) {
sound.play();
}
});
return group;
}
function stop() {
sounds.forEach((sound) => sound.stop());
return group;
}
function seek(percent) {
sounds.forEach((sound) => 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) {
const param = gain.gain;
const 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((sound) => sound.fade(volume, duration));
}
return group;
}
/*
* Load
*/
function load() {
sounds.forEach((sound) => sound.load());
}
/*
* Unload
*/
function unload() {
sounds.forEach((sound) => sound.unload());
}
/*
* Destroy
*/
function destroy() {
while (sounds.length) {
sounds.pop()
.destroy();
}
}
/*
* Api
*/
group = {
add,
find,
remove,
play,
pause,
resume,
stop,
seek,
setVolume,
mute,
unMute,
fade,
load,
unload,
destroy,
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((sound) => {
if (!sound.context) {
sound.groupVolume = value;
}
});
}
};
return group;
}
Group.Effects = Effects;
import 'core-js/fn/object/assign';
import context from './context';
import Effects from './effects';
import file from './utils/file';
import Group from './group';
import Loader from './utils/loader';
import log from './utils/log';
import pageVisibility from './utils/pageVisibility';
import Sound from './sound';
import SoundGroup from './utils/sound-group';
import touchLock from './utils/touchLock';
import utils from './utils/utils';
const VERSION = '2.1.4';
const bus = new Group(context, context.destination);
/*
* Get Sound by id
*/
function get(id) {
return bus.find(id);
}
/*
* Create group
*/
function group(sounds) {
const soundGroup = new SoundGroup(context, bus.gain);
if (sounds) {
sounds.forEach((sound) => soundGroup.add(sound));
}
return soundGroup;
}
/*
* Loading
*/
function add(config) {
const src = file.getSupportedFile(config.src || config.url || config.data || config);
const sound = new Sound(Object.assign({}, config || {}, {
src,
context,
destination: bus.gain
}));
sound.isTouchLocked = isTouchLocked;
if (config) {
sound.id = config.id || config.name || '';
sound.loop = !!config.loop;
sound.volume = config.volume;
sound.effects = config.effects || [];
}
bus.add(sound);
return sound;
}
function queue(config, loaderGroup) {
const sound = add(config).prepare();
if (loaderGroup) {
loaderGroup.add(sound.loader);
}
return sound;
}
function load(config) {
const src = config.src || config.url || config.data || config;
let sound, loader;
if (file.containsURL(src)) {
sound = queue(config);
loader = sound.loader;
} else if (Array.isArray(src) && file.containsURL(src[0].src || src[0].url)) {
sound = [];
loader = new Loader.Group();
src.forEach((url) => sound.push(queue(url, loader)));
} else {
const errorMessage = 'sono.load: No audio file URLs found in config.';
if (config.onError) {
config.onError('[ERROR] ' + errorMessage);
} else {
throw new Error(errorMessage);
}
return null;
}
if (config.onProgress) {
loader.on('progress', (progress) => config.onProgress(progress));
}
if (config.onComplete) {
loader.once('complete', () => {
loader.off('progress');
config.onComplete(sound);
});
}
loader.once('error', err => {
loader.off('error');
if (config.onError) {
config.onError(err);
} else {
console.error(err);
}
});
loader.start();
return sound;
}
/*
* Create Sound
*
* Accepted values for param config:
* Object config e.g. { id:'foo', url:['foo.ogg', 'foo.mp3'] }
* Array (of files e.g. ['foo.ogg', 'foo.mp3'])
* ArrayBuffer
* HTMLMediaElement
* Filename string (e.g. 'foo.ogg')
* Oscillator type string (i.e. 'sine', 'square', 'sawtooth', 'triangle')
*/
function create(config) {
// try to load if config contains URLs
if (file.containsURL(config)) {
return load(config);
}
const sound = add(config);
sound.data = config.data || config;
return sound;
}
/*
* Destroy
*/
function destroy(soundOrId) {
bus.find(soundOrId, (sound) => sound.destroy());
return sono;
}
function destroyAll() {
bus.destroy();
return sono;
}
/*
* Controls
*/
function mute() {
bus.mute();
return sono;
}
function unMute() {
bus.unMute();
return sono;
}
function fade(volume, duration) {
bus.fade(volume, duration);
return sono;
}
function pauseAll() {
bus.pause();
return sono;
}
function resumeAll() {
bus.resume();
return sono;
}
function stopAll() {
bus.stop();
return sono;
}
function play(id, delay, offset) {
bus.find(id, (sound) => sound.play(delay, offset));
return sono;
}
function pause(id) {
bus.find(id, (sound) => sound.pause());
return sono;
}
function stop(id) {
bus.find(id, (sound) => sound.stop());
return sono;
}
/*
* Mobile touch lock
*/
let isTouchLocked = touchLock(context, () => {
isTouchLocked = false;
bus.sounds.forEach(sound => (sound.isTouchLocked = false));
});
/*
* Page visibility events
*/
const pageHiddenPaused = [];
// pause currently playing sounds and store refs
function onHidden() {
bus.sounds.forEach(sound => {
if (sound.playing) {
sound.pause();
pageHiddenPaused.push(sound);
}
});
}
// play sounds that got paused when page was hidden
function onShown() {
while (pageHiddenPaused.length) {
pageHiddenPaused.pop().play();
}
}
const pageVis = pageVisibility(onHidden, onShown);
function register(name, fn, attachTo = Effects.prototype) {
attachTo[name] = fn;
sono[name] = fn;
return fn;
}
const sono = {
canPlay: file.canPlay,
context,
create,
createGroup: group,
createSound: create,
destroyAll,
destroy,
effects: bus.effects,
extensions: file.extensions,
fade,
file,
gain: bus.gain,
getOfflineContext: utils.getOfflineContext,
get,
getSound: get,
group,
hasWebAudio: !context.isFake,
isSupported: file.extensions.length > 0,
load,
log: () => log(sono),
mute,
pause,
pauseAll,
play,
register,
resumeAll,
stop,
stopAll,
unMute,
utils,
VERSION,
get effects() {
return bus.effects;
},
set effects(value) {
bus.effects.removeAll().add(value);
},
get fx() {
return this.effects;
},
set fx(value) {
this.effects = value;
},
get isTouchLocked() {
return isTouchLocked;
},
get playInBackground() {
return !pageVis.enabled;
},
set playInBackground(value) {
pageVis.enabled = !value;
if (!value) {
onShown();
}
},