diff web/light9-music.coffee @ 2376:4556eebe5d73

topdir reorgs; let pdm have its src/ dir; separate vite area from light9/
author drewp@bigasterisk.com
date Sun, 12 May 2024 19:02:10 -0700
parents light9/web/light9-music.coffee@e29315086f9f
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/light9-music.coffee	Sun May 12 19:02:10 2024 -0700
@@ -0,0 +1,73 @@
+log = debug('music')
+
+# port of light9/curvecalc/musicaccess.py
+coffeeElementSetup(class Music extends Polymer.Element
+  @is: "light9-music",
+  @getter_properties:
+    status: { type: String, notify: true }
+    statusTitle: { type: String, notify: true }
+    turboSign: { type: String, notify: true }
+    
+    duration: { type: Number, notify: true }
+    song: { type: String, notify: true }
+    # It does not yet work to write back to the playing/t
+    # properties. See seekPlayOrPause.
+    playing: { type: Boolean, notify: true }
+    t: { type: Number, notify: true }
+    
+  ready: ->
+    super.ready()
+    @turboUntil = 0
+    @poll()
+    setInterval(@estimateTimeLoop.bind(@), 30)
+
+  onError: (e) ->
+    req = @$.getTime.lastRequest
+    @status = "✘"
+    @statusTitle = "GET "+req.url+ " -> " + req.status + " " + req.statusText
+    setTimeout(@poll.bind(@), 2000)
+    
+  estimateTimeLoop: ->
+    if @playing
+      @t = @remoteT + (Date.now() - @remoteAsOfMs) / 1000
+    else
+      @t = @remoteT
+    
+  poll: ->
+    if not @$?.getTime?
+      setTimeout(@poll.bind(@), 200)
+      return
+    clearTimeout(@nextPoll) if @nextPoll
+    @$.getTime.generateRequest()
+    @status = "♫"
+    
+  onResponse: ->
+    @status = " "
+    @lastResponse = @$.getTime.lastResponse
+    now = Date.now()
+    if !@lastResponse.playing && @lastResponse.t != @remoteT
+      # likely seeking in another tool
+      @turboUntil = now + 1000
+    if now < @turboUntil
+      @turboSign = "⚡"
+      delay = 20
+    else
+      @turboSign = " "
+      delay = 700
+    
+    @nextPoll = setTimeout(@poll.bind(@), delay)
+    @duration = @lastResponse.duration
+    @playing = @lastResponse.playing
+    @song = @lastResponse.song
+
+    @remoteT = @lastResponse.t
+    @remoteAsOfMs = now
+
+  seekPlayOrPause: (t) ->
+    @$.seek.body = {t: t}
+    @$.seek.generateRequest()
+    
+    @turboUntil = Date.now() + 1000
+    @poll()
+)
+