0

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

Deviant
  • 1
  • 1

1 Answers1

0

As @Thierry pointed out above it will not be possible to get a dB SPL answer without calibrating your speaker/microphone system with actual measurements. It will be possible (as it seems you've done with your code snippet) to report relative levels.

These are the minimum requirements you need to report absolute dB SPL:

  1. dynamic range of your ADC (max/min voltage range) - what is the loudest sound that can be captured by the ADC of the computer.
  2. the sensitivity of your microphone (mV/Pa). For a sound of X Pascals pressure, how many mV (or sample-related measurement) are produced
  3. speaker calibration - given an input signal with Y rms, how many Pa (and thus dB SPL) will the output sound be?
Thejasvi
  • 200
  • 1
  • 11