Pyaudio : how to check volume

女生的网名这么多〃 提交于 2019-12-04 20:31:53

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!