pythonnumpysignal-processingwavpyaudio

Python: Get volume decibel Level real time or from a wav file


For a project work, I need to measure the volume level either from a recorded audio file or in real time recording using a mic. After my primary research, I have tried using soundfile library. The file was read using soundfile and using 20*np.log10(np.sqrt(np.mean(np.absolute(a)**2))), I have calculated the dB value. I'm getting a negative value for the sound file. But a normal sound may be in the range of 50-70 dB and I'm getting a negative value. Can anybody help me to sort out this?


Solution

  • Short answer: dB isn't the same as dB. Your results are probably correct.

    Long answer: dB levels always define a relation to some reference value. For audio / acoustics, there are many reference values, and you need to specify which one you are using for a value in dB to be meaningful. dB values are then calculated as

    20*log10(value/reference)dB

    (assuming it is a root-power quantity, for power quantities the factor is 10, not 20). One important thing to note is that dividing by the reference value removes the unit as expected.

    So, when you say

    normal sound may be in the range of 50-70 dB

    that's not really an accurate statement, you probably mean

    normal sound may be in the range of 50-70 dB SPL

    where you are giving a value relative to the reference sound pressure level of 20 µPa.

    In digital systems, sound files are typically represented by floating numbers < 1, then we speak of dB FS (dB full scale) with reference value 1 (no unit). By the laws of math, dB FS values are negative.

    It is also clear that you cannot directly relate dB FS values to dB SPL values: if you play the same audio file (i.e. taking some dB FS value) and play it twice, but turn up the volume knob of your speaker, it will lead to two different values dB SPL (what you hear).