mnrl mnrl - 1 year ago 140
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

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"
pipeline.add( autoaudiosrc)
pipeline.add( audioconvert)
pipeline.add( vorbisenc)
pipeline.add( oggmux)
pipeline.add( filesink) audioconvert) vorbisenc) oggmux) filesink)


But how can i play the audio while recording?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download