August 16th, 2024 · 1 min read
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 and created Picovoice Audio Recorders.
Hence we created PvRecorder, a cross-platform library that supports Linux, macOS, Windows, Raspberry Pi, NVIDIA Jetson, and BeagleBone. PvRecorder has SDKs for Python, Node.js, .NET, Go, and Rust.
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.
Install
Install PvRecorder using PIP:
pip3 install pvrecorder
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.
from pvrecorder import PvRecorder
for index, device in enumerate(PvRecorder.get_audio_devices()):
print(f"[{index}] {device}")
Running above on a Dell XPS laptop gives:
[0] Monitor of sof-hda-dsp HDMI3/DP3 Output
[1] Monitor of sof-hda-dsp HDMI2/DP2 Output
[2] Monitor of sof-hda-dsp HDMI1/DP1 Output
[3] Monitor of sof-hda-dsp Speaker + Headphones
[4] sof-hda-dsp Headset Mono Microphone + Headphones Stereo Microphone
[5] sof-hda-dsp Digital Microphone
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.
Record Audio
First, create an instance of PvRecoder. You need to provide a device_index (see above) and a frame_length. 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.
recorder = PvRecorder(device_index=-1, frame_length=512)
try: recorder.start()
while True:
frame = recorder.read()
# Do something ...
except KeyboardInterrupt:
recorder.stop()
finally:
recorder.delete()
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.
recorder = PvRecorder(device_index=-1, frame_length=512)
audio = []
try:
recorder.start()
while True:
frame = recorder.read()
audio.extend(frame)
except KeyboardInterrupt:
recorder.stop()
with wave.open(path, 'w') as f:
f.setparams((1, 2, 16000, 512, "NONE", "NONE"))
f.writeframes(struct.pack("h" * len(audio), *audio))
finally:
recorder.delete()
Top comments (0)