Skip to content

Instantly share code, notes, and snippets.

@bpostlethwaite
Created April 7, 2014 19:03
Show Gist options
  • Save bpostlethwaite/10031401 to your computer and use it in GitHub Desktop.
Save bpostlethwaite/10031401 to your computer and use it in GitHub Desktop.
emitter that emits modified sine waves
var EventEmitter = require('events').EventEmitter
var sinner = SinBox()
setInterval( function () {
sinner.setAmplitude(12)
}, 500)
setInterval( function () {
sinner.setFrequency(40)
}, 500)
sinner.on('data', function (data) {
console.log(data)
})
function SinBox () {
var self = new EventEmitter()
self.freq = 0 // should be between 200ms and 4 seconds?
self.amp = 0 // should be between 0 and 1
self.start = Date.now()
self.switcher = 0
self.setAmplitude = function (amp) {
self.amp = amp
self.switcher = !self.switcher
if (self.switcher)
self.computeSignal()
}
self.setFrequency = function (freq) {
self.freq = freq
self.switcher = !self.switcher
if (self.switcher)
self.computeSignal()
}
self.computeSignal = function () {
var now = Date.now()
, ts = (now - self.start) / 1000
var x = new Date(now - 14400000).toISOString().replace(/T/, ' ').replace(/Z/, '');
var y = self.amp * Math.sin(self.freq * ts)
self.emit('data', {x:x, y:y} )
}
return self
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment