Files
@ 76e326329610
Branch filter:
Location: light9/bin/ascoltami
76e326329610
11.3 KiB
text/plain
working on changing cursors in curvecalc. nothing to see yet
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 | #!/usr/bin/env python
# this is a fork from semprini/ascotalmi to use mpd
""" a separate player program from Semprini.py. name means 'listen to
me' in italian.
features and limitations:
xmlrpc interface for:
getting the current time in the playing song
requesting what song is playing
saying what song should play
todo:
presong and postsong silence
"""
from __future__ import division,nested_scopes
from optparse import OptionParser
import os,math,time
import Tkinter as tk
from twisted.internet import reactor,tksupport
from twisted.internet.error import CannotListenError
from twisted.web import xmlrpc, server
import run_local
from light9 import networking, showconfig
from pympd import Mpd
appstyle={'fg':'white','bg':'black'}
class XMLRPCServe(xmlrpc.XMLRPC):
def __init__(self,player):
xmlrpc.XMLRPC.__init__(self)
self.player=player
def xmlrpc_echo(self,x):
return x
def xmlrpc_playfile(self,musicfilename):
self.player.play(musicfilename)
return 'ok'
def xmlrpc_stop(self):
self.player.state.set('stop')
return 'ok'
def xmlrpc_gettime(self):
"""returns seconds from start of song"""
return float(self.player.smoothCurrentTime())
def xmlrpc_songlength(self):
"""song length, in seconds"""
return float(self.player.total_time.get())
def xmlrpc_songname(self):
"""song filename, or None"""
return self.player.filename_var.get() or "No song"
class Player:
"""semprini-style access to mpd. in here is where we add the padding"""
def __init__(self, app, playlist, media=None):
self.mpd = Mpd()
reactor.connectTCP(*(networking.mpdServer()+(self.mpd,)))
self.state = tk.StringVar()
self.state.set("stop") # 'stop' 'pause' 'play'
self.current_time = tk.DoubleVar()
self.total_time = tk.DoubleVar()
self.filename_var = tk.StringVar()
self.pre_post_names = showconfig.prePostSong()
self.last_poll_time = None
self.pollStatus()
def smoothCurrentTime(self):
"""like self.current_time.get, but more accurate"""
if self.last_poll_time and self.state.get() == 'play':
dt = time.time() - self.last_poll_time
else:
dt = 0
return self.current_time.get() + dt
def pollStatus(self):
if self.state.get() == 'stop':
self.current_time.set(-4)
self.mpd.status().addCallback(self.pollStatus2)
def pollStatus2(self, stat):
if self.state.get() != stat.state:
self.state.set(stat.state)
if hasattr(stat, 'time_elapsed'):
if stat.song == 1:
t = stat.time_elapsed
elif stat.song == 0:
t = stat.time_elapsed - stat.time_total
elif stat.song == 2:
t = self.total_time.get() + stat.time_elapsed
self.current_time.set(t)
self.last_poll_time = time.time()
reactor.callLater(.05, self.pollStatus)
def play(self, song_path):
self.mpd.clear()
self.mpd.add(showconfig.songInMpd(self.pre_post_names[0]))
self.mpd.add(showconfig.songInMpd(song_path))
self.mpd.add(showconfig.songInMpd(self.pre_post_names[1]))
self.filename_var.set(song_path)
# jump to song 1 to get its total_time
self.mpd.seek(seconds=0, song=1)
self.mpd.pause()
self.mpd.status().addCallback(self.play2)
def play2(self, stat):
self.total_time.set(stat.time_total)
self.mpd.seek(seconds=0, song=0)
def stop(self):
self.mpd.seek(seconds=0, song=0)
self.mpd.stop()
def seek_to(self, time):
if time < 0:
self.mpd.seek(seconds=time - (-4), song=0)
elif time > self.total_time.get():
self.mpd.seek(seconds=time - self.total_time.get(), song=2)
else:
self.mpd.seek(seconds=time, song=1)
def play_pause_toggle(self):
def finish(status):
if status.state == 'play':
self.mpd.pause()
else:
self.mpd.play()
self.mpd.status().addCallback(finish)
def buildsonglist(root,songfiles,player):
songlist=tk.Frame(root,bd=2,relief='raised',bg='black')
prefixlen=len(os.path.commonprefix(songfiles))
# include to the last os.sep- dont crop path elements in the middle
prefixlen=songfiles[0].rfind(os.sep)+1
maxsfwidth=max([len(x[prefixlen:]) for x in songfiles])
for i,sf in enumerate(songfiles):
b=tk.Button(songlist,text=sf[prefixlen:],width=maxsfwidth,
anchor='w',pady=0,bd=0,relief='flat',
font="arial 14 bold")
b.bind("<Configure>",lambda ev,b=b:
b.config(font="arial %d bold" % min(12,int((ev.height-3)*.8))))
try:
# rainbow colors
frac=i/len(songfiles)
b.config(bg='black',
fg="#%02x%02x%02x" % tuple([int(255*(.7+.3*
math.sin(frac*4+x))
) for x in 1,2,3]))
except Exception,e:
print "rainbow failed: %s"%e
b.config(command=lambda sf=sf: player.play(sf))
b.pack(side='top',fill='both',exp=1,padx=0,pady=0,ipadx=0,ipady=0)
def color_buttons(x, y, z, sf=sf, b=b):
name = player.filename_var.get()
if name == sf[prefixlen:]:
b['bg'] = 'grey50'
else:
b['bg'] = 'black'
player.filename_var.trace("w", color_buttons)
return songlist
class Seeker(tk.Frame):
"""scale AND labels below it"""
def __init__(self,master,player):
tk.Frame.__init__(self,master,bg='black')
self.scl = scl = tk.Scale(self, orient="horiz",
from_=-4,to_=100,
sliderlen=20,width=20,
res=0.001,
showvalue=0,
variable=player.current_time,
troughcolor='black',
bg='lightblue3',
)
scl.pack(fill='x',side='top')
self.buildstatus(player)
# dragging the scl changes the player time (which updates the scl)
# due to mpd having to seemingly play a whole block at every new
# seek point, we may want a mode that pauses playback while the
# mouse is down (or is moving too fast; or we've sent a seek too
# recently)
scl.mouse_state=0
def set_mouse_state(evt):
scl.mouse_state = evt.state
def seeker_cb(time):
if scl.mouse_state:
player.seek_to(float(time))
scl.config(command=seeker_cb)
scl.bind("<Motion>", set_mouse_state)
scl.bind("<B1-Motion>",set_mouse_state)
def b1down(evt):
scl.mouse_state = 1
def b1up(evt):
scl.mouse_state = 0
scl.bind("<ButtonPress-1>", b1down)
scl.bind("<ButtonRelease-1>", b1up)
def buildstatus(self,player):
left_var=tk.DoubleVar()
for txt,var,fmt in (('Current',player.current_time,"%.2f"),
('Song len',player.total_time,"%.2f",),
('Left',left_var, "%.2f"),
('Song',player.filename_var, "%s"),
('State', player.state, "%s")):
tk.Label(self,text=txt,
relief='raised',bd=1,font='arial 9',
**appstyle).pack(side='left',fill='y')
l = tk.Label(self,width=7, anchor='w', text=var.get(),
relief='sunken',bd=1,font='arial 12 bold',
padx=2,pady=2,
bg='#800000',fg='white')
if txt == 'Song':
l.config(anchor='e')
l.pack(side='left',expand=1, fill='x')
var.trace_variable('w',
lambda a,b,c,l=l,fmt=fmt,var=var:
l.config(text=fmt % var.get()))
# update end time as the song changes
player.total_time.trace("w",lambda *args: (
self.scl.config(to_=player.total_time.get()+15)))
def fixleft(*args):
# update time-left variable
left_var.set(player.total_time.get()-player.current_time.get())
if player.current_time.get() < 0 or left_var.get() < 0:
self.scl['troughcolor'] = 'blue'
else:
self.scl['troughcolor'] = 'black'
player.total_time.trace("w",fixleft)
player.current_time.trace("w",fixleft)
class ControlButtons(tk.Frame):
def __init__(self,master):
tk.Frame.__init__(self,master,bg='black')
self.statebuttons = {} # lowercased name : Button
for txt,cmd,key in (('Stop', player.stop, "<Control-s>"),
('Pause', player.play_pause_toggle, "<Control-p>"),
('Skip Intro',lambda: player.seek_to(-.5), "<Control-i>"),
):
b = tk.Button(self, text=txt, command=cmd, font='arial 16 bold',
height=3,**appstyle)
b.pack(side='left', fill='x', expand=1)
# keyboard bindings
root.bind(key, lambda evt, cmd=cmd: cmd())
self.statebuttons[txt.lower()] = b
def update_state_buttons(self,*args):
state = player.state.get()
print "State", state
if state in ('stop', 'pause'):
self.statebuttons['pause']['text'] = 'Play'
else:
self.statebuttons['pause']['text'] = 'Pause'
colors = {'stop' : 'red',
'play' : 'blue',
'pause' : 'green'} # very confusing -- play and pause supply colors
# for each other!
for name, button in self.statebuttons.items():
if name == 'pause' and state not in ('stop', 'pause'):
name = 'play'
if state == name: # name gets changed sometimes for 'pause' -- see right above
button['bg'] = colors.get(name, 'black')
else:
button['bg'] = 'black'
############################
parser=OptionParser()
(options,songfiles)=parser.parse_args()
if len(songfiles)<1:
songfiles = [f for f in os.listdir(showconfig.musicDir())
if f.endswith('wav')]
songfiles.sort()
root=tk.Tk()
root.wm_title("ascoltami")
#root.wm_geometry("+1270+440")
root.config(bg="black")
player=Player(None,None)
songlist = buildsonglist(root,songfiles,player)
songlist.pack(fill='both',exp=1)
f2 = tk.Frame(bg='black')
buts = ControlButtons(f2)
buts.pack(side='top',fill='x')
player.state.trace_variable('w', buts.update_state_buttons)
buts.update_state_buttons()
seeker = Seeker(f2,player)
seeker.pack(fill='x',exp=1)
f2.pack(side='bottom',fill='x')
tksupport.install(root,ms=10)
try:
reactor.listenTCP(networking.musicPort(),server.Site(XMLRPCServe(player)))
print "started server on %s" % networking.musicPort()
except CannotListenError:
print "no server started- %s is in use" % networking.musicPort()
root.bind("<Destroy>",lambda ev: reactor.stop)
root.protocol('WM_DELETE_WINDOW', reactor.stop)
reactor.run()
|