Python GStreamer src element

Glenn Pierce glennpierce at gmail.com
Tue Jan 17 03:40:38 PST 2012


I have a basic example going now


#!/usr/bin/env python
""" Acapela Src """

import gobject
gobject.threads_init()

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

import acapela
import math, numpy, struct

class acapela_src(gst.BaseSrc):

    __gstdetails__ = (
        "Acapela src plugin",
        "acapela_src.py",
        "Source element that creates sound from text",
        "Glenn Pierce <glennpierce at gmail.com>")

    _src_template = gst.PadTemplate ("src",
                                     gst.PAD_SRC,
                                     gst.PAD_ALWAYS,
                                     gst.caps_new_any ())

    __gsttemplates__ = (_src_template,)

    def __init__ (self, *args, **kwargs):
        gst.BaseSrc.__init__(self)
        gst.info('creating acapela src pad')
        self.src_pad = gst.Pad (self._src_template)
        self.src_pad.use_fixed_caps()

    def do_create(self, offset, length):

	values = [int(math.sin(a)*32767) for a in numpy.arange(0.0, 12*math.pi, 0.06)]
        data = struct.pack('<' + 'h'*len(values), *values)
        buf = gst.Buffer(data)
        caps = gst.caps_from_string('audio/x-raw-int, rate=44100,
endianness=1234, channels=1, width=16, depth=16, signed=true')
        buf.set_caps(caps)
	print "do_create", offset, length
        #buf.timestamp = 0 * gst.SECOND
        #buf.duration = 10 * gst.SECOND
        return gst.FLOW_OK, buf



# Register element class
gobject.type_register(acapela_src)
gst.element_register(acapela_src, 'acapelasrc', gst.RANK_MARGINAL)


if __name__ == "__main__":

  pipeline_str = 'acapelasrc ! audioconvert ! audioresample ! autoaudiosink'
  pipeline = gst.parse_launch(pipeline_str)
  pipeline.set_state(gst.STATE_PLAYING)

  main_loop = gobject.MainLoop()
  main_loop.run()


HTH


On 16 January 2012 17:26, Glenn Pierce <glennpierce at gmail.com> wrote:
> Looking again I suspect I have to use python struct like
> http://www.google.com/codesearch#r6HeTJTZfuY/old/somasrc.py&q=file:%5C.py%20BaseSrc%20do_create&type=cs&l=45
>
> I will experiment and post the results.
>
> On 16 January 2012 15:09, Glenn Pierce <glennpierce at gmail.com> wrote:
>> I didn't want to high jack the thread from Mahendra about writing a
>> src plugin in Python. However, I sure it would help him as well.
>>
>> I am also trying to create an element src in Python. I have found the
>> following code at
>> http://lists.freedesktop.org/archives/gstreamer-devel/2011-July/032110.html
>> This seems to work when placed into a pipeline with a fake sink.
>>
>> To extend this example and learn more I have decided to try to play a
>> sine wave.  (Ultimately I wan't to pass data from a text to speech
>> engine)
>>
>> I cant seem to find any good examples of what's need to be done in do_create.
>> What I have at the moment is
>>
>> def do_create(self, offset, length):
>>       data = [str(math.sin(a)*255.0) for a in numpy.arange(0.0,
>> 2*math.pi, 0.01)]  ? # How am I meant to populate data ?
>>       buf = gst.Buffer(data)
>>       caps = gst.caps_from_string('audio/x-raw-float, rate=44100,
>> channels=1, width=8')
>>       buf.set_caps(caps)
>>       buf.timestamp = 0 * gst.SECOND
>>       buf.duration = 4 * gst.SECOND
>>       return gst.FLOW_OK, buf
>>
>>
>> I basically need to know how I can populate the Buffer object with
>> audio data as a string.
>> Everything I try results in
>>
>> error: Internal data flow error.
>>
>> I am using to with the following pipeline to playback.
>>
>> 'acapelasrc ! audioconvert ! audioresample ! autoaudiosink'
>>
>> Any pointers would be greatly appreciated.
>>
>> Thanks


More information about the gstreamer-devel mailing list