Pyaudio : how to check volume

Go To


I'm currently developping a VOIP tool in python working as a client-server. My problem is that i'm currently sending the Pyaudio input stream as follows even when there is no sound (well, when nobody talks or there is no noise, data is sent as well) :

    CHUNK = 1024
    p = pyaudio.PyAudio()
    stream = = pyaudio.paInt16,
            channels = 1,
            rate = 44100,
            input = True,
            frames_per_buffer = CHUNK)

    while 1:

I would like to check volume to get something like this :

    data =
    if data.volume > 20%:

This way I could avoid sending useless data and spare connection/ increase performance. (Also, I'm looking for some kind of compression but I think I will have to ask it in another topic).

2014-09-16 12:11
by Waroulolz


Its can be done using root mean square (RMS).

One way to build your own rms function using python is:

def rms( data ):
    count = len(data)/2
    format = "%dh"%(count)
    shorts = struct.unpack( format, data )
    sum_squares = 0.0
    for sample in shorts:
        n = sample * (1.0/32768)
        sum_squares += n*n
    return math.sqrt( sum_squares / count )

Another choice is use audioop to find rms:

data =
rms = audioop.rms(data,2)

Now if do you want you can convert rms to decibel scale decibel = 20 * log10(rms)

2014-09-16 14:13
by ederwander