1

I'm calling for help for an issue I havn't been able to tackle so far... I'm using a Raspberry Pi 3 Model B on which I've plugged a Focusrite Scarlett 2i2 USB Sound Card on which is plugged a XLR microphone. The whole thing is working fine on Audacity for example. It can record and/or play a sound. Now I'd like to use this setup to record a stream in Python and analyse it with and FFT function to get a real time estimation of the pitch of the sound I'm listening to. When I run the code with Python 2.7 on the Raspberry, it works for 4 ou 5 seconds, then it stops with the following exception :

IOError: [Errno Input overflowed] -9981

Then it crashes the python console.

Please note that this code works fine on a basic computer, but I need it to work on the Pi3...

I wondered if it wasn't the threads causing the problem with an overflow of data, so I went back to very basic code issued by Pyaudio to "test" the setup. So I ran the code below, and found myself facing the exact same problem, with the same error.

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
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()

But I had an idea, as to reduce the CHUNK size, and I found after a few runs that above 1010, the exception appeared.

So my question is very simple, does someone know where does this come from? Why can't I have a CHUNK size of 1024 if I want or need to? and why does it run properly on a computer, and not on the Pi? Is there a solution?

Many thanks in advance, Edouard

  • 1
    Related: http://stackoverflow.com/questions/10733903/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. – Caramiriel Jun 27 '16 at 15:47

1 Answers1

0

Not sure if your question can be explicitly answered the way you have phrased it. Hopefully I can point you in a couple of directions. I've been doing some audio streaming work with my Model 3, so I'm very interested in your example.

The biggest difference between the Pi and a computer is the CPU speed and memory - 1.2GHz cpu and 1GB Ram are much lower specs than any low-end computer. Doing the stream processing with an interpreted language likely adds

It looks like you're doing all the recording to memory - then writing it out at the end. I imagine that's where the overflow occurs. I think chunk size is just a red herring.

If you're going to stick to Python processing I think the better option would be to write the frame data as it's received.

In my opinion, I think the better option would be to use arecord or ffmpeg/avconv directly to record the stream directly to a file. But I'm definitely interested in see if you can do this in Python

What O/S did you install - Raspbian or Raspbia Lite?

EDIT
Just for interest sake - have you tried adding a debug print before each append just to see size of frame string before it crashes?

dbmitch
  • 5,361
  • 4
  • 24
  • 38