2011-01-20 52 views
4

我想创建一个音频流,有一个不断的音频源(在这种情况下,audiotestsrc),我可以偶尔从文件中添加声音(各种格式,这就是为什么我使用解码器)通过play_file()方法。为此,我使用了一个加法器。但是,出于某种原因,我无法正确添加第二个声音。该程序不仅不正确地播放声音,而且还会完全停止原始的audiotestsrc。这是我的代码到目前为止:与加法器使用decodebin

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 

回答

4

感谢mogre #gstreamer,我意识到所有加法器源应该有相同的格式。我修改了上面的脚本,以便在加法器中的每个输入之前都有一个大写字母"audio/x-raw-int, endianness=(int)1234, channels=(int)1, width=(int)16, depth=(int)16, signed=(boolean)true, rate=(int)11025"(示例)。