使用decodebin与adder

4 投票
1 回答
1087 浏览
提问于 2025-04-16 10:19

我正在尝试创建一个音频流,这个音频流有一个固定的音源(在这个例子中是audiotestsrc),我可以偶尔通过play_file()方法从文件中添加声音(因为文件格式多样,所以我使用decodebin)。为此,我使用了一个添加器。不过,不知道为什么,我无法正确添加第二个声音。不仅程序播放声音的方式不对,还完全停止了原来的audiotestsrc。以下是我目前的代码:

import gst; import gobject; gobject.threads_init()

pipe = gst.Pipeline()

adder = gst.element_factory_make("adder", "adder")

first_sink = adder.get_request_pad('sink%d')
pipe.add(adder)

test = gst.element_factory_make("audiotestsrc", "test")
test.set_property('freq', 100)
pipe.add(test)
testsrc = test.get_pad("src")
testsrc.link(first_sink)

output = gst.element_factory_make("alsasink", "output")
pipe.add(output)
adder.link(output)

pipe.set_state(gst.STATE_PLAYING)

raw_input('Press key to play sound')

def play_file(filename):
    adder_sink = adder.get_request_pad('sink%d')

    audiofile = gst.element_factory_make('filesrc', 'audiofile')
    audiofile.set_property('location', filename)

    decoder = gst.element_factory_make('decodebin', 'decoder')

    def on_new_decoded_pad(element, pad, last):
        pad.link(adder_sink)

    decoder.connect('new-decoded-pad', on_new_decoded_pad)

    pipe.add(audiofile)
    pipe.add(decoder)
    audiofile.link(decoder)

    pipe.set_state(gst.STATE_PAUSED)
    pipe.set_state(gst.STATE_PLAYING)


play_file('sample.wav')

while True:
    pass

1 个回答

4

感谢moch在#gstreamer的帮助,我意识到所有的加法器源(adder sources)应该使用相同的格式。我修改了上面的脚本,让每个输入在加法器之前都加上这个格式的描述:"audio/x-raw-int, endianness=(int)1234, channels=(int)1, width=(int)16, depth=(int)16, signed=(boolean)true, rate=(int)11025"(这是一个例子)。

撰写回答