我正在尝试在我的 Mac(OS 10.7.2)上运行以下代码(来自 PyAudio 文档的示例):
import pyaudio
import sys
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
output = True,
frames_per_buffer = chunk)
print "* recording"
for i in range(0, 44100 / chunk * RECORD_SECONDS):
data = stream.read(chunk)
stream.write(data, chunk)
print "* done"
stream.stop_stream()
stream.close()
p.terminate()
我给出的错误是:
Traceback (most recent call last):
File "PyAudioExample.py", line 24, in <module>
data = stream.read(chunk)
File "/Library/Python/2.7/site-packages/pyaudio.py", line 564, in read
return pa.read_stream(self._stream, num_frames)
IOError: [Errno Input overflowed] -9981
我在谷歌上搜索了这个错误,发现无论是使块更大或更小都会有所帮助。我试过这个,没有任何区别。我还尝试添加以下代码来捕获重载异常:
try:
data = stream.read(chunk)
except IOError as ex:
if ex[1] != pyaudio.paInputOverflowed:
raise
data = '\x00' * chunk
这避免了错误,但我没有输出我的输入音频,而是听到了一堆响亮的咔嗒声。
为了排除故障,我注释掉了 output=True 行,程序运行良好,但没有输出任何内容。我注释掉了 input=True 并改为读取 Wave 文件,并且流能够输出音频。我尝试创建 2 个流,一个用于输入,一个用于输出,但这也不起作用。
我还能做些什么来避免这个错误吗?