在pyaudio中使用rate / format / chunk中的任何其他值给出错误:[Errno Input overflowed] -9981

时间:2013-04-23 23:23:22

标签: python microphone portaudio pyaudio

操作系统:Mac OSX 10.7.5 Python:Python 2.7.3(自制软件) pyaudio:0.2.7 portaudio:19.20111121(homebrew - portaudio)

以下脚本输出以下内容并显示我遇到的问题:

#!/usr/bin/env python
import pyaudio
from pprint import pprint

p = pyaudio.PyAudio()


# SUCCEEDS
pprint(p.is_format_supported(input_format=pyaudio.paInt8,input_channels=1,rate=44100,input_device=0)) # => True
try:
    stream = p.open(format=pyaudio.paInt8,channels=1,rate=44100,input=True,frames_per_buffer=1024)
    data = stream.read(1024)
except IOError as e:
    print 'This never happens: '+str(e)

# FAILS
pprint(p.is_format_supported(input_format=pyaudio.paInt8,input_channels=1,rate=22050,input_device=0)) # => True
try:
    stream = p.open(format=pyaudio.paInt8,channels=1,rate=22050,input=True,frames_per_buffer=1024)
    data = stream.read(1024)
except IOError as e:
    print 'This fails: '+str(e)

# FAILS
pprint(p.is_format_supported(input_format=pyaudio.paInt8,input_channels=1,rate=22050,input_device=0)) # => True
try:
    stream = p.open(format=pyaudio.paInt8,channels=1,rate=22050,input=True,frames_per_buffer=512)
    data = stream.read(1024)
except IOError as e:
    print 'This also fails: '+str(e)

# FAILS
pprint(p.is_format_supported(input_format=pyaudio.paInt8,input_channels=1,rate=11025,input_device=0)) # => True
try:
    stream = p.open(format=pyaudio.paInt8,channels=1,rate=11025,input=True,frames_per_buffer=512)
    data = stream.read(1024)
except IOError as e:
    print 'This also fails as well: '+str(e)

stream.stop_stream()
stream.close()
p.terminate()

以上输出如下:

True
True
This fails: [Errno Input overflowed] -9981
True
This also fails: [Errno Input overflowed] -9981
True
This also fails as well: [Errno Input overflowed] -9981

2 个答案:

答案 0 :(得分:5)

如果要检查操作系统和硬件是否支持所需的格式,通道,速率等设置,请执行以下操作:

import pyaudio
soundObj = pyaudio.PyAudio()

# Learn what your OS+Hardware can do
defaultCapability = soundObj.get_default_host_api_info()
print defaultCapability

# See if you can make it do what you want
isSupported = soundObj.is_format_supported(input_format=pyaudio.paInt8, input_channels=1, rate=22050, input_device=0)
print isSupported

isSupportedTrue,因为您的系统可以处理您的设置。内存溢出错误可能是由于某些OS +硬件问题引起的。您必须检查您的默认主机API实际可以执行的操作。您不需要通过“流类”“打开”和“关闭”soundObj来查询它。

看看这个问题:PyAudio Input overflowed

有关其他pyaudio文档和帮助,请访问:

http://people.csail.mit.edu/hubert/pyaudio/docs/

编辑:

事实证明“Errno输入溢出--9981”不是一个小问题:http://trac.macports.org/ticket/39150

我看到你有最新的portaudio版本(19.20111121),但19.20111121_4声称修复了这个bug。看看升级portaudio是否有效。

答案 1 :(得分:0)

即使上面的注释表明stop_stream()和close()没有修复它,但关闭它们对我来说很有用:

#!/usr/bin/env python
import pyaudio
from pprint import pprint

p = pyaudio.PyAudio()

pprint(p.is_format_supported(input_format=pyaudio.paInt8,
                             input_channels=1,
                             rate=44100,
                             input_device=0))
try:
    stream = p.open(format=pyaudio.paInt8,
                    channels=1,
                    rate=44100,
                    input=True,
                    input_device_index=0,
                    frames_per_buffer=1024)
    data = stream.read(1024)

except IOError as e:
    print e
stream.stop_stream()
stream.close()

pprint(p.is_format_supported(input_format=pyaudio.paInt8,
                              input_channels=1,
                              rate=22050,
                              input_device=0))

try:
    stream = p.open(format=pyaudio.paInt8,
                    channels=1,
                    rate=22050,
                    input=True,
                    input_device_index=0,
                    frames_per_buffer=1024)
    data = stream.read(1024)

except IOError as e:
    print e
stream.stop_stream()
stream.close()

p.terminate()

这给出了不支持的速率(例外)的预期结果:

Traceback (most recent call last):
  File "testaudio.py", line 28, in <module>
    input_device=2))
  File "/usr/lib64/python2.7/site-packages/pyaudio.py", line 934, in is_format_supported
    return pa.is_format_supported(rate, **kwargs)
ValueError: ('Invalid sample rate', -9997)