这里有一些python代码,使用
pyaudio library
可以用均方根
(RMS)
计算分贝值,并每0.3秒打印一次。
rms
和
db
都可以作为变量使用。
import pyaudio
import time
from math import log10
import audioop
p = pyaudio.PyAudio()
WIDTH = 2
RATE = int(p.get_default_input_device_info()['defaultSampleRate'])
DEVICE = p.get_default_input_device_info()['index']
rms = 0
print(p.get_default_input_device_info())
def callback(in_data, frame_count, time_info, status):
global rms
rms = audioop.rms(in_data, WIDTH) / 32767
return in_data, pyaudio.paContinue
stream = p.open(format=p.get_format_from_width(WIDTH),
input_device_index=DEVICE,
channels=1,
rate=RATE,
input=True,
output=False,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
db = 20 * log10(rms)
print(f"RMS: {rms} DB: {db}")
time.sleep(0.3)
stream.stop_stream()
stream.close()