如何从callback
中找到给定给调用callback
函数的函数的参数
下面的代码(不完整)将启动调用回调函数的音频流。它使用pyaudio
现在,callback
函数中有硬编码的内容。我正试图摆脱这些
我已经阅读了pyaudio文档,似乎无法向callback
函数传递额外的参数。我读过关于inspect
python模块的书,它的getsource
或getouterframes
对我来说似乎很有趣,希望能够得到PlayStream
函数的参数,但这并没有让我有所收获
如何从callback
中引用SoundGeneratorObject
参数
def PlayStream(SoundGeneratorObject):
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(SoundGeneratorObject.WIDTH),
channels = SoundGeneratorObject.CHANNELS,
rate = SoundGeneratorObject.BITRATE,
frames_per_buffer = SoundGeneratorObject.CHUNKSIZE,
output = True,
stream_callback = callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.1)
stream.stop_stream()
stream.close()
p.terminate()
def callback(in_data, frame_count, time_info, status_flags):
signal = waves.next()
return (signal, pyaudio.paContinue)
waves = SoundGenerator()
PlayStream(waves)
虽然答案已被接受,但我想展示一个替代方案,即如何从技术上使用inspect和globals()从父函数访问参数,此示例将起作用:
您可以这样做来为正在传递的回调创建一个作用域吗
如果可以,请这样使用:
相关问题 更多 >
编程相关推荐