#!/usr/bin/python
import pygst
pygst.require("0.10")
import gst
import gtk
import gobject

FIRST_FILE = "123Stop.ogg"

class Instrument:
	def __init__(self, project, filename, insnum):

		self.project = project

		gst.debug("Adding an instrument gnlcomposition")
		self.comp = gst.element_factory_make("gnlcomposition", "mycomposition" + str(insnum))
		self.comp.connect("pad-added", self.project.OnPad, self)

		gst.debug("Adding a gnlfilesource")
		self.audio = gst.element_factory_make("gnlfilesource", "audio" + str(insnum))
		self.audio.set_property("location", filename)
		self.audio.set_property("start", long(0))
		self.audio.set_property("duration", 10 * gst.SECOND)
		self.comp.add(self.audio)
		self.project.pipeline.add(self.comp)

		gst.debug("Adding an instrument audioconvert")
		self.convert = gst.element_factory_make("audioconvert", "convert" + str(insnum))
		self.project.pipeline.add(self.convert)
		self.convert.link(self.project.mastersink)

class Project:
	def __init__(self):
		self.pipeline = gst.Pipeline()
		self.mastersink = gst.element_factory_make("alsasink", "mysink")
		self.pipeline.add(self.mastersink)

		# instruments
		
		self.ins1 = Instrument(self, FIRST_FILE, 10)
		
		self.bus = self.pipeline.get_bus()
		self.bus.add_signal_watch()
		self.bus.connect("message::eos", self.Stop)
		
		# play
		print "Playing", FIRST_FILE

		self.pipeline.set_state(gst.STATE_PLAYING)

	def OnPad(self, comp, pad, instrument):
		print "pad added!"
		gst.debug("Pad added")
		convpad = instrument.convert.get_compatible_pad(pad, pad.get_caps())
		pad.link(convpad)
	
	def Stop(self, bus, message):
		print "EOS"
		gst.debug("EOS")
		gtk.main_quit()

Project()
gtk.main()
