Maker Portal

View Original

Recording Audio on the Raspberry Pi with Python and a USB Microphone

See this content in the original post

See this content in the original post

You can elect to purchase the USB Microphone from our store, or below in the list of links for USB microphones and sound cards 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. The microphone in our store is a fairly inexpensive microphone that is compatible with Raspberry Pi and also boasts a frequency response of 20 Hz - 16 kHz, so it is ideal for most acoustic applications.

See this product in the original post

After acquiring the USB mic and plugging it into one of the RPi USB ports, we need to ask the RPi if it is communicating with the audio device. Type the following into the command window:

See this content in the original post

The output should look something like the following:

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.

See this content in the original post

See this content in the original post

We will be using the Python library ‘pyaudio’ to record and play audio data from the USB mic. Before we can get started with ‘pyaudio,’ we need to ensure that the RPi has all the necessary prerequisites by installing the following packages:

See this content in the original post

If the above is successful, then we can download the ‘pyaudio’ library (I’m installing to Python 3.x with ‘pip3’):

See this content in the original post

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.


See this content in the original post

Open Python 3.x and type the following (I use IDLE):

See this content in the original post

This should output the index of each audio-capable device on your Pi. For my Pi, my output looked like this:

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…).

See this content in the original post

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.

See this content in the original post

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.

See this content in the original post

See this content in the original post

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.

See this content in the original post

For More in Raspberry Pi and Audio:

See this gallery in the original post