Search code examples
pythonaudioraspbianioerror

IOError: [Errno Input overflowed] -9981


I am trying to execute a PyAudio python capturing program on Rasbian in my RaspberryPi model B board, but getting error:

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

There are some other suggestions available but not effective Here is what I've tried, This is the code

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")
frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    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()

This is my USB Audio Card device info,

{'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}

can you please guide me resolve this problem?


Solution

  • After reading different users experience and their of correction with just changing the value of parameters.

    As an expert describe above, the actual reason of

    IOError: [Errno Input overflowed] -9981
    

    so I also start increasing the value of CHUNK and at last I also get success over this error. And now my coding after correction is:

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