开发者

Use decodebin with adder

开发者 https://www.devze.com 2023-02-05 14:04 出处:网络
I\'m trying to create an audio stream that has a constant audio source (in this case, audiotestsrc) to which I can occasionally add sounds from files (of various formats, that\'s why I\'m using decode

I'm trying to create an audio stream that has a constant audio source (in this case, audiotestsrc) to which I can occasionally add sounds from files (of various formats, that's why I'm using decodebin) through the play_file() method. I use an adder for that purpose. However, for some reason, I cannot add the second sound correctly. Not only does the program play the sound incorrectly, it also completely stops the original audiotestsrc. Here's my code so far:

import gst; import gobject; gobject.threads_init()

pipe = gst.Pipeline()

adder = gst.element_factory_make("adder", "adder")开发者_Go百科

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


Thanks to moch on #gstreamer, I realized that all adder sources should have the same format. I modified the above script so as to have the caps "audio/x-raw-int, endianness=(int)1234, channels=(int)1, width=(int)16, depth=(int)16, signed=(boolean)true, rate=(int)11025" (example) go before every input in the adder.

0

精彩评论

暂无评论...
验证码 换一张
取 消