   t = context.currentTime - timeOffset
   player.consumeEventsUntilTime(t + LOOKAHEAD)
 
+  # consumeSequencer()
+
+sequencerState = null
 consumeSequencer = ->
-  return unless sequencer
-  t = context.currentTime - timeOffset
-
-  sTime = sequencerState.time
-  timeSlice = t + LOOKAHEAD - sTime
+  return unless sequencer and player
+  
+  now = context.currentTime
+
+  if !sequencerState
+    console.log "START", now
+    sequencerState =
+      time: 0
+      offset: now
+
+  t = sequencerState.time
+  offset = sequencerState.offset
+
+  timeSlice = (now - offset) - t
 
   if timeSlice > 0
-    sequencerState.time = t + LOOKAHEAD
-
-    sequencer.notesAfter(sTime).filter ([time]) ->
+    sequencerState.time += timeSlice
+
+    sequencer.notesAfter(t).filter ([time]) ->
       time < timeSlice
     .forEach ([time, note]) ->
-      console.log t + time, note
+      console.log now + time, note
 
       noteOnEvent =
         channel: 0

        subtype: "noteOn"
        noteNumber: note
        velocity: 64
  
      noteOffEvent =
        channel: 0
        type: "channel"
        subtype: "noteOff"
        noteNumber: note
  
      player?.handleEvent noteOnEvent, time: t + time
      player?.handleEvent noteOffEvent, time: t + time

document.addEventListener "visibilitychange", (e) ->
  if document.hidden

  # Download wav

sequencer = null

do ->
  Sequencer = require "./sequencer"
