Global Sources
EE Times-Asia
Stay in touch with EE Times Asia
EE Times-Asia > Embedded

Creating a Mandolin-to-MIDI bridge

Posted: 25 Aug 2015 ?? ?Print Version ?Bookmark and Share

Keywords:MIDI? Fast Fourier Transforms? FFTs?

Several months ago, I decided to buy a mandolin. It had been quite a while since I last picked up a musical instrument and I thought: "Hey, this instrument looks small and easy to play." In hindsight (the one exact science), it's not turned out to be as easy as I expected. Somewhere down the line, I became curious to see if I could find a way to recognise which notes I was playing and then put them out in MIDI format to record them on a computer.

MIDI is a standard musical instrument format widely used in musical keyboards. It's basically an easy way to send musical notes encoded as particular numbers. A list correlating musical notes to MIDI numbers can be found here.

I finally got this running a couple of weeks agoyou can see me playing the mandolin (badly) and GarageBand recording MIDI notes in this video (you may have to bump up the volume a bit to hear it).

What follows is a dive into the murky world of Fast Fourier Transforms (FFTs), correlation, and the general head-scratching I went through to achieve this.

Figuring out instrument pitch
The fundamental problem is to find what note I'm playing. The pitch of a musical note almost always coincides with the fundamental frequency. In plucked string instruments specifically, this is always true as the first 'pluck' (the wave bounces around from the bridge to the fret) has the highest energy and then the wave begins to die out. The make and type of the instrument do matter, but for simplicity I assumed that the frequency with the highest energy content would be the fundamental frequency and hence the pitch of the note.

"Easy enough," I thought. "The best way to figure out the energy content over a set of frequencies is to use an FFT, right?" So I got a couple of .WAV recordings of a guitar note and ran them through some FFTs. There was one little problem with this though. The range of notes I wanted to measure was from E2 (82.4Hz) and G2 (87.3Hz) to around E6 (1318Hz). Based on this, our FFT bin frequency sizes should be something less than 2.5Hz to ensure we don't miss the lower frequency notes. At 2.5Hz, my frequency bins would be 0, 2.5Hz, 5Hz,... 82.5Hz (close toE2), 85Hz, 87.5Hz (Close to F2),... etc.

Since the highest frequency is around 1,300Hz, a sampling frequency of 4,000Hz will be sufficient to recognising all notes with the desired accuracy. The problem now moves to the FFT size required. The smallest FFT size that will satisfy both of the conditions is 2,048 (this gives a bin interval of 1.9Hz). I really wanted to run my project using a small embedded processor, but running a 2,048-point FFT is taxing even for processors like the Cortex M3/M4 with optimised libraries.

There had to be a better way. One approach is to use multiple short-length FFTs at different sampling frequencies to spread around the frequency spectrum. This can work pretty well because as the notes go higher, so does the amount of frequency separation; i.e., the difference between the lower two notes is 5Hz or so, while the separation between the higher notes is over 50Hz.

After a few trials, I ended up running five 64-point FFT "chunks," which turned out to be far more manageable.

Figure 1: Splitting the data to short FFTs.

Figure 2: Example of FFT bin frequencies vs. the actual musical notes.

1???2???3?Next Page?Last Page

Article Comments - Creating a Mandolin-to-MIDI bridge
*? You can enter [0] more charecters.
*Verify code:


Visit Asia Webinars to learn about the latest in technology and get practical design tips.

Back to Top