Files
@ 104ff4606565
Branch filter:
Location: light9/light9/effect/edit.py
104ff4606565
3.7 KiB
text/x-python
cleanup. internal names in edit.py
Ignore-this: a27582c2365bed1cd24a775227a02e24
Ignore-this: a27582c2365bed1cd24a775227a02e24
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 | import json
import cyclone.httpclient
from twisted.internet.defer import inlineCallbacks, returnValue
from rdflib import URIRef, Literal
from light9 import networking
from light9.namespaces import L9, RDF, RDFS
from light9.rdfdb.patch import Patch
from light9.curvecalc.curve import CurveResource
def clamp(x, lo, hi):
return max(lo, min(hi, x))
@inlineCallbacks
def getMusicStatus():
returnValue(json.loads((yield cyclone.httpclient.fetch(
networking.musicPlayer.path('time'), timeout=.5)).body))
@inlineCallbacks
def songEffectPatch(graph, dropped, song, event, ctx):
with graph.currentState(
tripleFilter=(dropped, None, None)) as g:
droppedTypes = list(g.objects(dropped, RDF.type))
droppedLabel = g.label(dropped)
droppedCodes = list(g.objects(dropped, L9['code']))
quads = []
fade = 2 if event == 'default' else 0
if _songHasEffect(graph, song, dropped):
# bump the existing curve
pass
else:
effect, q = _newEffect(graph, song, ctx)
quads.extend(q)
curve = graph.sequentialUri(song + "/curve-")
yield _newEnvelopeCurve(graph, ctx, curve, droppedLabel, fade)
quads.extend([
(song, L9['curve'], curve, ctx),
(effect, RDFS.label, droppedLabel, ctx),
(effect, L9['code'], Literal('env = %s' % curve.n3()), ctx),
])
if L9['EffectClass'] in droppedTypes:
quads.extend([
(effect, RDF.type, dropped, ctx),
] + [(effect, L9['code'], c, ctx) for c in droppedCodes])
elif L9['Submaster'] in droppedTypes:
quads.extend([
(effect, L9['code'], Literal('out = %s * env' % dropped.n3()),
ctx),
])
else:
raise NotImplementedError(
"don't know how to add an effect from %r (types=%r)" %
(dropped, droppedTypes))
_maybeAddMusicLine(quads, effect, song, ctx)
print "adding"
for qq in quads:
print qq
returnValue(Patch(addQuads=quads))
def _songHasEffect(graph, song, uri):
"""does this song have an effect of class uri or a sub curve for sub
uri? this should be simpler to look up."""
return False # todo
def musicCurveForSong(uri):
return URIRef(uri + 'music')
def _newEffect(graph, song, ctx):
effect = graph.sequentialUri(song + "/effect-")
quads = [
(song, L9['effect'], effect, ctx),
(effect, RDF.type, L9['Effect'], ctx),
]
print "_newEffect", effect, quads
return effect, quads
@inlineCallbacks
def _newEnvelopeCurve(graph, ctx, uri, label, fade=2):
"""this does its own patch to the graph"""
cr = CurveResource(graph, uri)
cr.newCurve(ctx, label=Literal(label))
yield _insertEnvelopePoints(cr.curve, fade)
cr.saveCurve()
@inlineCallbacks
def _insertEnvelopePoints(curve, fade=2):
# wrong: we might not be adding to the currently-playing song.
musicStatus = yield getMusicStatus()
songTime=musicStatus['t']
songDuration=musicStatus['duration']
t1 = clamp(songTime - fade, .1, songDuration - .1 * 2) + fade
t2 = clamp(songTime + 20, t1 + .1, songDuration)
curve.insert_pt((t1 - fade, 0))
curve.insert_pt((t1, 1))
curve.insert_pt((t2, 1))
curve.insert_pt((t2 + fade, 0))
def _maybeAddMusicLine(quads, effect, song, ctx):
"""
add a line getting the current music into 'music' if any code might
be mentioning that var
"""
for spoc in quads:
if spoc[1] == L9['code'] and 'music' in spoc[2]:
quads.extend([
(effect, L9['code'],
Literal('music = %s' % musicCurveForSong(song).n3()), ctx)
])
break
|