In this post I’ll be using two USB microphones to record audio, one for the intended sounds I wanted to record, the other to capture background noise that I want to filter out. I’m using two cheap USB embedded microphones that I bought for less than $10 each, http://a.co/eIsnp3Z. The machine I’m recording on is a Raspberry Pi 3 running Ubnutu Mate. Since the Raspberry Pi doesn’t have an audio in device built in, I selected the USB microphones.
These are the key commands used to capture the audio samples
arecord -l # This lists all of the audio devices, used in the FFMpeg command export AUDIODRIVER=alsa export AUDIODEV=hw:1,0 rec -c 1 USBMic1.wav trim 0 00:10 export AUDIODEV=hw:2,0 rec -c 1 USBMic2.wav trim 0 00:10 # SoX uses ALSA and whatever the default audio input device is # The input can be changed with the export AUDIODEV= statement, based on arecord -l results ffmpeg -f alsa -ac 1 -ar 44100 -i hw:1,0 -t 300 FiveMinuteSample_Mono_44ksampling.wav # This is the recording function, uses FFMpeg. # hw:1,0 refers to the device and card listed from the arecord command
Here are the results of the arecord -l command
**** List of CAPTURE Hardware Devices **** card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio] card 2: Device_1 [USB PnP Sound Device], device 0: USB Audio [USB Audio]
Here I have two USB microphones. To capture audio from these devices, I use separate FFMpeg commands.
ffmpeg -f alsa -ac 1 -ar 44100 -i hw:1,0 -t 10 '/home/local/Desktop/Dual Microphone Noise Cancellation/USB_Mic_1.wav' ffmpeg -f alsa -ac 1 -ar 44100 -i hw:2,0 -t 10 '/home/local/Desktop/Dual Microphone Noise Cancellation/USB_Mic_2.wav'
The next challenge is to get both of these commands to run at the same time. Now, I could have done the recording with SoX, but that would have required multiple lines of code. I wanted to keep this simple, so FFMPeg was used to record. Don’t despair, SoX will be used to apply the noise filtering later.
gnome-terminal -e "ffmpeg -f alsa -ac 1 -ar 44100 -i hw:1,0 -t 10 '/home/local/Desktop/Dual Microphone Noise Cancellation/USB_Mic_1.wav'" && gnome-terminal -e "ffmpeg -f alsa -ac 1 -ar 44100 -i hw:2,0 -t 10 '/home/local/Desktop/Dual Microphone Noise Cancellation/USB_Mic_2.wav'"
Much better, the audio track time is exact. This isn’t a time critical application. The noise cancellation doesn’t use phase reversal. I’ll cover in more detail about the noise profile that SoX creates later. For now, we’ll have SoX use one of the microphones as a noise profile source and the other to apply that noise filter too.
sox "/home/local/Desktop/Dual Microphone Noise Cancellation/USB_Mic_1.wav" -n noiseprof "/home/local/Desktop/Dual Microphone Noise Cancellation/USB_Mic_1.prof" sox "/home/local/Desktop/Dual Microphone Noise Cancellation/USB_Mic_2.wav" "/home/local/Desktop/Dual Microphone Noise Cancellation/USB_Mic_2_NoiseCancelled.wav" noisered "/home/local/Desktop/Dual Microphone Noise Cancellation/USB_Mic_1.prof" 0.075
I’m using a higher noise reduction value than I would if I were selecting a segment of silence. This is because the background noise microphone is providing a more accurate noise profile source than a segment selection would. Here is a comparison of noise reduction based on a segment of silence only, using the same noisered value of .075
sox "/home/local/Desktop/Dual Microphone Noise Cancellation/Noise_Segment.wav" -n noiseprof "/home/local/Desktop/Dual Microphone Noise Cancellation/Noise_Segment.prof" sox "/home/local/Desktop/Dual Microphone Noise Cancellation/USB_Mic_2.wav" "/home/local/Desktop/Dual Microphone Noise Cancellation/USB_Mic_2_NoiseReduced.wav" noisered "/home/local/Desktop/Dual Microphone Noise Cancellation/Noise_Segment.prof" 0.075
The noise reduction using the segment falls short on quality. Although it did reduce quite of bit of noise, there are noticable differences between the noise reduction filter and the noise canceling microphone filter. The edges around the speech are more pronounced using a noise canceling microphone. The noise reduction process leave aliasing around the audio, which resemble softer edges.
So I asked myself, what is this SoX profile file? It looks like a text file when I open it with an editor. It starts out with a channel designator and then a series of values. I resorted the values and opened them as a spreadsheet. It didn’t matter what the source of the profile was to create it, all my profile files had 1025 lines. When I plotted the profile, it became apparent that this was a band filter, based on the source file. Using the microphone allowed the profile to accurately create a frequency profile to attenuate with. In contrast, the segment clip was really short and the results are a noisy, pun intended.
So I decided to take the microphone file that I used as a noise reference and reverse the phase. Then I would apply this on top of the speech microphone to see what kind of results it would give. It ended up having more noise, so phase reversal is beyond the scope of this post, maybe another time.
Anyway, I hope you have enjoyed this post and look forward to covering some more topics about SoX audio processing.