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 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)
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 python3
import numpy as np
import sounddevice as sd
duration = 10 #in seconds
def 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 :)