我正在尝试从音频流中获取样本并将它们放入共享队列中。我有另一个从这个队列中拉出来的进程。
当我跑步时,我收到此错误:
* recording
Traceback (most recent call last):
File "record.py", line 43, in <module>
data = stream.read(CHUNK)
File "/Library/Python/2.7/site-packages/pyaudio.py", line 605, in read
return pa.read_stream(self._stream, num_frames)
IOError: [Errno Input overflowed] -9981
编辑:显然问题已经存在了一段时间没有发布解决方案(我尝试了他们的建议):
Geting IOError: [Errno Input overflowed] -9981 when setting PyAudio Stream input and output to True
这是(简化)代码:
import pyaudio
import wave
import array
import time
from multiprocessing import Queue, Process
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 2
p = pyaudio.PyAudio()
left = Queue()
right = Queue()
def other(q1, q2):
while True:
try:
a = q1.get(False)
except Exception:
pass
try:
b = q2.get(False)
except Exception:
pass
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
Process(target=other, args=(left, right)).start()
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
byte_string = ''.join(data)
nums = array.array('h', byte_string)
for elt in nums[1::2]:
left.put(elt)
for elt in nums[0::2]:
right.put(elt)
print("* done recording")
stream.stop_stream()
stream.close()
print "terminated"
我做错了什么?我在Mac OSX和Python 2.7上,我通过portaudio
安装了homebrew
并尝试了{python的pip
和dmg安装,但没有运气。
答案 0 :(得分:5)
缓冲区溢出错误是因为您的frames_per_buffer和读取chunksize可能太小。 尝试更大的值,即512,2048,4096,8192等
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 2
for CHUNK1 in [512,2048,4096,8192,16384]:
for CHUNK2 in [512,2048,4096,8192,16384]:
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK1)
try:
print CHUNK1,CHUNK2
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK2)
except:
print "Boohoo"
stream.stop_stream()
stream.close()
<强>更新强>
好吧,我想我明白了。如果你在下次阅读前等待太久,pyaudio库将引发溢出错误。byte_string = ''.join(data)
nums = array.array('h', byte_string)
for elt in nums[1::2]:
left.put(elt)
for elt in nums[0::2]:
right.put(elt)
这在这里进行了很多非常缓慢的处理。特别是python中的两个for
循环。让处理过程得到一大块单流数据,它可以处理,而不是一次一个int。
import numpy as np
...
n=np.fromstring(data,np.uint16)
left.put(n[1::2])
right.put(n[0::2])
我甚至不想想for
循环对延迟做了什么,但即使相对使用array
和{{1}之间的性能相对较小值得注意的是:
np.array