我尝试写了个,但是播放音乐的时候,分贝DB 没有任何变化,虽然他一直在刷新
import sounddevice as sd
import numpy as np
def callback(indata, frames, time, status):
if status:
print(status, flush=True)
# 计算输入数据的均方根(RMS)
rms = np.sqrt(np.mean(indata**2))
# 将RMS值转换为分贝(dB)
db = 20 * np.log10(rms)
print(f"当前声音大小(分贝): {db:.2f} dB", flush=True)
# 设置音频输入设备(通常是默认的输出设备)
device = sd.default.device
# 打开音频流
with sd.InputStream(device=device, channels=2, callback=callback):
sd.sleep(1000000)
import sounddevice as sd
import numpy as np
def callback(indata, frames, time, status):
if status:
print(status, flush=True)
# 计算输入数据的均方根(RMS)
rms = np.sqrt(np.mean(indata**2))
# 将RMS值转换为分贝(dB)
db = 20 * np.log10(rms)
print(f"当前声音大小(分贝): {db:.2f} dB", flush=True)
# 设置音频输入设备(通常是默认的输出设备)
device = sd.default.device
# 打开音频流
with sd.InputStream(device=device, channels=2, callback=callback):
sd.sleep(1000000)