如何防止同一台电脑从麦克风到扬声器的环回回声

时间:2014-07-20 23:27:14

标签: python audio gstreamer

我想用我的电脑作为声音放大器。基本上从麦克风中捕获音频,并从同一台PC的扬声器播放放大的声音。我使用此代码:

#!/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()

auto = Gst.ElementFactory.make("autoaudiosrc", "autoaudiosrc")
capsfilter = Gst.ElementFactory.make("capsfilter", "capsfilter")
capsfilter.caps = Gst.caps_from_string('audio/x-raw')
level = Gst.ElementFactory.make("level", "audiolevel")
level.props.interval = 50000000
audiosink = Gst.ElementFactory.make("autoaudiosink", "audiosink")

pipeline.add(auto)
pipeline.add(capsfilter)
pipeline.add(level)
pipeline.add(audiosink)

auto.link(capsfilter)
capsfilter.link(level)
level.link(audiosink)


class ProgressBarWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="ProgressBar Demo")
        self.set_border_width(10)

        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(vbox)

        self.progressbar = Gtk.ProgressBar()
        vbox.pack_start(self.progressbar, True, True, 0)
        bus = pipeline.get_bus()
        bus.add_signal_watch_full(1)
        bus.connect("message", self.on_messages)
        pipeline.set_state(Gst.State.PLAYING)


    def on_messages(self, bus, message):
        t = message.type
        if t == Gst.MessageType.ELEMENT:
           structure = message.get_structure()
           if structure.get_name() == "level":
              rms = structure.get_value("rms")[0]
           if rms < -50:
              rms = -50
           rep = rms + 50

           self.progressbar.set_fraction(rep/50 + 0.1)
           self.progressbar.set_text(str(int(rms)) + " dB")
           self.progressbar.set_show_text(1)


win = ProgressBarWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

但是当我运行python代码并增加扬声器音量时,播放会变得嘈杂并发生无限的声音循环。我知道这是物理因素,因为麦克风也会一次又一次地捕捉扬声器的声音。但有没有基于pythonic或软件的方法来防止这种情况?感谢。

1 个答案:

答案 0 :(得分:0)

如果引入时间延迟(可能是FIFO缓冲区),则可以避免反馈尖叫。这样渲染的音频就没有实时的前向反馈循环。