I wonder that how to get sound pressure level in dB. The input should be the signal from the microphone of PC (real-time audio signal). The output is the real-time sound pressure level of input signal. This is my simple code.
import pyaudio
import numpy as np
import wave
from threading import Thread
from pysine import sine
import math
import time
def print_sound():
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
pa = pyaudio.PyAudio()
stream = pa.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
buffer = []
while True:
string_audio_data = stream.read(CHUNK)
audio_data = np.frombuffer(string_audio_data, np.int16)
volume_norm = np.linalg.norm(audio_data)*10
dfft = 10.*np.log10(abs(np.fft.rfft(audio_data)))
print(int(volume_norm))
print_sound()
and this error is
Warning (from warnings module): File "C:\Users\Admin\1.py", line 27 dfft = 10.*np.log10(abs(np.fft.rfft(audio_data))) RuntimeWarning: divide by zero encountered in log10