# How do key signature calculating algorithms usually work?

Whenever I play a song in Virtual DJ, it instantly gives me the key. How does this work? How are major or minors handled? Is there an analysis of the most common notes that is broken down to figure out what's most prominent?

This is not authoritative at all but here is my best guess. It does a simple frequency analysis to see what notes are present. Each key as 7 notes that can be present and 5 that cannot. As soon as it detects a note that is not in a key, it can rule that key out.

Then, the keys of C and A minor (for example) have the same notes so the answer it has so far is ambiguous. It then sees whether the piece contains more Cs or As to pick between the two.

If this answer is totally out to lunch please feel free to correct it.

This is a fairly well-explored topic in DSP, and it's easy to find papers on it in computer music journals. Some keywords to search for would be "chord recognition," "key tracking," "chromagram," and "pitch class profile." What follows is an overall description of the general principle of key identification.

Start with the frequency spectrum of some fragment of the piece, sum up magnitudes from bins of frequencies corresponding to notes in the chromatic scale, and sum up notes that differ by octaves. This gives you a chromagram, a vector giving you the strength of each of the 12 pitch classes.

Normalize this vector, and then try to match this with the ideal chromagrams for certain keys or chords. A simple method would be to evaluate the squared difference between the measured chromagram vector and each target chromagram vector, and select the one with the lowest difference.

That gives you a guess for the key at a certain instantaneous moment in the piece. Do this for the entire piece and the actual key of the piece is likely to statistically dominate your results.