The Raspberry Pi is capable of recording audio through its USB 2.0 ports using the advanced Linux sound architecture (ALSA). The RPi can sample at 48kHz at a bit depth of 16-bits, which allows the user to record and playback fairly good quality audio. For this tutorial, I will demonstrate the process of enabling a USB audio device and using it to record and analyze acoustic signals using Python 3.x. The Python audio analysis is a great tool for engineers interested in acoustic or audio processing and even signal processing techniques.
Below are a few links for USB microphones compatible with the Raspberry Pi. Depending on the application, the user may want to buy a nicer microphone (higher bit-depth, larger dynamic frequency range, higher sampling audio card, etc.) or a cheaper one may suffice. Cheaper and mid-tier microphone links are listed below.
This output lets us know the Pi is reading the USB microphone because of its response: “Class=Audio, Driver=snd-usb-audio” - and if you are seeing a similar response, then congratulations! Your USB mic is ready to go. The Pi isn’t quite ready to use the USB mic, but I will discuss this in the next section.
Assuming the two installs above have been successful, open Python 3.x and import pyaudio. If everythin has been successful, we are ready to head to the next section and ensure that the USB mic is functioning and the Pi has selected the correct device.
Take note of the index of the USB device, because we will need to adjust the pyaudio device index according to the sequence above. For example, our USB device index is “2” (index 0 is ALSA blank, index 1 is IEC958/HDMI, etc…).
Take note of the index of the USB device, because we will need to adjust the pyaudio device index in reference to the sequence above. For example, my USB device is located at index 2 (index 0 is ALSA main, index 1 is IEC958/HDMI, etc…).
Now that we’ve identified the USB mic’s index, we can record a test sample with pyaudio. For in-depth information about the use of pyaudio, find its documentation here.
The output .wav file should be 3 seconds long (assuming the code above is unchanged) and is sampled at 44.1kHz with a maximum resolution of 16-bits. Depending on the microphone used, the sample rate can be increased to 48kHz. The bit-depth can be changed as well, though I’m not entirely sure of the limitations on the Pi’s capabilities there.
This tutorial covers how to record audio using a USB microphone and a Raspberry Pi. Using Python’s pyaudio library, I demonstrated how to prepare the Pi for audio recording and saving the audio as a .wav file. The Pi, with a high-quality microphone, is capable of mid-tier audio recording (16-bit, 48kHz). This method could be used to produce and record podcasts, instruments, or any type of audio recording at that resolution and sampling rate. In the next audio tutorial, I will demonstrate how to analyze the USB audio recorded on the RPi in real-time. That tutorial will involve more in-depth programming, signal processing, and acoustic analysis.