gpt4 book ai didi

python - Gstreamer+python : adding and removing audio sources while pipeline is running

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:34 25 4
gpt4 key购买 nike

我正在编写示例 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 在第一个返回键处阻塞。

有人有什么建议吗?我究竟做错了什么?提前谢谢你。

最佳答案

我自己找到了答案,确实很简单......我添加了其他组件,但它们存在于管道中并保持独立播放状态。所以解决方案是将所有管道设置为播放,这反过来将状态设置为所有 child 。

pipe.set_state(gst.STATE_PLAYING)

代替:

buzzer2.set_state(gst.STATE_PLAYING)

它再次工作。

关于python - Gstreamer+python : adding and removing audio sources while pipeline is running,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8142670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com