Functional Modelling of Musical Harmony
Functional Modelling of Musical Harmony
Functional Modelling of Musical Harmony
An experience report
Abstract Among the first to formalize these theories were Lerdahl and Jack-
Music theory has been essential in composing and performing endoff (1983), who gave an encompasing account on how experi-
music for centuries. Within Western tonal music, from the early enced listeners hierarchically organise tonal music. More formally,
Baroque on to modern-day jazz and pop music, the function of Steedman (1984) proposes a generative grammar for twelve-bar
chords within a chord sequence can be explained by harmony blues chord progressions, and Rohrmeier (2007, 2011) describes
theory. Although Western tonal harmony theory is a thoroughly the core of tonal harmony as a formal grammar. This grammar was
studied area, formalising this theory is a hard problem. implemented by De Haas et al. (2009) and used for modelling har-
We present a formalisation of the rules of tonal harmony as monic similarity. Models of tonal harmony are useful because they
a Haskell (generalized) algebraic datatype. Given a sequence of explain the role or function that a musical chord has within a piece
chord labels, the harmonic function of a chord in its tonal context of music. For instance, the same musical chord often has different
is automatically derived. For this, we use several advanced func- functions depending on the context in which it occurs.
tional programming techniques, such as type-level computations, We present H ARM T RACE (Harmony Analysis and Retrieval of
datatype-generic programming, and error-correcting parsers. As a Music with Type-level Representations of Abstract Chords Enti-
detailed example, we show how our model can be used to improve ties), an adaptation and extension of the Java approach of De Haas
content-based retrieval of jazz songs. et al. to a functional setting. Exploring the connection between a
We explain why Haskell is the perfect match for these tasks, and context-free grammar and an algebraic datatype, we represent dif-
compare our implementation to an earlier solution in Java. We also ferent musical harmonies as values of a datatype. Unlike in previ-
point out shortcomings of the language and libraries that limit our ous work, we encode all the musical restrictions in the type itself;
work, and discuss future developments which may ameliorate our strong static typing guarantees that well-typed values represent har-
solution. monical sequences. Furthermore, a strongly-typed model gives us
higher expressiveness and results in simpler code: through tech-
Categories and Subject Descriptors D.1.1 [Programming Tech- niques such as datatype-generic programming and type-level com-
niques]: Functional Programming; H.5.5 [Information Interfaces putation, most of the code is automatically derived from the types.
and Presentation]: Sound and Music Computing—Modelling In a way, the types are the code: most of the code (that would other-
wise have to be written manually) follows directly from the types.
General Terms Experimentation, Languages
A formal model of musical harmony can be used to improve
many other typical music processing tasks. Content-based Mu-
1. Introduction sic Information Retrieval (MIR, Downie 2003), for instance, is a
[Tonality is] the art of combining tones in such succes- rapidly expanding area within multimedia research which aims at
sions and such harmonies or successions of harmonies, that keeping large repositories of digital music maintainable and acces-
the relation of all events to a fundamental tone is made pos- sible. Within MIR the notion of similarity is crucial: songs that are
sible. similar in one or more features to a given relevant song are likely to
Arnold Schoenberg, in Problems of Harmony be relevant as well. The majority of approaches to notation-based
music retrieval focus on melodic similarity. Using our harmony
The deep connection between mathematics and music has been model, we present a method that allows the retrieval of music based
known at least since the times of Plato (Mountford 1923). In the on harmonic similarity. We compare harmonic analyses in a tree
realm of tonal harmony in particular, when studying the relation- form (which explains the functions of chords within a sequence)
ships between sequential chords, we notice order and regularity; using a generic edit-distance function, and show that this compar-
some combinations sound pleasing while others sound peculiar. ison predicts harmonic similarity better than an edit-distance be-
These observations led music theorists to develop ways to analyse tween the original textual sequences of chords. Chord labels which
the function of a chord in its tonal context (e.g. Riemann 1893). do not “fit” in our model are automatically corrected at the parsing
stage, allowing comparison to proceed.
{
{
{{ I IV V/V V I
C D E F G A B C CD E F G A B C C F D7 G7 C
Figure 1. On the left: the C major scale with the names of the Figure 2. An often occurring chord sequence. The chord labels
notes at the bottom and three example intervals above the notes. are printed below the score, and the scale degrees and functional
On the right: a schematic piano keyboard containing note names analysis above the score.
and highlighting the semitone interval.