Getargs 格式不是元组 运行 PyAudio 实时输入

Getargs format is not a tuple running PyAudio with real-time input

我正在尝试使用 PyAudio 处理来自麦克风的实时数据。我遵循了 PyAudio Documentation and this blog post 上的示例。当我在没有回调的情况下使用 stream.is_active() 时,我没有错误并且一切正常,但是如果我使用回调,我会得到以下 "SystemError: new style getargs format but argument is not a tuple"

我已经查看了问题 Where is the error? “SystemError: new style getargs format but argument is not a tuple”,但是,这并没有转化为我的问题。我将在我的代码下面解释我尝试过的事情:

FORMAT = pyaudio.paInt32
CHANNELS = 2
RATE = 44100
CHUNK = 1024  

def callback(in_data, frame_count, time_info, flag):
    print('Hi')

audio = pyaudio.PyAudio()

# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
                rate=RATE, input=True,
                frames_per_buffer=CHUNK,stream_callback = callback)

stream.start_stream()   

while stream.is_active():
    time.sleep(0.1)
    print('1')
stream.stop_stream()
stream.close()
audio.terminate()

我确定错误是我添加 stream_callback = callback 的任何时间。如果我删除它,我不会收到错误。当我只包含上面的语句时,回调中的确切代码似乎不会影响错误(尽管我的整个代码计算了两个通道之间的时间延迟)。我的回调参数直接从文档中定义。我不太确定还能尝试什么,因为我没有直接在回调中做任何事情。

正如the example you linked所说:

In callback mode, PyAudio will call a specified callback function (2) whenever it needs new audio data (to play) and/or when there is new (recorded) audio data available. Note that PyAudio calls the callback function in a separate thread. The function has the following signature callback(<input_data>, <frame_count>, <time_info>, <status_flag>) and must return a tuple containing frame_count frames of audio data and a flag signifying whether there are more frames to play/record.

但是你的回调函数没有这样做:

def callback(in_data, frame_count, time_info, flag):
    print('Hi')

这根本没有 return 语句,所以它只是 returns None.

将其与引用的示例进行比较,后者 return 是一个缓冲区和一个标志。这就是你需要做的。

你得到特定错误而不是更容易理解的原因是,在幕后的某个地方,PyAudio 正在使用 C API 解析你的 return 值采用 Python 元组的函数,不进行任何显式错误处理。这并不理想,因为当您不知道错误消息的含义时,错误消息更难阅读……

我遇到了同样的问题。我通过添加

解决了
return None, pyaudio.paContinue

回调函数结束。