Search code examples
pythonraspberry-pigstreamerpipelinemicrophone

Microphone stops sending voice at speakers after couple of seconds of using a Gstreamer pipeline


I 've been trying to create a "communication wire" from my Raspberry Pi microphone (usb headsets) to my soundcard in order for the script to send whatever I say at microphone to my speakers through a pipeline.

Problem:

When i run the script below, the mic works and sends voice at my speakers for a couple of seconds. After that it stops repeating what i am saying.

The code i have is shown below:

#!/usr/bin/env python
import gi
import os

gi.require_version('Gst', '1.0')
gi.require_version('Gtk', '3.0')
from gi.repository import GObject, Gst, Gtk, GLib
#GObject.threads_init()
Gst.init(None)
loop =  GLib.MainLoop ()


pipeline = Gst.parse_launch("autoaudiosrc ! audioconvert ! tee name=source ! queue ! vorbisenc ! oggmux ! filesink location=file.ogg source. ! queue ! audioconvert ! alsasink")

#autoaudiosrc = Gst.ElementFactory.make("autoaudiosrc", "autoaudiosrc")
#audioconvert = Gst.ElementFactory.make("audioconvert", "audioconvert")
#vorbisenc = Gst.ElementFactory.make("vorbisenc", "vorbisenc")
#oggmux = Gst.ElementFactory.make("oggmux", "oggmux")
#filesink = Gst.ElementFactory.make("filesink", "filesink")
#url = "1.ogg"
#filesink.set_property("location",url)
#pipeline.add( autoaudiosrc)
#pipeline.add( audioconvert)
#pipeline.add( vorbisenc)
#pipeline.add( oggmux)
#pipeline.add( filesink)



#autoaudiosrc.link( audioconvert)
#audioconvert.link( vorbisenc)
#vorbisenc.link( oggmux)
#oggmux.link( filesink)


pipeline.set_state(Gst.State.PLAYING)
loop.run()  
pipeline.set_state(Gst.State.NULL)

Gtk.main()

At the start of the project I tried to create the pipeline with a different way (that's why I left in the code the comments in order for you to check my thought process), until i found a code that does my work here Simple microphone to speakers implementation so I tried to implement it to my project.

While debugging my first thought was that maybe pipeline closes after a while but some tests i did implied that this was not the case.

Please keep in mind that i am relatively new to GStreamer(been using it for less than 3 days) so I might have some silly mistakes.

If you have the time please explain to me the solution of the problem (if you could spot it).

Thank you in advance.

**Edit 1: Just today I executed the code on a different raspberry Pi and I saw that a file is being created normally everytime the code executed that records my input whether I leave it open for 10 or 30 seconds (so the code on recorder's end is good). Probably the thing that needs to be fixed is the "play" code that plays the file that is created.


Solution

  • I tried the same piece of code on a Raspberry that i had previously installed Ubuntu 20. As expected the script was working fine. So in conclusion it is a Raspberry Pi OS issue and not an error on code. Also the simplest form of this pipeline can be achieved with:

    pipeline = Gst.parse_launch("autoadiosrc ! pulsesink")