How to read real-time microphone sound volume in python and ffmpeg or similar

I am trying to read in almost real time the volume coming from a USB audio microphone in Python.

I have pieces, but I can’t figure out how to do this.

If I already have a WAV file, I can just read it using wavefile :

from wavefile import WaveReader with WaveReader("/Users/rmartin/audio.wav") as r: for data in r.read_iter(size=512): left_channel = data[0] volume = np.linalg.norm(left_channel) print volume 

This works fine, but I want to process the audio from the microphone in real time, not from a file.

So, I was thinking of using something like ffmpeg for real-time PIPE output in WaveReader, but my byte knowledge is somewhat lacking.

 import subprocess import numpy as np command = ["/usr/local/bin/ffmpeg", '-f', 'avfoundation', '-i', ':2', '-t', '5', '-ar', '11025', '-ac', '1', '-acodec','aac', '-'] pipe = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=10**8) stdout_data = pipe.stdout.read() audio_array = np.fromstring(stdout_data, dtype="int16") print audio_array 

It looks beautiful, but it does little. It does not work with [NULL @ 0x7ff640016600] Unable to find the appropriate output format for the 'pipe:' error .

I guess this is a pretty simple thing because I only need to check the sound for volume levels.

Does anyone know how to do this simply? FFMPEG is not a requirement, but it needs to work with OSX and Linux.

+5
source share
2 answers

Thanks to @Matthias for suggesting using the sounddevice module. This is exactly what I need.

For posterity, here is a working example that prints real-time sound levels for a shell:

 # Print out realtime audio volume as ascii bars import sounddevice as sd duration = 10 # seconds 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(duration * 1000) 

enter image description here

+8
source

Python 3 user here
I had few problems to do this 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 changed the 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 works :)

0
source

Source: https://habr.com/ru/post/1258491/


All Articles