view vixm/player.py @ 40:06b5a7db3d19 pure

Some cleanup, got to next song implemented
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Sat, 04 Nov 2006 23:11:49 -0500
parents 87aa31e41f4a
children bc6db55448e1
line wrap: on
line source

import time
from threading import Thread

import mad, ao

import playlist

class playerThread(Thread):
	def __init__(self):
		Thread.__init__(self)

		self.shutdown = False
		self.playing  = False
		self.end_current = False

		self.current  = None

		self.blksize  = 40960
		self.dev      = ao.AudioDevice("oss")

	def play_next(self, lists):
		try:
			# pop song off the PRIO queue
			s = lists[playlist.LIST_PRIO].pop()
		except IndexError:
			# no song to pop
			s = lists[playlist.LIST_DEFAULT].next()
		except ValueError:
			print "WTF is going on?!"
			raise ValueError

		self.play(s)

	def play(self, s):
		if self.playing:
			self.end_current = True

		self.playing = True
		self.current = s

	def run(self):
		while not self.shutdown:
			if self.current and self.playing:
				# play the file
				mf = mad.MadFile(self.current["file"])
		
				while True:
					buf = mf.read(self.blksize)
					if buf is None or \
					   self.shutdown or \
					   self.end_current:
						break
					self.dev.play(buf, len(buf))

				if self.end_current:
					self.end_current = False
				else:
					self.playing = False
			else:
				# nothing to play, just sleep
				time.sleep(0.5)