DEV Community

Cover image for Day 15: Recording Audio without PyAudio
Dilek Karasoy for Picovoice

Posted on • Updated on

Day 15: Recording Audio without PyAudio

Existing third-party libraries (e.g. PyAudio) are not cross-platform and have external dependencies. 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.

Image description

Install
Install PvRecorder using PIP:

pip3 install pvrecorder
Enter fullscreen mode Exit fullscreen mode

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}")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)