Files
@ 454e381cd24f
Branch filter:
Location: light9/bin/curvecalc - annotation
454e381cd24f
22.0 KiB
text/plain
curvecalc refactor and module fixes
Ignore-this: afb03eba0af8d2daa7f03d4cace00d3e
Ignore-this: afb03eba0af8d2daa7f03d4cace00d3e
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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | a301a0039c66 f41004d5a507 f41004d5a507 a6662d61ebcd a6662d61ebcd a6662d61ebcd a6662d61ebcd a6662d61ebcd f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 54c863b2553e cbc98de2ab06 d4aff817a304 d4aff817a304 d4aff817a304 d4aff817a304 f41004d5a507 454e381cd24f f41004d5a507 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 9ed6c40b3d6a a301a0039c66 b2858c2d4a4d 2193eab0650b 2193eab0650b f41004d5a507 f41004d5a507 40b6a06bd090 cbc98de2ab06 f41004d5a507 454e381cd24f 454e381cd24f 0f112a7dd6b3 0f112a7dd6b3 8a1ec8aca432 1c590824dd14 1c590824dd14 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 454e381cd24f 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 f41004d5a507 f41004d5a507 f41004d5a507 8d6f6d8a4719 5322639d61e9 54c863b2553e f41004d5a507 f41004d5a507 54c863b2553e 54c863b2553e 54c863b2553e 8d6f6d8a4719 f41004d5a507 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 8d6f6d8a4719 e7630a2072bd e7630a2072bd 8d6f6d8a4719 46d319974176 454e381cd24f 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 f41004d5a507 1c590824dd14 40b6a06bd090 1c590824dd14 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d f41004d5a507 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 b2858c2d4a4d b2858c2d4a4d 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 40b6a06bd090 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 a65a1634fce4 a65a1634fce4 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 a65a1634fce4 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 9bf9685f5aae 9bf9685f5aae 9bf9685f5aae 9bf9685f5aae e99bd20dad85 e99bd20dad85 e99bd20dad85 e99bd20dad85 e99bd20dad85 e99bd20dad85 e99bd20dad85 e99bd20dad85 e99bd20dad85 e99bd20dad85 e99bd20dad85 e99bd20dad85 e99bd20dad85 f41004d5a507 73b181155555 73b181155555 73b181155555 f41004d5a507 415c206f7534 f41004d5a507 f41004d5a507 b50e4d43dd3e f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 cbc98de2ab06 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 cbc98de2ab06 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 a3267d8c498e a3267d8c498e a3267d8c498e a3267d8c498e 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 b8012dcb714b 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 5322639d61e9 f41004d5a507 fc87327e29c4 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 73b181155555 73b181155555 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 f41004d5a507 63601fe0c3b0 f41004d5a507 73b181155555 f41004d5a507 f41004d5a507 f41004d5a507 73b181155555 f41004d5a507 73b181155555 415c206f7534 b50e4d43dd3e b50e4d43dd3e b50e4d43dd3e b50e4d43dd3e b50e4d43dd3e b50e4d43dd3e 5322639d61e9 5322639d61e9 5322639d61e9 b50e4d43dd3e 415c206f7534 f41004d5a507 f41004d5a507 cbc98de2ab06 cbc98de2ab06 f41004d5a507 f41004d5a507 f41004d5a507 73b181155555 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 f41004d5a507 f41004d5a507 f41004d5a507 c5f0b530ade5 9ed6c40b3d6a 9ed6c40b3d6a 9ed6c40b3d6a 9ed6c40b3d6a 73b181155555 c5f0b530ade5 c5f0b530ade5 e3362ad9a123 f41004d5a507 5bfcf309e1ad 5bfcf309e1ad 5bfcf309e1ad e3362ad9a123 e3362ad9a123 0e3743492eae d482699decbd d482699decbd c5f0b530ade5 d482699decbd 0e3743492eae 0e3743492eae 0e3743492eae 0e3743492eae 0e3743492eae f41004d5a507 5322639d61e9 7771f37252da 7771f37252da 7771f37252da 7771f37252da 7771f37252da 7771f37252da 7771f37252da 7771f37252da 7771f37252da 7771f37252da 7771f37252da 7771f37252da b50e4d43dd3e 7771f37252da 7771f37252da 7771f37252da 9ed6c40b3d6a 7771f37252da 7771f37252da 7771f37252da 7771f37252da 73b181155555 8a1ec8aca432 0cf00fffd921 b2858c2d4a4d 73b181155555 73b181155555 b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d b2858c2d4a4d 73b181155555 b2858c2d4a4d 5322639d61e9 7771f37252da 7771f37252da a6662d61ebcd ad17b0a09f5c 415c206f7534 415c206f7534 415c206f7534 415c206f7534 415c206f7534 83c239d3fd33 415c206f7534 415c206f7534 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 f41004d5a507 cbc98de2ab06 cbc98de2ab06 7771f37252da 7771f37252da 7771f37252da cbc98de2ab06 cbc98de2ab06 f41004d5a507 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 c113c759559b 7ee76a895427 7ee76a895427 7ee76a895427 7ee76a895427 7ee76a895427 7ee76a895427 7ee76a895427 7ee76a895427 7ee76a895427 7ee76a895427 7ee76a895427 7ee76a895427 c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b c113c759559b 7ee76a895427 cbc98de2ab06 c113c759559b c113c759559b cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 ad5043f70fda ad5043f70fda 454e381cd24f 454e381cd24f ad5043f70fda ad5043f70fda ad5043f70fda cbc98de2ab06 cbc98de2ab06 ad17b0a09f5c cbc98de2ab06 ad5043f70fda cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 ad17b0a09f5c ad17b0a09f5c ad17b0a09f5c ad17b0a09f5c cbc98de2ab06 cbc98de2ab06 0cf00fffd921 ad17b0a09f5c 0cf00fffd921 ad17b0a09f5c cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 ad5043f70fda cbc98de2ab06 cbc98de2ab06 73b181155555 cbc98de2ab06 73b181155555 cbc98de2ab06 af5539fe35a7 af5539fe35a7 af5539fe35a7 af5539fe35a7 af5539fe35a7 af5539fe35a7 af5539fe35a7 73b181155555 ad17b0a09f5c 73b181155555 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 7ee76a895427 7ee76a895427 7ee76a895427 7ee76a895427 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 f2dbb0b1fb35 54341e5e8d82 f2dbb0b1fb35 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 73b181155555 c113c759559b 73b181155555 73b181155555 73b181155555 cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 73b181155555 73b181155555 73b181155555 f2dbb0b1fb35 f2dbb0b1fb35 f2dbb0b1fb35 f2dbb0b1fb35 f2dbb0b1fb35 cbc98de2ab06 f15ffbfc5cd6 ad17b0a09f5c ad17b0a09f5c ad17b0a09f5c ad17b0a09f5c cbc98de2ab06 cbc98de2ab06 cbc98de2ab06 | #!bin/python
"""
now launches like this:
% bin/curvecalc http://light9.bigasterisk.com/show/dance2007/song1
todo: curveview should preserve more objects, for speed maybe
"""
from __future__ import division
import time,textwrap,math,random,os,optparse, urllib2
import Tix as tk
try:
from dispatch import dispatcher
except ImportError:
import louie as dispatcher
from twisted.internet import reactor,tksupport
import json, restkit
import twisted
from twisted.web.client import Agent
from twisted.internet.protocol import Protocol
from twisted.internet.defer import Deferred
from zope.interface import implements
from twisted.internet.defer import succeed
from twisted.web.iweb import IBodyProducer
from rdflib import Literal, URIRef, RDF, RDFS
from rdflib import Graph
import rdflib
import logging
log = logging.getLogger()
import run_local
from light9 import Submaster, dmxclient, networking, showconfig, prof, Patch
from light9.TLUtility import make_attributes_from_args
from light9.zoomcontrol import Zoomcontrol
from light9.curve import Curveset
from light9.curveview import Curvesetview
from light9.wavelength import wavelength
from light9.uihelpers import toplevelat
from light9.namespaces import L9
import light9.Effects
class GatherJson(Protocol):
"""calls back the 'finished' deferred with the parsed json data we
received"""
def __init__(self, finished):
self.finished = finished
self.buf = ""
def dataReceived(self, bytes):
self.buf += bytes
def connectionLost(self, reason):
self.finished.callback(json.loads(self.buf))
class StringProducer(object):
# http://twistedmatrix.com/documents/current/web/howto/client.html
implements(IBodyProducer)
def __init__(self, body):
self.body = body
self.length = len(body)
def startProducing(self, consumer):
consumer.write(self.body)
return succeed(None)
def pauseProducing(self):
pass
def stopProducing(self):
pass
class Music:
def __init__(self):
self.recenttime=0
self.player = Agent(reactor)
dispatcher.connect(self.seekplay_or_pause,"music seek")
self.timePath = networking.musicPlayer.path("time")
def current_time(self):
"""return deferred which gets called with the current
time. This gets called really often"""
d = self.player.request("GET", self.timePath)
d.addCallback(self._timeReturned)
return d
def _timeReturned(self, response):
done = Deferred()
done.addCallback(self._bodyReceived)
response.deliverBody(GatherJson(done))
return done
def _bodyReceived(self, data):
dispatcher.send("input time",val=data['t'])
return data['t'] # pass along to the real receiver
def seekplay_or_pause(self,t):
d = self.player.request("POST",
networking.musicPlayer.path("seekPlayOrPause"),
bodyProducer=StringProducer(json.dumps({"t" : t})))
class Expr(object):
"""singleton, provides functions for use in subterm expressions,
e.g. chases"""
def __init__(self):
self.effectGlobals = light9.Effects.configExprGlobals()
def exprGlobals(self, startDict, t):
"""globals dict for use by expressions"""
glo = startDict.copy()
# add in functions from Effects
glo.update(self.effectGlobals)
glo['nsin'] = lambda x: (math.sin(x * (2 * math.pi)) + 1) / 2
glo['ncos'] = lambda x: (math.cos(x * (2 * math.pi)) + 1) / 2
glo['within'] = lambda a, b: a < t < b
glo['bef'] = lambda x: t < x
def smoove(x):
return -2 * (x ** 3) + 3 * (x ** 2)
glo['smoove'] = smoove
def aft(t, x, smooth=0):
left = x - smooth / 2
right = x + smooth / 2
if left < t < right:
return smoove((t - left) / (right - left))
return t > x
glo['aft'] = lambda x, smooth=0: aft(t, x, smooth)
def chan(name):
return Submaster.Submaster(
leveldict={Patch.get_dmx_channel(name) : 1.0},
temporary=True)
glo['chan'] = chan
def smooth_random(speed=1):
"""1 = new stuff each second, <1 is slower, fade-ier"""
x = (t * speed) % len(self._smooth_random_items)
x1 = int(x)
x2 = (int(x) + 1) % len(self._smooth_random_items)
y1 = self._smooth_random_items[x1]
y2 = self._smooth_random_items[x2]
return y1 + (y2 - y1) * ((x - x1))
def notch_random(speed=1):
"""1 = new stuff each second, <1 is slower, notch-ier"""
x = (t * speed) % len(self._smooth_random_items)
x1 = int(x)
y1 = self._smooth_random_items[x1]
return y1
glo['noise'] = smooth_random
glo['notch'] = notch_random
return glo
exprglo = Expr()
class Subexpr:
curveset = None
def __init__(self,curveset,expr=""):
self.curveset = curveset
self.lasteval = None
self.expr=expr
self._smooth_random_items = [random.random() for x in range(100)]
def eval(self,t):
if self.expr=="":
dispatcher.send("expr_error",sender=self,exc="no expr, using 0")
return 0
glo = self.curveset.globalsdict()
glo['t'] = t
glo = exprglo.exprGlobals(glo, t)
try:
self.lasteval = eval(self.expr,glo)
except Exception,e:
dispatcher.send("expr_error",sender=self,exc=e)
else:
dispatcher.send("expr_error",sender=self,exc="ok")
return self.lasteval
def expr():
doc = "python expression for level as a function of t, using curves"
def fget(self):
return self._expr
def fset(self, value):
self._expr = value
dispatcher("expr_changed",sender=self)
return locals()
expr = property(**expr())
class Subexprview(tk.Frame):
def __init__(self,master,se,**kw):
self.subexpr=se
tk.Frame.__init__(self,master,**kw)
self.evar = tk.StringVar()
e = self.ent = tk.Entry(self,textvariable=self.evar)
e.pack(side='left',fill='x',exp=1)
self.expr_changed()
self.evar.trace_variable('w',self.evar_changed)
dispatcher.connect(self.expr_changed,"expr_changed",
sender=self.subexpr)
self.error = tk.Label(self)
self.error.pack(side='left')
dispatcher.connect(lambda exc: self.error.config(text=str(exc)),
"expr_error",sender=self.subexpr,weak=0)
def expr_changed(self):
if self.subexpr.expr!=self.evar.get():
self.evar.set(self.subexpr.expr)
def evar_changed(self,*args):
self.subexpr.expr = self.evar.get()
class Subterm:
"""one Submaster and its Subexpr"""
def __init__(self, submaster, subexpr):
make_attributes_from_args('submaster', 'subexpr')
def scaled(self, t):
subexpr_eval = self.subexpr.eval(t)
# we prevent any exceptions from escaping, since they cause us to
# stop sending levels
try:
if isinstance(subexpr_eval, Submaster.Submaster):
# if the expression returns a submaster, just return it
return subexpr_eval
else:
# otherwise, return our submaster multiplied by the value
# returned
return self.submaster * subexpr_eval
except Exception, e:
dispatcher.send("expr_error", sender=self.subexpr, exc=str(e))
return Submaster.Submaster('Error: %s' % str(e), temporary=True)
def __repr__(self):
return "<Subterm %s %s>" % (self.submaster, self.subexpr)
class Subtermview(tk.Frame):
def __init__(self, master, graph, st, **kw):
self.subterm = st
tk.Frame.__init__(self,master,bd=1,relief='raised',**kw)
l = tk.Label(self, text="sub %s" % self.subterm.submaster.name)
l.pack(side='left')
sev=Subexprview(self,self.subterm.subexpr)
sev.pack(side='left',fill='both',exp=1)
class Output:
lastsendtime=0
lastsendlevs=None
def __init__(self, subterms, music):
make_attributes_from_args('subterms','music')
self.recent_t=[]
self.later = None
self.update()
def update(self):
d = self.music.current_time()
d.addCallback(self.update2)
d.addErrback(self.updateerr)
def updateerr(self,e):
print e.getTraceback()
dispatcher.send("update status",val=e.getErrorMessage())
if self.later and not self.later.cancelled and not self.later.called:
self.later.cancel()
self.later = reactor.callLater(1,self.update)
def update2(self,t):
# spot alsa soundcard offset is always 0, we get times about a
# second ahead of what's really getting played
#t = t - .7
dispatcher.send("update status",
val="ok: receiving time from music player")
if self.later and not self.later.cancelled and not self.later.called:
self.later.cancel()
self.later = reactor.callLater(.02, self.update)
self.recent_t = self.recent_t[-50:]+[t]
period = (self.recent_t[-1] - self.recent_t[0]) / len(self.recent_t)
dispatcher.send("update period", val=period)
self.send_dmx(t)
def send_dmx(self,t):
dispatcher.send("curves to sliders", t=t)
scaledsubs=[]
for st in self.subterms:
scl = st.scaled(t)
scaledsubs.append(scl)
out = Submaster.sub_maxes(*scaledsubs)
levs = out.get_levels()
now=time.time()
if now-self.lastsendtime>5 or levs!=self.lastsendlevs:
dispatcher.send("output levels",val=levs)
dmxclient.outputlevels(out.get_dmx_list(),
twisted=1,clientid='curvecalc')
self.lastsendtime = now
self.lastsendlevs = levs
def makeStatusLines(master):
"""various labels that listen for dispatcher signals"""
for signame,textfilter in [
('input time',lambda t: "%.2fs"%t),
('output levels',
lambda levels: textwrap.fill("; ".join(["%s:%.2f"%(n,v)
for n,v in
levels.items()[:5]
if v>0]),70)),
('update period',lambda t: "%.1fms"%(t*1000)),
('update status',lambda t: str(t)),
]:
l = tk.Label(master, anchor='w', justify='left', text='%s:' % signame)
l.pack(side='top',fill='x')
dispatcher.connect(lambda val,l=l,sn=signame,tf=textfilter:
l.config(text=sn+": "+tf(val)),
signame, weak=False)
def add_one_subterm(graph, subUri, curveset, subterms, master, expr=None):
subname = graph.label(subUri)
print "%s's label is %s" % (subUri, subname)
if not subname: # fake sub, like for a chase
st = graph.subjects(L9['sub'], subUri).next()
subname = graph.label(st)
print "using parent subterm's name instead. parent %r, name %r" % (st, subname)
assert subname, "%s has no name" % subUri
if expr is None:
expr = '%s(t)' % subname
term = Subterm(Submaster.Submaster(graph=graph, name=subname, sub=subUri),
Subexpr(curveset,expr))
subterms.append(term)
stv=Subtermview(master, graph, term)
stv.pack(side='top',fill='x')
return term
def makeSubtermCommandRow(master, curveset, subterms, root, ssv, graph):
"""
the row that starts with 'reload subs' button
"""
f=tk.Frame(master,relief='raised',bd=1)
newname = tk.StringVar()
def add_cmd(evt):
uri = L9['sub/%s' % newname.get()]
graph.add((uri, RDF.type, L9.Subterm))
graph.add((uri, RDFS.label, Literal(newname.get())))
add_one_subterm(graph, uri,
curveset, subterms, ssv, None)
if evt.state & 4: # control key modifier
curveset.new_curve(newname.get())
newname.set('')
def reload_subs():
dispatcher.send('reload all subs')
tk.Button(f, text="reload subs (C-r)",
command=reload_subs).pack(side='left')
tk.Label(f, text="new subterm named (C-Enter for curve too, C-n for focus):").pack(side='left')
entry = tk.Entry(f, textvariable=newname)
entry.pack(side='left', fill='x', exp=1)
entry.bind("<Key-Return>", add_cmd)
def focus_entry():
entry.focus()
dispatcher.connect(focus_entry, "focus new subterm", weak=False)
return f
def savesubterms(filename,subterms):
raise NotImplementedError
s=""
for st in subterms:
s=s+"%s %s\n" % (st.submaster.name, st.subexpr.expr)
file(filename,'w').write(s)
def createSubtermGraph(song, subterms):
"""rdf graph describing the subterms, readable by add_subterms_for_song"""
graph = Graph()
for subterm in subterms:
assert subterm.submaster.name, "submaster has no name"
uri = URIRef(song + "/subterm/" + subterm.submaster.name)
graph.add((song, L9['subterm'], uri))
graph.add((uri, RDF.type, L9['Subterm']))
graph.add((uri, RDFS.label, Literal(subterm.submaster.name)))
graph.add((uri, L9['sub'], L9['sub/%s' % subterm.submaster.name]))
graph.add((uri, L9['expression'], Literal(subterm.subexpr.expr)))
return graph
def add_subterms_for_song(graph, song, curveset, subterms, master):
for st in graph.objects(song, L9['subterm']):
log.info("song %s has subterm %s", song, st)
try:
add_one_subterm(graph, graph.value(st, L9['sub']), curveset,
subterms, master, graph.value(st, L9['expression']))
except rdflib.exceptions.UniquenessError:
print "working around curvecalc save corruption"
# curvecalc put all the expressions on one subterm, which is wrong
for expr in graph.objects(st, L9['expression']):
add_one_subterm(graph, graph.value(st, L9['sub']),
curveset, subterms, master, expr)
def graphPathForSubterms(song):
return showconfig.subtermsForSong(showconfig.songFilenameFromURI(song)) + ".n3"
@prof.logTime
def read_all_subs(graph):
"""read all sub files into this graph so when add_one_subterm tries
to add, the sub will be available"""
subsDir = showconfig.subsDir()
for filename in os.listdir(subsDir):
# parsing nt is faster, but it should try n3 format if the parsing fails
graph.parse(os.path.join(subsDir, filename), format="n3")
def makeGraph():
graphOrig = showconfig.getGraph()
graph = Graph() # a copy, since we're going to add subs into it
for s in graphOrig:
graph.add(s)
read_all_subs(graph)
return graph
def savekey(song, subterms, curveset):
print "saving", song
g = createSubtermGraph(song, subterms)
g.serialize(graphPathForSubterms(song), format="nt")
curveset.save(basename=os.path.join(showconfig.curvesDir(),
showconfig.songFilenameFromURI(song)))
print "saved"
def setupKeyBindings(root, song, subterms, curveset):
root.bind("<Control-Key-s>",
lambda *args: savekey(song, subterms, curveset))
root.bind("<Control-Key-r>", lambda evt: dispatcher.send('reload all subs'))
root.bind("<Control-Key-n>",
lambda evt: dispatcher.send('focus new subterm'))
root.bind("<Control-Key-N>", lambda evt: dispatcher.send('focus new curve'))
root.bind("<Control-Key-q>",lambda ev: reactor.stop)
root.bind("<Destroy>",lambda ev: reactor.stop)
root.protocol('WM_DELETE_WINDOW', reactor.stop)
def setupMenubar(barFrame, root, song, subterms, curveset):
class newMenu(object):
def __init__(self, name):
self.name = name
def __enter__(self):
m = tk.Menubutton(barFrame, text=self.name)
m.pack(side='left')
mm = tk.Menu(m)
m.config(menu=mm)
return mm
def __exit__(self, type, value, traceback):
return False
def notImpl(*args):
print "sorry, menu command binding isn't done yet. Use the keyboard shortcut"
with newMenu("Curvecalc") as m:
m.add_command(label='Save', underline=0, accelerator="Ctrl+s",
command=lambda *args: savekey(song, subterms, curveset))
m.add_command(label='Quit', command=root.destroy)
with newMenu("View") as m:
m.add_command(label="See current time", accelerator="Esc", command=notImpl)
m.add_command(label="See from current time -> end", accelerator="Shift+Esc", command=notImpl)
m.add_command(label="Zoom all", accelerator="Ctrl+Esc", command=notImpl)
m.add_command(label="Zoom in", accelerator="Wheel up", command=notImpl)
m.add_command(label="Zoom out", accelerator="Wheel down", command=notImpl)
with newMenu("Playback") as m:
m.add_command(label="Play/pause at mouse", accelerator="Ctrl+P", command=notImpl)
with newMenu("Points") as m:
m.add_command(label="Delete", accelerator="Del", command=notImpl)
def createHelpLines(root):
for helpline in ["Mousewheel zoom; C-p play/pause music at mouse",
"Curve point bindings: B1 drag point; C-B1 curve add point; S-B1 sketch points; 1..5 add point at time; B1 drag select points",
"Available in functions: nsin/ncos period=amp=1; within(a,b) bef(x) aft(x) compare to time; smoove(x) cubic smoothstep; chan(name); curvename(t) eval curve"]:
line = tk.Label(root, text=helpline, font="Helvetica -12 italic",
anchor='w')
line.pack(side='top',fill='x')
def currentlyPlayingSong():
"""ask the music player what song it's on"""
player = restkit.Resource(networking.musicPlayer.url)
t = json.loads(player.get("time").body_string())
if t['song'] is None:
raise ValueError("music player is not playing any song")
return URIRef(t['song'])
def main():
startTime = time.time()
parser = optparse.OptionParser()
parser.set_usage("%prog [opts] [songURI]")
parser.add_option("--sliders", action='store_true',
help='use hardware sliders')
parser.add_option("--skip-music", action='store_true',
help="ignore music and smooth_music curve files")
parser.add_option("--debug", action="store_true",
help="log at DEBUG")
parser.add_option("--startup-only", action='store_true',
help="quit after loading everything (for timing tests)")
opts, args = parser.parse_args()
logging.basicConfig(format="%(asctime)s %(levelname)-5s %(name)s %(filename)s:%(lineno)d: %(message)s")
log.setLevel(logging.DEBUG if opts.debug else logging.INFO)
log.debug("startup: music %s", time.time() - startTime)
try:
song = URIRef(args[0])
except IndexError:
song = currentlyPlayingSong()
music=Music()
graph = makeGraph()
curveset = Curveset(sliders=opts.sliders)
subterms = []
subtermPath = graphPathForSubterms(song)
try:
graph.parse(subtermPath, format='n3')
except urllib2.URLError, e:
if e.reason.errno != 2:
raise
log.info("%s not found, starting with empty graph" % subtermPath)
log.debug("startup: output %s", time.time() - startTime)
out = Output(subterms, music)
musicfilename = showconfig.songOnDisk(song)
maxtime = wavelength(musicfilename)
dispatcher.connect(lambda: maxtime, "get max time", weak=False)
root=tk.Tk()
root.tk_setPalette("gray50")
toplevelat("curvecalc",root)
root.tk_focusFollowsMouse()
root.title("curvecalc - %s" % graph.label(song))
menubar = tk.Frame(root)
menubar.pack(side='top', fill='x')
if 'fixed top rows':
zc = Zoomcontrol(root)
zc.pack(side='top', fill='x')
if 'panes':
panes = tk.PanedWindow(root, height=1)
panes.add('curvesetView')
panes.add('subterms')
panes.pack(side='top', fill='both', expand=True)
curvesetView = Curvesetview(panes.subwidget('curvesetView'), curveset,
height=600)
curvesetView.pack(fill='both', expand=True)
subtermArea = tk.Frame(panes.subwidget('subterms'), height=100)
subtermArea.pack(fill='both', expand=True)
subtermScroll = tk.ScrolledWindow(subtermArea)
subtermScroll.pack(fill='both')
if 'fixed bottom rows':
makeSubtermCommandRow(root, curveset, subterms, root, subtermArea,
graph).pack(side='top', fill='x')
makeStatusLines(root)
helpBox = tk.Frame(root)
createHelpLines(helpBox)
helpBox.pack(side='top', fill='x')
add_subterms_for_song(graph, song, curveset, subterms,
subtermScroll.subwidget('window'))
setupKeyBindings(root, song, subterms, curveset)
setupMenubar(menubar, root, song, subterms, curveset)
# curvesetview must already exist, since this makes 'add_curve'
# signals for all the initial curves
curveset.load(basename=os.path.join(showconfig.curvesDir(),
showconfig.songFilenameFromURI(song)),
skipMusic=opts.skip_music)
dispatcher.send("max time",maxtime=maxtime)
dispatcher.send("show all")
# this is scheduled after some tk shuffling, to try to minimize
# the number of times we redraw the curve at startup. If tk is
# very slow, it's ok. You'll just get some wasted redraws.
reactor.callLater(.1, curvesetView.goLive)
tksupport.install(root, ms=10)
log.debug("startup: run %s", time.time() - startTime)
if opts.startup_only:
log.debug("quitting now because of --startup-only")
return
prof.run(reactor.run, profile=False)
main()
|