import gobject
gobject.threads_init()
import gst

caps = gst.Caps("audio/x-raw-float")

mixer = gst.element_factory_make("adder")
mixop = gst.element_factory_make("gnloperation")
mixop.add(mixer)
mixop.props.expandable = True
mixop.props.priority = 0

def makeSrc(start, pattern, priority):
    tst = gst.element_factory_make("audiotestsrc")
    flt = gst.element_factory_make("capsfilter")
    tst.props.freq = pattern
    flt.props.caps = caps
    srcbin = gst.Bin()
    srcbin.add(tst, flt)
    gst.element_link_many(tst, flt)
    srcbin.add_pad(gst.GhostPad("src", flt.get_pad("src")))

    src = gst.element_factory_make("gnlsource")
    src.add(srcbin)
    src.props.priority = priority
    src.props.start = start
    src.props.media_start = 0
    src.props.duration = 5 * gst.SECOND
    src.props.media_duration = 5 * gst.SECOND
    return src

def makeOp(operation, priority):
    op = gst.element_factory_make(operation)
    csp_in = gst.element_factory_make("audioconvert")
    csp_out = gst.element_factory_make("audioconvert")
    opbin = gst.Bin()
    opbin.add(csp_in, op, csp_out)
    gst.element_link_many(csp_in, op, csp_out)
    opbin.add_pad(gst.GhostPad("sink", csp_in.get_pad("sink")))
    opbin.add_pad(gst.GhostPad("src", csp_out.get_pad("src")))

    gnlop = gst.element_factory_make("gnloperation")
    gnlop.add(opbin)
    gnlop.props.priority = priority 
    gnlop.props.start = long(2.5 * gst.SECOND)
    gnlop.props.media_start = 0
    gnlop.props.duration = long(2.5 * gst.SECOND)
    gnlop.props.media_duration = long(2.5 * gst.SECOND)
    gnlop.props.active = True
    return gnlop

def comp_pad_added(comp, pad):
    tpad = csp.get_pad("sink")
    pad.link(tpad)

comp = gst.element_factory_make("gnlcomposition")
mixop.props.priority=0
comp.add(mixop)
comp.add(makeOp("volume", 2))
comp.add(makeOp("volume", 1))
comp.add(makeSrc(0, 440, 3))

comp.connect("pad-added", comp_pad_added)
csp = gst.element_factory_make("audioconvert")
sink = gst.element_factory_make("autoaudiosink")

p = gst.Pipeline()
p.add(comp, csp, sink)
gst.element_link_many(csp, sink)
p.set_state(gst.STATE_PLAYING)
ml = gobject.MainLoop()
ml.run()


