chordify_annotateintervals.py

#

Example using .chordify and .annotateIntervals

#

Report normalized counts of chord figures for every simultaneity in the Bach chorales

from music21 import *
from collections import Counter

counts = Counter()
chorales_parsed = 0
#

Iterate through the chorales using corpus.chorales.Iterator(). Use parameters to iterate through a selection of chorals; e.g. corpus.chorales.Iterator(1, 15).

for chorale in corpus.chorales.Iterator():
#

Keep track of and report the number of chorales parsed.

    chorales_parsed += 1
    print(chorales_parsed, 'chorale(s) have been parsed.')
#

chodify the current chorale

    chorale_chords = chorale.chordify()
#

Iterate through the chord objects of the chordified chorale (chorale_chords)

    for c in chorale_chords.flat.getElementsByClass('Chord'):
#

Reduce each chord to close position

        c.closedPosition(inPlace=True)
#

annotateIntervals adds a lyric object to a chord's lyrics property for each generic interval above the bass (e.g. '5', '3', etc.).

        c.annotateIntervals()
#

Get the text from each lyric object in the chord's lyrics property and join into a single string (e.g. '53', '643', etc.).

        figure = ''.join([l.text for l in c.lyrics])
#

count the occurrences of each figure

        counts.update([figure])

print()
#

get total count of figures in order to report normalized results

total_count = sum(counts.values())
#

sort the counts Counter by values from high to low and iterate through 10 most common figures

for figure in sorted(counts, key=counts.get, reverse=True)[:10]:
    figure_percentage = round(100 * counts[figure] / total_count)
#

The output confirms intuitions about the most common figures:
Figure 53 : 36 percent
Figure 63 : 18 percent
Figure 753 : 8 percent
Figure 653 : 6 percent
Figure 642 : 5 percent
Figure 64 : 3 percent
Figure 54 : 3 percent
Figure 73 : 3 percent
Figure 643 : 3 percent
Figure 532 : 2 percent

    print('Figure', figure, ':', figure_percentage, 'percent')