Another Unicorn HAT project that I’ve been fiddling around with for a while. I was looking for a way to introduce kids to the idea of Fast Fourier Transforms (FFTs) and signal processing so with my love of blinking lights I thought I’d create an audio visualiser just like the one you tend to find in media-players.
It’s been a while since I last looked at FFTs (back in University for image processing), but here’s my attempt at explaining them: simply FFTs take your input signal from the time-domain to the frequency-domain. For audio data the time-domain shows us how the amplitude of the signal changes over time, but the frequency domain lets us see what frequencies the signal is composed of and in what proportion. Useful!
Fortunately for me, the Numpy package contains functions for doing just that. The code (which as usual can be found on my GitHub and was largely based on this) takes a block of the audio data at a time, runs the FFT on it and then displays a range of frequency data on the Unicorn HAT.
For a bit of extra visual flair, I added some code to show a peak-history, the pink LEDs show the recent maximum and take a while to decay. To bring it all together I threw my diffuser over the top:
The Pi is a perfectly capable device for this real-time processing and while I’m running on my Pi 3, I don’t see any reason why this couldn’t be run on an older Pi – the FFT is exactly that, very fast!
A couple of caveats:
- The audio currently has to be in the raw WAV file format for us to correctly parse it with the FFT. I had some success with converting MP3 files on the fly but that won’t be included in this article.
- Secondly, the audio has to be routed out of the HDMI port for this to work, as the analogue audio interferes with the operation of the Unicorn HAT.
Sandy over at Pimoroni recently did an article on this topic but using the Scroll pHAT instead of the Unicorn HAT, it’s well worth a read and he does a better job of explaining things.