Recording audio from a microphone using Python is tricky! Why? Because Python doesn't provide a standard library for it. Existing third-party libraries (e.g. PyAudio) are not cross-platform and have external dependencies. We learned this the hard way as we needed microphone recording functionality in our voice recognition demos.
Below we learn how to record audio in Python using
PvRecorder. The Python SDK captures audio suitable for speech recognition, meaning the audio captured is already 16 kHz and 16-bit.
PvRecorder Python SDK runs on Linux, macOS, Windows, Raspberry Pi, NVIDIA Jetson, and BeagleBone.
PvRecorder using PIP:
Find Available Microphones
A computer can have multiple microphones. For example, a laptop has a built-in microphone and might have a headset attached to it. The first step is to find the microphone we want to record.
Running above on a Dell XPS laptop gives:
Take note of the index of your target microphone. We pass this to the constructor of
PvRecorder. When unsure, pass
-1 to the constructor to use the default microphone.
First, create an instance of
PvRecoder. You need to provide a
device_index (see above) and a
frame_length is the number of audio samples you wish to receive at each read. We set it to 512 (32 milliseconds of 16 kHz audio). Then call
.start() to start the recording. Once recording, keep calling
.read() in a loop to receive audio. Invoke
.stop() to stop recording and then
.delete() to release resources when done.
Save Audio to File
You can do whatever you wish using the code snippet above. Whether you want to detect wake words, recognize voice commands, transcribe speech to text, index audio for search, or save it to a file. The code snippet below shows how to save audio into a WAVE file format.