When it comes to music, a massive variety of genres abound from which to choose. Whether you enjoy listening to the light guitar strumming of country, upbeat and catchy pop, or the laid back soothing sounds of jazz, there's something for everyone. But for Phil Caridi, he likes his music loud and bold, which typically means he listens to heavy metal music. So as a fun application of machine learning and audio processing, he decided to build a metal detector that can determine just how rock-and-roll a sample of audio really is.
As the basis of his device, Caridi's metal detector uses an Arduino Nano 33 BLE Sense due to its onboard microphone, adequate processing power, and tight integration with Edge Impulse's data ingestion service. In order to move the needle back and forth as the amount of metal changes, a micro servo is positioned at its base. And finally, a single 9V battery supplies power to everything via a 5V linear regulator.
After getting the components together, Caridi then moved onto the next step of teaching a neural network about what exactly metal music sounds like when compared to other genres. To accomplish this, he recorded around 20 minutes of audio samples and split them into two categories: "metal" and "non_metal." Each sample was then put through a Mel-filterbank processing block on Edge Impulse, which takes in the raw waveform, generates a spectrogram, and then extracts certain frequencies to better match the human ear's interpretation.
Once trained on this now-processed data, the Keras neural network was able to achieve an impressive accuracy of
88.2%. Caridi then built and exported this model as an Arduino library for use his sketch. His Edge Impulse project can be found here.
Caridi's code starts by initializing the microphone to take audio samples at a rate of 16KHz, after which it gets stored in a buffer for later inferencing by the neural network. The result of this process is a pair of values that ascribe a probability to each label, with a higher number for the "metal" label being equal to the song having more hard rock elements in it. The servo motor's position is derived from this value that allows it to move further to the right if the music is right.
To see this project in action, you can watch Caridi's demonstration video above or read more about the project here on his blog.