I have an algorithm to determine a chord's name. A chord is a set of notes, like A B C. I'd like to improve the algorithm and have several questions.

First of all, I'm aware of this thread but that's not what I want. Also please let's omit information stated in this answer. So I just need answers on the following straightforward questions:

  1. A chord and all of its inversions can be resolved to the same name, yes? So C E G, C G E, E C G, E G C, G C E, G E C are all the Cmaj chord, right?
  2. If a notes collection has the same notes, can we throw duplicates away? So A B C and A B B C and A A B C C are all just A B C?
  3. Does that work for adjacent notes only? Are A B C A and A B C the same chords or different ones?
  4. Am I right that all inversions of a chord is a collection of all permutations of the chord's notes (which gives us n! possible permutations)? Or we can reduce somehow the number of possible variants?


I see many people are trying to know additional details about the algorithm instead of just answering my simple questions. Thanks @Andy Boner for the answer!

Well, here answers on your questions (that have no relation to my post so we're all just wasting our time, but... why not?):

  1. Yes, the same notes (well, pitches) can designate multiple chords names. In this case I just return all detected names. My algorithm returns a collection of names, not a single name.
  2. I wrote "My current algorithm works fine for users needs" and the question has appeared: "could you add a description of these assumed needs in the question?". Well, those needs are not "assumed", they are real ones that have been satisfied several years ago. The problem now is how I can improve performance of the algorithm (the task has been raised from this user's message). That's why I've asked the questions above.

So I don't ask how to build the algorithm, what music theory details are exist around the chord theory and so on. I have specific questions and want answers on them only.

Anyway I got the information I needed. Thank you!

  • 5
    I feel like all of your questions are things you should know before starting on such an app. We could answer all these questions but you’ll probably have more questions and gaps in your understanding of music theory that will make the app hard to finish. Commented Oct 25, 2023 at 15:17
  • 1
    @ToddWilcox - so very true! Lack of (basic) information/knowledge/understanding will make app-making an unsuccessful task.
    – Tim
    Commented Oct 25, 2023 at 15:26
  • 1
    Meh - my brain was in a bucket in a corner:\ I used to work with algorithms like this - which were so secret even 25 years later I can't tell you what they are. [btw, as you could see from my original, now deleted, comment - I wasn't the one to devise them;))
    – Tetsujin
    Commented Oct 25, 2023 at 15:32
  • 1
    @Tetsujin - I'll leave the comment for those coming behind to ponder upon... for a little while!
    – Tim
    Commented Oct 25, 2023 at 16:06
  • 1
    see this answer, Python implementation included music.stackexchange.com/questions/125891/… I suppose you assume a style of music where people consider tertian harmony as a norm. There are others. You should explicate the intended target audience and usage purpose of these "chord names". For whom does your algorithm produce output, and for what purpose? Commented Oct 25, 2023 at 16:59

5 Answers 5


I have an algorithm to determine a chord's name. A chord is a set of notes, like A B C.

This is a problem right off the bat. Sometimes a set of notes could be explained in more than one way. It's as if you said, "I have an algorithm to determine the meaning of a word. A word is a set of letters, like B A R K." But "bark" can mean "the sound a dog makes," or "the outside layer of a tree," or even "an archaic word for a boat." We choose between these meanings by analyzing context. So sometimes "E G C" is an inverted C chord, and sometimes it's actually an E minor chord that has had the top note suspended from an earlier C and is about to resolve to a B. To print a chord in staff notation is to make a simple, specific, objective statement: These pitches sound at the same time. To label it with a roman numeral or letter name is an act of analysis. Sometimes the analysis is straightforward and obvious, but it still happens.

But many of your specific questions can be answered:

A chord and all of its inversions can be resolved to the same name, yes? So C E G, C G E, E C G, E G C, G C E, G E C are all the Cmaj chord, right?

IF we're already sure that this IS a C chord, then you're right. If we have decided that we're looking at a C chord, then it doesn't matter how the pitches are distributed throughout octaves or voices, or which note is on the bottom. These are both C major chords:

enter image description here

However, there's no guarantee that those notes are always a C chord; see my warning in the first paragraph. There are plenty of situations where the most obvious chord name for a certain stack of notes is not actually the right way to explain the situation. For instance, in this bit, the reasonable analysis is that there are only two chords, an E minor and an F:

enter image description here

So here, in the last 8th note of the first measure, we have E G C, but it is not a C chord, just an anticipation. It would still be true if it were written like this:

enter image description here

Similarly, chord naming relies more on context once we start adding notes, jazz-style. For instance, is C E G A a C chord with an added 6th? Or is it in fact a first inversion A minor 7th chord? Only context can tell.

If a notes collection has the same notes, can we throw duplicates away?

Yes. These are both C chords (assuming context tells us they're C chords):

enter image description here

Does that work for adjacent notes only? Are A B C A and A B C the same chords or different ones?

It seems as if all these questions are coming from the same misunderstanding. A chord name dictates a set of pitch classes, not specific pitches. Those pitch classes may be distributed in any order across any octaves, and all may be doubled (or omitted!). So yes, those are the same chord (assuming the same context).

Am I right that all inversions of a chord is a collection of all permutations of the chord's notes (which gives us n! possible permutations)? Or we can reduce somehow the number of possible variants?

The important word here is "inversion." We don't have to worry about all the permutations of which notes go in which order, just what's in the bass. There are only as many inversions as there are pitch classes in the chord, because "inversion" is just a fancy word for "which note is on the bottom." There can be no more and no fewer inversions than there are unique pitches classes in the chord. "C E G" has three possible notes to be on the bottom. Or, in other words, these are both a first-inversion C chords:

enter image description here

  • 2
    Thanks you a lot for such a detailed description! But this statement is unclear for me: "Well, the number of inversions is equal to the number of pitch classes in the chord". The number of inversions for CEG is 3 or 6? Yes, there are three possible notes in the bottom, but for each such variant there are two options how other two notes can be placed, right?
    – Maxim
    Commented Oct 25, 2023 at 15:34
  • 2
    Only the bottom note determines the inversion. "E G C" and "E C G" are both first-inversion C chords. To reverse-engineer that statement: If I say "Play a 1st-inversion C chord," all I've specified is which note is in the bass. You have many many options of how to voice the rest of it. Commented Oct 25, 2023 at 15:38
  • 1
    @Maxim Specialized term here is “voicing,” meaning “how you arrange the members of the chord.“ So “three inversions, each with an infinite number of voicings.“ Commented Oct 25, 2023 at 17:03
  • 1
    @phoog Yeah, I was speaking without assuming a finite set of voices, since Maxim appears to be dealing in the abstract. And Maxim, I've avoided mentioning it until now, but there could be even more "variants" because you could even have some chord members missing and still analyze the chord; e.g. you could have only "C E" and be certain from context that it's a C chord. In fact, you could argue that the trumpet's first C note in Also Sprach Zarathustra is already a "C chord," with only one pitch (largely because of the big picture of where things are headed). Commented Oct 25, 2023 at 17:31
  • 2
    It looks to me that the OP doesn't really know what his end-users actually do with the chord names, and this question is trying to get an answer to that, without asking from the actual users. Commented Oct 26, 2023 at 9:44
  1. Since the second quarter of the 18th century, yes, but with the advent of jazz theory, perhaps around the second quarter of the 20th century, not necessarily. For example, on a lead sheet you might find Am7/C (A minor seventh with C in the bass) and you might find C6 (C major with an added sixth). Both chords contain the same set of pitches, but some might argue that they are different in some theoretical sense.

  2. Yes. Duplicate pitches do not change the identity of a chord.

  3. Not at all; C-E-G-C is C major and so are C-E-G-G and C-G-C-E.

  4. Inversion is determined only by the bass note. E-G-C is a C major chord in first inversion, and so is E-C-G.

As others have noted, passing tones and other non-chord tones can complicate an algorithm of this sort. Or, more likely, they certainly will. Sometimes lead sheets pay too much attention to non-chord tones, which makes them confusing and difficult to read.

For example, consider the beginning of "Joy to the World," which consists of a descending major scale, the first four notes of which are over a tonic chord. Mostly, assuming the key is C, you just have a C chord during the first measure, but a naive algorithm will probably come up with C - Cmaj7 - C6 - C or, worse, C - Cmaj7 - Am7/C - C

  • @Tim I don't understand. The two examples I gave are the same two you're suggesting, aren't they? (Albeit in the opposite order.)
    – phoog
    Commented Oct 25, 2023 at 18:26
  • Sorry, not sure what I meant there! Deleted. What's the difference between C6 and Cadd6?
    – Tim
    Commented Oct 26, 2023 at 7:46
  • @Tim one is less ambiguous? My exposure to lead sheet usage is in the receding past, and this particular chord is tainted by figured bass practice where 6 means a sixth with no fifth. I've taken out the "add."
    – phoog
    Commented Oct 26, 2023 at 13:07

I have looked at this a few times but I didn't get very far. There are lots of references on the web. Google Scholar is a good place to search.

One problem is that there are simultaneously sounding notes that are not really chords. Take a simple country walking bass, that I will abbreviate with slash notation: C, C/B, C/A, G. The second and third chords are still C chords; the pattern C-B-A-G is just a scale-wise descent in the bass and should be analyzed as a quasi-independent component.

It might help (though I haven't looked) to preprocess music to extract the melody, bass line, and bunch of strummed chords. This isn't that simple to do algorithmically as the melody may have a varying number of voices.

  • My algorithm is a simple one that just tackles with notes (pitches) offsets from the root. Slash chords are also supported. Thanks for the answer!
    – Maxim
    Commented Oct 26, 2023 at 8:48
  • the lowest note is note always the root. i wonder how you are determining what the root is.
    – user94972
    Commented Oct 27, 2023 at 6:20

first your questions answered:

  1. yes, but then your algo will miss some subtleties that are important in Music. And therefore your analysis will be of limited musical value.

  2. yes, duplicate notes do not change chord quality or name.
    I wonder though, what is this "notes collection" you speak of? If the notes are not sounding at the same time, then you are not dealing with chords.

  3. yes, again, duplicates do not change chord quality.

  4. sounds like you are complicating things. A chord with 3 notes, made up of its root, third, and fifth, (a triad), CEG, has a root position, 1st inversion and a second inversion. 3 combinations. A chord with 4 notes (a seventh chord) CEGB likewise has root position plus 3 inversions. Again, I don't know what your "notes collection" looks like or where you are getting them from, but simply looking at any 4 different notes does not imply a seventh chord, nor would it imply it has inversions. Inversions as I understand them are limited to having either the 3rd, 5th, and or 7th (of those said triads or seventh chords) in the bass. Distance between the notes is important though. ABC is not a chord, it's a cluster. ACB, spanning the interval of a 9th implies a chord, but we could not name it without context of what comes before and after.

I should add that your opening statement should actually be a question. "A chord is a set of notes". By your definition, it is clear that some understanding is missing. To start on the right track, you should really be looking for a proper definition first, which some of the other answers here should have touched upon. A chord is NOT a set of notes. That's just too imprecise a sentence. A set is a collection, and a collection can be something like 'the first note from each measure in a piece'. That's a nice set of notes, but it's not a chord. If your goal is to analyze things that way, then that's a different story, and maybe your particular use case makes sense in a way specifically outside of normal musical analysis.

Maybe if you explained the reason behind what your larger goal is, you could get some wider direction that would help you with aspects you are not even going to know that you need to ask about. It sounds a little bit like what you are not telling us is that you want to analyze a set of notes (or some sequence other than a vertically aligned chord on a staff), such as a melodic phrase and figure out what chord is implied. Correct me if I'm wrong.

Hope that's helpful. Feel free to add to your questions.


To expand on a great point from @Deeznatural, some chords are implied. Basic major and minor chords consist of the 1st, 3rd, and 5th note of a major or minor scale. In root position (non-inverted chord with minimal distance between notes), it is the interval between the 1st and 3rd note that determines whether the chord is major or minor. With C on bottom, an E is 4 half-steps higher (not counting C itself, the half-steps in ascending order are C#, D, D#, and then E). Then the G is an additional 3 half steps higher (F,F#,G). Only the 1st and 3rd tones in the chord are required to imply a chord. So C|E implies C|E|G (C major chord), while playing C|E-flat implies C|E-flat|G (C minor chord). For clarity, I am spelling out flatted notes in this response instead of denoting those with a lowercase "b" since that can be confusing in a discussion about note names, and it is not technically the right symbol. Moving on, if you omit the third and play only C|G, you are implying some type of C chord, but it is unclear whether the chord is major or minor.

Take this one step further though and you will find that some of your three note clusters are actually implied 7th chords.

There are multiple types of 7th chords. Most common in Western music are flat-7 chords. In C major, that would be C|E|G|B-flat. So if you encounter a chord with B-flat|C|E, that should generally be classified as a C7 chord where the G was omitted. If you encounter C|E-flat|B-flat in any order it is a C minor 7th chord. Some may argue that E-flat|B-flat implies either an E-flat major chord or an E-flat minor chord, and since C is the 6th degree (note) of the E-flat scale, it is possible the chord is actually some kind of E-flat chord with an added 6th, sometimes denoted as E-flat +6. But that would only be true in the right context (most common in jazz), and clearly it makes the most sense to call it a C-flat 7 chord instead. Music is a language, and like words that have multiple meanings, chords can take on multiple meanings (chord names) when viewed independently. It is the context that brings full meaning to our words, and similarly full meaning to the intended chord. To aid in your algorithm, there may be value in considering common chord progressions. For example, 2-5-1 chord progressions are quite common in Western music and are the foundation of jazz. So if a song is in the key of C and you encounter a D-minor chord (D|F|A in any inversion) followed by a chord containing C|E|B-flat in any inversion, and then going to C|E|G (typically C would be in the bass at this point), then the context makes it crystal clear the second chord is a C7 and not the questionable jazz alternative (E add 6).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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