4

操作系统: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
4

2 回答 2

7

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

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

isSupported以防您的系统可以True处理您的设置。内存溢出错误可能是由于某些操作系统+硬件问题造成的。您必须检查您的默认主机 API 实际可以做什么。您不需要soundObj通过“流类”“打开”和“关闭”来查询它。

看看这个 SO 问题:PyAudio Input overflowed

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

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

编辑:

事实证明,“Errno Input overflowed - 9981”不是一个小问题:http ://trac.macports.org/ticket/39150

我看到您拥有最新的 portaudio 版本 (19.20111121) 但 19.20111121_4 声称已修复该错误。看看升级 portaudio 是否有效。

于 2013-09-21T11:59:15.953 回答
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)
于 2013-04-24T02:43:31.937 回答