Search code examples
pythonpython-3.xaudio

How do I get the sound level in Python?


For a project I am doing I need to get the live decibel level of my mic.

I have seen the plotters:

# Print out realtime audio volume as ascii bars

import sounddevice as sd
import numpy as np

def print_sound(indata, outdata, frames, time, status):
    volume_norm = np.linalg.norm(indata)*10
    print ("|" * int(volume_norm))

with sd.Stream(callback=print_sound):
    sd.sleep(10000)

But i just need the volume as an integer, how do I do that?


Solution

  • Just print the number instead of bars:

    def print_sound(indata, outdata, frames, time, status):
        volume_norm = np.linalg.norm(indata)*10
        print(int(volume_norm))