Add an ear to your Raspberry Pi system and open up the possibilities of audio-based projects. In this tutorial, you are going to learn how to record audio with the Raspberry Pi.
Even the latest Raspberry Pi release doesn’t have its own audio device. To add audio input and output to your Pi, you need to set up an external microphone and speaker. This opens up a wide variety of audio-based applications like voice recognition using machine learning or AI, a portable studio sound recorder, a DIY Google home assistant, or even a spy bug (highly discouraged for obvious reasons). These projects are often complex and hard to replicate, so we won’t concern ourselves with that yet. In this tutorial, we will set up a simple USB microphone—a relatively simpler task.
Why choose a USB microphone?
There are four ways you can connect a microphone to the Raspberry Pi: through GPIO, through a microphone jack via an external sound card, through Bluetooth connection, and USB.
First, through the GPIO pins, you will need a pre-amplifier circuit and a dedicated DAC to convert the analog signal from an electret mic to digital. The pros are that it can give you better audio quality, depending on your hardware. The cons are that it will cost more and look cruder because every component of your audio processing is exposed.
The second option is through a microphone jack via an external sound card. This method will give the best audio quality but is the most costly. Since the Raspberry Pi doesn’t include a microphone jack, you will need to buy a HAT or a USB-based sound card to connect to the Raspberry Pi.
The third is Bluetooth. Depending on the Bluetooth device, this may be the easiest way to add a microphone as it doesn’t require a physical connection. Compatibility issues, audio quality, and price are the problem with Bluetooth devices. Bluetooth headsets are slightly more expensive than their wired counterparts. Apart from that, microphones aren’t really prioritized with their design. I tested mine out, and while it connected successfully to the Pi, the quality of the record is most unimpressive.
Finally, USB microphones. USB microphones are plug-and-play. They fit nicely with the system and don’t require additional hardware or software to work. The sound quality is not the best but is pretty decent for simple audio processing projects.
Setting up a USB microphone using a Desktop is seamless. Once you connect it to the Raspberry Pi, the Pi automatically detects the microphone. To Open the Audio Device Settings in the Menu: go to Preferences > Audio Device Settings. There you will see your microphone and even change the amplification with a handy gauge controller. Just press the red record button to start recording audio.
Using Command Line
To check the USB device connection, enter the following command:
You should expect an output similar to this:
Bus 001 Device 001 is the Raspberry Pi’s physical hub, while Bus 001 Device 002 is the USB microphone. With other Raspberry Pi models, this could show 2 to 5 devices.
To see the list of audio input devices, enter:
This output should be shown:
So the microphone matches with card 1 / device 0. This will be mapped in the following with
Finally, we can start recording audio by entering the following command:
arecord -D plughw:1,0 -d 3 test.wav
The following output should appear:
You can check this file by connecting and downloading it with your favorite FTP software (for example, Filezilla) to your RPI with ad SFTP connection (using your RPI user and password, pi and raspberry if you left the factory default ones).
Recording volume can be very low in some cases. Alsamixer helps us with a simple interface to manage audio device volumes. Type in your terminal:
You’ll see the default console:
Now, you have to switch to the correct audio device by pressing F6:
In my case, the correct sound card is the one marked as bcm2835 ALSA:
Use up / down arrow keys to regulate at your choice. Press ESC to save and exit.
Sign Up & Get at Least Two $5 Coupons Now: https://jlcpcb.com/quote