Today we will introduce a fun and practical thing. We use Python to implement a recording function. Without further ado, let’s get started.
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 may also be connected to a headset. The first step is to find the microphone we want to use for recording.
from pvrecorder import PvRecorder
for index, device in enumerate(PvRecorder.get_audio_devices()):
print(f"[{index}] {device}")
Running the above code on a Dell XPS laptop will yield:
[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
Note down the index of the target microphone, which we will pass to the constructor of PvRecorder. If you are unsure, you can pass -1 to the constructor to use the default microphone.
Record audio
First, create a PvRecorder instance. You need to provide a device_index (see above) and a frame_length. The frame_length is the number of audio samples you wish to receive each time you read. We set it to 512 (32 milliseconds for 16 kHz audio). Then call .start() to begin recording. After the recording starts, continue to call .read() in a loop to receive audio. Call .stop() to stop recording, and then call .delete() to release resources after completion.
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 the audio to a file
You can add your own logic to the above code snippet to do whatever we want, whether it’s detecting wake words, recognizing voice commands, transcribing speech to text, indexing audio for search, or saving it to a file. For example, if we want to save it to a file, we can use the following code, which shows how to save audio in WAVE file format.
from pvrecorder import PvRecorder
import wave
import struct
recorder = PvRecorder(device_index=-1, frame_length=512)
audio = []
try:
recorder.start()
whileTrue:
frame = recorder.read()
audio.extend(frame)
except KeyboardInterrupt:
recorder.stop()
with wave.open('audiotest', 'w') as f:
f.setparams((1, 2, 16000, 512, "NONE", "NONE"))
f.writeframes(struct.pack("h" * len(audio), *audio))
finally:
recorder.delete()