Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the key of a song by its chords

How can I programmatically find the key of a song just by knowing the chord sequence of the song?
I asked some people how they would determine the key of a song and they all said they do it 'by ear' or by 'trial and error' and by telling if a chord resolves a song or not... For the average musician that is probably fine, but as a programmer that really isn't the answer that I was looking for.

So I started looking for music related libraries to see if anyone else has written an algorithm for that yet. But although I found a really big library called 'tonal' on GitHub: https://danigb.github.io/tonal/api/index.html I couldn't find a method that would accept an array of chords and return the key.

My language of choice will be JavaScript (NodeJs), but I'm not necessarily looking for a JavaScript answer. Pseudo code or an explanation that can be translated into code without too much trouble would be totally fine.

As some of you mentioned correctly, the key in a song can change. I'm not sure if a change in key could be detected reliably enough. So, for now let's just say, I'm looking for an algorithm that makes a good approximation on the key of a given chord sequence.

... After looking into the circle of fifths, I think I found a pattern to find all chords that belong to each key. I wrote a function getChordsFromKey(key) for that. And by checking the chords of a chord sequence against every key, I can create an array containing probabilities of how likely it is that the key matches the given chord sequence: calculateKeyProbabilities(chordSequence). And then I added another function estimateKey(chordSequence), which takes the keys with the highest probability-score and then checks if the last chord of the chord sequence is one of them. If that is the case, it returns an array containing only that chord, otherwise it returns an array of all chords with the highest probability-score. This does an OK job, but it still doesn't find the correct key for a lot of songs or returns multiple keys with equal probabililty. The main problem being chords like A5, Asus2, A+, A°, A7sus4, Am7b5, Aadd9, Adim, C/G etc. that are not in the circle of fifths. And the fact that for instance the key C contains the exact same chords as the key Am, and G the same as Em and so on...
Here is my code:

'use strict'  const normalizeMap = {      "Cb":"B",  "Db":"C#",  "Eb":"D#", "Fb":"E",  "Gb":"F#", "Ab":"G#", "Bb":"A#",  "E#":"F",  "B#":"C",      "Cbm":"Bm","Dbm":"C#m","Eb":"D#m","Fbm":"Em","Gb":"F#m","Ab":"G#m","Bbm":"A#m","E#m":"Fm","B#m":"Cm"  }  const circleOfFifths = {      majors: ['C', 'G', 'D', 'A',  'E',  'B',  'F#', 'C#', 'G#','D#','A#','F'],      minors: ['Am','Em','Bm','F#m','C#m','G#m','D#m','A#m','Fm','Cm','Gm','Dm']  }    function estimateKey(chordSequence) {      let keyProbabilities = calculateKeyProbabilities(chordSequence)      let maxProbability = Math.max(...Object.keys(keyProbabilities).map(k=>keyProbabilities[k]))      let mostLikelyKeys = Object.keys(keyProbabilities).filter(k=>keyProbabilities[k]===maxProbability)        let lastChord = chordSequence[chordSequence.length-1]        if (mostLikelyKeys.includes(lastChord))           mostLikelyKeys = [lastChord]      return mostLikelyKeys  }    function calculateKeyProbabilities(chordSequence) {      const usedChords = [ ...new Set(chordSequence) ] // filter out duplicates      let keyProbabilities = []      const keyList = circleOfFifths.majors.concat(circleOfFifths.minors)      keyList.forEach(key=>{          const chords = getChordsFromKey(key)          let matchCount = 0          //usedChords.forEach(usedChord=>{          //    if (chords.includes(usedChord))          //        matchCount++          //})          chords.forEach(chord=>{              if (usedChords.includes(chord))                  matchCount++          })          keyProbabilities[key] = matchCount / usedChords.length      })      return keyProbabilities  }    function getChordsFromKey(key) {      key = normalizeMap[key] || key      const keyPos = circleOfFifths.majors.includes(key) ? circleOfFifths.majors.indexOf(key) : circleOfFifths.minors.indexOf(key)      let chordPositions = [keyPos, keyPos-1, keyPos+1]      // since it's the CIRCLE of fifths we have to remap the positions if they are outside of the array      chordPositions = chordPositions.map(pos=>{          if (pos > 11)              return pos-12          else if (pos < 0)              return pos+12          else              return pos      })      let chords = []      chordPositions.forEach(pos=>{          chords.push(circleOfFifths.majors[pos])          chords.push(circleOfFifths.minors[pos])      })      return chords  }    // TEST    //console.log(getChordsFromKey('C'))  const chordSequence = ['Em','G','D','C','Em','G','D','Am','Em','G','D','C','Am','Bm','C','Am','Bm','C','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Am','Am','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em','Em','C','D','Em']    const key = estimateKey(chordSequence)  console.log('Example chord sequence:',JSON.stringify(chordSequence))  console.log('Estimated key:',JSON.stringify(key)) // Output: [ 'Em' ]
like image 786
Forivin Avatar asked Jul 30 '17 10:07

Forivin


People also ask

How do you find the key of a song?

What is the Easiest Way to Find the Key of a Song? The easiest way to find the key of a song is to look at the key signature included on the song's sheet music. If the sheet music doesn't include a key signature, you will either have to analyze the chords or notes used in the song to figure out the key.

How do you know what key Something is in?

At the top of a well-written chart, you'll see a clef & a time signature, and in between them is a key signature—the number of sharps or flats tell you what key the song is in. If the last chord in the song gives you a sense of resolution, it's probably the I.

Is there an app that can tell me what key a song is in?

You can use Auto-Key mobile on any audio coming from a speaker or from an acoustic instrument or a voice. Tap on the Auto-Key logo in the app home screen to find the key. Auto-Key Mobile was developed by Antares Audio Technologies, the inventors of Auto-Tune®.


2 Answers

The chords in a song of a particular key are predominantly members of the key's scale. I imagine you could get a good approximation statistically (if there is enough data) by comparing the predominant accidentals in the chords listed to the key signatures of the keys.

See https://en.wikipedia.org/wiki/Circle_of_fifths

Of course, a song in any key can/will have accidentals not in the keys scale, so it would likely be a statistical approximation. But over several bars, if you add up the accidentals and filter out all but the ones that occur most often, you may be able to match to a key signature.

Addendum: as Jonas w correctly points out, you may be able to get the signature, but you won't likely be able to determine if it is a major or minor key.

like image 67
RichGoldMD Avatar answered Oct 04 '22 03:10

RichGoldMD


Here's what I came up with. Still new with modern JS so apologies for messiness and bad use of map().

I looked around the internals of the tonal library, it has a function scales.detect(), but it was no good since it required every note present. Instead I used it as inspiration and flattened the progression into a simple note list and checked this in all transpositions as a subset of all the possible scales.

const _ = require('lodash'); const chord = require('tonal-chord'); const note = require('tonal-note'); const pcset = require('tonal-pcset'); const dictionary = require('tonal-dictionary'); const SCALES = require('tonal-scale/scales.json'); const dict = dictionary.dictionary(SCALES, function (str) { return str.split(' '); });  //dict is a dictionary of scales defined as intervals //notes is a string of tonal notes eg 'c d eb' //onlyMajorMinor if true restricts to the most common scales as the tonal dict has many rare ones function keyDetect(dict, notes, onlyMajorMinor) {     //create an array of pairs of chromas (see tonal docs) and scale names     var chromaArray = dict.keys(false).map(function(e) { return [pcset.chroma(dict.get(e)), e]; });     //filter only Major/Minor if requested     if (onlyMajorMinor) { chromaArray = chromaArray.filter(function (e) { return e[1] === 'major' || e[1] === 'harmonic minor'; }); }  //sets is an array of pitch classes transposed into every possibility with equivalent intervals  var sets = pcset.modes(notes, false);   //this block, for each scale, checks if any of 'sets' is a subset of any scale  return chromaArray.reduce(function(acc, keyChroma) {     sets.map(function(set, i) {         if (pcset.isSubset(keyChroma[0], set)) {             //the midi bit is a bit of a hack, i couldnt find how to turn an int from 0-11 into the repective note name. so i used the midi number where 60 is middle c             //since the index corresponds to the transposition from 0-11 where c=0, it gives the tonic note of the key             acc.push(note.pc(note.fromMidi(60+i)) + ' ' + keyChroma[1]);             }         });         return acc;     }, []);      }  const p1 = [ chord.get('m','Bb'), chord.get('m', 'C'), chord.get('M', 'Eb') ]; const p2 = [ chord.get('M','F#'), chord.get('dim', 'B#'), chord.get('M', 'G#') ]; const p3 = [ chord.get('M','C'), chord.get('M','F') ]; const progressions = [ p1, p2, p3 ];  //turn the progression into a flat string of notes seperated by spaces const notes = progressions.map(function(e) { return _.chain(e).flatten().uniq().value(); }); const possibleKeys = notes.map(function(e) { return keyDetect(dict, e, true); });  console.log(possibleKeys); //[ [ 'Ab major' ], [ 'Db major' ], [ 'C major', 'F major' ] ] 

Some drawbacks:
- doesn't give the enharmonic note you want necessarily. In p2, the more correct response is C# major, but this could be fixed by checking somehow with the original progression.
-‎ won't deal with 'decorations' to chords that are out of the key, which might occur in pop songs, eg. CMaj7 FMaj7 GMaj7 instead of C F G. Not sure how common this is, not too much I think.

like image 24
Lucas Avatar answered Oct 04 '22 04:10

Lucas