Raspberry Pi can record and playback fairly good quality audio through its USB 2.0 ports. For recording audio and playback we need two peripheral devices, a USB microphone, and a speaker. You can choose to use a USB speaker, or a speaker with a 3.5mm sound jack. Alternatively, if you are connected to a device with inbuilt speakers through HDMI(like your laptop, that will work too. The audio recorded can then be used for several applications using the Python audio analysis.
Hardware requirements:
- Raspberry Pi with OS installed (available on the official website)
- Ethernet cable/WiFi
- Power cable
- Monitor
- HDMI connector
- USB or Bluetooth mouse
- USB or Bluetooth keyboard
- Speaker (USB or with 3.5mm sound jack)
- USB microphone
Setup for recording audio:
Hardware:
- Attach the Speaker and microphone to the Pi
- Power it on
Software:
1. Open configuration settings
Open the terminal, and open the configuration settings by
sudo raspi-config
Alternatively, you can open it from: start menu –> Preferences —> Raspberry Pi Configuration
2. Select the output
Use your arrow keys to scroll down to Advanced Options and click enter to open the options
Scroll down and select Audio
Depending on the Speaker you are using, select the option you need.
Then click Ok and then Finish
3. Identify input device and card number
In the terminal type the command:
arecord -l
You will then see the list of capture hardware devices. From this, note down the card number and device number of the microphone.
4. Identify output device and card number
In the terminal type the command:
aplay -l
You will then see the list of playback hardware devices. From this, note down the card number and device number of the speaker. If you are using an audio jack, it usually shows up as Analog or bcm2835 ALSA.
5. Create a file in home directory
In the terminal, type:
nano .asoundrc
In the GNU opened on clicking enter after previous command type
pcm.!default{ type asym capture.pcm “mic” playback.pc “speaker” } pcm.mic { type plug slave { pcm “hw:< insert card number>,<device number>” } } pcm.speaker { type plug slave { pcm “hw:< insert card number>,<device number>” } }
Replace the numbers to look like this,
Save and then exit. Then, to check if it has been stored in the home directory, type:
ls -a
This will enlist the whole list of the current directory including the hidden files, in which your file should also be visible. If it isnt, go back and check if you saved it in the right directory.
6. Adjust the playback volume:
This will control how loud the playback will be. Playback means playing ‘back’ the pre-recorded material. To do this first type:
alsamixer
You can then control the volume using the up and down arrow keys. Optimally, bring it to around 70.
7. Test the speaker output
Speaker output can be tested by playing a test audio. To do so, in the terminal type:
speaker-test -t wav
Once you can hear the voice, click ctrl+C. If you are unable to hear anything. Recheck your speaker connection.
8. Recording audio clip
To start a recording, in the terminal type
arecord --format=S16_LE --duration=5 --rate=16000 --filetype=raw sample.wav
This will record a short audio clip that is 5 seconds long. You can however, change the value in the arecord command to increase the duration of the clip.
9. Play the recording
To check if the recording has been saved successfully, we can now play it. To do this, in the terminal type:
aplay --format=S16_LE --rate=16000 sample.wav
This should play back the audio for you successfully!
In case of error:
If error occurred on testing the speaker with the test audio, and you werent able to hear: check the hardware connections. In addition to this, also ensure that you used the right device and card number. Else, replace the speaker.
If the speaker worked with the test audio, but not for the playback – check the microphone to ensure all is working. Otherwise, retry with another one.
You can now go ahead and use this in many applications. Whether its building a home assistant, or a speech-to-text system you can use this simple set of commands as a foundation!
\