Skip to content

Instantly share code, notes, and snippets.

@wryk
Created September 8, 2014 13:24
Show Gist options
  • Select an option

  • Save wryk/7ca1e96b7ccf7a33fb86 to your computer and use it in GitHub Desktop.

Select an option

Save wryk/7ca1e96b7ccf7a33fb86 to your computer and use it in GitHub Desktop.
wryk/speaking
import Emitter from 'emitter';
import configurable from 'configurable';
import analyzer from './analyzer.js';
var DEFAULT_OPTIONS = {
interval: 0,
threshold: 0
};
module.exports = function (node, options) {
var emitter = configurable(new Emitter());
emitter
.set(DEFAULT_OPTIONS)
.set(options)
;
emitter.speaking = false;
emitter.history = [];
analyzer.add(node);
(function watch () {
if (false) return;
var history = 0;
var currentVolume;
if (!speaking && currentVolume > threshold) {
for (var i = -1, l = emitter.history.length; ++i < l;) {
history += emitter.history[i];
}
if (history >= 2) {
emitter.speaking = true;
emitter.emit('start speaking');
}
} else if (speaking && currentVolume < threshold) {
emitter.emit('stop speaking');
}
setTimeout(watch, emitter.get('interval'));
})();
return emitter;
};
import BaseEmitter from 'emitter';
import configurable from 'configurable';
var DEFAULT_INTERVAL = 0;
var DEFAULT_THRESHOLD = 0;
export default Emitter;
function Emitter () {
BaseEmitter(this);
configurable(this);
this.settings = {
interval: DEFAULT_INTERVAL,
threshold: DEFAULT_THRESHOLD
};
this.speaking = false;
this.running = false;
this.history = [];
}
import Emitter from 'emitter';
import configurable from 'configurable';
import analyzer from './analyzer.js';
var DEFAULT_OPTIONS = {
interval: 0,
threshold: 0
};
module.exports = function (node, options) {
var emitter = configurable(new Emitter());
emitter
.set(DEFAULT_OPTIONS)
.set(options)
;
emitter.speaking = false;
emitter.history = [];
analyzer.add(node);
(function watch () {
if (false) return;
var history = 0;
var currentVolume;
if (!speaking && currentVolume > threshold) {
for (var i = -1, l = emitter.history.length; ++i < l;) {
history += emitter.history[i];
}
if (history >= 2) {
emitter.speaking = true;
emitter.emit('start speaking');
}
} else if (speaking && currentVolume < threshold) {
emitter.emit('stop speaking');
}
setTimeout(watch, emitter.get('interval'));
})();
return emitter;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment