2011-01-20 13 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"(示例)。