This was a quick, fun project that solves a practical problem I had — wanting to learn the names of piano chords, but not seeming to want to spend time staring at chord charts. I figured perhaps I could learn some music theory more deeply by encoding the logic into software, as well.

I came across this table <https://en.wikipedia.org/wiki/Chord_(music)#Examples> that breaks down the composition of chords logically. I was reminded of a bitmask, so I translated each chord into a 12–bit bitmask with a bit for each distinct note letter name (e.g. “C” or “B♭”). Decoding binary was involved in interfacing with MIDI … that might have been the inspiration — regardless, a bitmask seems ideal for this purpose.

The most challenging part by far was the logic that determines whether say, “A♯/B♭” (which are considered to be the same note in the 12–tone chromatic scale) should be rendered as “A♯” or “B♭”. As best as I understand, this depends on key signature context, and the logic regarding this isn’t well-described. I settled on finding the diatonic scale (7–note) that contains the maximum number of notes that the chord also contains. That diatonic scale provides the context for the note letter names. This logic isn’t perfect yet — the scales that include double flats and double sharps (which I wasn’t previously aware of) still provide ambiguous results.

Good job. Couple things - it seems to struggle with 7th chord inversions, and doesn't appear to handle 9ths at all.

There's an old online tool called ChordDisplay that handles these a bit better and also uses web midi as well. I think it's also open-source so you could probably take a look at the logic it uses as a reference point.

https://chord-display.rednet.io

My first exposure to realtime chord detection was actually on a old Yamaha arranger keyboard from the early 90s. When you are designating the chord for the instruments to follow using your left hand, the LCD display will show the chord in the progression.

EDIT: My bad, 7ths are working - I'm just used to inversions being expressed as slash chords!

I appreciate the feedback and sanity checking!

I’m definitely planning on add a wider array of chords types now that the app is basically working. As far as the 7th inversions, I essentially “ignored” inversions by flattening the notes into a single octave … there may be an issue with this logic.

I came across <https://chord-display.rednet.io> myself yesterday and took a cursory look. Nice app. They use a library called `tonal` <https://github.com/tonaljs/tonal> for their music theory logic, which ought to be a good resource to validate against.

I've never used tonal but it looks really nice! I updated my original comment BTW - I'm so used to seeing inversions being expressed as slash chords C7/B♭ that I originally thought 7ths weren't working!

Ahh—thanks for the update. I’ll look into changing that notation to make it less confusing regarding slash chords.