本文介绍了PyAudio输入溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做出实时的蟒蛇绘制声音。我需要从我的麦克风获取块。

I'm trying to make real-time plotting sound in python. I need to get chunks from my microphone.

使用PyAudio,尝试使用

Using PyAudio, try to use

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()

之后,我得到跟随着错误:

After, I getting the followin error:

* 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模式,因此,如果块不可,我要等待这些块。但是,当我创建除段或睡眠(0.1)的尝试,我听到的点击,所以这不是我想要的。

I can't understand this buffer. I want, to use blocking IO mode, so if chunks not available, i want to wait for those chunks. But when I creating try except segment or sleep(0.1), i hear clicks, so this is not what i want.

请建议最好的解决方案我ploblem?

Please suggest the best solution for my ploblem?

推荐答案

这似乎是一个很多人都遇到这个问题。我挖了一点进去,我认为这意味着previous调用 stream.read()这个当前通话之间,距离流数据丢失(即缓冲区填满比你清除了它)更快。

It seems like a lot of people are encountering this issue. I dug a bit into it and I think it means that between the previous call to stream.read() and this current call, data from the stream was lost (i.e. the buffer filled up faster than you cleared it).

从文档的 Pa_ReadStream()(该PortAudio函数 stream.read()最终结束调用):

From the doc for Pa_ReadStream() (the PortAudio function that stream.read() eventually ends up calling):

@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 然后导致的IOError 在pyaudio包装)。

(PaInputOverflowed then causes an IOError in the pyaudio wrapper).

如果它的确定,为您无法捕捉每一帧中,那么你可能会忽略这个错误。如果这是绝对关键的你有每一帧,那么你就需要找到一种方法来增加您的应用程序的优先级。我不够熟悉Python知道一个Python的方式做到这一点,但它是值得尝试一个简单的很好命令,或改变调度策略,以SCHED_DEADLINE。

If it's OK for you to not capture every single frame, then you may ignore this error. If it's absolutely critical for you to have every frame, then you'll need to find a way to increase the priority of your application. I'm not familiar enough with Python to know a pythonic way to do this, but it's worth trying a simple nice command, or changing the scheduling policy to SCHED_DEADLINE.

编辑:

现在的一个问题是,当引发IOError被抛出,你失去了在通话中收集的所有帧。若要改为忽略溢出,只是返回我们有什么,你可以应用补丁的下方,这将导致stream.read()从PortAudio忽略输出溢和输入溢出错误(但仍扔东西,如果发生了不同的错误)。更好的方法是使这种行为(扔/无掷)定制根据您的需要。

One issue right now is that when IOError is thrown, you lose all the frames collected in that call. To instead ignore the overflow and just return what we have, you can apply the patch below, which will cause stream.read() to ignore output underrun and input overflow errors from PortAudio (but still throw something if a different error occurred). A better way would be to make this behaviour (throw/no throw) customizable depending on your needs.

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;

这篇关于PyAudio输入溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:11