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.
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.
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...