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

# example playing two audio streams using gnonlin
# problem: the playback ends when the first audio file ends, even if the second could carry on playing

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

        self.project = project

        print "Adding an instrument gnlcomposition"
        self.comp = gst.element_factory_make("gnlcomposition", "mycomposition" + str(insnum))
	# composition is linked once its pad is added
        self.comp.connect("pad-added", self.project.OnPadAdded, self)

        print "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) * gst.SECOND)
        self.audio.set_property("duration", dur * gst.SECOND)
        self.audio.set_property("media-start", long(0) * gst.SECOND)
        self.audio.set_property("media-duration", dur * gst.SECOND)
        self.audio.set_property("priority", 0)
        self.comp.add(self.audio)

	# the default source that makes the silence between the tracks
	self.silentGnlSource = gst.element_factory_make("gnlsource")		
	self.silenceAudioSource = gst.element_factory_make("audiotestsrc", "testsrc" + str(insnum))
#	self.silenceAudioSource.set_property("wave", 4)	#4 is silence
        self.silentGnlSource.set_property("priority", 99)
	self.silentGnlSource.set_property("start", 0 * gst.SECOND)
	self.silentGnlSource.set_property("duration", long(10) * gst.SECOND)
	self.silentGnlSource.set_property("media-start", 0 * gst.SECOND)
	self.silentGnlSource.set_property("media-duration", long(10) * gst.SECOND)
	self.silentGnlSource.add(self.silenceAudioSource)
	self.comp.add(self.silentGnlSource)

        self.project.pipeline.add(self.comp)

        print "Adding an instrument audioconvert"

        self.identity = gst.element_factory_make("identity", "identity" + str(insnum))
	self.identity.set_property("single-segment", True);
        self.project.pipeline.add(self.identity)

        self.audioresample = gst.element_factory_make("audioresample", "audioresample" + str(insnum))
        self.project.pipeline.add(self.audioresample)

        self.convert = gst.element_factory_make("audioconvert", "convert" + str(insnum))
        self.project.pipeline.add(self.convert)

        self.identity.link(self.audioresample)
        self.audioresample.link(self.convert)
        self.convert.link(self.project.masteradder)

class Project:
    def __init__(self):
        print "Running class"

        self.pipeline = gst.Pipeline("pipeline")

        print "Adding master adder"
        self.masteradder = gst.element_factory_make("adder", "masteradder")
        self.pipeline.add(self.masteradder)

        print "Adding alsasink"

        self.alsasink = gst.element_factory_make("alsasink", "out")
        self.alsasink.set_property("device", "default")
        self.pipeline.add(self.alsasink)

        print "Linking masteradder to alsasink"

        self.masteradder.link(self.alsasink)

        # create the instruments

	# first audio file, plays for 5 secs
        self.ins1 = Instrument(self, "song5secs.ogg", 1, 5)

	# second audio file, plays for 10 secs
        self.ins2 = Instrument(self, "song10secs.ogg", 2, 10)

        # play - plays only for 5 secs. If the first instrument is removed, plays for 10 secs.

        self.pipeline.set_state(gst.STATE_PLAYING)

	# ugly way to poll position - just to see it..
        while (1):
		pos = self.pipeline.query_position(gst.FORMAT_TIME)[0]
		print "POS:"+str(pos)


    def OnPadAdded(self, comp, pad, instrument):
        print "Pad added"
        sinkpad = instrument.identity.get_compatible_pad(pad, pad.get_caps())
        pad.link(sinkpad)

class Main:
    def __init__(self):
        self.project = Project()

start=Main()
gtk.main()

