Search code examples
algorithmmusic-notation

Text, string-based chord recognition algorithms?


I'm looking for algorithms that take notes represented by strings as input and produce the name of the chord as an output.

For example:

chordName("C", "E", "G")
>>> "C major"
chordName("C", "E", "G", "B")
>>> "C major 7"
chordName("A", "C", "E")
>>> "A minor"

Solution

  • Tutting my own horn (i.e., I'm the lead developer of the library, so biased, of course), but with music21 (http://web.mit.edu/music21/) you can do:

    >>> from music21 import chord
    >>> chord.Chord(['C','E','G']).pitchedCommonName
    'C-major triad'    
    >>> chord.Chord(['C','E','G','B']).pitchedCommonName
    'C-major seventh chord'
    

    or more obscure things...

    >>> chord.Chord(['C','C#','D','E','F#']).pitchedCommonName
    'D-tritone-expanding pentachord'
    

    the full docs for Chord (http://web.mit.edu/music21/doc/moduleReference/moduleChord.html) will help you figure out how to get the text output in exactly the format you want.