mnrl mnrl - 1 month ago 11
Python Question

Python Gstreamer record audio from mic and play immediately

I want to record audio from mic and play it immediately from same pc's speakers using gstreamer. In other words; make a wire between input and output record a
few samples and play them back immediately. I can record audio to an ogg file with this code:

#!/usr/bin/env python
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst, Gtk
GObject.threads_init()
Gst.init(None)

pipeline = Gst.Pipeline()

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)
Gtk.main()


But how can i play the audio while recording?

Answer

After audioconvert, you can add a tee and queue to have a new branch. You can have something like that:

autoaudiosrc ! audioconvert ! tee name="source" ! queue ! vorbisenc ! oggmux ! filesink location=file.ogg source. ! queue ! audioconvert ! alsasink