我正在处理一个示例 python 脚本,最初在这里找到:Adding and removing audio sources to/from GStreamer pipeline on-the-go。 目的是制作一个如上所述的脚本,能够在管道运行时插入和删除音频源但在源和加法器之间使用 audioconvert 元素。这是因为在更一般的情况下, Adder 希望传入的流格式相同。
所以这是代码;我们创造了2个发电机(蜂鸣器)。第一个发出1000Hz音调并等待返回键。第二个是500Hz音调,按键后总和为第一个音调。再次,按回车键,只能听到第二台发电机。
#!/usr/bin/python
import gobject;
gobject.threads_init()
import gst
# THE FOLLOWING FUNCTION IS A REWORK OF THE ORIGINAL, STILL DOING THE JOB
def create_raw_audiotest_signal(pipe, freq, adder):
# create buzzer of a given freq
buzzer = gst.element_factory_make("audiotestsrc","buzzer%d" % freq)
buzzer.set_property("freq",freq)
pipe.add(buzzer)
buzzersrc=buzzer.get_pad("src")
# Gather a request sink pad on the mixer
sinkpad=adder.get_request_pad("sink%d")
# .. and connect it to the buzzer
buzzersrc.link(sinkpad)
return buzzer, buzzersrc, sinkpad
# THIS IS A MODIFIED VERSION, NOT WORKING, THAT JUST PUTS AN AUDIOCONVERT
# ELEMENT BETWEEN THE GENERATOR AND THE ADDER.
def create_audiotest_signal_with_converter(pipe, freq, adder):
# create buzzer of a given freq
buzzer = gst.element_factory_make("audiotestsrc","buzzer%d" % freq)
buzzer.set_property("freq",freq)
# add a converter because adder wants inputs with the same format.
ac = gst.element_factory_make("audioconvert", "ac%d" % freq)
pipe.add(buzzer, ac)
# link the buzzer with the converter ...
buzzer.link(ac)
buzzersrc=buzzer.get_pad("src")
# Gather a request sink pad on the mixer
sinkpad=adder.get_request_pad("sink%d")
# and then the converter to the adder
ac.get_pad('src').link(sinkpad)
return buzzer, buzzersrc, sinkpad
if __name__ == "__main__":
# First create our pipeline
pipe = gst.Pipeline("mypipe")
# Create a software mixer with "Adder"
adder = gst.element_factory_make("adder","audiomixer")
pipe.add(adder)
# Create the first buzzer..
#buzzer1, buzzersrc1, sinkpad1 = create_raw_audiotest_signal(pipe, 1000, adder)
buzzer1, buzzersrc1, sinkpad1 = create_audiotest_signal_with_converter(pipe, 1000, adder)
# Add some output
output = gst.element_factory_make("autoaudiosink", "audio_out")
pipe.add(output)
adder.link(output)
# Start the playback
pipe.set_state(gst.STATE_PLAYING)
raw_input("1kHz test sound. Press <ENTER> to continue.")
# Get another generator
#buzzer2, buzzersrc2, sinkpad2 = create_raw_audiotest_signal(pipe, 500, adder)
buzzer2, buzzersrc2, sinkpad2 = create_audiotest_signal_with_converter(pipe, 500, adder)
# Start the second buzzer (other ways streaming stops because of starvation)
buzzer2.set_state(gst.STATE_PLAYING)
raw_input("1kHz + 500Hz test sound playing simoultenously. Press <ENTER> to continue.")
# Before removing a source, we must use pad blocking to prevent state changes
buzzersrc1.set_blocked(True)
# Stop the first buzzer
buzzer1.set_state(gst.STATE_NULL)
# Unlink from the mixer
buzzersrc1.unlink(sinkpad2)
# Release the mixers first sink pad
adder.release_request_pad(sinkpad1)
# Because here none of the Adder's sink pads block, streaming continues
raw_input("Only 500Hz test sound. Press <ENTER> to stop.")
如果您在两个调用中使用 create_raw_audiotest_signal 代替 create_audiotest_signal_with_converter ,那么它当然有效。如果你使用两者的混合物,它可以工作,但中间有一个不必要的额外延迟。最有趣的情况是在两个调用中使用audioconvert,但在第一个返回键时使用gtk块。
有人有任何建议吗?我究竟做错了什么? 提前谢谢。
答案 0 :(得分:2)
我自己找到了答案,确实很简单...... 我添加了其他组件,但它们存在于管道中并保持独立的播放状态。所以解决方案是将所有管道设置为播放,然后将状态设置为所有孩子。
<强> pipe.set_state(gst.STATE_PLAYING)强>
而不是:
<强> buzzer2.set_state(gst.STATE_PLAYING)强>
它再次有效。