Python IOError:[Errno输入溢出]-9981
我试图在RaspberryPi B型板上的Rasbian上执行PyAudio python捕获程序,但出现错误:Python IOError:[Errno输入溢出]-9981,python,audio,raspbian,ioerror,Python,Audio,Raspbian,Ioerror,我试图在RaspberryPi B型板上的Rasbian上执行PyAudio python捕获程序,但出现错误: Traceback (most recent call last): File "/home/pi/pythonsound/record.py", line 35, in <module> data = stream.read(CHUNK) File "/usr/local/lib/python2.7/dist-packages/pyaudio.py",
Traceback (most recent call last):
File "/home/pi/pythonsound/record.py", line 35, in <module>
data = stream.read(CHUNK)
File "/usr/local/lib/python2.7/dist-packages/pyaudio.py", line 605, in read
return pa.read_stream(self._stream, num_frames)
IOError: [Errno Input overflowed] -9981
这是我的USB声卡设备信息
{'defaultSampleRate': 44100.0,
'defaultLowOutputLatency': 0.011609977324263039,
'defaultLowInputLatency': 0.011609977324263039,
'maxInputChannels': 1L,
'structVersion': 2L,
'hostApi': 0L,
'index': 0,
'defaultHighOutputLatency': 0.046439909297052155,
'maxOutputChannels': 2L,
'name': u
'USB PnP Sound Device: USB Audio (hw:0,0)',
'defaultHighInputLatency': 0.046439909297052155}
您能指导我解决这个问题吗?在阅读了不同的用户体验和他们的修正后,只需更改参数值 正如上面一位专家所描述的那样,发生这种情况的实际原因是什么
IOError: [Errno Input overflowed] -9981
所以我也开始增加CHUNK的值,最后我也成功地克服了这个错误。
现在我更正后的编码是:
import pyaudio, wave, time, sys
from datetime import datetime
CHUNK = 8192
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
current_time = str(datetime.now()) #"Date/Time for File Name"
current_time = "_".join(current_time.split()).replace(":","-")
current_time = current_time[:-7]
WAVE_OUTPUT_FILENAME = 'Audio_'+current_time+'.wav'
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels = CHANNELS, rate = RATE, input = True, input_device_index = 0, frames_per_buffer = CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
print i
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
我也有同样的问题,试图改变块大小和采样率,但没有成功。通过使用多处理,问题得到了解决。这是我的密码: 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/"
我在交互式环境(Jupyter笔记本)中运行时也遇到了同样的问题,对我来说,这是由于在代码运行之间缓冲区没有被清除而导致的。请注意,for循环可能会使块未读,具体取决于RATE、chunk、RECORD_SECONDS的值。运行此代码段以查看:
CHUNK = 1024
RATE = 44100
RECORD_SECONDS = 5
count = RATE * RECORD_SECONDS
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
count = count - CHUNK
print("Buffer unread count:", count)`
这导致缓冲液中仍有340个样品。我认为这会在下次执行代码时导致缓冲区溢出错误。我尝试了多次运行,速率/块比率正好低于1.0,正好高于1.0,但我不清楚是什么条件触发了溢出错误。当我在3秒钟的样本中运行一个略小于1(48000/4799)的比率,然后再运行一个略大于1(48000/4801)的比率时,发生了超限。这似乎是运行将数据留在缓冲区中的累积效应,在某个点触发溢出条件
为了解决这个问题,我最终使用了一个采样频率的偶数倍的数据块(比如4800对48000 kHz),并记录了一个整数秒长的数据,这样缓冲区中就没有数据了。通常,输入溢出意味着数据到达的速度比计算机读取数据的速度快。你能告诉我你是否收到了任何数据吗?@HeatfanJohn在命令终端上它正在接收来自这个命令的音频输入
arecord-D plug:default-f S16_LE-c 1-r 16000-D 300 a.wav
@HeatfanJohn我怎么知道python上它是否真的在接收数据,尽管上面显示了错误,但在python脚本中的data=stream
行之前可能会添加一个print i
。@HeatfanJohn我是在python脚本*中的data=stream
行之前添加print i
行以记录0 1回溯(最近一次调用最后一次):文件时获得此输出的“/home/pi/pythonsound/myaudio01JAN27.py”,第25行,在data=stream.read(CHUNK)文件“/usr/local/lib/python2.7/dist packages/pyaudio.py”中,第605行,在读取返回pa.read_流(self._流,num_帧)ioerrno输入溢出]-9981
CHUNK = 1024
RATE = 44100
RECORD_SECONDS = 5
count = RATE * RECORD_SECONDS
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
count = count - CHUNK
print("Buffer unread count:", count)`