Python - read audio stream

4.2k views Asked by At

So I'm trying to set up an app that records a audio input from a microphone, and checks to see if you say a specific phase/command. Once it reads that command, it then launches a function. I'm recording audio input using the sound device package. Actually to the docs you use the rec command to read in the input, but that is a start stop blocking operation. I cant evaluate the values of that recording. Does anyone know how to use a stream for the audio buffer ?

import sounddevice as sd
import numpy as np

fs = 44100
duration = 5  # seconds
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2, dtype='float64')
# I would like to read myrecording into a stream at this point. 
print("Recording Audio")
sd.wait()
print("Audio recording complete , Play Audio")
sd.play(myrecording, fs)
sd.wait()
print("Play Audio Complete")
1

There are 1 answers

0
PatriceG On BEST ANSWER

You can't read a stream with the "rec" function. You have to use a callback function (as Matthias said in its comment).

Have a look on http://python-sounddevice.readthedocs.io/en/latest/examples.html, for instance the Real-Time Text-Mode Spectrogram.