[gst-devel] Python: playing several sounds using adder and mix

Nicolas Rougier Nicolas.Rougier at loria.fr
Wed Nov 5 08:55:18 CET 2008


Hello everybody,

I'm pretty new to gstreamer and I've been playing with python tutorials
to play a simple sound file. From what I've understood and if I want to
play several sounds simultaneously, I have to use the adder and mix
plugins but it is not yet clear to me how to do that in python.

What I would like to do indeed is to read a (typewriter) sound each time
the user press a key as in the example below. Currently, each time the
user press a key, I check whether a sound is being played and play the
new one, but this does not sound nice since I may miss some keystrokes.
I would prefer to mix the sounds with whatever is being played at the
time I stroke a key. Does anybody know how to do that ?



Nicolas


-----
You need a short sound file named 'key.wav' to run the example


#!/usr/bin/env python

import sys, os
import pygtk, gtk, gobject
import pygst
pygst.require("0.10")
import gst

class GTK_Main:
    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("destroy", gtk.main_quit, "WM destroy")
        vbox = gtk.VBox()
        window.add(vbox)
        vbox.pack_start(gtk.Label("Please type text"))
        entry = gtk.Entry()
        vbox.pack_start(entry)
        entry.connect("key-press-event", self.key_press_event)
        window.show_all()

        self.player = gst.element_factory_make("playbin", "player")
        fakesink = gst.element_factory_make("fakesink", "fakesink")
        self.player.set_property("video-sink", fakesink)
        bus = self.player.get_bus()
        bus.add_signal_watch()
        bus.connect("message", self.on_message)

    def key_press_event (self,widget, event):
        filepath = os.path.abspath("./key.wav")
        self.player.set_property("uri", "file://" + filepath)
        if self.player.get_state() and gst.STATE_NULL:
            self.player.set_state(gst.STATE_PLAYING)
						
    def on_message(self, bus, message):
        t = message.type
        if t == gst.MESSAGE_EOS:
            self.player.set_state(gst.STATE_NULL)
        elif t == gst.MESSAGE_ERROR:
            self.player.set_state(gst.STATE_NULL)
            err, debug = message.parse_error()
            print "Error: %s" % err, debug

GTK_Main()
gtk.gdk.threads_init()
gtk.main()







More information about the gstreamer-devel mailing list