annotate vixm/player.py @ 46:2d570448aba5 pure

Abstract out the stats code a little bit more This is in preparation for moving the stats code completely out of player code
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Mon, 06 Nov 2006 05:19:23 -0500
parents c46d3f8833f9
children 8396cc3816ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
44
c46d3f8833f9 Move list and stats into ~/.vixm/
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 43
diff changeset
1 import time, os
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
2 from threading import Thread
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
3
35
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
4 import mad, ao
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
5
43
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
6 import playlist, stats, rb_dbus
40
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
7
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
8 class playerThread(Thread):
35
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
9 def __init__(self):
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
10 Thread.__init__(self)
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
11
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
12 self.shutdown = False
32
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 30
diff changeset
13 self.playing = False
40
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
14 self.end_current = False
32
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 30
diff changeset
15
34
8028f2addb00 Destroy all traces of xmms
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 32
diff changeset
16 self.current = None
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
17
37
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
18 self.blksize = 40960
36
c6a3a4d09a9a Small fix & change output to OSS
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 35
diff changeset
19 self.dev = ao.AudioDevice("oss")
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
20
44
c46d3f8833f9 Move list and stats into ~/.vixm/
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 43
diff changeset
21 self.stats = stats.statsengine(os.path.join(os.environ['HOME'], ".vixm/stats"))
42
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 40
diff changeset
22
43
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
23 self.rb = rb_dbus.RhythmboxDbusThread(self)
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
24 self.rb.start()
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
25
40
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
26 def play_next(self, lists):
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
27 try:
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
28 # pop song off the PRIO queue
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
29 s = lists[playlist.LIST_PRIO].pop()
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
30 except IndexError:
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
31 # no song to pop
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
32 s = lists[playlist.LIST_DEFAULT].next()
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
33 except ValueError:
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
34 print "WTF is going on?!"
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
35 raise ValueError
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
36
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
37 self.play(s)
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
38
32
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 30
diff changeset
39 def play(self, s):
40
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
40 if self.playing:
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
41 self.end_current = True
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
42
35
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
43 self.playing = True
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
44 self.current = s
46
2d570448aba5 Abstract out the stats code a little bit more
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 44
diff changeset
45 self.stats.playing(s)
35
a7d370f326df Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 34
diff changeset
46
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
47 def run(self):
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
48 while not self.shutdown:
43
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
49 c = self.current
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
50
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
51 if c and self.playing:
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
52 # notify everyone
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
53
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
54 self.rb.notify("file://" + c["file"], \
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
55 c["title"], c["artist"])
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
56
37
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
57 # play the file
43
d3ac3a46a294 Abuse the Rhythmbox dbus messages to announce what is being played
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 42
diff changeset
58 mf = mad.MadFile(c["file"])
37
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
59
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
60 while True:
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
61 buf = mf.read(self.blksize)
40
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
62 if buf is None or \
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
63 self.shutdown or \
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
64 self.end_current:
37
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
65 break
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
66 self.dev.play(buf, len(buf))
40
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
67
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
68 if self.end_current:
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
69 self.end_current = False
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
70 else:
42
bc6db55448e1 statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 40
diff changeset
71 self.stats.played()
40
06b5a7db3d19 Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 37
diff changeset
72 self.playing = False
37
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
73 else:
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
74 # nothing to play, just sleep
87aa31e41f4a Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 36
diff changeset
75 time.sleep(0.5)
30
860c891de6bb Remove lots of xmms-related code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
76