PyAudio输入溢出

40
我正在尝试在 Python 中实现实时绘图声音。我需要从麦克风获取块。使用 PyAudio,尝试使用。
import pyaudio
import wave
import sys

chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
                channels = CHANNELS,
                rate = RATE,
                input = True,
                frames_per_buffer = chunk)

print "* recording"
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
    data = stream.read(chunk)
    all.append(data)
print "* done recording"

stream.close()
p.terminate()

之后,我遇到了以下错误:

* recording
Traceback (most recent call last):
  File "gg.py", line 23, in <module>
    data = stream.read(chunk)
  File "/usr/lib64/python2.7/site-packages/pyaudio.py", line 564, in read
    return pa.read_stream(self._stream, num_frames)
IOError: [Errno Input overflowed] -9981

我不理解这个缓冲区。我想使用阻塞IO模式,所以如果块不可用,我想等待这些块。但是当我创建try except段或sleep(0.1)时,我听到了点击声,所以这不是我想要的。

请建议我解决这个问题的最佳方法?


4
也许你的块大小太小了。可能是由于块大小足够小,Python代码无法跟上而导致缓冲区中获取到更多数据而没有取出。 - Demolishun
你好。请问这个问题有任何更新吗?我偶尔会遇到[Errno Input overflowed] -9981错误。我已经检查了我使用的格式,p.is_format_supported是正确的。 - Jack Kelly
10个回答

34
pyaudio.Stream.read()方法有一个关键字参数exception_on_overflow,将其设置为False。
对于您的示例代码,应该像这样写:
import pyaudio
import wave
import sys

chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
                channels = CHANNELS,
                rate = RATE,
                input = True,
                frames_per_buffer = chunk)

print "* recording"
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
    data = stream.read(chunk, exception_on_overflow = False)
    all.append(data)
print "* done recording"

stream.close()
p.terminate()

请参阅PyAudio文档以获取更多详细信息。


6
我遇到了一个错误: TypeError: read() 函数接收到了一个未预期的关键字参数 'exception_on_overflow'。 - Nathan B

16

看起来很多人遇到了这个问题。我稍微研究了一下,我认为它的意思是在上一次调用 stream.read() 和当前调用之间,从流中丢失了数据(即缓冲区填满得比您清除它的速度更快)。

根据Pa_ReadStream()的文档(stream.read()最终会调用该PortAudio函数):

@return On success PaNoError will be returned, or PaInputOverflowed if
input data was discarded by PortAudio after the previous call and
before this call.

(PaInputOverflowed导致pyaudio封装器中的IOError发生)

如果你不需要捕获每一个帧,那么你可以忽略这个错误。如果你非常需要每一帧,那么你需要找到一种方法提高应用程序的优先级。我对Python不够熟悉,不知道有没有Pythonic的方法来做到这一点,但是可以尝试使用简单的nice命令或将调度策略更改为SCHED_DEADLINE。

编辑:

目前存在一个问题,当抛出IOError时,会丢失在该调用中收集的所有帧。为了忽略溢出并返回我们所拥有的内容,您可以使用下面的补丁,它将使stream.read()忽略来自PortAudio的输出欠载和输入溢出错误(但仍然抛出其他错误)。更好的方法是根据您的需求自定义此行为(是否抛出异常)。

diff --git a/src/_portaudiomodule.c b/src/_portaudiomodule.c
index a8f053d..0878e74 100644
--- a/src/_portaudiomodule.c
+++ b/src/_portaudiomodule.c
@@ -2484,15 +2484,15 @@ pa_read_stream(PyObject *self, PyObject *args)
     } else {
       /* clean up */
       _cleanup_Stream_object(streamObject);
+
+      /* free the string buffer */
+      Py_XDECREF(rv);
+
+      PyErr_SetObject(PyExc_IOError,
+                       Py_BuildValue("(s,i)",
+                                     Pa_GetErrorText(err), err));
+      return NULL;
     }
-
-    /* free the string buffer */
-    Py_XDECREF(rv);
-
-    PyErr_SetObject(PyExc_IOError,
-                   Py_BuildValue("(s,i)",
-                                 Pa_GetErrorText(err), err));
-    return NULL;
   }

   return rv;

12

当我运行您的代码时,我遇到了相同的错误。 我查看了我的默认音频设备的默认采样率,即我的MacBook内置麦克风,发现它是48000Hz而不是44100Hz。

p.get_device_info_by_index(0)['defaultSampleRate']
Out[12]: 48000.0

当我将RATE更改为这个值时,它起作用了。


1
我遇到了同样的错误,你的解决方案(将其提高到48000)起作用了。但是我已经运行了以下代码:如果p.is_format_supported(44100.0,#采样率                          input_device = devinfo [“index”],                          input_channels = devinfo ['maxInputChannels'],                          input_format = pyaudio.paInt16):   print '耶!'……它也起作用了!所以我很困惑问题出在哪里。有什么见解吗? - user426364
尝试升级portaudio,这解决了我的一些速率问题。我使用了“brew install portaudio --HEAD”。 - Joseph Sheedy
这对我有用,我没有意识到声卡的默认采样率是48khz,谢谢! - Jeff
谢谢,这也是我的问题。我不会想到在预算硬件上会有这个问题,但也许48k正在成为事实上的标准? - Jason L.

7

我在OS X 10.10上进行了这项工作,尝试从SYBA USB卡(C Media芯片组)的麦克风获取音频,并实时处理它与fft等,结果遇到了同样的错误:

IOError: [Errno Input overflowed] -9981

当使用回调模式而不是阻塞模式时,由libbkmz编写的代码完全解决了溢出问题。(https://www.python.org/dev/peps/pep-0263/)
基于此,工作代码的一部分看起来像这样:
"""
Creating the audio stream from our mic
"""
rate=48000
self.chunk=2**12
width = 2

p = pyaudio.PyAudio()

# callback function to stream audio, another thread.
def callback(in_data,frame_count, time_info, status):
    self.audio = numpy.fromstring(in_data,dtype=numpy.int16)
    return (self.audio, pyaudio.paContinue)

#create a pyaudio object
self.inStream = p.open(format = p.get_format_from_width(width, unsigned=False),
                       channels=1,
                       rate=rate,
                       input=True,
                       frames_per_buffer=self.chunk,
                       stream_callback = callback)

"""
Setting up the array that will handle the timeseries of audio data from our input
"""
self.audio = numpy.empty((self.buffersize),dtype="int16")

    self.inStream.start_stream()

while True:
  try:
    self.ANY_FUNCTION() #any function to run parallel to the audio thread, running forever, until ctrl+C is pressed. 

  except KeyboardInterrupt:

    self.inStream.stop_stream()
    self.inStream.close()
    p.terminate()
    print("* Killed Process")
    quit()

这段代码将创建一个回调函数,然后创建一个流对象,启动它,然后在任何函数中循环。单独的线程会流式传输音频,当主循环停止时,该流被关闭。self.audio在任何函数中都可以使用。如果不终止,线程运行时间过长也会出现问题。
由于Pyaudio在单独的线程中运行此流,因此使音频流稳定,阻塞模式可能会根据脚本中其余进程的速度或时间而饱和。
请注意,块大小为2^12,但较小的块同样有效。还有其他参数我考虑并尝试进行调整,以确保它们都是有意义的:
- 块大小更大或更小(没有影响) - 缓冲区中字的位数和格式,例如此处使用带符号16位。 - 变量的符号性(尝试使用无符号变量并得到饱和模式) - 麦克风输入的性质和系统默认选择,增益等等。
希望这能对某人有所帮助!

5

我的另一个回答解决了大多数情况下的问题。然而,有时错误仍然会发生。

这就是我放弃使用pyaudio并转而使用pyalsaaudio的原因。现在我的Raspy可以顺畅地录制任何声音。

import alsaaudio   
import numpy as np
import array

# constants
CHANNELS    = 1
INFORMAT    = alsaaudio.PCM_FORMAT_FLOAT_LE
RATE        = 44100
FRAMESIZE   = 1024

# set up audio input
recorder=alsaaudio.PCM(type=alsaaudio.PCM_CAPTURE)
recorder.setchannels(CHANNELS)
recorder.setrate(RATE)
recorder.setformat(INFORMAT)
recorder.setperiodsize(FRAMESIZE)


buffer = array.array('f')
while <some condition>:
    buffer.fromstring(recorder.read()[1])

data = np.array(buffer, dtype='f')

非常有帮助,谢谢!我使用了普通列表而不是array.array,虽然更简单但对我来说效果很好,所以主要更改是pyaudio=>pyalsaaudio。此外,我的麦克风需要PCM_FORMAT_S16_LE - Putnik

3
FORMAT = pyaudio.paInt16

请确保设置正确的格式,我的内置麦克风设置为24位(请参见Audio-Midi-Setup应用程序)。


2

我在树莓派上遇到了同样的问题,因为树莓派速度太慢,但是我通过使用更快的array模块来存储数据,解决了这个问题(对于大多数情况而言)。

import array
import pyaudio 

FORMAT = pyaudio.paInt16
CHANNELS = 1
INPUT_CHANNEL=2
RATE = 48000
CHUNK = 512

p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=INPUT_CHANNEL,
                frames_per_buffer =CHUNK)

print("* recording")


try:
    data = array.array('h')
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data.fromstring(stream.read(CHUNK))
finally:
    stream.stop_stream()
    stream.close()
    p.terminate()

print("* done recording")
< p > < code > data 的内容在之后相当于二进制。但是你可以使用 < code > numpy.array(data, dtype='i') 来获取一个整数的numpy数组。


2

取代

chunk = 1024

使用:

chunk = 4096

这对于USB麦克风在我的设备上可行。


0

这对我有帮助:

input_ = stream.read(chunk, exception_on_overflow=False)
exception_on_overflow = False

0

对我来说,这很有帮助:https://stackoverflow.com/a/46787874/5047984

我使用了多进程并行写入文件以记录音频。这是我的代码:

recordAudioSamples.py

import pyaudio
import wave
import datetime
import signal
import ftplib
import sys
import os

# configuration for assos_listen
import config

# run the audio capture and send sound sample processes
# in parallel
from multiprocessing import Process

# CONFIG
CHUNK = config.chunkSize
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = config.samplingRate
RECORD_SECONDS = config.sampleLength

# HELPER FUNCTIONS

# write to ftp
def uploadFile(filename):

    print("start uploading file: " + filename)
    # connect to container
    ftp = ftplib.FTP(config.ftp_server_ip, config.username, config.password)

    # write file
    ftp.storbinary('STOR '+filename, open(filename, 'rb'))
    # close connection
    ftp.quit()
    print("finished uploading: " +filename)

# write to sd-card
def storeFile(filename,frames):

    print("start writing file: " + filename)
    wf = wave.open(filename, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
    wf.close()
    print(filename + " written")

# abort the sampling process
def signal_handler(signal, frame):
    print('You pressed Ctrl+C!')

    # close stream and pyAudio
    stream.stop_stream()
    stream.close()
    p.terminate()

    sys.exit(0)

# MAIN FUNCTION
def recordAudio(p, stream):

    sampleNumber = 0
    while (True):
        print("*  recording")
        sampleNumber = sampleNumber +1

        frames = []
        startDateTimeStr = datetime.datetime.now().strftime("%Y_%m_%d_%I_%M_%S_%f")
        for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
            data = stream.read(CHUNK)
            frames.append(data)

        fileName =  str(config.sensorID) + "_" + startDateTimeStr + ".wav"

        # create a store process to write the file in parallel
        storeProcess = Process(target=storeFile, args=(fileName,frames))
        storeProcess.start()

        if (config.upload == True):
            # since waiting for the upload to finish will take some time
            # and we do not want to have gaps in our sample
            # we start the upload process in parallel
            print("start uploading...")
            uploadProcess = Process(target=uploadFile, args=(fileName,))
            uploadProcess.start()

# ENTRYPOINT FROM CONSOLE
if __name__ == '__main__':

    p = pyaudio.PyAudio()
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

    # directory to write and read files from
    os.chdir(config.storagePath)

    # abort by pressing C
    signal.signal(signal.SIGINT, signal_handler)
    print('\n\n--------------------------\npress Ctrl+C to stop the recording')

    # start recording
    recordAudio(p, stream)

config.py

### configuration file for assos_listen
# upload
upload = False

# config for this sensor
sensorID = "al_01"

# sampling rate & chunk size
chunkSize = 8192
samplingRate = 44100 # 44100 needed for Aves sampling
# choices=[4000, 8000, 16000, 32000, 44100] :: default 16000

# sample length in seconds
sampleLength = 10

# configuration for assos_store container
ftp_server_ip = "192.168.0.157"
username = "sensor"
password = "sensor"

# storage on assos_listen device
storagePath = "/home/pi/assos_listen_pi/storage/"

为什么不使用线程?阻塞 I/O 会释放 GIL,可以有效地利用多个核心而不需要使用 multiprocessing 的复杂性。 - Alex Grönholm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接