一、依赖库
import wave from math import log import numpy as np import pyaudio
二、代码
def savewavfile(count):
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output"+str(count)+".wav"
# 初始化Pyaudio
p = pyaudio.PyAudio()
# 打开音频流
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
#print("* recording")
# 录制音频
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
#print("* done recording")
# 关闭音频流
stream.stop_stream()
stream.close()
p.terminate()
# 保存音频文件
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
def median(data):
data.sort()
half = len(data) // 2
return (data[half] + data[~half])/2
def outputDB(count):
wf = wave.open('output' + str(count) + '.wav', "rb")
nframes = wf.getnframes()
wavedata = wf.readframes(nframes)
wavewidth = wf.getsampwidth()
wavechannel = wf.getnchannels()
framerate = wf.getframerate()
wavedata = np.frombuffer(wavedata, dtype=np.short)
wavedata = wavedata.T
wavedata = abs(wavedata)
sum_data = sum(wavedata)
avg_data = sum_data / len(wavedata)
# print('sum_data = ',sum_data,' avg_data = ',avg_data,max(wavedata))
fenbei1 = 20 * log(max(wavedata), 10) # 最大分贝值
fenbei2 = 20 * log(median(wavedata), 10) # 中位数
fenbei3 = 20 * log(avg_data, 10) # 平均值
print(fenbei3)
return fenbei3 # 取平均数作为判断
if __name__ == '__main__':
savewavfile(1)
currentDB = outputDB(1)
if currentDB > 50:
print("通过")
三、运行结果
D:\1_python\python.exe D:\3_pythonWorkSpace\Autotest_SVAE\common\voice\voice_verify.py
42.952020134468974
Process finished with exit code 0
四、如何使用
使用前先采集环境音,例如环境音42分贝
再采集测试音,例如测试音分贝60分贝
那么可以设置50分贝作为阈值来判断是否出声
五、如何提高准确率
1.电脑接mic,mic紧靠音源
2.mic和喇叭放置在隔音罩里