Skip to content

Commit

Permalink
debugging in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jbilcke committed Jun 15, 2012
1 parent 8f145ea commit fe0d73d
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 58 deletions.
31 changes: 18 additions & 13 deletions lib/cursor.js

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

25 changes: 15 additions & 10 deletions lib/simple.js

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

3 changes: 3 additions & 0 deletions lib/stream.js

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

22 changes: 11 additions & 11 deletions src/cursor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@

# standard modules
{log,error,inspect} = require 'util'
events = require 'events'

# third party modules
moment = require 'moment'

# project modules
{delay,contains} = require './misc/toolbox'

class module.exports
class module.exports extends events.EventEmitter

constructor: (options) ->

Expand All @@ -43,10 +44,6 @@ class module.exports
@speed = options.speed
@looped = options.looped

@onError = options.on.error
@onData = options.on.data
@onEnd = options.on.end

@enabled = yes
@paused = no
@next = no
Expand All @@ -60,9 +57,11 @@ class module.exports
resume: () =>
unless @enabled
#@onEnd()
@onError "cannot resume: we are not enabled"
@emit 'error', "cannot resume: we are not enabled"
return

@emit 'begin'

@paused = no

# du we already have a running cursor or not?
Expand All @@ -74,7 +73,7 @@ class module.exports
#log "fire: next is #{inspect @next}"
unless @enabled
#@onEnd()
@onError "cannot fire: we are not enabled"
@emit 'error', "cannot fire: we are not enabled"
return

if @paused
Expand All @@ -92,21 +91,22 @@ class module.exports
# when did we fire?
fired = moment()

# emit the event to our Player
delay 0, => @onData evt.timestamp, evt.data
# emit the event to our Player - note this will introduce some latency
# TODO replace by @emit 'data', {timestamp,data}
@emit 'data', timestamp: evt.timestamp, data: evt.data

@store.next evt, (next) =>

unless next
log "error, no more next in the DB.."
@onError "store.next gave us nothing"
@emit 'error', "store.next gave us nothing"
return

# did we hit the loop cue point?
if next is @store.first
unless @looped
@enabled = no
@onEnd()
@emit 'end'
return

# theorical delay until next event (with threshold applied)
Expand Down
21 changes: 10 additions & 11 deletions src/simple.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class exports.Player
autoplay: on
timestamp: no
looped: no
onBegin: ->
onData: (tm,data) -> #log "#{tm}: #{data}"
onEnd: ->
onError: (err) ->
Expand All @@ -81,24 +82,22 @@ class exports.Player
record: @record
speed: @config.speed
looped: @config.looped
on:
data: (timestamp, data) =>
#log "CURSOR SENT US #{timestamp} ~ #{inspect data}"
@config.onData timestamp, data
end: =>
#log "CURSOR SENT 'end'"
@config.onEnd()
error: (err) =>
#log "CURSOR SENT 'error': #{err}"
@config.onError err
@cursor.on 'begin', =>
delay 0, => @config.onBegin()
@cursor.on 'data', (timestamp, data) =>
delay 0, => @config.onData packet.timestamp, packet.data
@cursor.on 'end', =>
delay 0, => @config.onEnd()
@cursor.on 'error', (err) =>
delay 0, => @config.onError(err)

if @config.autoplay
@start()

start: ->
@resume()

start: ->
resume: ->
#log "simple.Player#start()"
@cursor.resume()

Expand Down
19 changes: 11 additions & 8 deletions src/stream.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,17 @@ class exports.Player extends Stream
record: @record
speed: @config.speed
looped: @config.looped
on:
data: (timestamp, data) =>
@emit 'data', data
end: =>
@emit 'end'
error: (err) =>
log "error: #{err}"
#@emit 'error', err

@cursor.on 'begin', => @config.onBegin()
@cursor.on 'data', (packet) =>
log "CURSOR SENT US ~ #{inspect packed}"
@emit 'data', packet
@cursor.on 'end', =>
log "CURSOR SENT 'end'"
@emit 'end'
@cursor.on 'error', (err) =>
log "CURSOR SENT 'error': #{err}"
@emit 'error', err

if @config.autoplay
@resume()
Expand Down
29 changes: 24 additions & 5 deletions test/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,28 @@ class Newsfeed extends Stream
remainingItems = remainingItems[1..]
if event
if cb
log "CALLING CB event"
cb event
else
log "CALLING EMIT 'data', event"
@emit 'data', event

if (remainingItems.length is 0) or (!event)
log "LATEST DONE.. CALLING CB"
if cb
log "CALLING CB()"
cb()
else
log "CALLING EMIT 'END'"
@emit 'end'
return

delay (50+Math.random(50)), =>
t = (50+Math.random(50))
delay t, =>
run remainingItems, cb
run @events, cb


TIMEOUT = 50 # 10 milliseconds
TIMEOUT = 20 # 10 milliseconds

# our tests
describe 'new Record(\'test.sample\')', ->
Expand All @@ -59,17 +64,31 @@ describe 'new Record(\'test.sample\')', ->
#@timeout 10000
recorder = new SimpleRecorder record
feed = new Newsfeed()

t = moment()
feed.resume (event) ->
e = moment() - t

log "resume (event): elapsed: #{e}"

if event
log "event"
recorder.write event
else
length = record.length()
log "ENDED RECORD. length: #{length}"
record.save() # save the file
done()

it 'playback at normal speed', (done) ->
@timeout TIMEOUT + (length / 1.0)
new SimplePlayer record, onEnd: -> done()
t = moment()
new SimplePlayer record,
onBegin: ->
@timeout TIMEOUT + (length / 1.0)
onEnd: ->
e = moment() - t
log "play expected: #{TIMEOUT + (length / 1.0)} elapsed: #{e}"
done()

###
Expand Down

0 comments on commit fe0d73d

Please sign in to comment.