view 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
line wrap: on
line source

import time, os
from threading import Thread

import mad, ao

import playlist, stats, rb_dbus

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")

		self.stats    = stats.statsengine(os.path.join(os.environ['HOME'], ".vixm/stats"))

		self.rb       = rb_dbus.RhythmboxDbusThread(self)
		self.rb.start()

	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
		self.stats.playing(s)

	def run(self):
		while not self.shutdown:
			c = self.current

			if c and self.playing:
				# notify everyone

				self.rb.notify("file://" + c["file"], \
						c["title"], c["artist"])

				# play the file
				mf = mad.MadFile(c["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.stats.played()
					self.playing = False
			else:
				# nothing to play, just sleep
				time.sleep(0.5)