Files
@ 2ee97997ee56
Branch filter:
Location: light9/bin/effecteval - annotation
2ee97997ee56
11.0 KiB
text/plain
vidref now reads from picamserve
Ignore-this: 17bc49636b291f6640f2f2a4a8f93e40
Ignore-this: 17bc49636b291f6640f2f2a4a8f93e40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | e28a443bd153 9937e2e3d17b e28a443bd153 9937e2e3d17b 9937e2e3d17b a38414bd3929 b370618ce723 be016cd5e5c5 e28a443bd153 e28a443bd153 b5ee7aa9341a fce46850ed8c fce46850ed8c fce46850ed8c b370618ce723 a2081b9adfe4 fce46850ed8c 9937e2e3d17b e28a443bd153 473db8bebb8f e28a443bd153 4595a82f5a90 e28a443bd153 e28a443bd153 4595a82f5a90 4595a82f5a90 4595a82f5a90 4595a82f5a90 4595a82f5a90 4595a82f5a90 4595a82f5a90 4595a82f5a90 6ce00faec207 a2081b9adfe4 a2081b9adfe4 a2081b9adfe4 dc474426845d a2081b9adfe4 6ce00faec207 dc474426845d dc474426845d be016cd5e5c5 6ce00faec207 dc474426845d dc474426845d fce46850ed8c fce46850ed8c fce46850ed8c fce46850ed8c 6ce00faec207 be016cd5e5c5 a2081b9adfe4 a2081b9adfe4 a2081b9adfe4 dc474426845d a2081b9adfe4 dc474426845d fce46850ed8c a2081b9adfe4 aa45e5379c5a aa45e5379c5a aa45e5379c5a aa45e5379c5a aa45e5379c5a aa45e5379c5a aa45e5379c5a aa45e5379c5a aa45e5379c5a aa45e5379c5a aa45e5379c5a aa45e5379c5a 4595a82f5a90 aa45e5379c5a aa45e5379c5a aa45e5379c5a aa45e5379c5a e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 a38414bd3929 5939fce98fad 5939fce98fad a38414bd3929 5939fce98fad b5ee7aa9341a 5939fce98fad a38414bd3929 a38414bd3929 a38414bd3929 b5ee7aa9341a b5ee7aa9341a b5ee7aa9341a b5ee7aa9341a b5ee7aa9341a b5ee7aa9341a b5ee7aa9341a b5ee7aa9341a 5939fce98fad 5939fce98fad 5939fce98fad 5939fce98fad b5ee7aa9341a 5939fce98fad 5939fce98fad b5ee7aa9341a 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b dc474426845d dc474426845d 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b b5ee7aa9341a 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b dfb23566a91f dfb23566a91f dfb23566a91f dfb23566a91f a2081b9adfe4 9937e2e3d17b dc474426845d a2081b9adfe4 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b c5e44bab7c9a 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d dc474426845d e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 aa45e5379c5a aa45e5379c5a 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b 9937e2e3d17b aa45e5379c5a 9937e2e3d17b 9937e2e3d17b e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 a4632a7b2e17 a4632a7b2e17 a4632a7b2e17 e28a443bd153 5939fce98fad a2081b9adfe4 5939fce98fad 9937e2e3d17b 9937e2e3d17b aa45e5379c5a aa45e5379c5a 9937e2e3d17b e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 e28a443bd153 | #!bin/python
from __future__ import division
from run_local import log
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks, returnValue
import cyclone.web, cyclone.websocket, cyclone.httpclient
import sys, optparse, logging, subprocess, json, time, traceback
from rdflib import URIRef, Literal
sys.path.append(".")
from light9 import networking, showconfig, Submaster, dmxclient
from light9.curvecalc.curve import CurveResource
from light9.effecteval.effect import EffectNode
from light9.greplin_cyclone import StatsForCyclone
from light9.namespaces import L9, RDF, RDFS
from light9.rdfdb.patch import Patch
from light9.rdfdb.syncedgraph import SyncedGraph
from greplin import scales
from lib.cycloneerr import PrettyErrorHandler
class EffectEdit(PrettyErrorHandler, cyclone.web.RequestHandler):
def get(self):
self.write(open("light9/effecteval/effect.html").read())
def delete(self):
graph = self.settings.graph
uri = URIRef(self.get_argument('uri'))
with graph.currentState(tripleFilter=(None, L9['effect'], uri)) as g:
song = ctx = list(g.subjects(L9['effect'], uri))[0]
self.settings.graph.patch(Patch(delQuads=[
(song, L9['effect'], uri, ctx),
]))
class SongEffects(PrettyErrorHandler, cyclone.web.RequestHandler):
def post(self):
song = URIRef(self.get_argument('uri'))
dropped = URIRef(self.get_argument('drop'))
ctx = song
graph = self.settings.graph
effect = graph.sequentialUri(song + "/effect-")
curve = graph.sequentialUri(song + "/curve-")
with graph.currentState(
tripleFilter=(dropped, RDFS.label, None)) as g:
droppedSubLabel = g.label(dropped)
cr = CurveResource(graph, curve)
cr.newCurve(ctx, label=Literal('sub %s' % droppedSubLabel))
cr.saveCurve()
graph.patch(Patch(addQuads=[
(song, L9['curve'], curve, ctx),
(song, L9['effect'], effect, ctx),
(effect, RDF.type, L9['Effect'], ctx),
(effect, L9['code'],
Literal('out = sub(%s, intensity=%s)' % (dropped.n3(), curve.n3())),
ctx),
]))
class SongEffectsUpdates(cyclone.websocket.WebSocketHandler):
def connectionMade(self, *args, **kwargs):
self.graph = self.settings.graph
self.graph.addHandler(self.updateClient)
def updateClient(self):
# todo: abort if client is gone
playlist = self.graph.value(showconfig.showUri(), L9['playList'])
songs = list(self.graph.items(playlist))
out = []
for s in songs:
out.append({'uri': s, 'label': self.graph.label(s)})
out[-1]['effects'] = [{'uri': uri} for uri in sorted(self.graph.objects(s, L9['effect']))]
self.sendMessage({'songs': out})
class EffectUpdates(cyclone.websocket.WebSocketHandler):
"""
stays alive for the life of the effect page
"""
def connectionMade(self, *args, **kwargs):
log.info("websocket opened")
self.uri = URIRef(self.get_argument('uri'))
self.sendMessage({'hello': repr(self)})
self.graph = self.settings.graph
self.graph.addHandler(self.updateClient)
def updateClient(self):
# todo: if client has dropped, abort and don't get any more
# graph updates
self.sendMessage({'code': self.graph.value(self.uri, L9['code'])})
def connectionLost(self, reason):
log.info("websocket closed")
def messageReceived(self, message):
log.info("got message %s" % message)
# write a patch back to the graph
class EffectEval(PrettyErrorHandler, cyclone.web.RequestHandler):
@inlineCallbacks
def get(self):
# return dmx list for that effect
uri = URIRef(self.get_argument('uri'))
response = yield cyclone.httpclient.fetch(
networking.musicPlayer.path('time'))
songTime = json.loads(response.body)['t']
node = EffectNode(self.settings.graph, uri)
outSub = node.eval(songTime)
self.write(json.dumps(outSub.get_dmx_list()))
# Completely not sure where the effect background loop should
# go. Another process could own it, and get this request repeatedly:
class SongEffectsEval(PrettyErrorHandler, cyclone.web.RequestHandler):
def get(self):
song = URIRef(self.get_argument('song'))
effects = effectsForSong(self.settings.graph, song)
raise NotImplementedError
self.write(maxDict(effectDmxDict(e) for e in effects))
# return dmx dict for all effects in the song, already combined
class EffectLoop(object):
"""maintains a collection of the current EffectNodes, gets time from
music player, sends dmx"""
def __init__(self, graph, stats):
self.graph, self.stats = graph, stats
self.currentSong = None
self.currentEffects = []
self.lastLogTime = 0
self.lastLogMsg = ""
self.graph.addHandler(self.setEffects)
self.period = 1 / 30
self.coastSecs = .3 # main reason to keep this low is to notice play/pause
self.songTimeFromRequest = 0
self.requestTime = 0 # unix sec for when we fetched songTime
reactor.callLater(self.period, self.sendLevels)
def setEffects(self):
self.currentEffects = []
if self.currentSong is None:
return
for effectUri in self.graph.objects(self.currentSong, L9['effect']):
self.currentEffects.append(EffectNode(self.graph, effectUri))
@inlineCallbacks
def getSongTime(self):
now = time.time()
if now - self.requestTime < self.coastSecs:
estimated = self.songTimeFromRequest
if self.currentSong is not None and self.currentPlaying:
estimated += now - self.requestTime
returnValue((estimated, self.currentSong))
else:
response = json.loads((yield cyclone.httpclient.fetch(
networking.musicPlayer.path('time'))).body)
self.requestTime = now
self.currentPlaying = response['playing']
self.songTimeFromRequest = response['t']
returnValue(
(response['t'], (response['song'] and URIRef(response['song']))))
@inlineCallbacks
def sendLevels(self):
t1 = time.time()
try:
with self.stats.sendLevels.time():
with self.stats.getMusic.time():
songTime, song = yield self.getSongTime()
if song != self.currentSong:
self.currentSong = song
# this may be piling on the handlers
self.graph.addHandler(self.setEffects)
if song is None:
return
outSubs = []
for e in self.currentEffects:
try:
outSubs.append(e.eval(songTime))
except Exception as exc:
log.error("effect %s: %s" % (e.uri, exc))
out = Submaster.sub_maxes(*outSubs)
self.logLevels(t1, out)
dmx = out.get_dmx_list()
with self.stats.writeDmx.time():
yield dmxclient.outputlevels(dmx, twisted=True)
elapsed = time.time() - t1
dt = max(0, self.period - elapsed)
except Exception:
self.stats.errors += 1
traceback.print_exc()
dt = 1
reactor.callLater(dt, self.sendLevels)
def logLevels(self, now, out):
# this would look nice on the top of the effecteval web pages too
if log.isEnabledFor(logging.DEBUG):
log.debug(self.logMessage(out))
else:
if now > self.lastLogTime + 5:
msg = self.logMessage(out)
if msg != self.lastLogMsg:
log.info(msg)
self.lastLogMsg = msg
self.lastLogTime = now
def logMessage(self, out):
return ("send dmx: {%s}" %
", ".join("%r: %.3g" % (str(k), v)
for k,v in out.get_levels().items()))
class App(object):
def __init__(self, show):
self.show = show
self.graph = SyncedGraph("effectEval")
self.graph.initiallySynced.addCallback(self.launch)
self.stats = scales.collection('/',
scales.PmfStat('sendLevels'),
scales.PmfStat('getMusic'),
scales.PmfStat('writeDmx'),
scales.IntStat('errors'),
)
def launch(self, *args):
self.loop = EffectLoop(self.graph, self.stats)
SFH = cyclone.web.StaticFileHandler
self.cycloneApp = cyclone.web.Application(handlers=[
(r'/()', SFH,
{'path': 'light9/effecteval', 'default_filename': 'index.html'}),
(r'/effect', EffectEdit),
(r'/(websocket\.js)', SFH, {'path': 'light9/rdfdb/web/'}),
(r'/effect\.js', StaticCoffee, {'src': 'light9/effecteval/effect.coffee'}),
(r'/index\.js', StaticCoffee, {'src': 'light9/effecteval/index.coffee'}),
(r'/effectUpdates', EffectUpdates),
(r'/songEffectsUpdates', SongEffectsUpdates),
(r'/static/(.*)', SFH, {'path': 'static/'}),
(r'/effect/eval', EffectEval),
(r'/songEffects', SongEffects),
(r'/songEffects/eval', SongEffectsEval),
(r'/stats', StatsForCyclone),
], debug=True, graph=self.graph, stats=self.stats)
reactor.listenTCP(networking.effectEval.port, self.cycloneApp)
log.info("listening on %s" % networking.effectEval.port)
class StaticCoffee(PrettyErrorHandler, cyclone.web.RequestHandler):
def initialize(self, src):
super(StaticCoffee, self).initialize()
self.src = src
def get(self):
self.set_header('Content-Type', 'application/javascript')
self.write(subprocess.check_output([
'/usr/bin/coffee', '--compile', '--print', self.src]))
if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option('--show',
help='show URI, like http://light9.bigasterisk.com/show/dance2008',
default=showconfig.showUri())
parser.add_option("-v", "--verbose", action="store_true",
help="logging.DEBUG")
parser.add_option("--twistedlog", action="store_true",
help="twisted logging")
(options, args) = parser.parse_args()
log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
if not options.show:
raise ValueError("missing --show http://...")
app = App(URIRef(options.show))
if options.twistedlog:
from twisted.python import log as twlog
twlog.startLogging(sys.stderr)
reactor.run()
|