Mercurial > code > home > repos > light9
changeset 936:11aa214cb9a1
refactor vidref MusicTime class
Ignore-this: 20302df87d13931fe0355f3bc3435973
author | drewp@bigasterisk.com |
---|---|
date | Wed, 12 Jun 2013 19:27:14 +0000 |
parents | 89adbbb06bcd |
children | b0337e6f68f1 |
files | light9/vidref/main.py light9/vidref/musictime.py |
diffstat | 2 files changed, 74 insertions(+), 70 deletions(-) [+] |
line wrap: on
line diff
--- a/light9/vidref/main.py Wed Jun 12 07:31:29 2013 +0000 +++ b/light9/vidref/main.py Wed Jun 12 19:27:14 2013 +0000 @@ -8,84 +8,16 @@ """ import pygst pygst.require("0.10") -import gst, gobject, time, json, restkit, logging, os, traceback +import gst, gobject, time, logging, os, traceback import gtk from twisted.python.util import sibpath import Image from threading import Thread from Queue import Queue -from light9 import networking from light9.vidref.replay import ReplayViews, songDir, takeDir, framerate -from restkit.errors import ResourceNotFound -import http_parser.http +from light9.vidref.musictime import MusicTime log = logging.getLogger() - -class MusicTime(object): - """ - fetch times from ascoltami in a background thread; return times - upon request, adjusted to be more precise with the system clock - """ - def __init__(self, period=.2, onChange=lambda position: None): - """period is the seconds between http time requests. - - We call onChange with the time in seconds and the total time - - The choice of period doesn't need to be tied to framerate, - it's more the size of the error you can tolerate (since we - make up times between the samples, and we'll just run off the - end of a song) - """ - self.period = period - self.onChange = onChange - self.musicResource = restkit.Resource(networking.musicPlayer.url) - self.curveCalc = restkit.Resource(networking.curveCalc.url) - t = Thread(target=self._timeUpdate) - t.setDaemon(True) - t.start() - - def getLatest(self): - """ - dict with 't' and 'song', etc. - """ - if not hasattr(self, 'position'): - return {'t' : 0, 'song' : None} - pos = self.position.copy() - if pos['playing']: - pos['t'] = pos['t'] + (time.time() - self.positionFetchTime) - else: - try: - # todo: this is blocking for a long while if CC is - # down. Either make a tiny timeout, or go async - r = self.curveCalc.get("hoverTime") - except (ResourceNotFound, http_parser.http.NoMoreData, Exception): - pass - else: - pos['hoverTime'] = json.loads(r.body_string())['hoverTime'] - return pos - - def _timeUpdate(self): - while True: - try: - position = json.loads(self.musicResource.get("time").body_string()) - - # this is meant to be the time when the server gave me its - # report, and I don't know if that's closer to the - # beginning of my request or the end of it (or some - # fraction of the way through) - self.positionFetchTime = time.time() - - self.position = position - self.onChange(position) - except restkit.RequestError, e: - log.error(e) - time.sleep(1) - time.sleep(self.period) - - def sendTime(self, t): - """request that the player go to this time""" - self.musicResource.post("time", payload=json.dumps({"t" : t}), - headers={"content-type" : "application/json"}) class VideoRecordSink(gst.Element): _sinkpadtemplate = gst.PadTemplate ("sinkpadtemplate",
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/light9/vidref/musictime.py Wed Jun 12 19:27:14 2013 +0000 @@ -0,0 +1,72 @@ +import restkit, time, json, logging +from light9 import networking +from threading import Thread +from restkit.errors import ResourceNotFound +import http_parser.http +log = logging.getLogger() + +class MusicTime(object): + """ + fetch times from ascoltami in a background thread; return times + upon request, adjusted to be more precise with the system clock + """ + def __init__(self, period=.2, onChange=lambda position: None): + """period is the seconds between http time requests. + + We call onChange with the time in seconds and the total time + + The choice of period doesn't need to be tied to framerate, + it's more the size of the error you can tolerate (since we + make up times between the samples, and we'll just run off the + end of a song) + """ + self.period = period + self.onChange = onChange + self.musicResource = restkit.Resource(networking.musicPlayer.url) + self.curveCalc = restkit.Resource(networking.curveCalc.url) + t = Thread(target=self._timeUpdate) + t.setDaemon(True) + t.start() + + def getLatest(self): + """ + dict with 't' and 'song', etc. + """ + if not hasattr(self, 'position'): + return {'t' : 0, 'song' : None} + pos = self.position.copy() + if pos['playing']: + pos['t'] = pos['t'] + (time.time() - self.positionFetchTime) + else: + try: + # todo: this is blocking for a long while if CC is + # down. Either make a tiny timeout, or go async + r = self.curveCalc.get("hoverTime") + except (ResourceNotFound, http_parser.http.NoMoreData, Exception): + pass + else: + pos['hoverTime'] = json.loads(r.body_string())['hoverTime'] + return pos + + def _timeUpdate(self): + while True: + try: + position = json.loads(self.musicResource.get("time").body_string()) + + # this is meant to be the time when the server gave me its + # report, and I don't know if that's closer to the + # beginning of my request or the end of it (or some + # fraction of the way through) + self.positionFetchTime = time.time() + + self.position = position + self.onChange(position) + except restkit.RequestError, e: + log.error(e) + time.sleep(1) + time.sleep(self.period) + + def sendTime(self, t): + """request that the player go to this time""" + self.musicResource.post("time", payload=json.dumps({"t" : t}), + headers={"content-type" : "application/json"})