# How to convert @tonicjs/tonic chord data to guitar chords?

I am looking at https://github.com/tonaljs/tonal/tree/main/packages/chord which has a lot of music theory I'm less familiar with. When I was playing the guitar for many years 20 years ago, I learned a bit about jazz music theory. In that, we learned how to play 3-string chords (3 notes each), along the fretboard. So if I remember correctly, on strings 3, 4, and 5, you could play the same chord in 3+ different ways, something like that.

So "C major" chord can be C E G on the piano, or like these 5 notes on the guitar.

In that guitar chord, it seems the 3 is a C, and the 1 is a C, so you have some duplication. The 3, 4, 5 strings are 2 0 1, E G C, so that is how my jazz teacher would have had us make the chord. Then move up the fretboard for another position, etc..

The question I have is, given I can do Chord.get("Cmaj").notes === ["C", "E", "G"] from that tonaljs library (you don't need to know code really for this question, it's mainly music theory), I get the basic set of notes for the chord.

The next task that I want to figure out is, how do I compute all possible chords, across all possible tunings, across all possible sets of strings?

Say we have a 6-stringed guitar (or 4-stringed bass), with tunings like these. Given a chord is a minimum of 3 notes, we can choose 3, 4, 5, or 6 strings to play the chord on. Given the hand can reach about let's say 6 frets, that gives us a range of what notes we can play.

How should I go about figuring out how to create a function something like this?

getPossibleStringedInstrumentChords(numStrings, notes, tuning, maxFretDistance)

That is, the tuning would have all the information of which fret has which notes, would that be how you'd do it? The notes is a list like ["C", "E", "G"]. The numStrings is 6 on the guitar. And maxFretDistance is 6 for the finger stretching.

It seems then I would just have to figure out how to create an algorithm which would find all combinations of notes, that is the hard part I will have to work on.

But will this approach work is my overall question? Does it make sense? I guess then I'd wonder what are the most important chords from the set, if that can be automatically figured out somehow... Or you just have to manually create the lists of chords for each tuning for each instrument. Not sure.

Is there something that does this already? Or any more information on the topic that would be helpful? Or does my algorithm sound like the ideal approach to finding all "chord layouts" for a given chord on stringed instruments? I keep seeing things like the "tonic" and the "bass" and "root" and "chroma" and "degree" and "interval" and the like in this library, all new concepts to me, and there's also some midi code dealing with chroma, so I'm not sure if there's a more appropriate conceptual way of getting guitar chords from the chord name or chord notes list.

• There are surely things that do this already, at least "hardcoded" to a given instrument, because there seem to be a million apps and websites that, for a given chord name, will give you all the possible fingerings... Mar 6 at 19:20
• Also btw see music.stackexchange.com/a/132314/78419 , though you're working in the opposite direction so don't have some of the same concerns Mar 6 at 19:23
• I think I once tried this, but I couldn't figure out what anyone could possibly have done with the results. A lot of it is unplayable, because a simple fret stretch limit isn't enough to say if the chord can be played, even if the note combination could theoretically have a meaningful musical purpose. It's much more useful to actually learn to play the guitar than go through thousands of random fingerings, IMO. My gut instinct says that there are too many useless combinations, it's better to learn how to build chords you actually need for a known musical purpose. Mar 6 at 20:37
• @piiperiReinstateMonica If I remember correctly, when improvising/soloing on jazz guitar, we memorized the possible 3 variations of each 3-string chord (strings 3/4/5), then that was tied to the scale. So you could memorize 3 * say 100 chords, and now you can match the base rhythm with a solo easily, then branch off from there. Mar 6 at 21:19
• You don't memorize all chords separately. You only memorize some basic shapes and patterns, which can be moved across the fretboard, modified, altered, adapted to taste. And they can get different names depending on enharmonic spellings, voicings and interpretation. Quite often a guitarist leaves out some notes. It may look like a basic minor triad, but the guitarist is doing a 9 chord with those three notes, playing only the upper structure notes. It's not about memorizing a million things. You learn some basic patterns and gradually learn to apply the patterns to different contexts. Mar 6 at 21:33

Steps:

1. Transform your base chords to chromatic pitch classes say c=0 to b=11.
2. Transform your string tunings to chromatic pitch classes
3. The lowest fret of a chord must be a chord note, so you only need to consider fret positions where at least one string has a chord note
4. For each such position you get for each string a set of possible pitches, given by the empty pitch, no pitch, or position to position+span (realistically that span would depend on position, e.g. factor * 2^(position / 12)).
5. Form all combinations of such pitches, optionally with constraints (e.g. all notes must appear at least once, some notes have to appear at least once, the root note must be lowest, ...)

Example:

• Chord: C-E-G ≈ 0-4-7
• Tuning: E-A-D-G-B-E ≈ 4-9-2-7-11-4

Possible fret positions:

• E-strings: 0=E, 3=G, 8=C, 12=E, 15=G, 20=C, 24=E
• A-string: 3=C, 7=E, 10=G, 15=C, 19=E
• D-string: 2=E, 5=G, 10=C, 14=E, 17=G, 22=C
• G-string: 0=G, 5=C, 9=E, 12=G, 17=C, 21=E, 24=G
• B-string: 1=C, 5=E, 8=G, 13=C, 17=E, 20=G

So relevant positions are:

• 0 (E-x-x-G-x-E)
• 1 (x-x-x-x-C-x)
• 2 (x-x-E-x-x-x)
• 3 (G-E-x-x-x-G)
• 5 (x-x-G-C-E-x)
• 7 (x-E-x-x-x-x)
• 8 (C-x-x-x-G-C)
• 9 (x-x-x-E-x-x)
• 10 (x-G-C-x-x-x) and octaves of these positions (12, 13, 14, 15, 17, 19, 20, 21, 22, 24).

It is not necessary to generate positions that can be derived from other positions by leaving out strings, as these can be generated by combining a position with all ways of muting a string. This is relevant to make this example shorter ...

If we use an uniform span of at most 5 frets we get:

• Position 0: (0-4)
• (0,3,2,0,1,0) → E,C,E,G,C,E
• (0,3,2,0,1,3) → E,C,E,G,C,G
• (3,3,2,0,1,0) → G,C,E,G,C,E
• (3,3,2,0,1,3) → G,C,E,G,C,G
• Position 1: (0, 1-5)
• (0,3,5,0,1,0) → E,C,G,G,C,E
• (0,3,5,0,1,3) → E,C,G,G,C,G
• (3,3,5,0,1,0) → G,C,G,G,C,E
• (3,3,5,0,1,0) → G,C,G,G,C,G
• (0,3,2,5,1,0) → E,C,E,C,C,E
• ...
• ...

All in all you can get the positions by pseudocode:

input: notes
input: tuning
input: span
input: maxfret
input: filter_pred

notes = semitone_dist(notes, c) % 12
tuning_bases = semitone_dist(tuning, c) % 12
first_octave_positions = [(notes - tuning_base + 12) % 12 for tuning_base in tuning_bases]
all_positions = extend_positions_by_octaves(first_octave_positions, maxfret)
flat_positions = sorted_unique(all_positions)

solutions

for pos in flat_positions:
pos_span = pos + span(pos) - 1
possible_positions = [(0, x, positions in pos:pos_span) for positions in all_positions]
for combination in cartesian product of possible_positions where at least one coord is pos:
if filter_pred(combination):
solutions.append(combination)
• Wow this is unbelievable! Way more clever than what I was imagining! Still trying to wrap my head around this. Mar 6 at 21:12
• I don't get the "relative position" numbers, can you expand them and provide variable names perhaps? Mar 6 at 21:15
• @Lance I’ve replaced the chromatic pitch numbers by notenames to be less confusing. Now the numbers all mean fret positions.
– Lazy
Mar 7 at 8:15
• How does this algorithm generate, say, a C13 chord? It has seven notes. Mar 9 at 18:11
• @piiperiReinstateMonica Obviously it is not possible to play a chord with 7 notes on six strings. But you may omit certain notes. By specifying a suitable selection predicate we may decide which omissions we consider acceptable.
– Lazy
Mar 9 at 22:04