# How to compute key of a song?

I'm looking for a precise way to compute the key of a song. BPM would be cool too. Analog is ok. I tried to approach it from the DFT angle, but got stuck and can't really put a finger on what's the issue. New perspective would allow me to get out of the mental hole I'm in.

• this is no small problem. even if the input is monophonic (one note at a time, usually from a single instrument or voice) this is no small problem. and if it is polyphonic, a mix of an ensemble of instruments, it's a female canine. i dunno how Melodyne does it. if it is monophonic, you need a pitch detector and from that result you need to make a histogram of the detected pitches. from the notes most often played you must match that set to the set of notes of candidate key. and differentiating between relative major and minor (and other modes) with the same key signature is also hard. Commented Sep 6, 2019 at 4:50
• You can infer the scale by looking at the intervals (not the individual notes but their "distance"). The intervals are like a signature for the scale and they can reveal if the scale is minor or major. Still, as RBW says, this would only be possible for a one-voice melody, possibly not even from an instrument as you would then have to also deal with timbre. ( @robertbristow-johnson I have a feeling that melodyne does it with something akeen to Prony's method (see 2:20. I've been losing sleep too :) )
– A_A
Commented Sep 6, 2019 at 8:50
• well @A_A , you can only possibly infer the mode of the musical tune from looking at only the intervals. you won't know the difference between A minor or B minor or C minor. t's like running a signal through a differentiator. you lose the DC component. Commented Sep 6, 2019 at 18:21

Like others have said, doing this for a simple, clean, monophonic tune played using a sinusoid tone generator is one thing. Doing it for a complex mix is another. Simple sing-along note books ofte get it plain wrong (over and above simplifying things), so it is hard for humans, too.

For simple input, I would consider doing a STFT, assigning pitches to 440*2^(1/12) half-tone steps and «wrapping» all spectral peaks into a single octave for simplicity. By looking at thirds and fifths in that octave over time, you may be able to learn something sometimes. Sounds like a ML problem.

Then there is the «high-level-musical-interpretation» bit. I have heard about musical couples having heated arguments over if a song is F# major or Gb major. A song can move in complex ways between C major and A minor using basically the same set of notes but a different center of gravity, or it can modulate mid-way. A single chord can be interpreted as a C major with an E in the bass, or as a Em+5, depending on context and movement.

-k

Insert a tuner into the audio chain and take down the notes coming through. Use the circle of fifths to calculate the key as best as possible.

The signal processing challenges are formidable. But even with a histogram of signal energies binned into the twelve chromatic pitches, an algorithm is going to need a lot of musical knowledge incorporated. It has to identify the start of a chord progression cycle, since that is where you are most likely to get the tonic chord.

For example, here are the chords to "They'll Be Some Changes Made", a swing tune nearly a hundred years old. It is in the key of Bb:

G C7 D7 G7 C7 F7 G7 C7 D7 G7 C7 F7 Bb G7 F7 Bb

but it doesn't have a lot of Bb's in it!

Personally, I have found it easier (although still very difficult) to improve my ear training to where I can hear stuff like this. Although that tune is still beyond me...