import pygtk
pygtk.require("2.0")
import gobject
gobject.threads_init()
import pygst
pygst.require('0.10')
import gst

import sys

def pad_added(element, pad, (srcpadname, sinkpad)):
	if pad.get_name() == srcpadname:
		pad.link(sinkpad)


pipeline = gst.Pipeline("pipeline")
mainloop = gobject.MainLoop()


#
# choose a file with at least 2 audio channels (e.g., any stereo audio file
# you have laying around).
#

head = gst.element_factory_make("filesrc")
head.set_property("location", sys.argv[1])
pipeline.add(head)

elem = gst.element_factory_make("decodebin2")
pipeline.add(elem)
head.link(elem)
head = elem

elem = gst.element_factory_make("audioconvert")
pipeline.add(elem)
head.connect("pad-added", pad_added, ("src0", elem.get_pad("sink")))
head = elem

elem = gst.element_factory_make("audiorate")
pipeline.add(elem)
head.link(elem)
head = elem

#
# audiofirfilter (and possibly every element derived from GstAudioFilter)
# has a bug that causes it to drop (N-1) of every N buffers where N is the
# number of audio channels in the stream being processed.  For example if
# processing stereo audio, then every other buffer is dropped.
#

if True:	# False to hear original audio, True to demo audiofx bug
	elem = gst.element_factory_make("audiofirfilter")
	# FIR kernel and latency that should make audiofirfilter an
	# identity transform on the audio stream.
	elem.set_property("kernel", [1.0])
	elem.set_property("latency", 0)
	pipeline.add(elem)
	head.link(elem)
	head = elem

elem = gst.element_factory_make("progressreport")
pipeline.add(elem)
head.link(elem)
head = elem

elem = gst.element_factory_make("autoaudiosink")
pipeline.add(elem)
head.link(elem)

pipeline.set_state(gst.STATE_PLAYING)
mainloop.run()
