Automated key detection #785
Replies: 5 comments 3 replies
-
This is possible although "human verified" is the important component 😉 . Keyscapes use correlation of the pitch classes in a selection of music compared to histograms of pitch classes expected in a key. This method generally requires a sufficient amount of input music to make a reasonable guess. When there is a modulation, around the point of modulation there is a transition which is noisy since one key is fading away while another is growing in prominence. For example, when a secondary dominant chord happens, the key does not instantly change and then change back, and there is no feeling of a modulation. Here is an example:
In C major:
The DF#A is a secondary dominant of G. If you play that chord sequence, the last GBD chord sounds like V in C rather than I in G. What happens after the last chord could force a modulation to happen and C major could weaken and be replaced by G (or by using that chord to pivot to E minor or some more distantly related key). After secondary chords, would be tonicizations which are brief sections of the music that last about a phrase (~4 measure). And then modulations would be more structural to the form of the music. In sonata allegro form, for example, the first theme is in the tonic key of the work, then the second key is either in the dominant, or usually the relative major for a minor keyed sonata allegro. Then there is a development section containing mostly tonicizations that try to obscure the initial key before returning clearly to the tonic key at its end. Then the recapitulation repeats the 1st them in the tonic key, and the 2nd theme is then transposed to the tonic key (or the parallel major of a minor tonic key). There is often (in Beethoven and less so with Mozart) an extended transition between the 1st and 2nd themes that could be characterized as tonicizations, such as passing through the subdominant key before arriving at the dominant key for the 2nd theme. In addition to labeling modulations, it would therefore be interesting to indicate what category it is in: secondary, tonicization or full modulation. One problem with doing that is that there is a continuum between these three categories, and it will sometimes be difficult to say that it is purely in one category rather than another. But categorizing them for analysis is better than nothing. Keyscapes is one way of handling the problem: the bottom of the plots are secondary dominants (not really visible), then tonicizations are higher up in the plot, and then modulations are even higher. So with the plot, you can see the categorizations without having to explicitly label them . Humdrum Extras has a tool/filter called Here is an example use of keycor -c h://chorales/chor001.krn
The first column is the best key for the given region of the music. The second column is the R-value (statistical strength of the key with 1.0 being an exact match and 0 meaning random (getting close to the ideal of 12-tone music). The next three columns indicate the start, midpoint and ending of the region of music being analyzed to measure a key. Not available in the current implementation, but for modulations it would be good to have multiple windows of music to detect modulation. For example for measure 50 of a piece of music, you could measure the key from mm. 40–50 and from mm. 50–60, and if there is a change in key between those two regions, then there is likely to be a modulation somewhere near measure 50. It's location could be refined by shrinking the window to 5 measures rather than 10 (but that generally causes more noise in the analysis). Or adding another window from 45–55, or using multiple windows at smaller sizes (something like what keyscapes do). For this choral, I do not see any key changes. This matches to the https://verovio.humdrum.org/?file=chorales/chor001.krn&filter=satb2gs#mh13 Note that you can also adjust the window size (the duration of music being analyzed) with the Here is an example of using a window size of 6 (two measures) for the same piece:
At this resolution (window size), the key is also always identified as G major (which is good). Here is the keyscape for the chorale: mkeyscape h://chorales/chor001.krn -ln | convert - z.png The green region near the middle is the V7/IV to IV chords. It is nice that they are the next largest region after blue (G major), but measure 10–11 is in G major, The darker blue is the dominant (D major, and the dark red is an E minor chord. E minor is Note that the keyscape available on VHV (the little key icon at the top left of the window when displaying the chorale) takes the above image and does some post processing to clean up likely key analysis errors: https://kern.humdrum.org/data?file=chor001.krn&l=chorales&format=keyscape-htmlIn this case G major surrounds the cadence to C, so the post-processing "trims" the C region. The E minor region cannot be trimmed very much since it is adjacent to a D chord (that is higher than others since it is a half note long). I will do a similar analysis with a different chorale that has modulation in it, but in another post—as this one is getting a bit long (and I would not be happy if I accidentally lost it...). |
Beta Was this translation helpful? Give feedback.
-
This chorale has two modulations in it (overall key is A major, and there is an E major region near the beginning and a very clear B minor one in the middle): The key of the music is clearly A major, but the opening has a very weak definition of the tonic key (so it is very noisy at the start of the music. There are also no dominant chords in the first measure, so that adds to the weak opening in A major as well. Looking at the score, the opening A major section is four beats long (about one measure), so the above keyscape is correct (the color between the start of the music and the first barline (tick) is purple, representing A major. The E major (dominant key) goes from measure 2 though 4. An the B minor modulation is from measure 6, beat 4 through the cadence in measure 8. Here is an analysis with **key **rval **conf **start **mid **end
E 0.847 22 =0 =1 =2
E 0.902 46 =1 =2 =3
E 0.892 20 =1 =2 =3
E 0.940 39 =1 =2 =3
E 0.928 45 =1 =2 =3
E 0.867 22 =2 =3 =4
E 0.916 39 =2 =3 =4
E 0.916 39 =2 =3 =4
E 0.916 39 =2 =3 =4
E 0.891 29 =3 =4 =5
E 0.921 42 =3 =4 =5
A 0.908 33 =3 =4 =5
E 0.880 0 =3 =4 =5
A 0.900 17 =4 =5 =6
A 0.962 70 =4 =5 =6
A 0.966 90 =4 =5 =6
A 0.966 90 =4 =5 =6
A 0.944 79 =5 =6 =7
A 0.959 90 =5 =6 =7
A 0.895 100 =5 =6 =7
A 0.832 60 =5 =6 =7
b 0.804 37 =6 =7 =8
b 0.834 64 =6 =7 =8
b 0.883 69 =6 =7 =8
b 0.881 85 =6 =7 =8
b 0.865 87 =7 =8 =9
b 0.887 82 =7 =8 =9
b 0.852 88 =7 =8 =9
b 0.879 67 =7 =8 =9
b 0.772 29 =8 =9 =10
A 0.835 47 =8 =9 =10
A 0.901 46 =8 =9 =10
A 0.961 88 =8 =9 =10
A 0.923 59 =9 =10 =11
A 0.900 86 =9 =10 =11
A 0.930 76 =9 =10 =11
A 0.905 59 =9 =10 =11
A 0.962 68 =10 =11 =12
A 0.962 57 =10 =11 =12
A 0.921 33 =10 =11 =12
A 0.924 50 =10 =11 =12
A 0.968 68 =11 =12 =13
A 0.956 72 =11 =12 =13
A 0.929 73 =11 =12 =13
*- *- *- *- *- *- The analysis does a reasonably good job. The You can see that the modulation from E to A is a bit weak according to the analysis: E 0.891 29 =3 =4 =5
E 0.921 42 =3 =4 =5
A 0.908 33 =3 =4 =5
E 0.880 0 =3 =4 =5
A 0.900 17 =4 =5 =6
A 0.962 70 =4 =5 =6 The E analysis on line 4 can be discounted as noise due to the number in the third column (which I did not explain in the above post). That number is a "confidence" measure. The R-values give approximate confidence since it is the highest value in the calculation of the algorithm. The "confidence" value is the equation (I think):
Where R is the Pearson (normalized) correlation value calculated in the key-finding algorithm. In other words the difference between the top two choices for the key according to the correlation algorithm. The multiplication by 300 is an empirical scaling value to put the confidence range approximately in the range from 0 to 100. So any line that has a noticeably low confidence between two higher confidence lines is likely to be noise (in which case you have to refer to the score). There is also a secondary chord at beat 4 of the first measure where the chord progression is V6 to i in E minor (an interchange of mode for the dominant of E minor). So that would be a good example for how to deal with secondary chords (and is a good example of a complication of why there is an E minor chord when E major is expected in A major). This chord progression could also be thought of as being V6 to i in E major, but that is a weird way of starting a modulation to the parallel minor of the key being modulated into. That progression serves to weaken A major, and indirectly hints at the E major key coming up, but it is difficult to say that it is in either A major or E major, and it is too short to be a modulation. In my opinion it is a non-functional chord sequence that is the result of the bass line going up chromatically C# to D to D# to E, and then using D# to do a secondary dominant chord to a surprise chord (E minor) also outside of A major (but not too far away). |
Beta Was this translation helpful? Give feedback.
-
It would be good for me to enhance
Related to this, I have been thinking of enhancing Click to view Humdrum score for chorale no. 2 (BWV 347): Ich dank' dir, liber Herre which for hand annotations for modulations and a color spine to color notes in the score for the given key (based on the keyscape colors).**kern **kern **kern **kern **color
*ICvox *ICvox *ICvox *ICvox *
*Ibass *Itenor *Ialto *Isoprn *
*I"Bass *I"Tenor *I"Alto *I"Soprano *
*>[A,A,B] *>[A,A,B] *>[A,A,B] *>[A,A,B] *>[A,A,B]
*>norep[A,B] *>norep[A,B] *>norep[A,B] *>norep[A,B] *>norep[A,B]
*>A *>A *>A *>A *>A
*clefF4 *clefGv2 *clefG2 *clefG2 *
*k[f#c#g#] *k[f#c#g#] *k[f#c#g#] *k[f#c#g#] *
*A: *A: *A: *A: *A:
*M4/4 *M4/4 *M4/4 *M4/4 *
*met(c) *met(c) *met(c) *met(c) *
*MM100 *MM100 *MM100 *MM100 *
8AL 4c# 4e 4a #cd29ff
8G#J . . . .
=1 =1 =1 =1 =1
4F# 4c# 4f# 4a .
4C# 8c#L 4e 4a .
. 8BJ . . .
4D 8AL 4f# 4a .
. 8G#J . . .
*e: *e: *e: *e: *e:
4D# 4F# 4f# 4b #af0001
=2 =2 =2 =2 =2
4E 4.B 4e 4g .
*E: *E: *E: *E: *E:
4BB . 4d# 4f# #ff0000
. 8A . . .
4EE; 4G#X; 4B; 4e; .
4E 4e 4g# 4b .
=3 =3 =3 =3 =3
4A 4e 4a 4cc# .
4B 4d# 8g#L 4b .
. . 8f#J . .
4c# 2c# [2e 4a .
8BL . . 8g#L .
8AJ . . 8f#J .
=4 =4 =4 =4 =4
4B 4.B 4e] 4g# .
4BB . 4d# 4f# .
. 8A . . .
4E; 4G#; 4B; 4e; .
=:|! =:|! =:|! =:|! =:|!
*>B *>B *>B *>B *>B
*A: *A: *A: *A: *A:
4C# 4G# 8cc#L 4ee #cd29ff
. . 8bJ . .
=5 =5 =5 =5 =5
8F#L 8AL 4a 4dd .
8G#J 8BJ . . .
4A 8c#L 4a 4cc# .
. 8dJ . . .
4E 4e 4g# 4b .
8C#L 4e 8aL 4a .
8AAJ . 8gJ . .
=6 =6 =6 =6 =6
4D 4d 8f#L 8aL .
. . 8g#XJ 8bJ .
8C#L 4e 4a 4cc# .
8DJ . . . .
4E; 4e; 4g#; 4b; .
*b: *b: *b: *b: *b:
4C# 4e 4a# 4cc# #d36b01
=7 =7 =7 =7 =7
4BB 8f# 8bL 4dd .
. 4B 8aJ . .
4C# . 4g 4cc# .
. 8A# . . .
8DL 4B 8f#L 4b .
8GJ . 8eJ . .
4F# 4c# 4f# 4a# .
=8 =8 =8 =8 =8
2.BB; 2.d; 2.f#; 2.b; .
*A: *A: *A: *A: *A:
4C# 4G# 8eL 4e #cd29ff
. . 8dJ . .
=9 =9 =9 =9 =9
4F# 4A 4c# 4a .
8EL 8G#L 4d 4b .
8DJ 8F#J . . .
4C# 8EL 4e 4cc# .
. 8eJ . . .
4BB 4d 8f#L 4dd .
. . 8g#J . .
=10 =10 =10 =10 =10
8AAL 8c#L 2a 4ee .
8BB 8d . . .
8C# 8e . 8ddL .
8DJ 8f#J . 8cc#J .
4E; 4g#; 4e; 4b; .
4BB 4f# 4b 4dd .
=11 =11 =11 =11 =11
8F#L 8f#L 8aL 4cc# .
8G#J 8eJ 8g#J . .
4A 8dL 4f# 4b .
. 8c#J . . .
8G#L 4B 4e 4.ee .
8EJ . . . .
[4A 8c#L 8f#L . .
. 8dJ 8g#J 8dd .
=12 =12 =12 =12 =12
8AL] 4.e 2a 8cc#L .
8G#J . . 8b .
4F# . . 8a .
. 8d . 8bJ .
8EL 8c#L 4a 4cc# .
8DJ 8f#J . . .
4E 8BL 4g# 4b .
. 16eL . . .
. 16dJJ . . .
=13 =13 =13 =13 =13
2.AA; 2.c#; 2.e; 2.a; .
== == == == ==
*- *- *- *- *- |
Beta Was this translation helpful? Give feedback.
-
Yes, it does. There are two versions of I tested both, and the Humdrum Extras one is transposing the key interpretations in the
For transposing figure bass as well, I would have to think about it. It is not trivial, but could be relatively straight-forward to do. If relative accidentals are given, then there would be no need to transpose... But probably most figure bass notations use the more modern absolute accidental system that indicates the chromatic alteration of the pitch-class rather than of the scale degree. |
Beta Was this translation helpful? Give feedback.
-
Analyzing with the rule of the octave and Bassstufen there could also be e small section in e minor in Is it possible to reset the current key label to "none" if I don't want to map a key to a section of the score? E.g. I encoded this with For this section I could also think of Another example in You could say e minor could somewhat fit there without the major 3rd, but it's probably better to label this passage as a romanesca sequence. So there is no need for a key for the first two beats of the sequence (measure 15, beat 1 with upbeat). Related to this: What's the best option to label passages as sequences such as descending fifths, ascending fifths, romanesca, Lamento-Bass (more Satzmodelle on this link). It it possible to label an alternative key for the same passage? Two examples for phrygian here: |
Beta Was this translation helpful? Give feedback.
-
Based on our discussion on rism-digital/verovio#3166 about scale degrees I decided to start a new discussion here because it's a bit off topic to verovio and the Humdrum-to-MEI converter.
I wonder if it is possible to auto-detect the keys, e.g. with
keyscape
, and thus automatically provide e.g. the Bach chorales with the correct keys for modulations (or at least a best guess that would need to be human verified). This would be super useful in combination with the scale degree for the bass and soprano.Related to this: will the humlib
transpose
filter also transpose the key name interpretations? E.g.VHV
Transpose to E flat major with
transpose -k E-
.Expected output:
VHV
Note that ideally transposed figured bass numbers would also recognize whether a sharp, flat or a natural is needed.
Beta Was this translation helpful? Give feedback.
All reactions