Recording Stereo Audio on a Raspberry Pi
“As an Amazon Associates Program member, clicking on links may result in Maker Portal receiving a small commission that helps support future projects.”
Once the Raspberry Pi OS is loaded onto the SD card, insert the card into the RPi board. A Raspberry Pi 4 Model B board is used going forward.
We start by installing Python3 and pip:
Next, it is good to install the Python Integrated Development Learning Environment (IDLE), where we can write scripts and visualize data a bit easier:
Assuming the above did not result in any errors, we can run the .py file that installs the i2smic capability onto the RPi:
The following prompt will ask the user whether they want the I2S input to be loaded at boot. ‘y’ should be inputted, unless the user has a preference to keep the boot minimal (not recommended for heavy audio use).
After agreeing to load at boot, the install will take several minutes (depending on internet speed). Once the install completes, the user will again be prompted to reboot. Reboot and then continue with the next step.
The following should be outputted to the Python shell:
1x Raspberry Pi 4B Computer (4GB RAM) - $57.99 [Board Only on: Amazon], $99.99 [Kit on Amazon], $55.00 [2GB from Our Store]
2x INMP441 MEMS Microphones - $10.00 [Our Store]
1x Mini Breadboard - $3.00 [Our Store]
7x Male-to-Male Jumper Wires - $1.05 [Our Store]
5x Male-to-Female Jumper Wires - $0.75 [Our Store]
The wiring diagram between the Raspberry Pi computer and the INMP441 MEMS Microphone is given below:
Record 1 second of background noise (at 44.1kHz sample rate)
Record 5 seconds of data
Save the recorded audio as a .wav file under a folder called ‘data’ with the filename corresponding to the current date/time
Remove the background noise from the 5 seconds data
Select the peak frequency of the frequency response (computed with the Numpy FFT)
Plot the time series and frequency response of the 5 second recording
The resulting output should look similar to the plot shown below:
A 3114Hz signal was inputted using a smartphone frequency generator placed a foot away from the INMP441 MEMS microphone. At this stage, with the frequency response matching the input frequency generated by the app - we are sure that the microphone is being read properly by the Raspberry Pi! It also may be noticeable that the amplitude is quite low on the mic response - we will address this as well as the adjustments to be made to the code in order to permit stereo recording.
The subsequent output of the code above should result in two channels being plotted in a similar manner to the plot above in the mono input case. The stereo time series and frequency response for a 1012Hz input frequency generated via a smartphone app is given below:
It is easy to perceive exactly what was happening in the test case above: first, channel 0 had the input signal closer to its port; then, channel 1 had the input signal closer to its port - both of which are visible in the amplitude changes in the time series plot.
ALL CODES CAN BE FOUND ON OUR GITHUB PAGE:
See More in Raspberry Pi and Acoustics