Mercurial > code > home > repos > light9
annotate bin/ascoltami @ 229:c5a79314afdf
bigger asco window
author | drewp@bigasterisk.com |
---|---|
date | Sun, 05 Jun 2005 07:03:32 +0000 |
parents | c7797ad42684 |
children | d19c607c41dc |
rev | line source |
---|---|
217 | 1 #!/usr/bin/env python |
2 | |
3 # this is a fork from semprini/ascotalmi to use mpd | |
4 | |
5 """ a separate player program from Semprini.py. name means 'listen to | |
6 me' in italian. | |
7 | |
8 features and limitations: | |
9 | |
10 xmlrpc interface for: | |
11 getting the current time in the playing song | |
12 requesting what song is playing | |
13 saying what song should play | |
14 | |
15 todo: | |
16 | |
17 presong and postsong silence | |
18 | |
19 """ | |
20 | |
21 from __future__ import division,nested_scopes | |
22 | |
23 from optparse import OptionParser | |
227 | 24 import os,math,time |
217 | 25 import Tkinter as tk |
26 | |
27 from twisted.internet import reactor,tksupport | |
28 from twisted.internet.error import CannotListenError | |
29 from twisted.web import xmlrpc, server | |
30 | |
31 import run_local | |
32 from light9 import networking, showconfig | |
33 | |
34 from pympd import Mpd | |
35 | |
36 appstyle={'fg':'white','bg':'black'} | |
37 | |
38 class XMLRPCServe(xmlrpc.XMLRPC): | |
39 def __init__(self,player): | |
40 xmlrpc.XMLRPC.__init__(self) | |
41 self.player=player | |
42 | |
43 def xmlrpc_echo(self,x): | |
44 return x | |
45 | |
46 def xmlrpc_playfile(self,musicfilename): | |
47 self.player.play(musicfilename) | |
48 return 'ok' | |
49 def xmlrpc_stop(self): | |
50 self.player.state.set('stop') | |
51 return 'ok' | |
52 def xmlrpc_gettime(self): | |
53 """returns seconds from start of song""" | |
227 | 54 return float(self.player.smoothCurrentTime()) |
217 | 55 def xmlrpc_songlength(self): |
56 """song length, in seconds""" | |
57 return float(self.player.total_time.get()) | |
58 def xmlrpc_songname(self): | |
59 """song filename, or None""" | |
223 | 60 return self.player.filename_var.get() or "No song" |
217 | 61 |
62 class Player: | |
223 | 63 """semprini-style access to mpd. in here is where we add the padding""" |
217 | 64 |
65 def __init__(self, app, playlist, media=None): | |
66 | |
67 self.mpd = Mpd() | |
226 | 68 reactor.connectTCP(*(networking.mpdServer()+(self.mpd,))) |
217 | 69 |
70 self.state = tk.StringVar() | |
71 self.state.set("stop") # 'stop' 'pause' 'play' | |
72 | |
73 self.current_time = tk.DoubleVar() | |
74 self.total_time = tk.DoubleVar() | |
75 self.filename_var = tk.StringVar() | |
76 | |
227 | 77 self.pre_post_names = showconfig.prePostSong() |
78 | |
79 self.last_poll_time = None | |
80 | |
217 | 81 self.pollStatus() |
227 | 82 |
83 | |
84 def smoothCurrentTime(self): | |
85 """like self.current_time.get, but more accurate""" | |
86 if self.last_poll_time and self.state.get() == 'play': | |
87 dt = time.time() - self.last_poll_time | |
88 else: | |
89 dt = 0 | |
90 return self.current_time.get() + dt | |
91 | |
217 | 92 def pollStatus(self): |
227 | 93 if self.state.get() == 'stop': |
94 self.current_time.set(-4) | |
95 | |
217 | 96 self.mpd.status().addCallback(self.pollStatus2) |
97 | |
227 | 98 def pollStatus2(self, stat): |
99 | |
100 if self.state.get() != stat.state: | |
101 self.state.set(stat.state) | |
102 | |
217 | 103 |
227 | 104 if hasattr(stat, 'time_elapsed'): |
105 if stat.song == 1: | |
106 t = stat.time_elapsed | |
107 elif stat.song == 0: | |
108 t = stat.time_elapsed - stat.time_total | |
109 elif stat.song == 2: | |
110 t = self.total_time.get() + stat.time_elapsed | |
217 | 111 |
227 | 112 self.current_time.set(t) |
113 | |
114 self.last_poll_time = time.time() | |
115 | |
217 | 116 reactor.callLater(.05, self.pollStatus) |
117 | |
118 def play(self, song_path): | |
220
13c089886f61
added the forgotten curve.py; mpd song path fix
drewp@bigasterisk.com
parents:
218
diff
changeset
|
119 self.mpd.clear() |
227 | 120 self.mpd.add(showconfig.songInMpd(self.pre_post_names[0])) |
121 self.mpd.add(showconfig.songInMpd(song_path)) | |
122 self.mpd.add(showconfig.songInMpd(self.pre_post_names[1])) | |
123 self.filename_var.set(song_path) | |
124 | |
125 # jump to song 1 to get its total_time | |
126 self.mpd.seek(seconds=0, song=1) | |
127 self.mpd.pause() | |
128 self.mpd.status().addCallback(self.play2) | |
129 | |
130 def play2(self, stat): | |
131 self.total_time.set(stat.time_total) | |
132 self.mpd.seek(seconds=0, song=0) | |
217 | 133 |
134 def stop(self): | |
227 | 135 self.mpd.seek(seconds=0, song=0) |
217 | 136 self.mpd.stop() |
137 | |
138 def seek_to(self, time): | |
227 | 139 if time < 0: |
140 self.mpd.seek(seconds=time - (-4), song=0) | |
141 elif time > self.total_time.get(): | |
142 self.mpd.seek(seconds=time - self.total_time.get(), song=2) | |
143 else: | |
144 self.mpd.seek(seconds=time, song=1) | |
217 | 145 |
146 def play_pause_toggle(self): | |
147 def finish(status): | |
148 if status.state == 'play': | |
149 self.mpd.pause() | |
150 else: | |
151 self.mpd.play() | |
152 self.mpd.status().addCallback(finish) | |
153 | |
154 | |
155 def buildsonglist(root,songfiles,player): | |
156 songlist=tk.Frame(root,bd=2,relief='raised',bg='black') | |
157 | |
158 prefixlen=len(os.path.commonprefix(songfiles)) | |
159 # include to the last os.sep- dont crop path elements in the middle | |
160 prefixlen=songfiles[0].rfind(os.sep)+1 | |
161 maxsfwidth=max([len(x[prefixlen:]) for x in songfiles]) | |
162 | |
163 for i,sf in enumerate(songfiles): | |
164 b=tk.Button(songlist,text=sf[prefixlen:],width=maxsfwidth, | |
165 anchor='w',pady=0,bd=0,relief='flat', | |
166 font="arial 17 bold") | |
167 b.bind("<Configure>",lambda ev,b=b: | |
168 b.config(font="arial %d bold" % min(15,int((ev.height-3)*.8)))) | |
169 try: | |
170 # rainbow colors | |
171 frac=i/len(songfiles) | |
172 b.config(bg='black', | |
173 fg="#%02x%02x%02x" % tuple([int(255*(.7+.3* | |
174 math.sin(frac*4+x)) | |
175 ) for x in 1,2,3])) | |
176 except Exception,e: | |
177 print "rainbow failed: %s"%e | |
178 | |
179 b.config(command=lambda sf=sf: player.play(sf)) | |
180 b.pack(side='top',fill='both',exp=1,padx=0,pady=0,ipadx=0,ipady=0) | |
181 | |
182 | |
183 def color_buttons(x, y, z, sf=sf, b=b): | |
184 name = player.filename_var.get() | |
185 if name == sf[prefixlen:]: | |
186 b['bg'] = 'grey50' | |
187 else: | |
188 b['bg'] = 'black' | |
189 player.filename_var.trace("w", color_buttons) | |
190 return songlist | |
218 | 191 |
217 | 192 |
218 | 193 class Seeker(tk.Frame): |
194 """scale AND labels below it""" | |
195 def __init__(self,master,player): | |
196 tk.Frame.__init__(self,master,bg='black') | |
217 | 197 |
218 | 198 self.scl = scl = tk.Scale(self, orient="horiz", |
199 from_=-4,to_=100, | |
200 sliderlen=20,width=20, | |
201 res=0.001, | |
202 showvalue=0, | |
203 variable=player.current_time, | |
204 troughcolor='black', | |
205 bg='lightblue3', | |
206 ) | |
207 scl.pack(fill='x',side='top') | |
217 | 208 |
218 | 209 self.buildstatus(player) |
217 | 210 |
211 | |
218 | 212 # dragging the scl changes the player time (which updates the scl) |
213 | |
214 # due to mpd having to seemingly play a whole block at every new | |
215 # seek point, we may want a mode that pauses playback while the | |
216 # mouse is down (or is moving too fast; or we've sent a seek too | |
217 # recently) | |
217 | 218 |
218 | 219 scl.mouse_state=0 |
220 def set_mouse_state(evt): | |
221 scl.mouse_state = evt.state | |
222 def seeker_cb(time): | |
223 if scl.mouse_state: | |
224 player.seek_to(float(time)) | |
225 | |
226 scl.config(command=seeker_cb) | |
227 scl.bind("<Motion>", set_mouse_state) | |
228 scl.bind("<B1-Motion>",set_mouse_state) | |
229 | |
230 def b1down(evt): | |
231 scl.mouse_state = 1 | |
232 def b1up(evt): | |
233 scl.mouse_state = 0 | |
234 scl.bind("<ButtonPress-1>", b1down) | |
235 scl.bind("<ButtonRelease-1>", b1up) | |
217 | 236 |
218 | 237 def buildstatus(self,player): |
238 left_var=tk.DoubleVar() | |
239 for txt,var,fmt in (('Current',player.current_time,"%.2f"), | |
240 ('Song len',player.total_time,"%.2f",), | |
241 ('Left',left_var, "%.2f"), | |
242 ('Song',player.filename_var, "%s"), | |
243 ('State', player.state, "%s")): | |
244 tk.Label(self,text=txt, | |
245 relief='raised',bd=1,font='arial 9', | |
223 | 246 **appstyle).pack(side='left',fill='y') |
218 | 247 l = tk.Label(self,width=7, anchor='w', text=var.get(), |
248 relief='sunken',bd=1,font='arial 12 bold', | |
223 | 249 padx=2,pady=2, |
218 | 250 bg='#800000',fg='white') |
251 if txt == 'Song': | |
252 l.config(anchor='e') | |
253 l.pack(side='left',expand=1, fill='x') | |
254 | |
255 var.trace_variable('w', | |
256 lambda a,b,c,l=l,fmt=fmt,var=var: | |
257 l.config(text=fmt % var.get())) | |
258 | |
259 # update end time as the song changes | |
260 player.total_time.trace("w",lambda *args: ( | |
261 self.scl.config(to_=player.total_time.get()+15))) | |
217 | 262 |
218 | 263 def fixleft(*args): |
264 # update time-left variable | |
265 left_var.set(player.total_time.get()-player.current_time.get()) | |
266 | |
267 if player.current_time.get() < 0 or left_var.get() < 0: | |
268 self.scl['troughcolor'] = 'blue' | |
269 else: | |
270 self.scl['troughcolor'] = 'black' | |
271 | |
272 player.total_time.trace("w",fixleft) | |
273 player.current_time.trace("w",fixleft) | |
217 | 274 |
218 | 275 class ControlButtons(tk.Frame): |
276 def __init__(self,master): | |
277 tk.Frame.__init__(self,master,bg='black') | |
278 | |
279 self.statebuttons = {} # lowercased name : Button | |
280 for txt,cmd,key in (('Stop', player.stop, "<Control-s>"), | |
281 ('Pause', player.play_pause_toggle, "<Control-p>"), | |
282 ('Skip Intro',lambda: player.seek_to(-.5), "<Control-i>"), | |
283 ): | |
284 b = tk.Button(self, text=txt, command=cmd, font='arial 16 bold', | |
285 height=3,**appstyle) | |
286 b.pack(side='left', fill='x', expand=1) | |
287 # keyboard bindings | |
288 root.bind(key, lambda evt, cmd=cmd: cmd()) | |
289 self.statebuttons[txt.lower()] = b | |
290 | |
291 def update_state_buttons(self,*args): | |
292 state = player.state.get() | |
293 print "State", state | |
294 | |
295 if state in ('stop', 'pause'): | |
296 self.statebuttons['pause']['text'] = 'Play' | |
297 else: | |
298 self.statebuttons['pause']['text'] = 'Pause' | |
299 | |
300 colors = {'stop' : 'red', | |
301 'play' : 'blue', | |
302 'pause' : 'green'} # very confusing -- play and pause supply colors | |
303 # for each other! | |
304 for name, button in self.statebuttons.items(): | |
305 if name == 'pause' and state not in ('stop', 'pause'): | |
306 name = 'play' | |
307 | |
308 if state == name: # name gets changed sometimes for 'pause' -- see right above | |
309 button['bg'] = colors.get(name, 'black') | |
310 else: | |
311 button['bg'] = 'black' | |
217 | 312 |
313 ############################ | |
314 | |
315 parser=OptionParser() | |
316 | |
317 (options,songfiles)=parser.parse_args() | |
318 | |
319 if len(songfiles)<1: | |
320 songfiles = [f for f in os.listdir(showconfig.musicDir()) | |
321 if f.endswith('wav')] | |
322 | |
323 root=tk.Tk() | |
324 root.wm_title("ascoltami") | |
229 | 325 root.wm_geometry("+1270+440") |
217 | 326 root.config(bg="black") |
327 player=Player(None,None) | |
328 | |
218 | 329 songlist = buildsonglist(root,songfiles,player) |
217 | 330 songlist.pack(fill='both',exp=1) |
331 | |
218 | 332 f2 = tk.Frame(bg='black') |
333 buts = ControlButtons(f2) | |
334 buts.pack(side='top',fill='x') | |
217 | 335 |
218 | 336 player.state.trace_variable('w', buts.update_state_buttons) |
337 buts.update_state_buttons() | |
217 | 338 |
218 | 339 seeker = Seeker(f2,player) |
217 | 340 seeker.pack(fill='x',exp=1) |
341 f2.pack(side='bottom',fill='x') | |
342 | |
343 tksupport.install(root,ms=10) | |
344 | |
345 try: | |
218 | 346 reactor.listenTCP(networking.musicPort(),server.Site(XMLRPCServe(player))) |
347 print "started server on %s" % networking.musicPort() | |
217 | 348 except CannotListenError: |
218 | 349 print "no server started- %s is in use" % networking.musicPort() |
217 | 350 |
351 root.bind("<Destroy>",lambda ev: reactor.stop) | |
352 root.protocol('WM_DELETE_WINDOW', reactor.stop) | |
353 | |
354 reactor.run() |