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.