Mercurial > vixm
annotate vixm/player.py @ 42:bc6db55448e1 pure
statistics engine
author  Josef "Jeff" Sipek <jeffpc@josefsipek.net> 

date  Sun, 05 Nov 2006 19:25:30 0500 
parents  06b5a7db3d19 
children  d3ac3a46a294 
rev  line source 

30
860c891de6bb
Remove lots of xmmsrelated code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

1 import time 
860c891de6bb
Remove lots of xmmsrelated 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 xmmsrelated 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 
42
bc6db55448e1
statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
40
diff
changeset

6 import playlist, stats 
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 xmmsrelated 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 xmmsrelated 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 xmmsrelated code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

11 
860c891de6bb
Remove lots of xmmsrelated 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 xmmsrelated 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 xmmsrelated code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

20 
42
bc6db55448e1
statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
40
diff
changeset

21 self.stats = stats.statsengine("stats") 
bc6db55448e1
statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
40
diff
changeset

22 
40
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

23 def play_next(self, lists): 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

24 try: 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

25 # pop song off the PRIO queue 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

26 s = lists[playlist.LIST_PRIO].pop() 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

27 except IndexError: 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

28 # no song to pop 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

29 s = lists[playlist.LIST_DEFAULT].next() 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

30 except ValueError: 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

31 print "WTF is going on?!" 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

32 raise ValueError 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

33 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

34 self.play(s) 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

35 
32
f97eb9f0c207
Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
30
diff
changeset

36 def play(self, s): 
40
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

37 if self.playing: 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

38 self.end_current = True 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

39 
35
a7d370f326df
Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
34
diff
changeset

40 self.playing = True 
a7d370f326df
Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
34
diff
changeset

41 self.current = s 
42
bc6db55448e1
statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
40
diff
changeset

42 self.stats.current = s 
35
a7d370f326df
Implement song playing
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
34
diff
changeset

43 
30
860c891de6bb
Remove lots of xmmsrelated code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

44 def run(self): 
860c891de6bb
Remove lots of xmmsrelated code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

45 while not self.shutdown: 
37
87aa31e41f4a
Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
36
diff
changeset

46 if self.current and self.playing: 
87aa31e41f4a
Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
36
diff
changeset

47 # play the file 
87aa31e41f4a
Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
36
diff
changeset

48 mf = mad.MadFile(self.current["file"]) 
87aa31e41f4a
Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
36
diff
changeset

49 
87aa31e41f4a
Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
36
diff
changeset

50 while True: 
87aa31e41f4a
Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
36
diff
changeset

51 buf = mf.read(self.blksize) 
40
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

52 if buf is None or \ 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

53 self.shutdown or \ 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

54 self.end_current: 
37
87aa31e41f4a
Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
36
diff
changeset

55 break 
87aa31e41f4a
Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
36
diff
changeset

56 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

57 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

58 if self.end_current: 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

59 self.end_current = False 
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

60 else: 
42
bc6db55448e1
statistics engine
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
40
diff
changeset

61 self.stats.played() 
40
06b5a7db3d19
Some cleanup, got to next song implemented
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
37
diff
changeset

62 self.playing = False 
37
87aa31e41f4a
Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
36
diff
changeset

63 else: 
87aa31e41f4a
Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
36
diff
changeset

64 # nothing to play, just sleep 
87aa31e41f4a
Play the files in a separate thread
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
36
diff
changeset

65 time.sleep(0.5) 
30
860c891de6bb
Remove lots of xmmsrelated code & get ready to make the whole program a
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

66 