4

我正在编写一个示例python脚本,最初在这里找到:Adding and remove 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 在第一个返回键处阻塞。

有人有什么建议吗?我究竟做错了什么?先感谢您。

4

1 回答 1

2

I found the answer myself, it was simple indeed... I added other components, but they live in the pipeline and keep having an independent play status. So the solution is set all the pipeline to playing, which in turn sets the status to all the children.

pipe.set_state(gst.STATE_PLAYING)

instead of:

buzzer2.set_state(gst.STATE_PLAYING)

and it works again.

于 2011-11-16T13:54:22.120 回答