Related: Music: what's in this chord?, Notes to Tablature, Generating guitar tabs?, Translate number pairs to guitar notes
Given a guitar fingering, output the chord it represents. You can use standard input and output, or write a function that returns a string.
The input fingerings will be classifiable as one of the following chords, to be expressed as follows (if the root note were C):
- major triad:
C
- minor triad:
Cm
- (dominant) seventh:
C7
- minor seventh:
Cm7
The chord might be inverted, so you can't rely on the lowest note being the root. Nor can you rely on this being an easy or common fingering in the real world. More generally, your program's output must ignore the octaves of the pitches, and treat all pitches that correspond to the same musical note (i.e., A
) as equal.
This is code-golf, so the shortest code in bytes wins.
Input format
The input is a series of 6 values that indicate, for each string of a 6-string guitar in standard tuning (E A D G B E), which fret that string will be played at. It could also indicate that the string is not played at all. The "zeroth" fret is also known as the open position, and fret numbers count up from there. Assume the guitar has 21 fret positions, such that the highest fret position is number 20.
For example, the input X 3 2 0 1 0
means placing ones fingers at the following positions at the top of the guitar's neck:
(6th) |---|---|---|---|---
|-X-|---|---|---|---
|---|---|---|---|---
|---|-X-|---|---|---
|---|---|-X-|---|---
(1st) |---|---|---|---|---
and strumming the 2nd through the 6th strings. It corresponds to this ASCII tab:
e |-0-|
B |-1-|
G |-0-|
D |-2-|
A |-3-|
E |---|
You have some flexibility in choosing the kind of input you want: each fret position can be expressed as a string, or a number. Guitar strings that are not played are commonly indicated with an X
, but you can choose a different sentinel value if that makes it easier for you (such as -1
if you're using numbers). The series of 6 fret positions can be input as any list, array, or sequence type, a single space-separated string, or as standard input—once again, your choice.
You can rely on the input corresponding to one of the 4 chord types mentioned above.
Please explain in your post what form of input your solution takes.
Output format
You must either return or print to standard output a string describing the chord the fingering is for. This string is composed of two parts concatenated together. Capitalization matters. Trailing whitespace is allowed.
The first part indicates the root note, one of A
, A#
/Bb
, B
, C
, C#
/Db
, D
, D#
/Eb
, E
, F
, F#
/Gb
, G
, or G#
/Ab
. (I'm using #
instead of ♯
, and b
instead of ♭
, to avoid requiring Unicode.) Root notes that can be expressed without a sharp or flat must be expressed without them (never output B#
, Fb
, or Dbb
); those that cannot must be expressed with a single sharp or flat symbol (i.e. either C#
or Db
, but never B##
). In other words, you must minimize the number of accidentals (sharps or flats) in the note's name.
The second part indicates the type of chord, either empty for a major triad, m
for a minor triad, 7
for the dominant seventh, or m7
for the minor seventh. So a G major is output simply as G
, while a D♯ minor seventh could be output as either D#m7
or Ebm7
. More examples can be found in the test cases at the end.
Theory & hints
Musical notes
The chromatic scale has 12 pitches per octave. When tuned to equal temperament, each of these pitches is equally distant from its neighbors1. Pitches that are 12 semitones apart (an octave) are considered to be the same musical note. This means we can treat notes like integers modulo 12, from 0 to 11. Seven of these are given letter names2 from A to G. This isn't enough to name all 12 pitches, but adding accidentals fixes that: adding a ♯ (sharp) to a note makes it one semitone higher, and adding a ♭ (flat) makes it one semitone lower.
Chords
A chord is 2 or more notes played together. The type of chord depends on the relationships between the notes, which can be determined by the distances between them. A chord has a root note, as mentioned earlier. We'll treat the root note as 0 in these examples, but this is arbitrary, and all that matters in this challenge is the distance between notes in modulo arithmetic. There will always be one unique chord type for the answer, either a triad or a seventh chord. The root note will not always be the lowest-frequency pitch; choose the root note such that you can describe the chord as one of the four following chord types:
- A major triad is a chord with the notes
0 4 7
. - A minor triad is a chord with the notes
0 3 7
. - A dominant (or major/minor) seventh chord has the notes
0 4 7 10
. - A minor (or minor/minor) seventh chord has the notes
0 3 7 10
.3
Guitar tuning
Standard tuning on a 6-string guitar starts with E on the lowest string, and then hits notes at intervals of 5, 5, 5, 4, then 5 semitones going up the strings. Taking the lowest E as 0, this means strumming all the strings of the guitar gives you pitches numbered 0 5 10 15 19 24
, which modulo 12 is equivalent to 0 5 10 3 7 0
, or the notes E A D G B E
.
Worked examples
If your input is 0 2 2 0 0 0
, this corresponds to the notes E B E G B E
, so just E, B, and G. These form the chord Em
, which can be seen by numbering them with the root as E, giving us 0 3 7
. (The result would be the same for X 2 X 0 X 0
, or 12 14 14 12 12 12
.)
If your input is 4 4 6 4 6 4
, numbering these with a root of C♯ gives 7 0 7 10 4 7
, or 0 4 7 10
, so the answer is C#7
(or Db7
). If it was instead 4 4 6 4 5 4
, the numbering would give 7 0 7 10 3 7
, or 0 3 7 10
, which is C#m7
(or Dbm7
).
Test cases
X 3 2 0 1 0 ---> C
0 2 2 0 0 0 ---> Em
X 2 X 0 X 0 ---> Em
4 4 6 4 6 4 ---> C#7 (or Db7)
4 4 6 4 5 4 ---> C#m7 (or Dbm7)
0 2 2 1 0 0 ---> E
0 0 2 2 2 0 ---> A
X X 4 3 2 2 ---> F# (or Gb)
3 2 0 0 0 1 ---> G7
X X 0 2 1 1 ---> Dm7
3 3 5 5 5 3 ---> C
4 6 6 5 4 4 ---> G# (or Ab)
2 2 4 4 4 5 ---> B7
0 7 5 5 5 5 ---> Am7
7 6 4 4 X X ---> B
8 6 1 X 1 3 ---> Cm
8 8 10 10 9 8 --> Fm
0 19 5 16 8 7 --> Em
6 20 0 3 11 6 --> A# (or Bb)
X 14 9 1 16 X --> G#m (or Abm)
12 14 14 12 12 12 --> Em
15 14 12 12 12 15 --> G
20 X 20 20 20 20 --> Cm7
X 13 18 10 11 10 --> A#7 (or Bb7)
1by the logarithms of their frequencies
2or, in solfège, names like do, re, mi. In this challenge, use the letter names.
3This could also be called a major sixth chord, with a different choice of root note. In this challenge, call it by its minor seventh name.