lundi 27 juin 2016

python2.7 on Raspberry Pi 3 - Pyaudio Input overflowed


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


Aucun commentaire:

Enregistrer un commentaire