Pyaudio : how to check volume

2019-04-16 00:25发布

问题:

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 = p.open(format = pyaudio.paInt16,
            channels = 1,
            rate = 44100,
            input = True,
            frames_per_buffer = CHUNK)

    while 1:
        self.conn.sendVoice(stream.read(CHUNK))

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

    data = stream.read(CHUNK)
    if data.volume > 20%:
        self.conn.sendVoice(data)

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).

回答1:

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 = stream.read(CHUNK)
rms = audioop.rms(data,2)

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