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 never continue through playlist. maybe use 1-song mpd playlists?
|
|
20
|
|
21 install pympd, (pypi too)
|
|
22
|
|
23 """
|
|
24
|
|
25 from __future__ import division,nested_scopes
|
|
26
|
|
27 from optparse import OptionParser
|
|
28 import os,math
|
|
29 import Tkinter as tk
|
|
30
|
|
31 from twisted.internet import reactor,tksupport
|
|
32 from twisted.internet.error import CannotListenError
|
|
33 from twisted.web import xmlrpc, server
|
|
34
|
|
35 import run_local
|
|
36 from light9 import networking, showconfig
|
|
37
|
|
38 import sys
|
|
39 sys.path.append("/home/drewp/projects/pympd")
|
|
40 from pympd import Mpd
|
|
41
|
|
42 appstyle={'fg':'white','bg':'black'}
|
|
43
|
|
44 class XMLRPCServe(xmlrpc.XMLRPC):
|
|
45 def __init__(self,player):
|
|
46 xmlrpc.XMLRPC.__init__(self)
|
|
47 self.player=player
|
|
48
|
|
49 def xmlrpc_echo(self,x):
|
|
50 return x
|
|
51
|
|
52 def xmlrpc_playfile(self,musicfilename):
|
|
53 self.player.play(musicfilename)
|
|
54 return 'ok'
|
|
55 def xmlrpc_stop(self):
|
|
56 self.player.state.set('stop')
|
|
57 return 'ok'
|
|
58 def xmlrpc_gettime(self):
|
|
59 """returns seconds from start of song"""
|
|
60 return float(self.player.current_time.get())
|
|
61 def xmlrpc_songlength(self):
|
|
62 """song length, in seconds"""
|
|
63 return float(self.player.total_time.get())
|
|
64 def xmlrpc_songname(self):
|
|
65 """song filename, or None"""
|
|
66 return self.player.filename or "No song"
|
|
67
|
|
68 class Player:
|
|
69 """semprini-style access to mpd"""
|
|
70
|
|
71 def __init__(self, app, playlist, media=None):
|
|
72
|
|
73 self.mpd = Mpd()
|
|
74 reactor.connectTCP('dash',6600,self.mpd)
|
|
75
|
|
76 self.state = tk.StringVar()
|
|
77 self.state.set("stop") # 'stop' 'pause' 'play'
|
|
78
|
|
79 self.current_time = tk.DoubleVar()
|
|
80 self.total_time = tk.DoubleVar()
|
|
81 self.filename_var = tk.StringVar()
|
|
82
|
|
83 self.pollStatus()
|
|
84
|
|
85 def pollStatus(self):
|
|
86 self.mpd.status().addCallback(self.pollStatus2)
|
|
87
|
|
88 def pollStatus2(self,stat):
|
|
89 for attr1,attr2 in [('state','state'),
|
|
90 ('time_elapsed','current_time'),
|
|
91 ('time_total','total_time')]:
|
|
92 if not hasattr(stat,attr1):
|
|
93 continue
|
|
94 v = getattr(stat,attr1)
|
|
95 if getattr(self,attr2).get() != v:
|
|
96 getattr(self,attr2).set(v)
|
|
97 self.mpd.currentsong().addCallback(self.pollStatus3)
|
|
98
|
|
99 def pollStatus3(self,song):
|
|
100 if hasattr(song,'file'):
|
|
101 self.filename_var.set(song.file)
|
|
102
|
|
103 # if we're stopped, there will be no file, and the UI will
|
|
104 # show a stale filename. that means Play might not play the
|
|
105 # indicated file (if another mpd client has changed the song
|
|
106 # choice). another method is needed to get the file that is
|
|
107 # about to play
|
|
108
|
|
109 reactor.callLater(.05, self.pollStatus)
|
|
110
|
|
111 def play(self, song_path):
|
|
112 self.mpd.play()
|
|
113
|
|
114 def stop(self):
|
|
115 self.mpd.stop()
|
|
116
|
|
117 def seek_to(self, time):
|
|
118 self.mpd.seek(seconds=time)
|
|
119
|
|
120 def play_pause_toggle(self):
|
|
121 def finish(status):
|
|
122 if status.state == 'play':
|
|
123 self.mpd.pause()
|
|
124 else:
|
|
125 self.mpd.play()
|
|
126 self.mpd.status().addCallback(finish)
|
|
127
|
|
128
|
|
129 def buildsonglist(root,songfiles,player):
|
|
130 songlist=tk.Frame(root,bd=2,relief='raised',bg='black')
|
|
131
|
|
132 prefixlen=len(os.path.commonprefix(songfiles))
|
|
133 # include to the last os.sep- dont crop path elements in the middle
|
|
134 prefixlen=songfiles[0].rfind(os.sep)+1
|
|
135 maxsfwidth=max([len(x[prefixlen:]) for x in songfiles])
|
|
136
|
|
137 for i,sf in enumerate(songfiles):
|
|
138 b=tk.Button(songlist,text=sf[prefixlen:],width=maxsfwidth,
|
|
139 anchor='w',pady=0,bd=0,relief='flat',
|
|
140 font="arial 17 bold")
|
|
141 b.bind("<Configure>",lambda ev,b=b:
|
|
142 b.config(font="arial %d bold" % min(15,int((ev.height-3)*.8))))
|
|
143 try:
|
|
144 # rainbow colors
|
|
145 frac=i/len(songfiles)
|
|
146 b.config(bg='black',
|
|
147 fg="#%02x%02x%02x" % tuple([int(255*(.7+.3*
|
|
148 math.sin(frac*4+x))
|
|
149 ) for x in 1,2,3]))
|
|
150 except Exception,e:
|
|
151 print "rainbow failed: %s"%e
|
|
152
|
|
153 b.config(command=lambda sf=sf: player.play(sf))
|
|
154 b.pack(side='top',fill='both',exp=1,padx=0,pady=0,ipadx=0,ipady=0)
|
|
155
|
|
156
|
|
157 def color_buttons(x, y, z, sf=sf, b=b):
|
|
158 name = player.filename_var.get()
|
|
159 if name == sf[prefixlen:]:
|
|
160 b['bg'] = 'grey50'
|
|
161 else:
|
|
162 b['bg'] = 'black'
|
|
163 player.filename_var.trace("w", color_buttons)
|
|
164 return songlist
|
|
165
|
|
166 def buildseeker(master,player):
|
|
167 seeker=tk.Frame(master,bg='black')
|
|
168
|
|
169 scl=tk.Scale(seeker, orient="horiz",
|
|
170 from_=-4,to_=100,
|
|
171 sliderlen=20,width=20,
|
|
172 res=0.001,
|
|
173 showvalue=0,
|
|
174 variable=player.current_time,
|
|
175 troughcolor='black',
|
|
176 bg='lightblue3',
|
|
177 )
|
|
178 scl.pack(fill='x',side='top')
|
|
179
|
|
180 left_var=tk.DoubleVar()
|
|
181 for txt,var,fmt in (('Current',player.current_time,"%.2f"),
|
|
182 ('Song len',player.total_time,"%.2f",),
|
|
183 ('Left',left_var, "%.2f"),
|
|
184 ('Song',player.filename_var, "%s"),
|
|
185 ('State', player.state, "%s")):
|
|
186 tk.Label(seeker,text=txt,
|
|
187 relief='raised',bd=1,font='arial 9',
|
|
188 **appstyle).pack(side='left')
|
|
189 l = tk.Label(seeker,width=7, anchor='w', text=var.get(),
|
|
190 relief='sunken',bd=1,font='arial 12 bold',
|
|
191 bg='#800000',fg='white')
|
|
192 if txt == 'Song':
|
|
193 l.config(anchor='e')
|
|
194 l.pack(side='left',expand=1, fill='x')
|
|
195
|
|
196 var.trace_variable('w', lambda a,b,c,l=l,fmt=fmt,var=var: l.config(text=fmt % var.get()))
|
|
197
|
|
198 # update end time as the song changes
|
|
199 player.total_time.trace("w",lambda *args: (
|
|
200 scl.config(to_=player.total_time.get()+15)))
|
|
201
|
|
202 def fixleft(*args):
|
|
203 # update time-left variable
|
|
204 left_var.set(player.total_time.get()-player.current_time.get())
|
|
205
|
|
206 if 1: # new dmcc code
|
|
207 if player.current_time.get() < 0 or left_var.get() < 0:
|
|
208 scl['troughcolor'] = 'blue'
|
|
209 else:
|
|
210 scl['troughcolor'] = 'black'
|
|
211
|
|
212
|
|
213 player.total_time.trace("w",fixleft)
|
|
214 player.current_time.trace("w",fixleft)
|
|
215
|
|
216 # dragging the scl changes the player time (which updates the scl)
|
|
217
|
|
218 # due to mpd having to seemingly play a whole block at every new
|
|
219 # seek point, we may want a mode that pauses playback while the
|
|
220 # mouse is down (or is moving too fast; or we've sent a seek too
|
|
221 # recently)
|
|
222
|
|
223 scl.mouse_state=0
|
|
224 def set_mouse_state(evt):
|
|
225 scl.mouse_state = evt.state
|
|
226 def seeker_cb(time):
|
|
227 if scl.mouse_state:
|
|
228 player.seek_to(float(time))
|
|
229
|
|
230 scl.config(command=seeker_cb)
|
|
231 scl.bind("<Motion>", set_mouse_state)
|
|
232 scl.bind("<B1-Motion>",set_mouse_state)
|
|
233
|
|
234 def b1down(evt):
|
|
235 scl.mouse_state = 1
|
|
236 def b1up(evt):
|
|
237 scl.mouse_state = 0
|
|
238 scl.bind("<ButtonPress-1>", b1down)
|
|
239 scl.bind("<ButtonRelease-1>", b1up)
|
|
240
|
|
241 return seeker
|
|
242
|
|
243 ############################
|
|
244
|
|
245 parser=OptionParser()
|
|
246
|
|
247 (options,songfiles)=parser.parse_args()
|
|
248
|
|
249 if len(songfiles)<1:
|
|
250 songfiles = [f for f in os.listdir(showconfig.musicDir())
|
|
251 if f.endswith('wav')]
|
|
252
|
|
253 root=tk.Tk()
|
|
254 root.wm_title("ascoltami")
|
|
255 root.wm_geometry("656x736+263+0")
|
|
256 root.config(bg="black")
|
|
257 player=Player(None,None)
|
|
258
|
|
259
|
|
260 songlist=buildsonglist(root,songfiles,player)
|
|
261 songlist.pack(fill='both',exp=1)
|
|
262
|
|
263 f2=tk.Frame(bg='black')
|
|
264 f3=tk.Frame(f2,bg='black')
|
|
265 statebuttons = {} # lowercased name : Button
|
|
266 for txt,cmd,key in (('Stop', player.stop, "<Control-s>"),
|
|
267 ('Pause', player.play_pause_toggle, "<Control-p>"),
|
|
268 ('Skip Intro',lambda: player.seek_to(-.5), "<Control-i>"),
|
|
269 ):
|
|
270 b = tk.Button(f3,text=txt,command=cmd, font='arial 16 bold', height=3,**appstyle)
|
|
271 b.pack(side='left', fill='x', expand=1)
|
|
272 # keyboard bindings
|
|
273 root.bind(key, lambda evt, cmd=cmd: cmd())
|
|
274 statebuttons[txt.lower()] = b
|
|
275
|
|
276 f3.pack(side='top',fill='x')
|
|
277
|
|
278 def update_state_buttons(*args):
|
|
279 global statebuttons
|
|
280 state = player.state.get()
|
|
281 print "State", state
|
|
282
|
|
283 if state in ('stop', 'pause'):
|
|
284 statebuttons['pause']['text'] = 'Play'
|
|
285 else:
|
|
286 statebuttons['pause']['text'] = 'Pause'
|
|
287
|
|
288 colors = {'stop' : 'red',
|
|
289 'play' : 'blue',
|
|
290 'pause' : 'green'} # very confusing -- play and pause supply colors
|
|
291 # for each other!
|
|
292 for name, button in statebuttons.items():
|
|
293 if name == 'pause' and state not in ('stop', 'pause'):
|
|
294 name = 'play'
|
|
295
|
|
296 if state == name: # name gets changed sometimes for 'pause' -- see right above
|
|
297 button['bg'] = colors.get(name, 'black')
|
|
298 else:
|
|
299 button['bg'] = 'black'
|
|
300
|
|
301 player.state.trace_variable('w', update_state_buttons)
|
|
302 update_state_buttons()
|
|
303
|
|
304 seeker=buildseeker(f2,player)
|
|
305 seeker.pack(fill='x',exp=1)
|
|
306 f2.pack(side='bottom',fill='x')
|
|
307
|
|
308 tksupport.install(root,ms=10)
|
|
309
|
|
310 try:
|
|
311 reactor.listenTCP(networking.musicPort(),server.Site(XMLRPCServe(player)))
|
|
312 print "started server on %s" % networking.musicPort()
|
|
313 except CannotListenError:
|
|
314 print "no server started- %s is in use" % networking.musicPort()
|
|
315
|
|
316 root.bind("<Destroy>",lambda ev: reactor.stop)
|
|
317 root.protocol('WM_DELETE_WINDOW', reactor.stop)
|
|
318
|
|
319 def func_tracer(frame, event, arg):
|
|
320 if event == "call":
|
|
321 co = frame.f_code
|
|
322 if 'twisted' not in co.co_filename and \
|
|
323 'python2.3' not in co.co_filename and \
|
|
324 co.co_filename != '<string>':
|
|
325 print co.co_filename, co.co_name #, co_firstlineno
|
|
326
|
|
327 return func_tracer
|
|
328
|
|
329 # sys.settrace(func_tracer)
|
|
330
|
|
331 reactor.run()
|