3

I'm trying out pyaudio on Intel Edison board, but it fails with the build-in tests. Recording and playing alone works fine with my setting, but if I'm trying to wire input to output, it gives an error.

File "wire_full.py", line 33, in data = stream.read(CHUNK) File "/usr/lib/python2.7/site-packages/pyaudio.py", line 605, in read return pa.read_stream(self._stream, num_frames) IOError: [Errno Input overflowed] -9981

Does anybody understand what's the problem?

Below is the example code for wiring input to output in pyaudio:

""" 
PyAudio Example: Make a wire between input and output (i.e., record a 
few samples and play them back immediately). 


This is the full duplex version. 
"""  


import pyaudio  
import sys  


CHUNK = 1024  
WIDTH = 2  
CHANNELS = 2  
RATE = 44100  
RECORD_SECONDS = 5  


if sys.platform == 'darwin':  
    CHANNELS = 1  


p = pyaudio.PyAudio()  


stream = p.open(format=p.get_format_from_width(WIDTH),  
                channels=CHANNELS,  
                rate=RATE,  
                input=True,  
                output=True,  
                frames_per_buffer=CHUNK)  


print("* recording")  


for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):  
    data = stream.read(CHUNK)  
    stream.write(data, CHUNK)  


print("* done")  


stream.stop_stream()  
stream.close()  

p.terminate()
Xun Yang
  • 4,209
  • 8
  • 39
  • 68

2 Answers2

2

I solved it by changing the CHUNK size to 512. Still having the same error once in a while, but at least it works most of the time.

If there's any idea why this works, or how I may remove the error totally?

Update

OK, so I surrounded the read/write code with try.. except, also lowering the CHUNK size to 512, now it's working without any errors. If I use 1024 as the CHUNK size, a lot frames will be lost and the sound quality is terrible.

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):  
    try:
        data = stream.read(CHUNK)  
        stream.write(data, CHUNK)  
    except:
        pass

Thanks @Meta for the clue! :)

Xun Yang
  • 4,209
  • 8
  • 39
  • 68
  • 1
    See my answer at http://stackoverflow.com/questions/10733903/pyaudio-input-overflowed/28574532#28574532. In short, the input overflow message means that you are not reading the stream fast enough, i.e., the buffer that holds the audio data gets overrun by newer incoming data before you have time to read it. I don't think pyaudio should throw an exception in that case, since it may be acceptable. Also because it throws an error, you lose all the data for that call. See the patch in the link above for an ad-hod way to change its behaviour. – Meta Apr 06 '15 at 02:09
0

This code records audio and plays it back immediately:

import pyaudio
import time
import pickle

WIDTH = 2
CHANNELS = 2
RATE = 44100

p = pyaudio.PyAudio()

def callback(in_data, frame_count, time_info, status):
    return (in_data, pyaudio.paContinue)

stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():    
    time.sleep(0.1)

stream.stop_stream()
stream.close()

p.terminate()
shiva
  • 2,535
  • 2
  • 18
  • 32
  • when I run your code I get: python: src/hostapi/alsa/pa_linux_alsa.c:3636: PaAlsaStreamComponent_BeginPolling: Assertion `ret == self->nfds' failed. – user1759557 Mar 20 '19 at 23:26
  • See a solution for the latter here: stackoverflow.com/a/60824906/1509695. It's a patch for PortAudio, which I think PyAudio is using. – matanster May 12 '20 at 12:51