Ace
Ace

Reputation: 233

Javascript: How to determine the musical key of mp3 files

I've done a lot of Google searching but haven't been able to find an example on how to determine the musical note of mp3 files.

So far, I've read something about FFT (Fast Fourier Transform) from which the pitch of an audio file can be calculated and based on the pitch notation the musical note can be derived.

But then I read that the mp3 file format is in the time domain which due to the lossy compressed format doesn't contain the values of the samples necessary for frequency analysis... does that mean that you have to convert the mp3 to a wav file in order to the calculate the key?

I've found a couple of examples of real-time pitch detection for visual purpose but not for analysing an entire mp3 file and outputting the musical key.

I hope someone can point me in the right direction.

Thanks.

Upvotes: 4

Views: 3401

Answers (2)

Wolfpack'08
Wolfpack'08

Reputation: 4128

This guy wrote an incredible library that has worked extremely well for me, but I have been told by others that it does not work out-of-the-box for them: they gave me another link.

I'm just going to link to their sites because they tutorialize what they have done on the sites, and they have some licenses (MIT license or whatever)--don't want to mess things up by unintentionally violating their licenses by reposting.

Anyway, it worked great for me!

This is the one I prefer with a tutorial alongside:

https://alexanderell.is/posts/tuner/

Here is the one I was recommended by the individual who had one that did not work--could not find the tutorial, but it's out there somewhere:

https://harald.ist.org/tools/spectrum_analyser.html

Upvotes: 0

James Paul Millard
James Paul Millard

Reputation: 621

I created an application, PitchScope Player, which can do pitch detection upon MP3 files in realtime and its complete source code is posted on GitHub, however it is written in C++. Pitch detection and musical key detection, especially in realtime, is extremely demanding and probably needs the speed of C++ to be executed at this point in time. You have just begun to explore a very difficult audio engineering task, and really need to first get some background as to the physics of how we perceive ‘pitch’, what a ‘harmonic’ is, and explore the choices in how to make a frequency-domain transform from the raw signal (see Wikipedia link below).

When a single key is pressed upon a piano, what we hear is not just one frequency of sound vibration, but a composite of multiple sound vibrations occurring at different mathematically related frequencies. The elements of this composite of vibrations at differing frequencies are referred to as harmonics or partials. For instance, if we press the Middle C key on the piano, the individual frequencies of the composite's harmonics will start at 261.6 Hz as the fundamental frequency, 523 Hz would be the 2nd Harmonic, 785 Hz would be the 3rd Harmonic, 1046 Hz would be the 4th Harmonic, etc. The later harmonics are integer multiples of the fundamental frequency, 261.6 Hz ( ex: 2 x 261.6 = 523, 3 x 261.6 = 785, 4 x 261.6 = 1046 ). We detect pitch by finding for groups of harmonics which have that mathematical relationship in the spacing of their frequencies.

Rather than use a FFT, I use a modified Logarithmic DFT Transform so that its frequency channels can be aligned to where the harmonics are located within a musical signal. The Logarithmic DFT transform also gives a distinct speed advantage in execution.

Once you have detected numerous pitches in the musical signal, then you can detect the Musical Key by scoring the 12 different Key Candidates by the populations of member notes within that musical signal. Another application of mine, PitchScope Navigator, can also detect Musical Key in realtime.

You might want to acquire a C++ compiler and recompile my source code so you can step through its execution to see how my algorithms work. It will also decode an MP3 file. You could also download an executable of that application, PitchScope Player, from numerous places on the web in order to see how it performs on a Windows machine with a MP3 file of your choice.

https://github.com/CreativeDetectors/PitchScope_Player

https://en.wikipedia.org/wiki/Transcription_(music)#Pitch_detection

Below is the image of a Logarithmic DFT (created by my C++ software) for 3 seconds of a guitar solo on a polyphonic mp3 recording. It shows how the harmonics appear for individual notes on a guitar, while playing a solo. For each note on this Logarithmic DFT we can see its multiple harmonics extending vertically, because each harmonic will have the same time-width. enter image description here

Upvotes: 6

Related Questions