这并不是一个决定性的答案,而是试图帮助您自己追踪问题。
当尝试在 OS X 上使用 PyAudio 重现您的问题时,我总是遇到[Errno Input overflowed] -9981
(就像其他几个人一样,似乎)。p.is_format_supported()
报告为 OK 的配置也会导致这些错误。因此,我编写了一个脚本,它只是尝试使用录制设置的所有可能排列进行录制。
此脚本以防御方式探测设备列表、采样率、格式和通道的所有排列,并将结果保存到根据记录设置命名的文件中。
import os
import pyaudio
import sys
# === These parameters will be permuted ===========
DEVICES = [0, 1, 2]
RATES = [44100, 48000, 192000]
FORMATS = ['Float32', 'Int32', 'Int24', 'Int16', 'Int8', 'UInt8']
CHANNELS = [1, 2]
# =================================================
CHUNK = 1024
COLUMNS = (('filename', 30),
('result', 9),
('dev', 5),
('rate', 8),
('format', 9),
('channels', 10),
('chunk', 7),
('reason', 0))
STATUS_MSG = "Recording... "
pa = pyaudio.PyAudio()
def get_format(format):
fmt = getattr(pyaudio, 'pa%s' % format)
return fmt
def record(filename=None,
duration=5,
dev=0,
rate=44100,
format='Float32',
channels=2,
chunk=1024,):
"""Record `duration` seconds of audio from the device with index `dev`.
Store the result in a file named according to recording settings.
"""
if filename is None:
filename = "dev{dev}-{rate}-{format}-{channels}ch.raw".format(**locals())
result = 'FAILURE'
reason = ''
outfile = open(filename, 'w')
print STATUS_MSG,
sys.stdout.flush()
try:
stream = pa.open(input_device_index=dev,
rate=rate,
format=get_format(format),
channels=channels,
frames_per_buffer=chunk,
input=True,
)
try:
for i in range(0, rate / (chunk) * duration):
a = stream.read(chunk)
outfile.write(a)
result = 'SUCCESS'
# Catch exceptions when trying to read from stream
except Exception, e:
reason = "'%s'" % e
# Catch exceptions when trying to even open the stream
except Exception, e:
reason = "'%s'" % e
outfile.close()
# Don't leave files behind for unsuccessful attempts
if result == 'FAILURE':
os.remove(filename)
filename = ''
info = {}
for col_name, width in COLUMNS:
info[col_name] = str(locals()[col_name]).ljust(width)
msg = "{filename}{result}{dev}{rate}{format}{channels}{chunk}{reason}"
print msg.format(**info)
def main():
# Build the header line
header = 'STATUS'.ljust(len(STATUS_MSG) + 1)
for col_name, width in COLUMNS:
header += col_name.upper().ljust(width)
print header
print "=" * len(header)
# Record samples for all permutations of our parameter lists
for dev in DEVICES:
for rate in RATES:
for format in FORMATS:
for channels in CHANNELS:
record(duration=2,
dev=dev,
rate=rate,
format=format,
channels=channels,
chunk=CHUNK)
if __name__ == '__main__':
main()
示例输出(简化):
STATUS FILENAME RESULT DEV RATE FORMAT CHANNELS CHUNK REASON
==================================================================================================
Recording... dev0-44100-Float32-1ch.raw SUCCESS 0 44100 Float32 1 1024
Recording... dev0-44100-Float32-2ch.raw SUCCESS 0 44100 Float32 2 1024
Recording... dev0-44100-Int16-1ch.raw SUCCESS 0 44100 Int16 1 1024
Recording... dev0-44100-Int16-2ch.raw SUCCESS 0 44100 Int16 2 1024
Recording... FAILURE 0 192000 Float32 1 1024 '[Errno Input overflowed] -9981'
Recording... FAILURE 0 192000 Float32 2 1024 '[Errno Input overflowed] -9981'
Recording... FAILURE 0 192000 Int16 1 1024 '[Errno Input overflowed] -9981'
Recording... FAILURE 0 192000 Int16 2 1024 '[Errno Input overflowed] -9981'
Recording... dev1-44100-Float32-1ch.raw SUCCESS 1 44100 Float32 1 1024
Recording... dev1-44100-Float32-2ch.raw SUCCESS 1 44100 Float32 2 1024
Recording... dev1-44100-Int16-1ch.raw SUCCESS 1 44100 Int16 1 1024
Recording... dev1-44100-Int16-2ch.raw SUCCESS 1 44100 Int16 2 1024
Recording... FAILURE 1 192000 Float32 1 1024 '[Errno Input overflowed] -9981'
Recording... FAILURE 1 192000 Float32 2 1024 '[Errno Input overflowed] -9981'
Recording... FAILURE 1 192000 Int16 1 1024 '[Errno Input overflowed] -9981'
Recording... FAILURE 1 192000 Int16 2 1024 '[Errno Input overflowed] -9981'
Recording... FAILURE 2 44100 Float32 1 1024 '[Errno Invalid number of channels] -9998'
Recording... FAILURE 2 44100 Float32 2 1024 '[Errno Invalid number of channels] -9998'
Recording... FAILURE 2 44100 Int16 1 1024 '[Errno Invalid number of channels] -9998'
Recording... FAILURE 2 44100 Int16 2 1024 '[Errno Invalid number of channels] -9998'
Recording... FAILURE 2 192000 Float32 1 1024 '[Errno Invalid number of channels] -9998'
Recording... FAILURE 2 192000 Float32 2 1024 '[Errno Invalid number of channels] -9998'
Recording... FAILURE 2 192000 Int16 1 1024 '[Errno Invalid number of channels] -9998'
Recording... FAILURE 2 192000 Int16 2 1024 '[Errno Invalid number of channels] -9998'