Extracting Harmonies from Leadsheets#

Say you have one or a lot of leadsheets in musicXML format such as Effendi’s Realbook collection

[1]:
from music21 import chord, key, stream, converter, harmony
[2]:
f = chord.Chord('F4 C5 A5')
kf = key.Key('F')
sf = stream.Measure([kf, f])
sf.show()
_images/Extracting_Harmonies_2_0.png

Loading a Song#

Assuming you have already downloaded Effendi’s Whole collection to your local machine you can use music21 to open them and get a lot of information out.

[4]:
bm = converter.parse('/mnt/Datos/RealBooks/effendi.me/jazz/repo/II/Blue Moon.xml')
bm.show()
_images/Extracting_Harmonies_4_0.png
[5]:
bm.analyze('key')
[5]:
<music21.key.Key of E- major>
[6]:
bm.metadata.all()
[6]:
[('composer', 'Richard Rodgers Lorenz Hart\nArr. 6EQUJ5'),
 ('copyright', '©2016 Finmusic/effendi.me'),
 ('movementName', 'Blue Moon')]
[7]:
bm.metadata.title
[7]:
'Blue Moon'
[8]:
bm.metadata.composer
[8]:
'Richard Rodgers Lorenz Hart\nArr. 6EQUJ5'
[9]:
bm.getTimeSignatures().show()
/home/fccoelho/Documentos/Software_projects/musicir/venv/lib/python3.10/site-packages/music21/musicxml/m21ToXml.py:477: MusicXMLWarning: <music21.stream.Score 0x7f335214a890> is not well-formed; see isWellFormedNotation()
  warnings.warn(f'{scOut} is not well-formed; see isWellFormedNotation()',
_images/Extracting_Harmonies_9_1.png
[10]:
m = bm.measure(1)
[11]:
m.show()
_images/Extracting_Harmonies_11_0.png

However, if you want to extract the harmony, i.e. the sequence of chords, measure by measure. Music21 does not help much.

[12]:
from musicir.leadsheets.musicxml import HarmonyParser, ChordParser, import_into_db
[14]:
HP = HarmonyParser("/mnt/Datos/RealBooks/effendi.me/jazz/repo/II/Black Ice.xml")
HP.number_of_measures
[14]:
62
[29]:
c= HP.get_measure_chords(1)[0]
[31]:
Ch = ChordParser(c)
Ch
[31]:
B7 - dominant
[32]:
HP.as_json()
[32]:
'[{"measure": 0, "chords": []}, {"measure": 1, "chords": ["B7 - dominant"]}, {"measure": 2, "chords": ["Emin9 - minor-ninth", "Fmaj7 - major-seventh"]}, {"measure": 3, "chords": ["Emin7 - minor-seventh", "A13 - dominant-13th"]}, {"measure": 4, "chords": ["Emin7 - minor-seventh", "F6 - major-sixth"]}, {"measure": 5, "chords": ["Emin7 - minor-seventh", "A13 - dominant-13th"]}, {"measure": 6, "chords": ["A13 - dominant-13th"]}, {"measure": 7, "chords": ["Emin7 - minor-seventh", "F7 - dominant"]}, {"measure": 8, "chords": ["Emin7 - minor-seventh", "A13 - dominant-13th"]}, {"measure": 9, "chords": ["Emin7 - minor-seventh", "Fmaj7 - major-seventh"]}, {"measure": 10, "chords": ["Emin9 - minor-ninth", "A13 - dominant-13th"]}, {"measure": 11, "chords": ["Emin9 - minor-ninth", "Fmaj7 - major-seventh"]}, {"measure": 12, "chords": ["Emin9 - minor-ninth", "A13 - dominant-13th"]}, {"measure": 13, "chords": ["Emin9 - minor-ninth", "F6 - major-sixth"]}, {"measure": 14, "chords": ["Emin9 - minor-ninth", "A13 - dominant-13th"]}, {"measure": 15, "chords": ["Emin7 - minor-seventh", "Dmin9 - minor-ninth", "G13 - dominant-13th"]}, {"measure": 16, "chords": ["Cmaj7 - major-seventh", "B7 - dominant"]}, {"measure": 17, "chords": ["Dmin7 - minor-seventh", "G7 - dominant", "Cmaj7 - major-seventh", "E\\u266d - major"]}, {"measure": 18, "chords": ["B\\u266dmaj9 - major-ninth", "A\\u266d13 - dominant-13th", "G7 - dominant"]}, {"measure": 19, "chords": ["F\\u266fmin11 - minor-11th"]}, {"measure": 20, "chords": ["C\\u266f9 - dominant-ninth"]}, {"measure": 21, "chords": ["F\\u266fmin11 - minor-11th"]}, {"measure": 22, "chords": ["C\\u266f9 - dominant-ninth"]}, {"measure": 23, "chords": ["F\\u266fmin - minor"]}, {"measure": 24, "chords": ["C\\u266f9 - dominant-ninth"]}, {"measure": 25, "chords": ["F\\u266fmin11 - minor-11th"]}, {"measure": 26, "chords": ["G - major", "B7 - dominant"]}, {"measure": 27, "chords": ["Emin - minor", "F6 - major-sixth"]}, {"measure": 28, "chords": ["Emin7 - minor-seventh", "A13 - dominant-13th"]}, {"measure": 29, "chords": ["Emin7 - minor-seventh", "F6 - major-sixth"]}, {"measure": 30, "chords": ["Emin6 - minor-sixth", "A13 - dominant-13th"]}, {"measure": 31, "chords": ["Emin9 - minor-ninth", "F6 - major-sixth"]}, {"measure": 32, "chords": ["Emin9 - minor-ninth", "A13 - dominant-13th"]}, {"measure": 33, "chords": ["Emin9 - minor-ninth", "F6 - major-sixth"]}, {"measure": 34, "chords": ["Emin9 - minor-ninth", "A13 - dominant-13th"]}, {"measure": 35, "chords": ["Emin9 - minor-ninth", "F6 - major-sixth"]}, {"measure": 36, "chords": ["Emin9 - minor-ninth", "A13 - dominant-13th"]}, {"measure": 37, "chords": ["Emin9 - minor-ninth", "A13 - dominant-13th"]}, {"measure": 38, "chords": ["Emin7 - minor-seventh", "Dmin9 - minor-ninth", "G13 - dominant-13th"]}, {"measure": 39, "chords": ["Cmaj7 - major-seventh", "B7 - dominant"]}, {"measure": 40, "chords": ["Dmin7 - minor-seventh", "G7 - dominant", "C6 - major-sixth", "E\\u266d - major"]}, {"measure": 41, "chords": ["B\\u266dmaj7 - major-seventh", "A\\u266d13 - dominant-13th", "G7 - dominant"]}, {"measure": 42, "chords": ["F\\u266fmin11 - minor-11th"]}, {"measure": 43, "chords": ["C\\u266f9 - dominant-ninth"]}, {"measure": 44, "chords": []}, {"measure": 45, "chords": []}, {"measure": 46, "chords": []}, {"measure": 47, "chords": []}, {"measure": 48, "chords": []}, {"measure": 49, "chords": []}, {"measure": 50, "chords": []}, {"measure": 51, "chords": []}, {"measure": 52, "chords": []}, {"measure": 53, "chords": []}, {"measure": 54, "chords": []}, {"measure": 55, "chords": []}, {"measure": 56, "chords": ["F\\u266fmin11 - minor-11th"]}, {"measure": 57, "chords": ["B7 - dominant"]}, {"measure": 58, "chords": ["F\\u266fmin11 - minor-11th"]}, {"measure": 59, "chords": ["C\\u266f9 - dominant-ninth"]}, {"measure": 60, "chords": ["F\\u266fmin11 - minor-11th"]}, {"measure": 61, "chords": ["G - minor", "B7 - dominant"]}]'
[34]:
for m in range(HP.number_of_measures):
    print(f'Measure {m}')
    h = HP.get_measure_chords(m)
    for c in h:
        print(ChordParser(c))
Measure 0
Measure 1
B7 - dominant
Measure 2
Emin9 - minor-ninth
Fmaj7 - major-seventh
Measure 3
Emin7 - minor-seventh
A13 - dominant-13th
Measure 4
Emin7 - minor-seventh
F6 - major-sixth
Measure 5
Emin7 - minor-seventh
A13 - dominant-13th
Measure 6
A13 - dominant-13th
Measure 7
Emin7 - minor-seventh
F7 - dominant
Measure 8
Emin7 - minor-seventh
A13 - dominant-13th
Measure 9
Emin7 - minor-seventh
Fmaj7 - major-seventh
Measure 10
Emin9 - minor-ninth
A13 - dominant-13th
Measure 11
Emin9 - minor-ninth
Fmaj7 - major-seventh
Measure 12
Emin9 - minor-ninth
A13 - dominant-13th
Measure 13
Emin9 - minor-ninth
F6 - major-sixth
Measure 14
Emin9 - minor-ninth
A13 - dominant-13th
Measure 15
Emin7 - minor-seventh
Dmin9 - minor-ninth
G13 - dominant-13th
Measure 16
Cmaj7 - major-seventh
B7 - dominant
Measure 17
Dmin7 - minor-seventh
G7 - dominant
Cmaj7 - major-seventh
E♭ - major
Measure 18
B♭maj9 - major-ninth
A♭13 - dominant-13th
G7 - dominant
Measure 19
F♯min11 - minor-11th
Measure 20
C♯9 - dominant-ninth
Measure 21
F♯min11 - minor-11th
Measure 22
C♯9 - dominant-ninth
Measure 23
F♯min - minor
Measure 24
C♯9 - dominant-ninth
Measure 25
F♯min11 - minor-11th
Measure 26
G - major
B7 - dominant
Measure 27
Emin - minor
F6 - major-sixth
Measure 28
Emin7 - minor-seventh
A13 - dominant-13th
Measure 29
Emin7 - minor-seventh
F6 - major-sixth
Measure 30
Emin6 - minor-sixth
A13 - dominant-13th
Measure 31
Emin9 - minor-ninth
F6 - major-sixth
Measure 32
Emin9 - minor-ninth
A13 - dominant-13th
Measure 33
Emin9 - minor-ninth
F6 - major-sixth
Measure 34
Emin9 - minor-ninth
A13 - dominant-13th
Measure 35
Emin9 - minor-ninth
F6 - major-sixth
Measure 36
Emin9 - minor-ninth
A13 - dominant-13th
Measure 37
Emin9 - minor-ninth
A13 - dominant-13th
Measure 38
Emin7 - minor-seventh
Dmin9 - minor-ninth
G13 - dominant-13th
Measure 39
Cmaj7 - major-seventh
B7 - dominant
Measure 40
Dmin7 - minor-seventh
G7 - dominant
C6 - major-sixth
E♭ - major
Measure 41
B♭maj7 - major-seventh
A♭13 - dominant-13th
G7 - dominant
Measure 42
F♯min11 - minor-11th
Measure 43
C♯9 - dominant-ninth
Measure 44
Measure 45
Measure 46
Measure 47
Measure 48
Measure 49
Measure 50
Measure 51
Measure 52
Measure 53
Measure 54
Measure 55
Measure 56
F♯min11 - minor-11th
Measure 57
B7 - dominant
Measure 58
F♯min11 - minor-11th
Measure 59
C♯9 - dominant-ninth
Measure 60
F♯min11 - minor-11th
Measure 61
G - minor
B7 - dominant
[ ]: