Files @ f22f925442a2
Branch filter:

Location: light9/bin/curvecalc - annotation

drewp@bigasterisk.com
a301a0039c66
f41004d5a507
f41004d5a507
a6662d61ebcd
a6662d61ebcd
a6662d61ebcd
a6662d61ebcd
a6662d61ebcd
f41004d5a507
f41004d5a507
f41004d5a507
f41004d5a507
34dacd800505
62d83d456f2e
34dacd800505
34dacd800505
34dacd800505
3d1112a894df
03453848ed4c
16042667ab51
2193eab0650b
2193eab0650b
f41004d5a507
f41004d5a507
847edbfe65c8
97b028ed0e47
929ccd4ec800
03453848ed4c
0f112a7dd6b3
8a1ec8aca432
847edbfe65c8
34dacd800505
847edbfe65c8
10ee0756a119
f41004d5a507
cbc98de2ab06
cbc98de2ab06
cbc98de2ab06
cbc98de2ab06
cbc98de2ab06
cbc98de2ab06
cbc98de2ab06
f41004d5a507
3d1112a894df
3d1112a894df
3d1112a894df
62d83d456f2e
2aac2ef23495
417e23dc0af0
417e23dc0af0
5656767ca025
417e23dc0af0
417e23dc0af0
155c7bb9c948
62d83d456f2e
d5692ab6bc2a
d5692ab6bc2a
62d83d456f2e
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
62d83d456f2e
62d83d456f2e
34dacd800505
ccdd04f4ca7d
20d6884478bc
155c7bb9c948
155c7bb9c948
155c7bb9c948
62f99e2a00ac
e8f9f4815ab4
e8f9f4815ab4
16042667ab51
417e23dc0af0
34dacd800505
34dacd800505
34dacd800505
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
f22f925442a2
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
ed50631cf333
c4a38a247b26
ed50631cf333
ed50631cf333
ed50631cf333
c4a38a247b26
ed50631cf333
c4a38a247b26
ed50631cf333
16042667ab51
c4a38a247b26
16042667ab51
16042667ab51
16042667ab51
16042667ab51
c4a38a247b26
c4a38a247b26
16042667ab51
3d1112a894df
3d1112a894df
c4a38a247b26
16042667ab51
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
3d1112a894df
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
e8f9f4815ab4
e8f9f4815ab4
e8f9f4815ab4
e8f9f4815ab4
e8f9f4815ab4
d5692ab6bc2a
d5692ab6bc2a
d5692ab6bc2a
d5692ab6bc2a
d5692ab6bc2a
d5692ab6bc2a
9865cf5e07fd
9865cf5e07fd
9865cf5e07fd
9865cf5e07fd
9865cf5e07fd
9865cf5e07fd
e94989da079c
e94989da079c
e94989da079c
10ee0756a119
10ee0756a119
10ee0756a119
d12bc8919d6e
d12bc8919d6e
d12bc8919d6e
d12bc8919d6e
d12bc8919d6e
d12bc8919d6e
d12bc8919d6e
d12bc8919d6e
34dacd800505
2aac2ef23495
2aac2ef23495
2aac2ef23495
2aac2ef23495
2aac2ef23495
2aac2ef23495
2aac2ef23495
34dacd800505
54a3dcba6597
34dacd800505
16042667ab51
16042667ab51
ccdd04f4ca7d
ccdd04f4ca7d
ccdd04f4ca7d
ccdd04f4ca7d
ccdd04f4ca7d
ccdd04f4ca7d
ccdd04f4ca7d
ccdd04f4ca7d
ccdd04f4ca7d
ccdd04f4ca7d
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
34dacd800505
5656767ca025
5656767ca025
34dacd800505
34dacd800505
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
34dacd800505
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
348b68723238
348b68723238
348b68723238
348b68723238
348b68723238
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
3d1112a894df
348b68723238
348b68723238
417e23dc0af0
417e23dc0af0
417e23dc0af0
417e23dc0af0
5f384443e3ed
5f384443e3ed
d1bff8a3b069
34dacd800505
34dacd800505
cbc98de2ab06
ad17b0a09f5c
cbc98de2ab06
ad5043f70fda
cbc98de2ab06
cbc98de2ab06
cbc98de2ab06
cbc98de2ab06
ad17b0a09f5c
ad17b0a09f5c
417e23dc0af0
417e23dc0af0
ad17b0a09f5c
ad17b0a09f5c
cbc98de2ab06
cbc98de2ab06
0cf00fffd921
ad17b0a09f5c
0cf00fffd921
ad17b0a09f5c
cbc98de2ab06
cbc98de2ab06
cbc98de2ab06
ad5043f70fda
cbc98de2ab06
417e23dc0af0
73b181155555
62d83d456f2e
cbc98de2ab06
73b181155555
cbc98de2ab06
d5a9933a2222
d5a9933a2222
d5a9933a2222
d5a9933a2222
d5a9933a2222
af5539fe35a7
af5539fe35a7
af5539fe35a7
d8202a0a7fd5
d8202a0a7fd5
af5539fe35a7
af5539fe35a7
73b181155555
ad17b0a09f5c
73b181155555
cbc98de2ab06
cbc98de2ab06
cbc98de2ab06
73b181155555
73b181155555
2aac2ef23495
ccdd04f4ca7d
34dacd800505
34dacd800505
34dacd800505
ccdd04f4ca7d
ccdd04f4ca7d
ccdd04f4ca7d
ccdd04f4ca7d
417e23dc0af0
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

from twisted.internet import gtk2reactor
gtk2reactor.install()
from twisted.internet import reactor

import time, textwrap, os, optparse, gtk, linecache, signal, traceback
import louie as dispatcher 
from rdflib import URIRef, Graph, Literal, RDF, RDFS
import logging
log = logging.getLogger()

import run_local
from light9 import showconfig, prof
from light9.curvecalc.curve import Curveset
from light9.curvecalc import curveview 
from light9.curvecalc.musicaccess import Music, currentlyPlayingSong
from light9.wavelength import wavelength
from light9.namespaces import L9
from light9.curvecalc.subterm import read_all_subs, savekey, graphPathForSubterms
from light9.curvecalc.subtermview import add_one_subterm
from light9.curvecalc.output import Output
from light9.gtkpyconsole import togglePyConsole

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

class SubtermExists(ValueError):
    pass

class Main(object):
    def __init__(self, graph, opts, song, curveset, subterms, music):
        self.graph, self.opts, self.song = graph, opts, song
        self.curveset, self.subterms, self.music = curveset, subterms, music
        self.lastSeenInputTime = 0

        wtree = self.wtree = gtk.Builder()
        wtree.add_from_file("light9/curvecalc/curvecalc.glade")
        mainwin = wtree.get_object("MainWindow")
        
        mainwin.connect("destroy", self.onQuit)
        wtree.connect_signals(self)
        gtk.rc_parse("theme/marble-ice/gtk-2.0/gtkrc")
        gtk.rc_parse_string("""style "default" {font_name = "sans 7"}""")
        if self.opts.reload:
            self.refreshTheme()
        mainwin.show_all()

        mainwin.connect("delete-event", lambda *args: reactor.crash())
        mainwin.set_title("curvecalc - %s" % graph.label(song))
        mainwin.parse_geometry("1x1-0+0")

        # this is the only one i found that would set the size right,
        # but it's a minimum size, which i don't really want
        mainwin.set_size_request(1678, 922)

        wtree.get_object("subterms").connect("add", self.onSubtermChildAdded)
        self.add_subterms_for_song(song, curveset, subterms)
        self.refreshCurveView()       
        
        self.makeStatusLines(wtree.get_object("status"))

        def connect(w):
            w.drag_dest_set(flags=gtk.DEST_DEFAULT_ALL,
                            targets=[('text/uri-list', 0, 0)],
                            actions=gtk.gdk.ACTION_COPY)
            w.connect("drag-data-received", self.onDataReceived)
        connect(mainwin)
        # that's not enough- deeper windows don't accept the
        # event. 
        mainwin.forall(connect) # not very effective
        connect(wtree.get_object("subterms")) # works for that area

    def onDataReceived(self, widget, context, x, y, selection,
                       targetType, time):
        uri = URIRef(selection.data.strip())
        subName = self.graph.label(uri)

        try:
            self.makeSubterm(subName, withCurve=True)
        except SubtermExists:
            pass
        curveView = self.curvesetView.row(subName).curveView
        t = self.lastSeenInputTime # curveView.current_time() # new curve hasn't heard the time yet. this has gotten too messy- everyone just needs to be able to reach the time source
        print "time", t
        curveView.add_point((t - .5, 0))
        curveView.add_point((t, 1))
            

    def onNewCurve(self, *args):
        dialog = self.wtree.get_object("newCurve")
        entry = self.wtree.get_object("newCurveName")
        # if you don't have songx, that should be the suggested name
        entry.set_text("")
        if dialog.run() == 1:
            self.curveset.new_curve(entry.get_text())
        dialog.hide()

    def onNewSubterm(self, *args):
        dialog = self.wtree.get_object("newSubterm")
        # the plan is to autocomplete this on existing subterm names
        # (but let you make one up, too)
        entry = self.wtree.get_object("newSubtermName").get_children()[0]
        entry.set_text("")
        entry.grab_focus()
        if dialog.run() == 1:
            newname = entry.get_text()
            wc = self.wtree.get_object("newSubtermMakeCurve").get_active()
            self.makeSubterm(newname, withCurve=wc)
        dialog.hide()

    def makeSubterm(self, newname, withCurve=False):
        uri = L9['sub/%s' % newname]
        if (uri, RDF.type, L9.Subterm) in self.graph:
            raise SubtermExists("already have a subterm named %r" % newname)
        self.graph.add((uri, RDF.type, L9.Subterm))
        self.graph.add((uri, RDFS.label, Literal(newname)))
        add_one_subterm(self.graph, uri,
                        self.curveset, self.subterms,
                        self.wtree.get_object("subterms"),
                        None, show=True)
        if withCurve:
            self.curveset.new_curve(newname)

    def refreshTheme(self):
        gtk.rc_reparse_all()
        reactor.callLater(1, self.refreshTheme)

    def onSubtermChildAdded(self, subtermsTable, *args):
        # this would probably work, but isn't getting called
        v = subtermsTable.get_parent().props.vadjustment
        v.props.value = v.props.upper

    def onQuit(self, *args):
        reactor.crash()
        # there's a hang after this, maybe in sem_wait in two
        # threads. I don't know whose they are.
        os.kill(os.getpid(), signal.SIGKILL)

    def onCollapseAll(self, *args):
        self.curvesetView.collapseAll()

    def onCollapseNone(self, *args):
        self.curvesetView.collapseNone()

    def onDelete(self, *args):
        self.curvesetView.onDelete()

    def onPythonConsole(self, item):
        togglePyConsole(self, item, self.__dict__)
        
    def onSeeCurrentTime(self, item):
        dispatcher.send("see time")

    def onSeeTimeUntilEnd(self, item):
        dispatcher.send("see time until end")

    def onZoomAll(self, item):
        dispatcher.send("show all")

    def onPlayPause(self, item):
        # since the X coord in a curveview affects the handling, one
        # of them may be able to pick this up
        results = dispatcher.send("onPlayPause")
        times = [t for listener, t in results if t is not None]
        self.music.playOrPause(t=times[0] if times else None)

    def onSave(self, *args):
        savekey(self.song, self.subterms, self.curveset)

    def add_subterms_for_song(self, song, curveset, subterms):
        master = self.wtree.get_object("subterms")
        for st in self.graph.objects(song, L9['subterm']):
            log.info("song %s has subterm %s", song, st)
            add_one_subterm(self.graph,
                            self.graph.value(st, L9['sub']),
                            curveset,
                            subterms,
                            master,
                            self.graph.value(st, L9['expression']))
        master.show_all()

    def makeStatusLines(self, master):
        """various labels that listen for dispatcher signals"""
        for row, (signame, textfilter) in enumerate([
            ('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 x: str(x)),
            ]):
            key = gtk.Label("%s:" % signame)
            value = gtk.Label("")
            master.resize(row + 1, 2)
            master.attach(key, 0, 1, row, row + 1)
            master.attach(value, 1, 2, row, row + 1)
            key.set_alignment(1, 0)
            value.set_alignment(0, 0)

            dispatcher.connect(lambda val, value=value, tf=textfilter:
                               value.set_text(tf(val)),
                               signame, weak=False)
        dispatcher.connect(lambda val: setattr(self, 'lastSeenInputTime', val),
                           'input time', weak=False)
        master.show_all()

    def refreshCurveView(self):
        wtree = self.wtree
        mtimes = [os.path.getmtime(f) for f in [
            'light9/curvecalc/curveview.py',
            'light9/curvecalc/zoomcontrol.py',
            ]]

        if (not hasattr(self, 'curvesetView') or
            self.curvesetView._mtimes != mtimes):
            print "reload curveview.py"
            curvesVBox = wtree.get_object("curves")
            zoomControlBox = wtree.get_object("zoomControlBox")
            [curvesVBox.remove(c) for c in curvesVBox.get_children()]
            [zoomControlBox.remove(c) for c in
             zoomControlBox.get_children()]
            try:
                linecache.clearcache()
                reload(curveview)

                # old ones are not getting deleted right
                if hasattr(self, 'curvesetView'):
                    self.curvesetView.live = False

                # mem problem somewhere; need to hold a ref to this
                self.curvesetView = curveview.Curvesetview(
                    curvesVBox, zoomControlBox, self.curveset)
                self.curvesetView._mtimes = mtimes

                # 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.
                self.curvesetView.goLive()
            except Exception:
                print "reload failed:"
                traceback.print_exc()
        if self.opts.reload:
            reactor.callLater(1, self.refreshCurveView)

    def onReloadSubs(self, *args): # wants to be ctrl-r  too
        read_all_subs(self.graph) # this will discover new subs (additive only)
        dispatcher.send('reload all subs') # this rereads each sub from its graph file
        dispatcher.send("all curves rebuild")


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("--reload", action="store_true",
                      help="live reload of themes and code")
    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 = []

    curveset.load(basename=os.path.join(
        showconfig.curvesDir(),
        showconfig.songFilenameFromURI(song)),
                  skipMusic=opts.skip_music)

    subtermPath = graphPathForSubterms(song)
    try:
        graph.parse(subtermPath, format='n3')
    except IOError, e:
        if e.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)

    start = Main(graph, opts, song, curveset, subterms, music)

    dispatcher.send("max time", maxtime=maxtime)
    dispatcher.send("show all")
        
    if opts.startup_only:
        log.debug("quitting now because of --startup-only")
        return

    prof.run(reactor.run, profile=False)

main()