diff --git a/README.md b/README.md index 770d1f4..bd4f448 100644 --- a/README.md +++ b/README.md @@ -69,18 +69,44 @@ ### Simple API +Recording + ``` coffeescript -{Record,simple} = require 'sampler' +{Record, SimpleRecorder} = require 'sampler' # create record. a record is where your events are stored record = new Record() # by default no arguments -> in memory store # now, you can start playing with your record. # let's record things in the record! for this, you need a Recorder -recorder = new simple.Recorder(record) +recorder = new .SimpleRecorder(record) + +# then just write things inside +recorder.write "hello" +recorder.write foo: "hello", bar: "world" +recorder.write new Buffer() + +# in the future, you will be able to add an event at a specific time +# recorder.writeAt moment(1982,1,1), "cold wave" + +``` + +Playback + +``` coffeescript + +{Record, SimplePlayer} = require 'sampler' + +# load an existing record - for the moment.. nothing is supported :) only in-memory +# but in the future, you will be able to load MongoDB, SQL, Redis records etc.. +record = new Record("redis://foobar") + +# now, you can start playing with your record. +# let's record things in the record! for this, you need a Player +player = new SimplePlayer(record) -recorder.rec "hello" +# by default the player start itself automatically ``` @@ -107,15 +133,15 @@ recorder.rec "hello" twit.stream 'statuses/sample', (stream) -> # that's all you have to do! - recorder = new sampler.stream.Recorder(stream) + recorder = new sampler.StreamRecorder(stream) terminate = -> - stream.destroy() + stream.pause() # let's play some music! # by default a sampler will simply playback the event # (it's in autorun + speed 1x by default) - player = new sampler.simple.Player(recorder.record) + player = new sampler.SimplePlayer(recorder.record) setTimeout terminate, 10000 diff --git a/lib/sampler.js b/lib/sampler.js index b57672f..6476683 100644 --- a/lib/sampler.js +++ b/lib/sampler.js @@ -1,10 +1,19 @@ // Generated by CoffeeScript 1.3.3 (function() { + var Record, simple, stream; + + stream = require('./stream'); + + simple = require('./simple'); + + Record = require('./record'); module.exports({ - Record: require('./record').Record, - stream: require('./stream'), - simple: require('./simple') + Record: Record, + SimpleRecorder: simple.Recorder, + SimplePlayer: simple.Player, + StreamRecorder: stream.Recorder, + StreamPlayer: stream.Player }); }).call(this); diff --git a/lib/simple.js b/lib/simple.js index b1aad20..1600ffc 100644 --- a/lib/simple.js +++ b/lib/simple.js @@ -21,20 +21,30 @@ if (url == null) { url = false; } - this.rec = __bind(this.rec, this); + this.writeAt = __bind(this.writeAt, this); + + this.write = __bind(this.write, this); log("simple.Recorder#constructor(" + url + ")"); this.record = simpleFactory(Record, url); } - Recorder.prototype.rec = function(data, status) { + Recorder.prototype.write = function(data, status) { if (status == null) { status = function() {}; } - log("simple.Recorder#rec(" + data + "): simple rec"); + log("SimpleRecorder#write(" + data + ")"); return this.record.write(moment(), data, status); }; + Recorder.prototype.writeAt = function(timestamp, data, status) { + if (status == null) { + status = function() {}; + } + log("SimpleRecorder#writeAt(" + timestamp + "," + data + ")"); + return this.record.write(timestamp, data, status); + }; + return Recorder; })(); @@ -52,11 +62,11 @@ autoplay: true, timestamp: false, looped: false, - onEach: function(event) { + onData: function(event) { return log("" + event.timestamp + ": " + event.data); }, onEnd: function() {}, - onErr: function(err) {} + onError: function(err) {} }; for (k in options) { @@ -72,14 +82,14 @@ looped: Player.config.looped, on: { data: function(timestamp, data) { - return Player.config.onEach(timestamp, data); + return Player.config.onData(timestamp, data); }, end: function() { return Player.config.onEnd(); }, error: function(err) { Player.config.onEnd(); - return Player.config.onErr(err); + return Player.config.onError(err); } } }); diff --git a/src/sampler.coffee b/src/sampler.coffee index 589abf5..fa3b175 100644 --- a/src/sampler.coffee +++ b/src/sampler.coffee @@ -1,5 +1,10 @@ +stream = require './stream' +simple = require './simple' +Record = require './record' module.exports - Record: require('./record').Record - stream: require './stream' - simple: require './simple' \ No newline at end of file + Record: Record + SimpleRecorder: simple.Recorder + SimplePlayer : simple.Player + StreamRecorder: stream.Recorder + StreamPlayer : stream.Player diff --git a/src/simple.coffee b/src/simple.coffee index a6293b9..40431b0 100644 --- a/src/simple.coffee +++ b/src/simple.coffee @@ -44,9 +44,14 @@ class Recorder # SimpleRecorder API - rec: (data,status=->) => - log "simple.Recorder#rec(#{data}): simple rec" + write: (data,status=->) => + log "SimpleRecorder#write(#{data})" @record.write moment(), data, status + + # SimpleRecorder API + writeAt: (timestamp, data,status=->) => + log "SimpleRecorder#writeAt(#{timestamp},#{data})" + @record.write timestamp, data, status class Player constructor: (url, options) -> diff --git a/test/main.coffee b/test/main.coffee index 14413ab..1badafd 100644 --- a/test/main.coffee +++ b/test/main.coffee @@ -7,7 +7,7 @@ {delay} = require '../lib/misc/toolbox' # The API we want to stress -{Record,simple,stream} = require '../lib/sampler' +{Record,SimplePlayer,SimpleRecorder,StreamPlayer,StreamRecorder} = require '../lib/sampler' class Newsfeed extends Stream constructor: -> @@ -52,31 +52,31 @@ describe 'Simple API', -> describe '#rec()', -> it 'should record 8 events (1 every 50~100ms)', (done) -> @timeout 3000 # TODO use something better here - recorder = new simple.Recorder record - feed.map (event) -> if event then recorder.rec(event) else done() + recorder = new SimpleRecorder record + feed.map (event) -> if event then recorder.write(event) else done() describe '#play()', -> length = record.length() it 'playback at normal speed', (done) -> @timeout TIMEOUT + (length / 1.0) - new simple.Player record, + new SimplePlayer record, onEnd: -> done() it 'playback at 2.0x speed', (done) -> @timeout TIMEOUT + (length / 2.0) - new simple.Player record, + new SimplePlayer record, rate: 2.0 onEnd: -> done() it 'playback at 10.0x speed', (done) -> @timeout TIMEOUT + (length / 10.0) - new simple.Player record, + new SimplePlayer record, rate: 10.0 onEnd: -> done() it 'playback at 0.345x speed', (done) -> @timeout TIMEOUT + (length / 0.345) - new simple.Player record, + new SimplePlayer record, rate: 0.345 onEnd: -> done() @@ -90,24 +90,28 @@ describe 'Stream API', -> describe '#stream.Recorder()', -> it 'should record 8 events (1 every 50~100ms)', (done) -> @timeout 3000 - recorder = new stream.Recorder record feed.on 'end', -> done() - feed.pipe() + recorder = new StreamRecorder history + feed.pipe(recorder) describe '#play()', -> length = record.length() it 'playback at normal speed', (done) -> @timeout TIMEOUT + (length / 1.0) - record.play -> done() + player = new StreamPlayer history + player.on 'end', -> done() it 'playback at 2.0x speed', (done) -> @timeout TIMEOUT + (length / 2.0) - record.play 2.0, -> done() + player = new StreamPlayer history, rate: 2.0 + player.on 'end', -> done() it 'playback at 10.0x speed', (done) -> @timeout TIMEOUT + (length / 10.0) - record.play 10.0, -> done() + player = new StreamPlayer history, rate: 10.0 + player.on 'end', -> done() it 'playback at 0.345x speed', (done) -> @timeout TIMEOUT + (length / 0.345) - record.play 0.345, -> done() \ No newline at end of file + player = new StreamPlayer history, rate: 0.345 + player.on 'end', -> done() \ No newline at end of file