From e0b82bd74e085df060545d459e5898ef95e9a0a5 Mon Sep 17 00:00:00 2001 From: Xander Dumaine Date: Wed, 3 Feb 2016 17:26:12 -0500 Subject: [PATCH] Cleanup analyser and bundle --- example/demo.bundle.js | 309 ++++++++++++++++++++++------------------- hark.bundle.js | 300 ++++++++++++++++++++------------------- hark.js | 8 +- 3 files changed, 326 insertions(+), 291 deletions(-) diff --git a/example/demo.bundle.js b/example/demo.bundle.js index daba360..0958b58 100644 --- a/example/demo.bundle.js +++ b/example/demo.bundle.js @@ -126,7 +126,7 @@ for (var i = 0; i < 100; i++) { window.requestAnimationFrame(draw); })(); -},{"../hark.js":2,"attachmediastream":5,"bows":4,"getusermedia":3}],3:[function(require,module,exports){ +},{"../hark.js":2,"attachmediastream":5,"bows":3,"getusermedia":4}],4:[function(require,module,exports){ // getUserMedia helper by @HenrikJoreteg var func = (navigator.getUserMedia || navigator.webkitGetUserMedia || @@ -273,7 +273,7 @@ module.exports = function(stream, options) { harker.setInterval = function(i) { interval = i; }; - + harker.stop = function() { running = false; harker.emit('volume_change', -100, threshold); @@ -281,6 +281,8 @@ module.exports = function(stream, options) { harker.speaking = false; harker.emit('stopped_speaking'); } + analyser.disconnect(); + sourceNode.disconnect(); }; harker.speakingHistory = []; for (var i = 0; i < history; i++) { @@ -291,12 +293,12 @@ module.exports = function(stream, options) { // and emit events if changed var looper = function() { setTimeout(function() { - + //check if stop has been called if(!running) { return; } - + var currentVolume = getMaxVolume(analyser, fftBins); harker.emit('volume_change', currentVolume, threshold); @@ -333,147 +335,161 @@ module.exports = function(stream, options) { } },{"wildemitter":6}],6:[function(require,module,exports){ -/* -WildEmitter.js is a slim little event emitter by @henrikjoreteg largely based -on @visionmedia's Emitter from UI Kit. - -Why? I wanted it standalone. - -I also wanted support for wildcard emitters like this: - -emitter.on('*', function (eventName, other, event, payloads) { - -}); - -emitter.on('somenamespace*', function (eventName, payloads) { - -}); - -Please note that callbacks triggered by wildcard registered events also get -the event name as the first argument. -*/ -module.exports = WildEmitter; - -function WildEmitter() { - this.callbacks = {}; -} - -// Listen on the given `event` with `fn`. Store a group name if present. -WildEmitter.prototype.on = function (event, groupName, fn) { - var hasGroup = (arguments.length === 3), - group = hasGroup ? arguments[1] : undefined, - func = hasGroup ? arguments[2] : arguments[1]; - func._groupName = group; - (this.callbacks[event] = this.callbacks[event] || []).push(func); - return this; -}; - -// Adds an `event` listener that will be invoked a single -// time then automatically removed. -WildEmitter.prototype.once = function (event, groupName, fn) { - var self = this, - hasGroup = (arguments.length === 3), - group = hasGroup ? arguments[1] : undefined, - func = hasGroup ? arguments[2] : arguments[1]; - function on() { - self.off(event, on); - func.apply(this, arguments); - } - this.on(event, group, on); - return this; -}; - -// Unbinds an entire group -WildEmitter.prototype.releaseGroup = function (groupName) { - var item, i, len, handlers; - for (item in this.callbacks) { - handlers = this.callbacks[item]; - for (i = 0, len = handlers.length; i < len; i++) { - if (handlers[i]._groupName === groupName) { - //console.log('removing'); - // remove it and shorten the array we're looping through - handlers.splice(i, 1); - i--; - len--; - } - } - } - return this; -}; - -// Remove the given callback for `event` or all -// registered callbacks. -WildEmitter.prototype.off = function (event, fn) { - var callbacks = this.callbacks[event], - i; - - if (!callbacks) return this; - - // remove all handlers - if (arguments.length === 1) { - delete this.callbacks[event]; - return this; - } - - // remove specific handler - i = callbacks.indexOf(fn); - callbacks.splice(i, 1); - return this; -}; - -/// Emit `event` with the given args. -// also calls any `*` handlers -WildEmitter.prototype.emit = function (event) { - var args = [].slice.call(arguments, 1), - callbacks = this.callbacks[event], - specialCallbacks = this.getWildcardCallbacks(event), - i, - len, - item, - listeners; - - if (callbacks) { - listeners = callbacks.slice(); - for (i = 0, len = listeners.length; i < len; ++i) { - if (listeners[i]) { - listeners[i].apply(this, args); - } else { - break; - } - } - } - - if (specialCallbacks) { - len = specialCallbacks.length; - listeners = specialCallbacks.slice(); - for (i = 0, len = listeners.length; i < len; ++i) { - if (listeners[i]) { - listeners[i].apply(this, [event].concat(args)); - } else { - break; - } - } - } - - return this; -}; - -// Helper for for finding special wildcard event handlers that match the event -WildEmitter.prototype.getWildcardCallbacks = function (eventName) { - var item, - split, - result = []; - - for (item in this.callbacks) { - split = item.split('*'); - if (item === '*' || (split.length === 2 && eventName.slice(0, split[0].length) === split[0])) { - result = result.concat(this.callbacks[item]); - } - } - return result; -}; - -},{}],4:[function(require,module,exports){ +/* +WildEmitter.js is a slim little event emitter by @henrikjoreteg largely based +on @visionmedia's Emitter from UI Kit. + +Why? I wanted it standalone. + +I also wanted support for wildcard emitters like this: + +emitter.on('*', function (eventName, other, event, payloads) { + +}); + +emitter.on('somenamespace*', function (eventName, payloads) { + +}); + +Please note that callbacks triggered by wildcard registered events also get +the event name as the first argument. +*/ + +module.exports = WildEmitter; + +function WildEmitter() { } + +WildEmitter.mixin = function (constructor) { + var prototype = constructor.prototype || constructor; + + prototype.isWildEmitter= true; + + // Listen on the given `event` with `fn`. Store a group name if present. + prototype.on = function (event, groupName, fn) { + this.callbacks = this.callbacks || {}; + var hasGroup = (arguments.length === 3), + group = hasGroup ? arguments[1] : undefined, + func = hasGroup ? arguments[2] : arguments[1]; + func._groupName = group; + (this.callbacks[event] = this.callbacks[event] || []).push(func); + return this; + }; + + // Adds an `event` listener that will be invoked a single + // time then automatically removed. + prototype.once = function (event, groupName, fn) { + var self = this, + hasGroup = (arguments.length === 3), + group = hasGroup ? arguments[1] : undefined, + func = hasGroup ? arguments[2] : arguments[1]; + function on() { + self.off(event, on); + func.apply(this, arguments); + } + this.on(event, group, on); + return this; + }; + + // Unbinds an entire group + prototype.releaseGroup = function (groupName) { + this.callbacks = this.callbacks || {}; + var item, i, len, handlers; + for (item in this.callbacks) { + handlers = this.callbacks[item]; + for (i = 0, len = handlers.length; i < len; i++) { + if (handlers[i]._groupName === groupName) { + //console.log('removing'); + // remove it and shorten the array we're looping through + handlers.splice(i, 1); + i--; + len--; + } + } + } + return this; + }; + + // Remove the given callback for `event` or all + // registered callbacks. + prototype.off = function (event, fn) { + this.callbacks = this.callbacks || {}; + var callbacks = this.callbacks[event], + i; + + if (!callbacks) return this; + + // remove all handlers + if (arguments.length === 1) { + delete this.callbacks[event]; + return this; + } + + // remove specific handler + i = callbacks.indexOf(fn); + callbacks.splice(i, 1); + if (callbacks.length === 0) { + delete this.callbacks[event]; + } + return this; + }; + + /// Emit `event` with the given args. + // also calls any `*` handlers + prototype.emit = function (event) { + this.callbacks = this.callbacks || {}; + var args = [].slice.call(arguments, 1), + callbacks = this.callbacks[event], + specialCallbacks = this.getWildcardCallbacks(event), + i, + len, + item, + listeners; + + if (callbacks) { + listeners = callbacks.slice(); + for (i = 0, len = listeners.length; i < len; ++i) { + if (!listeners[i]) { + break; + } + listeners[i].apply(this, args); + } + } + + if (specialCallbacks) { + len = specialCallbacks.length; + listeners = specialCallbacks.slice(); + for (i = 0, len = listeners.length; i < len; ++i) { + if (!listeners[i]) { + break; + } + listeners[i].apply(this, [event].concat(args)); + } + } + + return this; + }; + + // Helper for for finding special wildcard event handlers that match the event + prototype.getWildcardCallbacks = function (eventName) { + this.callbacks = this.callbacks || {}; + var item, + split, + result = []; + + for (item in this.callbacks) { + split = item.split('*'); + if (item === '*' || (split.length === 2 && eventName.slice(0, split[0].length) === split[0])) { + result = result.concat(this.callbacks[item]); + } + } + return result; + }; + +}; + +WildEmitter.mixin(WildEmitter); + +},{}],3:[function(require,module,exports){ (function(window) { var logger = require('andlog'), goldenRatio = 0.618033988749895, @@ -521,7 +537,8 @@ WildEmitter.prototype.getWildcardCallbacks = function (eventName) { return; } - if (ls && ls.debug && window.console) { + var andlogKey = ls.andlogKey || 'debug' + if (ls && ls[andlogKey] && window.console) { out = window.console; } else { var methods = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","), diff --git a/hark.bundle.js b/hark.bundle.js index 58c16bf..df4d46a 100644 --- a/hark.bundle.js +++ b/hark.bundle.js @@ -70,7 +70,7 @@ module.exports = function(stream, options) { harker.setInterval = function(i) { interval = i; }; - + harker.stop = function() { running = false; harker.emit('volume_change', -100, threshold); @@ -78,6 +78,8 @@ module.exports = function(stream, options) { harker.speaking = false; harker.emit('stopped_speaking'); } + analyser.disconnect(); + sourceNode.disconnect(); }; harker.speakingHistory = []; for (var i = 0; i < history; i++) { @@ -88,12 +90,12 @@ module.exports = function(stream, options) { // and emit events if changed var looper = function() { setTimeout(function() { - + //check if stop has been called if(!running) { return; } - + var currentVolume = getMaxVolume(analyser, fftBins); harker.emit('volume_change', currentVolume, threshold); @@ -130,145 +132,159 @@ module.exports = function(stream, options) { } },{"wildemitter":2}],2:[function(require,module,exports){ -/* -WildEmitter.js is a slim little event emitter by @henrikjoreteg largely based -on @visionmedia's Emitter from UI Kit. - -Why? I wanted it standalone. - -I also wanted support for wildcard emitters like this: - -emitter.on('*', function (eventName, other, event, payloads) { - -}); - -emitter.on('somenamespace*', function (eventName, payloads) { - -}); - -Please note that callbacks triggered by wildcard registered events also get -the event name as the first argument. -*/ -module.exports = WildEmitter; - -function WildEmitter() { - this.callbacks = {}; -} - -// Listen on the given `event` with `fn`. Store a group name if present. -WildEmitter.prototype.on = function (event, groupName, fn) { - var hasGroup = (arguments.length === 3), - group = hasGroup ? arguments[1] : undefined, - func = hasGroup ? arguments[2] : arguments[1]; - func._groupName = group; - (this.callbacks[event] = this.callbacks[event] || []).push(func); - return this; -}; - -// Adds an `event` listener that will be invoked a single -// time then automatically removed. -WildEmitter.prototype.once = function (event, groupName, fn) { - var self = this, - hasGroup = (arguments.length === 3), - group = hasGroup ? arguments[1] : undefined, - func = hasGroup ? arguments[2] : arguments[1]; - function on() { - self.off(event, on); - func.apply(this, arguments); - } - this.on(event, group, on); - return this; -}; - -// Unbinds an entire group -WildEmitter.prototype.releaseGroup = function (groupName) { - var item, i, len, handlers; - for (item in this.callbacks) { - handlers = this.callbacks[item]; - for (i = 0, len = handlers.length; i < len; i++) { - if (handlers[i]._groupName === groupName) { - //console.log('removing'); - // remove it and shorten the array we're looping through - handlers.splice(i, 1); - i--; - len--; - } - } - } - return this; -}; - -// Remove the given callback for `event` or all -// registered callbacks. -WildEmitter.prototype.off = function (event, fn) { - var callbacks = this.callbacks[event], - i; - - if (!callbacks) return this; - - // remove all handlers - if (arguments.length === 1) { - delete this.callbacks[event]; - return this; - } - - // remove specific handler - i = callbacks.indexOf(fn); - callbacks.splice(i, 1); - return this; -}; - -/// Emit `event` with the given args. -// also calls any `*` handlers -WildEmitter.prototype.emit = function (event) { - var args = [].slice.call(arguments, 1), - callbacks = this.callbacks[event], - specialCallbacks = this.getWildcardCallbacks(event), - i, - len, - item, - listeners; - - if (callbacks) { - listeners = callbacks.slice(); - for (i = 0, len = listeners.length; i < len; ++i) { - if (listeners[i]) { - listeners[i].apply(this, args); - } else { - break; - } - } - } - - if (specialCallbacks) { - len = specialCallbacks.length; - listeners = specialCallbacks.slice(); - for (i = 0, len = listeners.length; i < len; ++i) { - if (listeners[i]) { - listeners[i].apply(this, [event].concat(args)); - } else { - break; - } - } - } - - return this; -}; - -// Helper for for finding special wildcard event handlers that match the event -WildEmitter.prototype.getWildcardCallbacks = function (eventName) { - var item, - split, - result = []; - - for (item in this.callbacks) { - split = item.split('*'); - if (item === '*' || (split.length === 2 && eventName.slice(0, split[0].length) === split[0])) { - result = result.concat(this.callbacks[item]); - } - } - return result; -}; +/* +WildEmitter.js is a slim little event emitter by @henrikjoreteg largely based +on @visionmedia's Emitter from UI Kit. + +Why? I wanted it standalone. + +I also wanted support for wildcard emitters like this: + +emitter.on('*', function (eventName, other, event, payloads) { + +}); + +emitter.on('somenamespace*', function (eventName, payloads) { + +}); + +Please note that callbacks triggered by wildcard registered events also get +the event name as the first argument. +*/ + +module.exports = WildEmitter; + +function WildEmitter() { } + +WildEmitter.mixin = function (constructor) { + var prototype = constructor.prototype || constructor; + + prototype.isWildEmitter= true; + + // Listen on the given `event` with `fn`. Store a group name if present. + prototype.on = function (event, groupName, fn) { + this.callbacks = this.callbacks || {}; + var hasGroup = (arguments.length === 3), + group = hasGroup ? arguments[1] : undefined, + func = hasGroup ? arguments[2] : arguments[1]; + func._groupName = group; + (this.callbacks[event] = this.callbacks[event] || []).push(func); + return this; + }; + + // Adds an `event` listener that will be invoked a single + // time then automatically removed. + prototype.once = function (event, groupName, fn) { + var self = this, + hasGroup = (arguments.length === 3), + group = hasGroup ? arguments[1] : undefined, + func = hasGroup ? arguments[2] : arguments[1]; + function on() { + self.off(event, on); + func.apply(this, arguments); + } + this.on(event, group, on); + return this; + }; + + // Unbinds an entire group + prototype.releaseGroup = function (groupName) { + this.callbacks = this.callbacks || {}; + var item, i, len, handlers; + for (item in this.callbacks) { + handlers = this.callbacks[item]; + for (i = 0, len = handlers.length; i < len; i++) { + if (handlers[i]._groupName === groupName) { + //console.log('removing'); + // remove it and shorten the array we're looping through + handlers.splice(i, 1); + i--; + len--; + } + } + } + return this; + }; + + // Remove the given callback for `event` or all + // registered callbacks. + prototype.off = function (event, fn) { + this.callbacks = this.callbacks || {}; + var callbacks = this.callbacks[event], + i; + + if (!callbacks) return this; + + // remove all handlers + if (arguments.length === 1) { + delete this.callbacks[event]; + return this; + } + + // remove specific handler + i = callbacks.indexOf(fn); + callbacks.splice(i, 1); + if (callbacks.length === 0) { + delete this.callbacks[event]; + } + return this; + }; + + /// Emit `event` with the given args. + // also calls any `*` handlers + prototype.emit = function (event) { + this.callbacks = this.callbacks || {}; + var args = [].slice.call(arguments, 1), + callbacks = this.callbacks[event], + specialCallbacks = this.getWildcardCallbacks(event), + i, + len, + item, + listeners; + + if (callbacks) { + listeners = callbacks.slice(); + for (i = 0, len = listeners.length; i < len; ++i) { + if (!listeners[i]) { + break; + } + listeners[i].apply(this, args); + } + } + + if (specialCallbacks) { + len = specialCallbacks.length; + listeners = specialCallbacks.slice(); + for (i = 0, len = listeners.length; i < len; ++i) { + if (!listeners[i]) { + break; + } + listeners[i].apply(this, [event].concat(args)); + } + } + + return this; + }; + + // Helper for for finding special wildcard event handlers that match the event + prototype.getWildcardCallbacks = function (eventName) { + this.callbacks = this.callbacks || {}; + var item, + split, + result = []; + + for (item in this.callbacks) { + split = item.split('*'); + if (item === '*' || (split.length === 2 && eventName.slice(0, split[0].length) === split[0])) { + result = result.concat(this.callbacks[item]); + } + } + return result; + }; + +}; + +WildEmitter.mixin(WildEmitter); },{}]},{},[1])(1) }); diff --git a/hark.js b/hark.js index f62e2a5..b5e14fd 100644 --- a/hark.js +++ b/hark.js @@ -68,7 +68,7 @@ module.exports = function(stream, options) { harker.setInterval = function(i) { interval = i; }; - + harker.stop = function() { running = false; harker.emit('volume_change', -100, threshold); @@ -76,6 +76,8 @@ module.exports = function(stream, options) { harker.speaking = false; harker.emit('stopped_speaking'); } + analyser.disconnect(); + sourceNode.disconnect(); }; harker.speakingHistory = []; for (var i = 0; i < history; i++) { @@ -86,12 +88,12 @@ module.exports = function(stream, options) { // and emit events if changed var looper = function() { setTimeout(function() { - + //check if stop has been called if(!running) { return; } - + var currentVolume = getMaxVolume(analyser, fftBins); harker.emit('volume_change', currentVolume, threshold);