Long Smith
Long Smith

Reputation: 1411

Python alsaaudio capturing sound

I'm trying to capture data from microphone. Code is pretty simple but I get the error alsaaudio.ALSAAudioError: Capture data too large. Try decreasing period size for some reason. Tried google it but nothing...

res = []

recoder = alsaaudio.PCM(type=alsaaudio.PCM_CAPTURE)
recoder.setchannels(1)
recoder.setrate(8000)
recoder.setperiodsize(80)
recoder.setformat(alsaaudio.PCM_FORMAT_S8)

while len(res) < 8000*5:
    len, frame = recoder.read()

    if len(frame) != 0:
        print(len)
        res.extend(frame)

The most interesting part is that decreasing doesn't help. But increasing changes the error: period size more than 135 gives me Input output error.

OS Ubuntu 16.04. Does anybody know what the problem is?

UPDATE

Removing recoder.setchannels(1) does the trick and I can set rate/period to 8000/80 and it works but can't set to 8000/10 for example. Got the same error. It seems like a bug.

Upvotes: 0

Views: 1420

Answers (1)

Wei Yang
Wei Yang

Reputation: 78

First of all in your code,

len, frame = recoder.read()

len is the keyword in python, you can not use it as variable like that.

Otherwise it runs on my laptop OS, no such error you mentioned, but I did face the same issue as you. What I did is specifying the PCM mode and giving it an appropriate period size.

Upvotes: 1

Related Questions