# Bryan Schumaker (2 / 6 / 2011)
# NOTE: DO NOT CALL FUNCTIONS IN THIS FILE DIRECTLY
# USE THE FUNCTIONS IN __init__.py INSTEAD.  THIS
# WILL ENSURE THAT THE AUDIO LOCK IS HELD :-)

import gst

player = gst.element_factory_make("playbin2", "player")
time   = gst.Format(gst.FORMAT_TIME)
bus    = player.get_bus()
bus.add_signal_watch()


def reset():
	player.set_state(gst.STATE_NULL)

def load_file(type, file):
	reset()
	player.set_property("uri", "%s://%s" % (type, file))

def play():
	player.set_state(gst.STATE_PLAYING)

def pause():
	player.set_state(gst.STATE_PAUSED)

def stop():
	pause()
	seek_to(0)

def get_state():
	return player.get_state()[1]

def position():
	if get_state() == gst.STATE_NULL:
		pos = 0.0
	else:
		pos = player.query_position(time)[0]
	return float(pos)

def duration():
	if get_state() == gst.STATE_NULL:
		dur = 0.0
	else:
		dur = player.query_duration(time)[0]
	return float(dur)

def seek_to(new_spot):
	if new_spot < 0:
		new_spot = 0
	player.seek_simple(time, gst.SEEK_FLAG_FLUSH, new_spot)

def seek(amount):
	pos = position()
	if pos == 0.0:
		return
	seek_to( pos + (amount * 1000000000) )

def seek_prcnt(prcnt):
	seek_to(prcnt * duration())

def set_volume(prcnt):
	player.set_property("volume", prcnt)
