Trying to understand PyAudio error: [Errno -9981] Input overflowed
Asked Answered
L

0

9

I am trying to do a sound processing project with PyAudio, but I sometimes get this error and I don't understand why.

The following code plays back audio heard on my laptop mic, and then pauses for 3 seconds and then continues. This code runs fine, and during the pause if I clap, the audio doesn't play after the pause ends which makes it seem like once the buffer fills up any extra audio is lost.

But if I decrease CHUNK to a low number like 20, once it pauses for a second it throws the error

  File "C:\Anaconda3\lib\site-packages\pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
OSError: [Errno -9981] Input overflowed

So if audio can be lost during the pause due to (I think) the buffer filling up without throwing an error, why is there an error when I decrease the frames per buffer?

Code:

import pyaudio
import time

# initialize stream parameters
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024

p = pyaudio.PyAudio()

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


x = 1
while True:
    data = stream.read(CHUNK)
    stream.write(data)

    x=x%100 + 1
    if x == 100:
        print(time.time())
        time.sleep(3)

EDIT - I think I fixed the problem by adding this argument to the read:

data = stream.read(CHUNK, exception_on_overflow = False)

However, my question still stands. I don't understand why the error would only occur with a small CHUNK size even though it seems like the buffer is filling up in both cases.

Leighannleighland answered 13/2, 2018 at 23:13 Comment(1)
Related: https://mcmap.net/q/382955/-pyaudio-input-overflowed - check the input format (rate and format), and if its not that, the code is just too slow to process the data.Bora

© 2022 - 2024 — McMap. All rights reserved.