How to read realtime microphone audio volume in python and ffmpeg or similar
Thanks to @Matthias for the suggestion to use the sounddevice module. It's exactly what I need.
For posterity, here is a working example that prints real-time audio levels to the shell:
# Print out realtime audio volume as ascii barsimport sounddevice as sdimport numpy as npdef 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)
Python 3 user here
I had few problems to make that work so I used: https://python-sounddevice.readthedocs.io/en/0.3.3/examples.html#plot-microphone-signal-s-in-real-time
And I need to install sudo apt-get install python3-tk
for python 3.6 look Tkinter module not found on Ubuntu
Then I modified script:
#!/usr/bin/env python3import numpy as npimport sounddevice as sdduration = 10 #in secondsdef audio_callback(indata, frames, time, status): volume_norm = np.linalg.norm(indata) * 10 print("|" * int(volume_norm))stream = sd.InputStream(callback=audio_callback)with stream: sd.sleep(duration * 1000)
And yes it working :)