diff options
Diffstat (limited to 'client/lib/startAudioContext.js')
| -rw-r--r-- | client/lib/startAudioContext.js | 74 |
1 files changed, 36 insertions, 38 deletions
diff --git a/client/lib/startAudioContext.js b/client/lib/startAudioContext.js index f3a9793..56d4cae 100644 --- a/client/lib/startAudioContext.js +++ b/client/lib/startAudioContext.js @@ -7,13 +7,12 @@ (function (root, factory) { if (typeof define === "function" && define.amd) { define([], factory); - } else if (typeof module === 'object' && module.exports) { - module.exports = factory(); + } else if (typeof module === "object" && module.exports) { + module.exports = factory(); } else { root.StartAudioContext = factory(); } -}(this, function () { - +})(this, function () { /** * The StartAudioContext object */ @@ -22,28 +21,27 @@ * The audio context passed in by the user * @type {AudioContext} */ - context : null, + context: null, /** * The TapListeners bound to the elements * @type {Array} * @private */ - _tapListeners : [], + _tapListeners: [], /** * Callbacks to invoke when the audio context is started * @type {Array} * @private */ - _onStarted : [], + _onStarted: [], }; - /** * Set the context * @param {AudioContext} ctx * @returns {StartAudioContext} */ - StartAudioContext.setContext = function(ctx){ + StartAudioContext.setContext = function (ctx) { StartAudioContext.context = ctx; return StartAudioContext; }; @@ -53,31 +51,31 @@ * @param {Array|Element|String|jQuery} element * @returns {StartAudioContext} */ - StartAudioContext.on = function(element){ - if (Array.isArray(element) || (NodeList && element instanceof NodeList)){ - for (var i = 0; i < element.length; i++){ + StartAudioContext.on = function (element) { + if (Array.isArray(element) || (NodeList && element instanceof NodeList)) { + for (var i = 0; i < element.length; i++) { StartAudioContext.on(element[i]); } - } else if (typeof element === "string"){ + } else if (typeof element === "string") { StartAudioContext.on(document.querySelectorAll(element)); - } else if (element.jquery && typeof element.toArray === "function"){ + } else if (element.jquery && typeof element.toArray === "function") { StartAudioContext.on(element.toArray()); - } else if (Element && element instanceof Element){ + } else if (Element && element instanceof Element) { //if it's an element, create a TapListener var tap = new TapListener(element, onTap); StartAudioContext._tapListeners.push(tap); - } + } return StartAudioContext; }; /** - * Bind a callback to when the audio context is started. + * Bind a callback to when the audio context is started. * @param {Function} cb * @return {StartAudioContext} */ - StartAudioContext.onStarted = function(cb){ + StartAudioContext.onStarted = function (cb) { //if it's already started, invoke the callback - if (StartAudioContext.isStarted()){ + if (StartAudioContext.isStarted()) { cb(); } else { StartAudioContext._onStarted.push(cb); @@ -89,8 +87,11 @@ * returns true if the context is started * @return {Boolean} */ - StartAudioContext.isStarted = function(){ - return (StartAudioContext.context !== null && StartAudioContext.context.state === "running"); + StartAudioContext.isStarted = function () { + return ( + StartAudioContext.context !== null && + StartAudioContext.context.state === "running" + ); }; /** @@ -98,8 +99,7 @@ * @param {Element} element * @internal */ - var TapListener = function(element){ - + var TapListener = function (element) { this._dragged = false; this._element = element; @@ -115,15 +115,15 @@ /** * drag move event */ - TapListener.prototype._moved = function(e){ + TapListener.prototype._moved = function (e) { this._dragged = true; }; /** * tap ended listener */ - TapListener.prototype._ended = function(e){ - if (!this._dragged){ + TapListener.prototype._ended = function (e) { + if (!this._dragged) { onTap(); } this._dragged = false; @@ -132,7 +132,7 @@ /** * remove all the bound events */ - TapListener.prototype.dispose = function(){ + TapListener.prototype.dispose = function () { this._element.removeEventListener("touchmove", this._bindedMove); this._element.removeEventListener("touchend", this._bindedEnd); this._element.removeEventListener("mouseup", this._bindedEnd); @@ -143,12 +143,12 @@ /** * Invoked the first time of the elements is tapped. - * Creates a silent oscillator when a non-dragging touchend + * Creates a silent oscillator when a non-dragging touchend * event has been triggered. */ - function onTap(){ + function onTap() { //start the audio context with a silent oscillator - if (StartAudioContext.context && !StartAudioContext.isStarted()){ + if (StartAudioContext.context && !StartAudioContext.isStarted()) { var osc = StartAudioContext.context.createOscillator(); var silent = StartAudioContext.context.createGain(); silent.gain.value = 0; @@ -156,19 +156,19 @@ silent.connect(StartAudioContext.context.destination); var now = StartAudioContext.context.currentTime; osc.start(now); - osc.stop(now+0.5); + osc.stop(now + 0.5); } //dispose all the tap listeners - if (StartAudioContext._tapListeners){ - for (var i = 0; i < StartAudioContext._tapListeners.length; i++){ + if (StartAudioContext._tapListeners) { + for (var i = 0; i < StartAudioContext._tapListeners.length; i++) { StartAudioContext._tapListeners[i].dispose(); } StartAudioContext._tapListeners = null; } //the onstarted callbacks - if (StartAudioContext._onStarted){ - for (var j = 0; j < StartAudioContext._onStarted.length; j++){ + if (StartAudioContext._onStarted) { + for (var j = 0; j < StartAudioContext._onStarted.length; j++) { StartAudioContext._onStarted[j](); } StartAudioContext._onStarted = null; @@ -176,6 +176,4 @@ } return StartAudioContext; -})); - - +}); |
