Pyaudio:如何检查音量 [英] Pyaudio : how to check volume

查看:969
本文介绍了Pyaudio:如何检查音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用python作为客户端服务器开发VOIP工具。我的问题是,即使没有声音,我也正在按以下方式发送Pyaudio输入流(嗯,当没有人讲话或没有噪音时,也会发送数据):

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

推荐答案

可以使用均方根(RMS)来完成。

使用python构建自己的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 )

另一种选择是使用 audioop 查找有效值:

Another choice is use audioop to find rms:

data = stream.read(CHUNK)
rms = audioop.rms(data,2)

现在,如果要转换 rms 分贝刻度分贝= 20 * log10(rms)

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

这篇关于Pyaudio:如何检查音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆