I am trying to come up with an algorithm that gets the proper chord name given certain Notes, user may or may not inform which from the notes is the root.

I suppose that the process in recognizing a chord name differs for how many notes are given, so maybe there should be an algorithm for triads, one for 4 note chords, and another for 5+ notes? Or is there a general way of approaching this? maybe chord patterns?

  • You could transform every pitch class set to its prime form, and then use that as a key for a chord dictionary. This answer contains information about how to find the prime form: music.stackexchange.com/questions/82120/… Also check out en.wikipedia.org/wiki/Set_theory_(music) – Your Uncle Bob Jul 20 at 18:14
  • What in your opinion is a proper chord name? Even the proper chord name...? Can you give an example of an improper chord name? – piiperi Jul 20 at 21:11
  • @YourUncleBob this is pretty interesting, I did not knew about Set Theory, I think I'll get a book. – Cheche Romo Jul 21 at 21:17
  • Actually, the prime form of a pitch-class set in set theory is found through rotation and mirroring, which means e.g. that a major and minor triad both have prime form (0,3,7). For your use case, you should use only rotation, not mirroring, so that you get (0,4,7) for major triads. – Your Uncle Bob Jul 21 at 21:38
  • You haven't said what your input or output data will be. – Michael Curtis Jul 22 at 13:41

Some chords (at least in Common Practice Period harmony) cannot be named out of context. Some trivial examples: F-Ab-Db-F is a Db major chord in and of itself but if resolved to G, it may be a Neapolitan Sixth.

The collection: Ab-C-Eb-F# is a German Sixth if resolved to G-C-E-G thence to G-B-D-G(or F). It's a dominant seventh if resolved to Db-F-Ab or perhaps a tritone substitution. Possibly using F# for the German Sixth and Gb for the dominant seventh helps however composers do approach the chord as a German Sixth and resolve it as dominant seventh and vice versa.

Some ambiguities are matters of taste. In San Antonio Rose (the only song I can think of quickly that does this) the opening chords are Bb-Eb-C7-F7; when I play this piece or other similar pieces I think I-IV-II7-V7 (especially as vocalists may want me to transpose) but while laying out a chord scheme during composing, I think I-IV-V7/V-V7 which shows the structure and would explain a Bb-Eb-C7-d-g-c6-F7-Bb as a secondary dominant resolving on a deceptive cadence followed by a cycle of fifths.

One thing you could do (to get back to the original question) is to rearrange the note collection to have as many thirds as possible and name the chord from there. Moving the notes around should indicate which inversion one has. D-F-G-B can be arranged to be G-B-D-F with a maor third followed by by three minor thirds.

  • Maximizing the number of thirds makes sense to me, thanks I'll try it this way. – Cheche Romo Jul 21 at 21:24
  • N6 versus Db is analysis (identifying function in a key) versus naming (identifying root and chord quality.) – Michael Curtis Jul 22 at 13:39

Before starting an algorithm, ask yourself these questions

What is the purpose of my tool?

  • Providing a library of chords
  • Creating a chord recognition tool
  • ..

Is there an alternative available?

  • Perhaps you should save yourself the trouble and have a look at existing API's (for instance Sibelius).

Who is my audience?

  • A personal tool
  • An assistant for composers, artists and music lovers
  • An expert tool for theorists and hobbiests
  • ..

What is my scope?

  • Do I include microtonal and other exotic chords
  • Do I include western chords
  • Do I limit myself to Tertian Harmony
  • Do I include secundal, quartal, polychords, chord over bass, tone clusters
  • ..

What context I take into account?

  • Are chords context-free
  • Do I include the notion of a scale
  • Do I take the bass as root
  • Can any note be the root
  • Can the root be omitted
  • Do I focus on classical, jazz, modern chords
  • Which notation should I use
  • ..

What should be configurable

  • Notation systems
  • Chord qualities to include (might depend on genre or musical era)
  • Context of a scale and enharmonic notes
  • How to decide which note is root
  • Allow for voicings
  • Allow for custom rules (default notes omitted in some chords, special chord names, etc)
  • ..

Guidelines for exploring an algorithm

If you do decide to roll out your own algorithm, answers to the aforementioned questions would help in paving the road ahead.

Let's assert you want to provide a chord recognition tool adhering to Tertian Harmony, in Equal Temperament.

A high level description of a candidate algorithm:

  • configure which qualities to test chords against
  • normalise the requested voicing in closed position
  • perform all next steps on each inversion of the normalised chord

For each inversion:

  • plot the pitch classes against those of each configured quality
  • find the quality that matches best, using weighted formula to indicate penalties on altered, omitted, added and conflicting notes


  • either the inversion with a best match on some quality
  • the best match on quality for any inversion
  • the best match on quality for any chromatic note, regardless whether the tonic is played
  • They will - but they'll do it anonymously...they don't give reasons - or are expected to - except by me... – Tim Jul 20 at 20:33
  • @Tim That is really annoying on this site. I am used to better feedback on other subforums. A bit of a shame, really. – dfhwze Jul 20 at 20:36
  • 1
    I think you are missing one essential question, which is "Does an algorithm like this already exist?" The answer is yes, this functionality is already built into many keyboards, DAWs, and notation programs. So why re-invent the wheel? – Peter Jul 20 at 20:58
  • 2
    Indeed, this is all good advice, but it doesn't answer the question. – Your Uncle Bob Jul 20 at 21:00
  • @Peter exist, makes sense? Because I think the point of the OP was to create one from scratch? – dfhwze Jul 20 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.