Files
@ a18eb09762b7
Branch filter:
Location: light9/light9/effect/edit.py
a18eb09762b7
6.3 KiB
text/x-python
move rdfdb to a new project
Ignore-this: 8ce04a43895d1516cd7c14421840eeaf
Ignore-this: 8ce04a43895d1516cd7c14421840eeaf
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 | 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):
"""
some uri was 'dropped' in the curvecalc timeline. event is 'default' or 'start' or 'end'.
"""
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))
@inlineCallbacks
def songNotePatch(graph, dropped, song, event, ctx, note=None):
"""
drop into effectsequencer timeline
ported from timeline.coffee makeNewNote
"""
with graph.currentState(
tripleFilter=(dropped, None, None)) as g:
droppedTypes = list(g.objects(dropped, RDF.type))
quads = []
fade = 2 if event == 'default' else 0.1
if note:
musicStatus = yield getMusicStatus()
songTime = musicStatus['t']
_finishCurve(graph, note, quads, ctx, songTime)
else:
if L9['Effect'] in droppedTypes:
musicStatus = yield getMusicStatus()
songTime = musicStatus['t']
note = _makeNote(graph, song, note, quads, ctx, dropped, songTime, event, fade)
else:
raise NotImplementedError
returnValue((note, Patch(addQuads=quads)))
def _point(ctx, uri, t, v):
return [
(uri, L9['time'], Literal(round(t, 3)), ctx),
(uri, L9['value'], Literal(round(v, 3)), ctx)
]
def _finishCurve(graph, note, quads, ctx, songTime):
with graph.currentState() as g:
origin = g.value(note, L9['originTime']).toPython()
curve = g.value(note, L9['curve'])
pt2 = graph.sequentialUri(curve + 'p')
pt3 = graph.sequentialUri(curve + 'p')
quads.extend(
[(curve, L9['point'], pt2, ctx)] + _point(ctx, pt2, songTime - origin, 1) +
[(curve, L9['point'], pt3, ctx)] + _point(ctx, pt3, songTime - origin + .5, 0)
)
def _makeNote(graph, song, note, quads, ctx, dropped, songTime, event, fade):
note = graph.sequentialUri(song + '/n')
curve = graph.sequentialUri(note + 'c')
quads.extend([
(song, L9['note'], note, ctx),
(note, RDF.type, L9['Note'], ctx),
(note, L9['curve'], curve, ctx),
(note, L9['effectClass'], dropped, ctx),
(note, L9['originTime'], Literal(songTime), ctx),
(curve, RDF.type, L9['Curve'], ctx),
(curve, L9['attr'], L9['strength'], ctx),
])
if event == 'default':
coords = [(0 - fade, 0), (0, 1), (20, 1), (20 + fade, 0)]
elif event == 'start':
coords = [(0 - fade, 0), (0, 1), ]
elif event == 'end': # probably unused- goes to _finishCurve instead
coords = [(20, 1), (20 + fade, 0)]
else:
raise NotImplementedError(event)
for t,v in coords:
pt = graph.sequentialUri(curve + 'p')
quads.extend([(curve, L9['point'], pt, ctx)] + _point(ctx, pt, t, v))
return note
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
|