gpt4 book ai didi

python - 获取 IOError : [Errno Input overflowed] -9981 when setting PyAudio Stream input and output to True

转载 作者:太空狗 更新时间:2023-10-29 19:29:45 33 4
gpt4 key购买 nike

我正在尝试在我的 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

我在 Google 上搜索了这个错误,发现将 block 变大或变小都有帮助。我试过了,但没有任何区别。我还尝试添加以下代码来捕获过载异常:

try:
data = stream.read(chunk)
except IOError as ex:
if ex[1] != pyaudio.paInputOverflowed:
raise
data = '\x00' * chunk

这避免了错误,但我没有输出我的输入音频,而是听到了一堆响亮的咔哒声。

为了排除故障,我注释掉了 output=True 行,程序运行良好,但没有输出任何内容。我注释掉了 input=True 并改为读取 Wave 文件,并且流能够输出音频。我尝试创建 2 个流,一个用于输入,一个用于输出,但这也不起作用。

还有什么我可以做的来避免这个错误吗?

最佳答案

我在手动安装 pyaudio(从源代码构建 portaudio)时遇到了同样的情况,一个不太完美的解决方法是为 Apple Mac OS X (Universal) 下载 pyaudio安装它,这只会为 python 2.6 和以前的版本安装它。如果您使用/usr/bin/python2.6 运行您的代码,那么您就完成了。但如果你真的想要 2.7,请将安装的模块(pyaudio.py、_portaudio.so)复制到 2.7 文件夹/Library/Python/2.7/site-packages/。

我不知道为什么它不能从源代码构建模块。

关于python - 获取 IOError : [Errno Input overflowed] -9981 when setting PyAudio Stream input and output to True,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8567366/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com