1
$\begingroup$

Here's my code:

import pyaudio
import wave
import sys
import spl_lib as spl 
from scipy.signal import lfilter
import numpy

CHUNK = 4096   # This is the sample size
               # math.pow(2, 12) => RATE / CHUNK = 100ms = 0.1 sec
FORMAT = pyaudio.paInt16    # 16 bit
CHANNEL = 1    # 1 means mono. If stereo, put 2
RATE = 48000

error_count = 0
numerator, denominator = spl.A_weighting(RATE)

pa = pyaudio.PyAudio()

stream = pa.open(format = FORMAT,
                channels = CHANNEL,
                rate = RATE,
                input = True,
                frames_per_buffer = CHUNK)


print "Listening"

while True:
    try:
        # read() returns string. You need to decode it into an array later.
        block = stream.read(CHUNK)
    except IOError, e:
        error_count += 1
        print(" (%d) Error recording: %s" % (errorcount, e))

    decoded_block = numpy.fromstring(block, 'Int16')
    y = lfilter(numerator, denominator, decoded_block)
    decibel = 20*numpy.log10(spl.rms_flat(y))
    print('A-weighted: {:+.2f} dB'.format(decibel))


stream.stop_stream()
stream.close()
pa.terminate()

The code is calculating dBA.

I set CHUNK as 4096 because it is approximately 100ms and I thought that's a reasonable length. I thought chunk sizes should be power of 2 because all the web articles I read had 1024. This threw an error about every 1 second:

[Errno Input overflowed] -9981

I thought it was a memory issue but when I ran "top" or "free -m" I had enough memory. Then I changed the chunk size as 8192(2^13) and 1024. I expected chunk size of 8192 would throw more error as it will have calculations with larger buffer. However the result was interesting. It didn't matter whether it was 256 or 8192. They were all small enough for Raspberry Pi 2. However, what was more interesting or even exciting was that small difference of chunk size would bring dramatically different results.

With a chunk size of 258, error happened for every loop when 256 was error-free. I carefully made an assumption: Why does a chunk size should be a power of 2? So I tried with 4800 which is a tenth of 48000 and then it didn't throw any error.

So I'm really confused. What is it to do with chunk size? Does it necessarily need to be power of 2? Why did 256 didn't throw an error when 258 threw error on every loop?

$\endgroup$

closed as off-topic by Batman, jojek, lennon310, hotpaw2, ThP Jun 13 '15 at 3:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "General programming questions are off-topic here, but can be asked on Stack Overflow." – Batman, jojek, lennon310, hotpaw2, ThP
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Voting to close - General programming questions are off-topic here, but can be asked on Stack Overflow. $\endgroup$ – Batman Jun 12 '15 at 14:44
  • 1
    $\begingroup$ You wrote a good question, with all relevant information, and well formatted. Unfortunately, the problem lies in a particular library and has nothing really to do with DSP - it is more of a general programming question. Stack Overflow actually has a good many questions dealing with this very error message. $\endgroup$ – JRE Jun 12 '15 at 15:14
  • 1
    $\begingroup$ As a hint, I think the real problem liesbetween PyAudio and PortAudio. PyAudio uses PortAudio. PortAudio has to request a buffer of the size you specified, but it only gets what the underlying system can create. You say "258," and the underlying system can only do 512 so there's a buffer for 512 frames in there. Now, PyAudio reads from the stream but only reads 258 frames because that is what you set - presto, Buffer error. PortAudio through those buffer errors if you don't read all the bytes out before the next bunch comes in. $\endgroup$ – JRE Jun 12 '15 at 15:18
  • 1
    $\begingroup$ That is just a guess, though. I haven't looked at the PyAudio sources to verify what it does, and there may be other causes besides (take a quick gander over at stackoverflow.com/questions/16180830/… and related questions. $\endgroup$ – JRE Jun 12 '15 at 15:20
  • $\begingroup$ Thanks for all the comments. Can someone with higher than 3,000 reputations vote to migrate this question to Stackoverflow? Or should I just repost the same question to Stackoverflow? $\endgroup$ – YOUNG Jun 15 '15 at 3:30