#!/usr/bin/python

import pygst
pygst.require('0.10')
import gst
import time

DEVICE = "hw:1"

index = 0

def new_pad(elem, pad):
	global index
	newconv = gst.element_factory_make("audioconvert")
	newmux = gst.element_factory_make("oggmux")
	newenc = gst.element_factory_make("vorbisenc")
	newsink = gst.element_factory_make("filesink")
	newsink.set_property("location", str(index) + ".ogg")
	bin.add(newconv)
	bin.add(newmux)
	bin.add(newenc)
	bin.add(newsink)
	pad.link(newconv.get_pad("sink"))
	newconv.link(newenc)
	newenc.link(newmux)
	newmux.link(newsink)
	index += 1
	bin.set_state(gst.STATE_PLAYING)
	print pad


bin = gst.element_factory_make("bin")

src = gst.element_factory_make("alsasrc")
src.set_property("device", DEVICE)

split = gst.element_factory_make("deinterleave")

bin.add(src)
bin.add(split)

src.link(split)

split.connect("pad-added", new_pad)

print "Pipeline constructed..."

bin.set_state(gst.STATE_PLAYING)

print "Recording."

while 1:
	pass
