Files
@ c756638275d6
Branch filter:
Location: light9/light9/curvecalc/curve.py
c756638275d6
12.7 KiB
text/x-python
quneo input demo. optimize curve display a little.
Ignore-this: 4cf5b4b5853a94842c9fa8e2916bc6f4
Ignore-this: 4cf5b4b5853a94842c9fa8e2916bc6f4
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | from __future__ import division
import glob, time, logging, ast, os
from bisect import bisect_left,bisect
import louie as dispatcher
from rdflib import Literal
from light9 import showconfig
from light9.namespaces import L9, RDF, RDFS
from light9.rdfdb.patch import Patch
from bcf2000 import BCF2000
log = logging.getLogger()
# todo: move to config, consolidate with ascoltami, musicPad, etc
introPad = 4
postPad = 4
class Curve(object):
"""curve does not know its name. see Curveset"""
def __init__(self, uri, pointsStorage='graph'):
self.uri = uri
self.pointsStorage = pointsStorage
self.points = [] # x-sorted list of (x,y)
self._muted = False
def __repr__(self):
return "<%s (%s points)>" % (self.__class__.__name__, len(self.points))
def muted():
doc = "Whether to currently send levels (boolean, obviously)"
def fget(self):
return self._muted
def fset(self, val):
self._muted = val
dispatcher.send('mute changed', sender=self)
return locals()
muted = property(**muted())
def toggleMute(self):
self.muted = not self.muted
def load(self,filename):
self.points[:]=[]
for line in file(filename):
x, y = line.split()
self.points.append((float(x), ast.literal_eval(y)))
self.points.sort()
dispatcher.send("points changed",sender=self)
def set_from_string(self, pts):
self.points[:] = []
vals = pts.split()
pairs = zip(vals[0::2], vals[1::2])
for x, y in pairs:
self.points.append((float(x), ast.literal_eval(y)))
self.points.sort()
dispatcher.send("points changed",sender=self)
def points_as_string(self):
def outVal(x):
if isinstance(x, basestring): # markers
return x
return "%.4g" % x
return ' '.join("%s %s" % (outVal(p[0]), outVal(p[1]))
for p in self.points)
def save(self,filename):
# this is just around for markers, now
if filename.endswith('-music') or filename.endswith('_music'):
print "not saving music track"
return
f = file(filename,'w')
for p in self.points:
f.write("%s %r\n" % p)
f.close()
def eval(self, t, allow_muting=True):
if self.muted and allow_muting:
return 0
if not self.points:
raise ValueError("curve has no points")
i = bisect_left(self.points,(t,None))-1
if i == -1:
return self.points[0][1]
if self.points[i][0]>t:
return self.points[i][1]
if i>=len(self.points)-1:
return self.points[i][1]
p1,p2 = self.points[i],self.points[i+1]
frac = (t-p1[0])/(p2[0]-p1[0])
y = p1[1]+(p2[1]-p1[1])*frac
return y
__call__=eval
def insert_pt(self, new_pt):
"""returns index of new point"""
i = bisect(self.points, (new_pt[0],None))
self.points.insert(i,new_pt)
# missing a check that this isn't the same X as the neighbor point
return i
def live_input_point(self, new_pt, clear_ahead_secs=.01):
x, y = new_pt
exist = self.points_between(x, x + clear_ahead_secs)
for pt in exist:
self.remove_point(pt)
self.insert_pt(new_pt)
dispatcher.send("points changed", sender=self)
# now simplify to the left
def set_points(self, updates):
for i, pt in updates:
self.points[i] = pt
x = None
for p in self.points:
if p[0] <= x:
raise ValueError("overlapping points")
x = p[0]
def pop_point(self, i):
return self.points.pop(i)
def remove_point(self, pt):
self.points.remove(pt)
def indices_between(self, x1, x2, beyond=0):
leftidx = max(0, bisect(self.points, (x1,None)) - beyond)
rightidx = min(len(self.points),
bisect(self.points, (x2,None)) + beyond)
return range(leftidx, rightidx)
def points_between(self, x1, x2):
"""returns (x,y) points"""
return [self.points[i] for i in self.indices_between(x1,x2)]
def point_before(self, x):
"""(x,y) of the point left of x, or None"""
leftidx = self.index_before(x)
if leftidx is None:
return None
return self.points[leftidx]
def index_before(self, x):
leftidx = bisect(self.points, (x,None)) - 1
if leftidx < 0:
return None
return leftidx
class Markers(Curve):
"""Marker is like a point but the y value is a string"""
def eval(self):
raise NotImplementedError()
def slope(p1,p2):
if p2[0] == p1[0]:
return 0
return (p2[1] - p1[1]) / (p2[0] - p1[0])
class Sliders(BCF2000):
def __init__(self, cb, knobCallback, knobButtonCallback):
BCF2000.__init__(self)
self.cb = cb
self.knobCallback = knobCallback
self.knobButtonCallback = knobButtonCallback
def valueIn(self, name, value):
if name.startswith("slider"):
self.cb(int(name[6:]), value / 127)
if name.startswith("knob"):
self.knobCallback(int(name[4:]), value / 127)
if name.startswith("button-knob"):
self.knobButtonCallback(int(name[11:]))
class Curveset(object):
curves = None # curvename : curve
def __init__(self, graph, session, sliders=False):
"""sliders=True means support the hardware sliders"""
self.graph, self.session = graph, session
self.currentSong = None
self.curves = {} # name (str) : Curve
self.curveName = {} # reverse
self.sliderCurve = {} # slider number (1 based) : curve name
self.sliderNum = {} # reverse
if sliders:
self.sliders = Sliders(self.hw_slider_in, self.hw_knob_in,
self.hw_knob_button)
dispatcher.connect(self.curvesToSliders, "curves to sliders")
dispatcher.connect(self.knobOut, "knob out")
self.lastSliderTime = {} # num : time
self.sliderSuppressOutputUntil = {} # num : time
self.sliderIgnoreInputUntil = {}
else:
self.sliders = None
self.markers = Markers(uri=None, pointsStorage='file')
graph.addHandler(self.loadCurvesForSong)
def curveFromUri(self, uri):
# self.curves should be indexed by this
for c in self.curves.values():
if c.uri == uri:
return c
raise KeyError("no curve %s" % uri)
def loadCurvesForSong(self):
"""
current curves will track song's curves.
This fires 'add_curve' dispatcher events to announce the new curves.
"""
log.info('loadCurvesForSong')
dispatcher.send("clear_curves")
self.curves.clear()
self.curveName.clear()
self.sliderCurve.clear()
self.sliderNum.clear()
self.markers = Markers(uri=None, pointsStorage='file')
self.currentSong = self.graph.value(self.session, L9['currentSong'])
if self.currentSong is None:
return
for uri in sorted(self.graph.objects(self.currentSong, L9['curve'])):
pts = self.graph.value(uri, L9['points'])
c = Curve(uri, pointsStorage='file' if pts is None else 'graph')
if pts is not None:
c.set_from_string(pts)
else:
diskPts = self.graph.value(uri, L9['pointsFile'])
c.load(os.path.join(showconfig.curvesDir(), diskPts))
curvename = self.graph.label(uri)
if not curvename:
raise ValueError("curve %r has no label" % uri)
self.add_curve(curvename, c)
basename = os.path.join(
showconfig.curvesDir(),
showconfig.songFilenameFromURI(self.currentSong))
try:
self.markers.load("%s.markers" % basename)
except IOError:
print "no marker file found"
def save(self):
"""writes a file for each curve with a name
like basename-curvename, or saves them to the rdf graph"""
basename=os.path.join(
showconfig.curvesDir(),
showconfig.songFilenameFromURI(self.currentSong))
patches = []
for label, curve in self.curves.items():
if curve.pointsStorage == 'file':
log.warn("not saving file curves anymore- skipping %s" % label)
#cur.save("%s-%s" % (basename,name))
elif curve.pointsStorage == 'graph':
ctx = self.currentSong
patches.append(self.graph.getObjectPatch(
ctx,
subject=curve.uri,
predicate=L9['points'],
newObject=Literal(curve.points_as_string())))
else:
raise NotImplementedError(curve.pointsStorage)
self.markers.save("%s.markers" % basename)
# this will cause reloads that will clear our curve list
for p in patches:
self.graph.patch(p)
def sorter(self, name):
return self.curves[name].uri
def curveNamesInOrder(self):
return sorted(self.curves.keys(), key=self.sorter)
def add_curve(self, name, curve):
# should be indexing by uri
if isinstance(name, Literal):
name = str(name)
if name in self.curves:
raise ValueError("can't add a second curve named %r" % name)
self.curves[name] = curve
self.curveName[curve] = name
if self.sliders and name not in ['smooth_music', 'music']:
num = len(self.sliderCurve) + 1
if num <= 8:
self.sliderCurve[num] = name
self.sliderNum[name] = num
else:
num = None
else:
num = None
dispatcher.send("add_curve", slider=num, knobEnabled=num is not None,
sender=self, name=name)
def globalsdict(self):
return self.curves.copy()
def get_time_range(self):
return 0, dispatcher.send("get max time")[0][1]
def new_curve(self, name, renameIfExisting=True):
if isinstance(name, Literal):
name = str(name)
if name=="":
print "no name given"
return
if not renameIfExisting and name in self.curves:
return
while name in self.curves:
name=name+"-1"
uri = self.currentSong + ('/curve/c-%f' % time.time())
c = Curve(uri)
s, e = self.get_time_range()
c.points.extend([(s, 0), (e, 0)])
ctx = self.currentSong
self.graph.patch(Patch(addQuads=[
(self.currentSong, L9['curve'], uri, ctx),
(uri, RDF.type, L9['Curve'], ctx),
(uri, RDFS.label, Literal(name), ctx),
(uri, L9['points'], Literal(c.points_as_string()), ctx),
]))
def hw_slider_in(self, num, value):
try:
curve = self.curves[self.sliderCurve[num]]
except KeyError:
return
now = time.time()
if now < self.sliderIgnoreInputUntil.get(num):
return
# don't make points too fast. This is the minimum spacing
# between slider-generated points.
self.sliderIgnoreInputUntil[num] = now + .1
# don't push back on the slider for a little while, since the
# user might be trying to slowly move it. This should be
# bigger than the ignore time above.
self.sliderSuppressOutputUntil[num] = now + .2
dispatcher.send("set key", curve=curve, value=value)
def hw_knob_in(self, num, value):
try:
curve = self.curves[self.sliderCurve[num]]
except KeyError:
return
dispatcher.send("knob in", curve=curve, value=value)
def hw_knob_button(self, num):
try:
curve = self.curves[self.sliderCurve[num]]
except KeyError:
return
dispatcher.send("set key", curve=curve)
def curvesToSliders(self, t):
now = time.time()
for num, name in self.sliderCurve.items():
if now < self.sliderSuppressOutputUntil.get(num):
continue
# self.lastSliderTime[num] = now
value = self.curves[name].eval(t)
self.sliders.valueOut("slider%s" % num, value * 127)
def knobOut(self, curve, value):
try:
num = self.sliderNum[self.curveName[curve]]
except KeyError:
return
self.sliders.valueOut("knob%s" % num, value * 127)
|