UNPKG

audio-context-singleton

Version:

When you want to have an audioContext singleton but don't want to rewrite [this code](index.js), use this. Deals with browser differences in AudioContext and maintains the singleton.

34 lines (27 loc) 639 B
/* global webkitAudioContext */ function AudioContextSingleton(ctorOpts) { var audioContext; return { getCurrent, getNew }; function getCurrent(opts) { if (audioContext) { return audioContext; } return getNew(opts); } function getNew(opts) { if (ctorOpts && ctorOpts.offline) { audioContext = new OfflineAudioContext(opts); } else { if (typeof AudioContext === 'function') { audioContext = new AudioContext(opts); } else { audioContext = new webkitAudioContext(opts); } } return audioContext; } } module.exports = AudioContextSingleton;