Skip to content

Commit

Permalink
work in progress, writing some doc, updating tests..
Browse files Browse the repository at this point in the history
  • Loading branch information
jbilcke committed Jun 13, 2012
1 parent a190253 commit 5ecd3c5
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 34 deletions.
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```

Expand All @@ -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

Expand Down
15 changes: 12 additions & 3 deletions lib/sampler.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions lib/simple.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/sampler.coffee
Original file line number Diff line number Diff line change
@@ -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'
Record: Record
SimpleRecorder: simple.Recorder
SimplePlayer : simple.Player
StreamRecorder: stream.Recorder
StreamPlayer : stream.Player
9 changes: 7 additions & 2 deletions src/simple.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down
30 changes: 17 additions & 13 deletions test/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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: ->
Expand Down Expand Up @@ -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()

Expand All @@ -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()
player = new StreamPlayer history, rate: 0.345
player.on 'end', -> done()

0 comments on commit 5ecd3c5

Please sign in to comment.