mefiX
mefiX

Reputation: 1307

How to calculate the dBFS level using python sounddevice

I'm trying to get a decibel reading from an audio stream using the sounddevice library.

More specifically, I want to display the current level in a DAW like fashion.

According to this, most DAWs display either dBFS or LUFS levels in their meters. I'm interested in dbFS, because that seems to be the one more commonly used.

According to this, the dBFS level computes to

value_dBFS = 20*log10(rms(signal) * sqrt(2)) 

which can be simplified to

value_dBFS = 20*log10(rms(signal)) + 3.0103

The values coming from the sounddevice library with dtype='float32' are in a range between [-1,1]

Here's my code:

import sounddevice
import numpy as np

class SoundLevelReader(QObject):
    new_level = Signal(float)
    def sd_callback(self, indata, frames, time, status):
        # see https://en.wikipedia.org/wiki/Sound_pressure#Sound_pressure_level
        # see https://dsp.stackexchange.com/questions/8785/how-to-compute-dbfs
        rms  = np.sqrt(np.mean(np.absolute(indata)**2))
        dBFS = 20 * np.log10(rms * np.sqrt(2))
        self.new_level.emit(dBFS)

def main():
    # ...
    app    = QApplication(sys.argv)
    reader = SoundLevelReader()
    meter  = VolMeter(-60, 0)
    reader.new_level.connect(meter.set_meter_value)
    with sd.InputStream(callback=reader.sd_callback, dtype='float32', device=4, blocksize=5000):
        sys.exit(app.exec())

At first glance, the values seem reasonable. However, compared to my DAW (Logic Pro), my values are around 6dB lower and I do not understand why. I'm comparing the values of my DAW with the code above with a Sennheiser Profile USB-C microphone. Inside the DAW I didn't add any gain or effects. I only selected the input and that's it.

If a add a smudge factor to the log10() function, I can sort of match the levels, but I'd like to understand what the actual value is and where it comes from.

value_dBFS = 20*log10(dirty * rms(signal)) + 3.0103

Question:

Can someone explain, how to correctly calculate dBFS based on readings coming from the sounddevice python lib?

Upvotes: 0

Views: 77

Answers (0)

Related Questions