annotate flax/curvecalc @ 200:f5d3492981ab

moved Zoomcontrol to another file
author drewp
date Wed, 16 Jun 2004 13:12:07 +0000
parents ba2677823b35
children 97e21bc387fe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
1 #!/usr/bin/python
45b12307c695 Initial revision
drewp
parents:
diff changeset
2
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
3 """
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
4 todo: curveview should preserve more objects, for speed maybe
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
5
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
6 """
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
7 from __future__ import division
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
8 import xmlrpclib,time,socket,sys,textwrap,math
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
9 from bisect import bisect_left,bisect,bisect_right
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
10 import Tkinter as tk
45b12307c695 Initial revision
drewp
parents:
diff changeset
11 from dispatch import dispatcher
45b12307c695 Initial revision
drewp
parents:
diff changeset
12 from twisted.internet import reactor,tksupport
45b12307c695 Initial revision
drewp
parents:
diff changeset
13 from twisted.web.xmlrpc import Proxy
45b12307c695 Initial revision
drewp
parents:
diff changeset
14
45b12307c695 Initial revision
drewp
parents:
diff changeset
15 sys.path.append("../light8")
45b12307c695 Initial revision
drewp
parents:
diff changeset
16 import dmxclient
45b12307c695 Initial revision
drewp
parents:
diff changeset
17 import Submaster
45b12307c695 Initial revision
drewp
parents:
diff changeset
18 from TLUtility import make_attributes_from_args
45b12307c695 Initial revision
drewp
parents:
diff changeset
19
200
f5d3492981ab moved Zoomcontrol to another file
drewp
parents: 197
diff changeset
20 from zoomcontrol import Zoomcontrol
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
21
45b12307c695 Initial revision
drewp
parents:
diff changeset
22 class Curve:
45b12307c695 Initial revision
drewp
parents:
diff changeset
23 """curve does not know its name. see Curveset"""
45b12307c695 Initial revision
drewp
parents:
diff changeset
24 points = None # x-sorted list of (x,y)
45b12307c695 Initial revision
drewp
parents:
diff changeset
25 def __init__(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
26 self.points = []
45b12307c695 Initial revision
drewp
parents:
diff changeset
27
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
28 self.points = [(0,0),(1,1),(9,1),(10,0)]
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
29 for x in range(11,500):
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
30 self.points.append((x,.5))
200
f5d3492981ab moved Zoomcontrol to another file
drewp
parents: 197
diff changeset
31
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
32 def eval(self,t):
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
33 i = bisect_left(self.points,(t,None))-1
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
34
45b12307c695 Initial revision
drewp
parents:
diff changeset
35 if self.points[i][0]>t:
45b12307c695 Initial revision
drewp
parents:
diff changeset
36 return self.points[i][1]
45b12307c695 Initial revision
drewp
parents:
diff changeset
37 if i>=len(self.points)-1:
45b12307c695 Initial revision
drewp
parents:
diff changeset
38 return self.points[i][1]
45b12307c695 Initial revision
drewp
parents:
diff changeset
39
45b12307c695 Initial revision
drewp
parents:
diff changeset
40 p1,p2 = self.points[i],self.points[i+1]
45b12307c695 Initial revision
drewp
parents:
diff changeset
41 frac = (t-p1[0])/(p2[0]-p1[0])
45b12307c695 Initial revision
drewp
parents:
diff changeset
42 y = p1[1]+(p2[1]-p1[1])*frac
45b12307c695 Initial revision
drewp
parents:
diff changeset
43 return y
45b12307c695 Initial revision
drewp
parents:
diff changeset
44
45b12307c695 Initial revision
drewp
parents:
diff changeset
45 __call__=eval
45b12307c695 Initial revision
drewp
parents:
diff changeset
46
45b12307c695 Initial revision
drewp
parents:
diff changeset
47 class Curveview(tk.Canvas):
45b12307c695 Initial revision
drewp
parents:
diff changeset
48 def __init__(self,master,curve,**kw):
45b12307c695 Initial revision
drewp
parents:
diff changeset
49 self.curve=curve
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
50 tk.Canvas.__init__(self,master,width=10,height=10,
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
51 relief='sunken',bd=1,
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
52 closeenough=5,**kw)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
53 self.selected_points=[] # idx of points being dragged
45b12307c695 Initial revision
drewp
parents:
diff changeset
54 self.update()
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
55 self.bind("<Enter>",self.focus)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
56 dispatcher.connect(self.input_time,"input time")
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
57 dispatcher.connect(self.update,"zoom changed")
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
58 self.bind("<Configure>",self.update)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
59 def screen_from_world(self,p):
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
60 start,end = self.zoom
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
61 ht = self.winfo_height()
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
62 return (p[0]-start)/(end-start)*self.winfo_width(), (ht-5)-p[1]*(ht-10)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
63 def world_from_screen(self,x,y):
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
64 start,end = self.zoom
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
65 ht = self.winfo_height()
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
66 return x/self.winfo_width()*(end-start)+start, ((ht-5)-y)/(ht-10)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
67
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
68 def input_time(self,val):
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
69 t=val
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
70 pts = self.screen_from_world((val,0))+self.screen_from_world((val,1))
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
71 self.delete('timecursor')
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
72 self.create_line(*pts,**dict(width=2,fill='red',tags=('timecursor',)))
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
73 def update(self,*args):
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
74
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
75 self.zoom = dispatcher.send("zoom area")[0][1]
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
76 cp = self.curve.points
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
77
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
78 visible_x = (self.world_from_screen(0,0)[0],
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
79 self.world_from_screen(self.winfo_width(),0)[0])
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
80
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
81 visleftidx = max(0,bisect_left(cp,(visible_x[0],None))-1)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
82 visrightidx = min(len(cp)-1,bisect_left(cp,(visible_x[1],None))+1)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
83
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
84 visible_points = cp[visleftidx:visrightidx]
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
85
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
86 self.delete('curve')
45b12307c695 Initial revision
drewp
parents:
diff changeset
87 linepts=[]
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
88 for p in visible_points:
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
89 linepts.extend(self.screen_from_world(p))
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
90 line = self.create_line(*linepts,**{'tags':'curve'})
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
91
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
92 # canvas doesnt have keyboard focus, so i can't easily change the
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
93 # cursor when ctrl is pressed
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
94 # def curs(ev):
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
95 # print ev.state
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
96 # self.bind("<KeyPress>",curs)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
97 # self.bind("<KeyRelease-Control_L>",lambda ev: curs(0))
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
98 self.tag_bind(line,"<Control-ButtonPress-1>",self.newpoint)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
99
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
100 self.dots = {} # idx : canvas rectangle
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
101
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
102 if len(visible_points)<50: ###self.zoom[1]-self.zoom[0]<30 or len(visible_points)<:
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
103 for i,p in enumerate(visible_points):
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
104 rad=3
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
105 p = self.screen_from_world(p)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
106 # if p[0]-prevx<10:
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
107 # # too close- skip the dots
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
108 # continue
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
109 dot = self.create_rectangle(p[0]-rad,p[1]-rad,p[0]+rad,p[1]+rad,
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
110 outline='black',fill='blue',
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
111 tags=('curve','point'))
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
112 self.tag_bind(dot,"<ButtonPress-1>",
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
113 lambda ev,i=i: self.dotpress(ev,i))
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
114 self.bind("<Motion>",
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
115 lambda ev,i=i: self.dotmotion(ev,i))
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
116 self.bind("<ButtonRelease-1>",
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
117 lambda ev,i=i: self.dotrelease(ev,i))
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
118 self.dots[i]=dot
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
119
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
120 self.highlight_selected_dots()
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
121
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
122 def newpoint(self,ev):
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
123 cp = self.curve.points
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
124
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
125 p = self.world_from_screen(ev.x,ev.y)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
126 i = bisect(cp,(p[0],None))
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
127
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
128 self.unselect()
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
129 cp.insert(i,p)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
130 self.update()
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
131
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
132 def highlight_selected_dots(self):
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
133 for i,d in self.dots.items():
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
134 if i in self.selected_points:
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
135 self.itemconfigure(d,fill='red')
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
136 else:
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
137 self.itemconfigure(d,fill='blue')
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
138
45b12307c695 Initial revision
drewp
parents:
diff changeset
139 def dotpress(self,ev,dotidx):
45b12307c695 Initial revision
drewp
parents:
diff changeset
140 self.selected_points=[dotidx]
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
141 self.highlight_selected_dots()
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
142
45b12307c695 Initial revision
drewp
parents:
diff changeset
143 def dotmotion(self,ev,dotidx):
45b12307c695 Initial revision
drewp
parents:
diff changeset
144 cp = self.curve.points
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
145
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
146 moved=0
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
147 for idx in self.selected_points:
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
148 newp = self.world_from_screen(ev.x,ev.y)
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
149 if idx>0 and newp[0]<=cp[idx-1][0]:
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
150 continue
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
151 if idx<len(cp)-1 and newp[0]>=cp[idx+1][0]:
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
152 continue
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
153 moved=1
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
154 cp[idx] = newp
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
155 if moved:
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
156 self.update()
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
157 def unselect(self):
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
158 self.selected_points=[]
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
159 self.highlight_selected_dots()
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
160
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
161 def dotrelease(self,ev,dotidx):
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
162 self.unselect()
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
163
45b12307c695 Initial revision
drewp
parents:
diff changeset
164 class Curveset:
45b12307c695 Initial revision
drewp
parents:
diff changeset
165 curves = None # curvename : curve
45b12307c695 Initial revision
drewp
parents:
diff changeset
166 def __init__(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
167 self.curves = {}
45b12307c695 Initial revision
drewp
parents:
diff changeset
168 def add_curve(self,name,curve):
45b12307c695 Initial revision
drewp
parents:
diff changeset
169 self.curves[name] = curve
45b12307c695 Initial revision
drewp
parents:
diff changeset
170 dispatcher.send("add_curve",sender=self,name=name)
45b12307c695 Initial revision
drewp
parents:
diff changeset
171 def globalsdict(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
172 return self.curves.copy()
45b12307c695 Initial revision
drewp
parents:
diff changeset
173
45b12307c695 Initial revision
drewp
parents:
diff changeset
174 class Curvesetview(tk.Frame):
45b12307c695 Initial revision
drewp
parents:
diff changeset
175 curves = None # curvename : Curveview
45b12307c695 Initial revision
drewp
parents:
diff changeset
176 def __init__(self,master,curveset,**kw):
45b12307c695 Initial revision
drewp
parents:
diff changeset
177 self.curves = {}
45b12307c695 Initial revision
drewp
parents:
diff changeset
178 self.curveset = curveset
45b12307c695 Initial revision
drewp
parents:
diff changeset
179 tk.Frame.__init__(self,master,**kw)
45b12307c695 Initial revision
drewp
parents:
diff changeset
180 dispatcher.connect(self.add_curve,"add_curve",sender=self.curveset)
45b12307c695 Initial revision
drewp
parents:
diff changeset
181 def add_curve(self,name):
45b12307c695 Initial revision
drewp
parents:
diff changeset
182 f = tk.Frame(self,relief='raised',bd=1)
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
183 f.pack(side='top',fill='both',exp=1)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
184 tk.Label(f,text="curve %r"%name).pack(side='left')
45b12307c695 Initial revision
drewp
parents:
diff changeset
185 cv = Curveview(f,self.curveset.curves[name])
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
186 cv.pack(side='right',fill='both',exp=1)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
187 self.curves[name] = cv
45b12307c695 Initial revision
drewp
parents:
diff changeset
188
45b12307c695 Initial revision
drewp
parents:
diff changeset
189 class Music:
45b12307c695 Initial revision
drewp
parents:
diff changeset
190 def __init__(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
191 self.player=None # xmlrpc Proxy to player
45b12307c695 Initial revision
drewp
parents:
diff changeset
192 self.recenttime=0
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
193
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
194 def current_time(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
195 """return deferred which gets called with the current time"""
45b12307c695 Initial revision
drewp
parents:
diff changeset
196 if self.player is None:
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
197 self.player = Proxy("http://spot:8040")
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
198 d = self.player.callRemote("songlength")
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
199 def sendmax(l):
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
200 dispatcher.send("max time",maxtime=l)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
201 d.addCallback(sendmax)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
202 d = self.player.callRemote('gettime')
45b12307c695 Initial revision
drewp
parents:
diff changeset
203 def sendtime(t):
45b12307c695 Initial revision
drewp
parents:
diff changeset
204 dispatcher.send("input time",val=t)
45b12307c695 Initial revision
drewp
parents:
diff changeset
205 return t # pass along to the real receiver
45b12307c695 Initial revision
drewp
parents:
diff changeset
206 def error(e):
45b12307c695 Initial revision
drewp
parents:
diff changeset
207 pass#self.player=None
45b12307c695 Initial revision
drewp
parents:
diff changeset
208 d.addCallback(sendtime)
45b12307c695 Initial revision
drewp
parents:
diff changeset
209 return d
45b12307c695 Initial revision
drewp
parents:
diff changeset
210
45b12307c695 Initial revision
drewp
parents:
diff changeset
211 class Subexpr:
45b12307c695 Initial revision
drewp
parents:
diff changeset
212 curveset = None
45b12307c695 Initial revision
drewp
parents:
diff changeset
213 def __init__(self,curveset):
45b12307c695 Initial revision
drewp
parents:
diff changeset
214 self.curveset = curveset
45b12307c695 Initial revision
drewp
parents:
diff changeset
215 self.lasteval = None
45b12307c695 Initial revision
drewp
parents:
diff changeset
216 self.expr=""
45b12307c695 Initial revision
drewp
parents:
diff changeset
217 def eval(self,t):
45b12307c695 Initial revision
drewp
parents:
diff changeset
218 if self.expr=="":
45b12307c695 Initial revision
drewp
parents:
diff changeset
219 dispatcher.send("expr_error",sender=self,exc="no expr, using 0")
45b12307c695 Initial revision
drewp
parents:
diff changeset
220 return 0
45b12307c695 Initial revision
drewp
parents:
diff changeset
221 glo = self.curveset.globalsdict()
45b12307c695 Initial revision
drewp
parents:
diff changeset
222 glo['t'] = t
45b12307c695 Initial revision
drewp
parents:
diff changeset
223 try:
45b12307c695 Initial revision
drewp
parents:
diff changeset
224 self.lasteval = eval(self.expr,glo)
45b12307c695 Initial revision
drewp
parents:
diff changeset
225 except Exception,e:
45b12307c695 Initial revision
drewp
parents:
diff changeset
226 dispatcher.send("expr_error",sender=self,exc=e)
45b12307c695 Initial revision
drewp
parents:
diff changeset
227 else:
45b12307c695 Initial revision
drewp
parents:
diff changeset
228 dispatcher.send("expr_error",sender=self,exc="no errors")
45b12307c695 Initial revision
drewp
parents:
diff changeset
229 return self.lasteval
45b12307c695 Initial revision
drewp
parents:
diff changeset
230
45b12307c695 Initial revision
drewp
parents:
diff changeset
231 def expr():
45b12307c695 Initial revision
drewp
parents:
diff changeset
232 doc = "python expression for level as a function of t, using curves"
45b12307c695 Initial revision
drewp
parents:
diff changeset
233 def fget(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
234 return self._expr
45b12307c695 Initial revision
drewp
parents:
diff changeset
235 def fset(self, value):
45b12307c695 Initial revision
drewp
parents:
diff changeset
236 self._expr = value
45b12307c695 Initial revision
drewp
parents:
diff changeset
237 dispatcher("expr_changed",sender=self)
45b12307c695 Initial revision
drewp
parents:
diff changeset
238 return locals()
45b12307c695 Initial revision
drewp
parents:
diff changeset
239 expr = property(**expr())
45b12307c695 Initial revision
drewp
parents:
diff changeset
240
45b12307c695 Initial revision
drewp
parents:
diff changeset
241 class Subexprview(tk.Frame):
45b12307c695 Initial revision
drewp
parents:
diff changeset
242 def __init__(self,master,se,**kw):
45b12307c695 Initial revision
drewp
parents:
diff changeset
243 self.subexpr=se
45b12307c695 Initial revision
drewp
parents:
diff changeset
244 tk.Frame.__init__(self,master,**kw)
45b12307c695 Initial revision
drewp
parents:
diff changeset
245 self.evar = tk.StringVar()
45b12307c695 Initial revision
drewp
parents:
diff changeset
246 e = self.ent = tk.Entry(master,textvariable=self.evar)
45b12307c695 Initial revision
drewp
parents:
diff changeset
247 e.pack(side='left',fill='both',exp=1)
45b12307c695 Initial revision
drewp
parents:
diff changeset
248 self.expr_changed()
45b12307c695 Initial revision
drewp
parents:
diff changeset
249 self.evar.trace_variable('w',self.evar_changed)
45b12307c695 Initial revision
drewp
parents:
diff changeset
250 dispatcher.connect(self.expr_changed,"expr_changed",
45b12307c695 Initial revision
drewp
parents:
diff changeset
251 sender=self.subexpr)
45b12307c695 Initial revision
drewp
parents:
diff changeset
252 self.error = tk.Label(master)
45b12307c695 Initial revision
drewp
parents:
diff changeset
253 self.error.pack(side='left')
45b12307c695 Initial revision
drewp
parents:
diff changeset
254 dispatcher.connect(lambda exc: self.error.config(text=str(exc)),
45b12307c695 Initial revision
drewp
parents:
diff changeset
255 "expr_error",sender=self.subexpr,weak=0)
45b12307c695 Initial revision
drewp
parents:
diff changeset
256 def expr_changed(self):
45b12307c695 Initial revision
drewp
parents:
diff changeset
257 if self.subexpr.expr!=self.evar.get():
45b12307c695 Initial revision
drewp
parents:
diff changeset
258 self.evar.set(self.subexpr.expr)
45b12307c695 Initial revision
drewp
parents:
diff changeset
259 def evar_changed(self,*args):
45b12307c695 Initial revision
drewp
parents:
diff changeset
260 self.subexpr.expr = self.evar.get()
45b12307c695 Initial revision
drewp
parents:
diff changeset
261
45b12307c695 Initial revision
drewp
parents:
diff changeset
262 class Subterm:
45b12307c695 Initial revision
drewp
parents:
diff changeset
263 """one Submaster and its Subexpr"""
45b12307c695 Initial revision
drewp
parents:
diff changeset
264 def scaled(self,t):
45b12307c695 Initial revision
drewp
parents:
diff changeset
265 return self.sub * self.subexpr.eval(t)
45b12307c695 Initial revision
drewp
parents:
diff changeset
266
45b12307c695 Initial revision
drewp
parents:
diff changeset
267 class Subtermview(tk.Frame):
45b12307c695 Initial revision
drewp
parents:
diff changeset
268 def __init__(self,master,st,**kw):
45b12307c695 Initial revision
drewp
parents:
diff changeset
269 self.subterm = st
45b12307c695 Initial revision
drewp
parents:
diff changeset
270 tk.Frame.__init__(self,master,bd=1,relief='raised',**kw)
45b12307c695 Initial revision
drewp
parents:
diff changeset
271 tk.Label(self,text="sub %r" % self.subterm.sub.name).pack(side='left')
45b12307c695 Initial revision
drewp
parents:
diff changeset
272 sev=Subexprview(self,self.subterm.subexpr)
45b12307c695 Initial revision
drewp
parents:
diff changeset
273 sev.pack(side='left',fill='both',exp=1)
45b12307c695 Initial revision
drewp
parents:
diff changeset
274
45b12307c695 Initial revision
drewp
parents:
diff changeset
275 class Output:
45b12307c695 Initial revision
drewp
parents:
diff changeset
276 def __init__(self,subterms):
45b12307c695 Initial revision
drewp
parents:
diff changeset
277 make_attributes_from_args('subterms')
45b12307c695 Initial revision
drewp
parents:
diff changeset
278 def send_dmx(self,t):
45b12307c695 Initial revision
drewp
parents:
diff changeset
279
45b12307c695 Initial revision
drewp
parents:
diff changeset
280 scaledsubs=[]
45b12307c695 Initial revision
drewp
parents:
diff changeset
281 for st in self.subterms:
45b12307c695 Initial revision
drewp
parents:
diff changeset
282 scl = st.scaled(t)
45b12307c695 Initial revision
drewp
parents:
diff changeset
283 scaledsubs.append(scl)
45b12307c695 Initial revision
drewp
parents:
diff changeset
284 out = Submaster.sub_maxes(*scaledsubs)
45b12307c695 Initial revision
drewp
parents:
diff changeset
285 dispatcher.send("output levels",val=out.get_levels())
45b12307c695 Initial revision
drewp
parents:
diff changeset
286 dmxclient.outputlevels(out.get_dmx_list(),twisted=1)
45b12307c695 Initial revision
drewp
parents:
diff changeset
287
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
288 def statuslines(master):
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
289 for signame,textfilter in [
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
290 ('input time',lambda t: "%.2fs"%t),
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
291 ('output levels',
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
292 lambda levels: textwrap.fill("; ".join(["%s:%.2f"%(n,v)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
293 for n,v in
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
294 levels.items()]),70)),
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
295 ('update period',lambda t: "%.1fms"%(t*1000)),
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
296 ]:
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
297 l = tk.Label(master,anchor='w',justify='left')
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
298 l.pack(side='top',fill='x')
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
299 dispatcher.connect(lambda val,l=l,sn=signame,tf=textfilter:
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
300 l.config(text=sn+": "+tf(val)),
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
301 signame,weak=0)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
302
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
303
45b12307c695 Initial revision
drewp
parents:
diff changeset
304 #######################################################################
45b12307c695 Initial revision
drewp
parents:
diff changeset
305 root=tk.Tk()
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
306 root.wm_geometry("790x930")
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
307 #root.tk_focusFollowsMouse()
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
308
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
309 m=Music()
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
310
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
311 zc = Zoomcontrol(root)
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
312 zc.pack(side='top',fill='x')
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
313
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
314 cs = Curveset()
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
315 csv = Curvesetview(root,cs)
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
316 csv.pack(side='top',fill='both',exp=1)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
317
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
318 for loop in range(6):
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
319 cs.add_curve('c'+str(loop+1),Curve())
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
320
45b12307c695 Initial revision
drewp
parents:
diff changeset
321 subterms = []
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
322 for subname in "zip_orange","zip_red":
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
323
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
324 se = Subexpr(cs)
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
325 if subname=='zip_orange':
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
326 se.expr="c1(t)+c2(t)+c3(t)+c4(t)+c5(t)"
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
327
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
328 st=Subterm()
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
329 st.sub=Submaster.Submaster(subname)
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
330 st.subexpr=se
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
331
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
332 stv=Subtermview(root,st)
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
333 stv.pack(side='top',fill='x')
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
334 subterms.append(st)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
335
45b12307c695 Initial revision
drewp
parents:
diff changeset
336 out = Output(subterms)
45b12307c695 Initial revision
drewp
parents:
diff changeset
337
197
ba2677823b35 zoom control and other cleanups. also reads song length now
drewp
parents: 196
diff changeset
338 statuslines(root)
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
339
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
340 recent_t=[]
45b12307c695 Initial revision
drewp
parents:
diff changeset
341 def update():
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
342 d = m.current_time()
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
343 d.addCallback(update2)
45b12307c695 Initial revision
drewp
parents:
diff changeset
344 d.addErrback(updateerr)
45b12307c695 Initial revision
drewp
parents:
diff changeset
345 def updateerr(e):
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
346 reactor.callLater(1,update)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
347 def update2(t):
196
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
348 global recent_t
07bac5061d69 evaluates curves based on input time from ascoltami; outputs dmx
drewp
parents: 0
diff changeset
349 reactor.callLater(.001,update)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
350
45b12307c695 Initial revision
drewp
parents:
diff changeset
351 recent_t = recent_t[-50:]+[t]
45b12307c695 Initial revision
drewp
parents:
diff changeset
352 period = (recent_t[-1]-recent_t[0])/len(recent_t)
45b12307c695 Initial revision
drewp
parents:
diff changeset
353 dispatcher.send("update period",val=period)
45b12307c695 Initial revision
drewp
parents:
diff changeset
354 out.send_dmx(t)
45b12307c695 Initial revision
drewp
parents:
diff changeset
355 update()
45b12307c695 Initial revision
drewp
parents:
diff changeset
356
45b12307c695 Initial revision
drewp
parents:
diff changeset
357 tksupport.install(root,ms=10)
45b12307c695 Initial revision
drewp
parents:
diff changeset
358 if 0:
45b12307c695 Initial revision
drewp
parents:
diff changeset
359 sys.path.append("/home/drewp/projects/editor/pour")
45b12307c695 Initial revision
drewp
parents:
diff changeset
360 from utils import runstats
45b12307c695 Initial revision
drewp
parents:
diff changeset
361 runstats("reactor.run()")
45b12307c695 Initial revision
drewp
parents:
diff changeset
362 else:
45b12307c695 Initial revision
drewp
parents:
diff changeset
363 reactor.run()