Friday, May 11, 2007

Instant Bach goes Open Source

Introduction

The program "Instant Bach" was made as a project for an unnamed computer science class, which was when I was a sophomore computer science major, and a senior music performance and music composition double-major. Needless to say at the time of this program's creation I knew far more about musical theory than I did computer science. When I approached the professor of this class with my project idea his response was that "other people have tried stuff similar for this class and failed, and the same will happen to you." Despite this opinion I remained confident that a four-part harmony writing program could be made using the skills I currently had available, so I went ahead and made the program for my project. I have recently decided to resurrect this program to expand its capabilities as an open source project (http://sourceforge.net/projects/instantbach).

The original program was based of of static rules which limits it to a particular number of possible progressions:

My most immediate goal for this project is to move all of the rules of voice leading, doubling, chord structure, and progression to an external rules file, so that the program can be adjusted to work beyond just being able to voice major key progressions.

Purpose

The first two years of undergraduate music theory are spent learning and doing mostly one thing: four-part harmony writing. The purpose of this program is to be able to do four part harmony writing, with or without a user specified progression, according to all the rules of four part harmony writing, and have the ability to transpose the result into any key.

Musical Intervals

The distance between any two musical notes is referred to as an interval, and is represented by a two-character symbol. The first character denotes the quality, being P for perfect, A for augmented, D for diminished, m for minor, and M for major. The second character denotes the melodic distance with a number of how many lines and spaces exist between the two notes of the interval. Major and minor refer to whether an interval occurs within a natural minor scale or within a major scale. Augmented and diminished are used for intervals that do not exist in the major or natural minor scales, augmented being greater than, and diminished being less than. The following is a chart of the most common musical intervals:

Tertian Harmony

Tertian Harmony refers to simultaneously sounding pitches in which each of the pitches are separated by the distance of major or minor third, in at least one permutation of the order of notes. Tertian harmonies consisting of three distinct notes are referred to as triads, and harmonies consisting of four distinct notes are referred to as seventh chords. A triad can have four different classifications depending on the interval relationship between the notes in the triad. These different types of triads are called major, minor, augmented, and diminished. Seventh chords can be major-major, dominant, minor, half-diminished, or fully diminished.

Progression

For any given scale, for example a C Major scale (C, D, E, F, G, A, B), each of the pitches in that scale represent the root note of a tertian chord of some type. Each of these harmonies are referred to as chords and are represented by a roman numeral. The roman numeral indicates the quality of a chord (major, minor, augmented, diminished), the position of the chord inside the key, and which note is on the bottom, (the root), of that chord.

The first note of a chord is known as the root, the second is the third, the third is the fifth, and if there is a fourth note it is called the seventh. Whenever a chord cannot be spelled in thirds, it is in an inversion, meaning that a note other then the root is on the bottom of the chord. An inversion is denoted by one number on top of another and is written to the right of the chord symbol, but I will write them as fractions for simplicity. In a triad, when the third is on the bottom of the chord it is in first inversion and the symbol that is used is 6. When the fifth is on the bottom of a triad it is in second inversion and is written as 6/4. Seventh chords can have three inversions, with the third, fifth, and seventh on the bottom, denoted 6/5, 4/3, and 2.

Rules of Progression

The order in which chords can occur have multiple rules, the easiest rule being that I can move to anything, but rather than explain the progression behavior with words, I will use a picture of the progression graph, where each chord symbol is a vertex, and all edges are un-weighted.

Other Chord Symbols

As shown in the graph, there are other chord symbols that have not yet been described. They are the Neopolitan Sixth Chord (N6), the French Augmented Sixth Chord (Fr+6), and the secondary dominant (V/x). These chords incorporate notes that are outside of the given key, and have strict rules for which chord they can lead to. The N6 and Fr+6 must go to V, and secondary dominants must lead to chord symbol on the bottom of the fraction. Secondary dominants are considered to be a borrowed V from another key, and therefore must progress to the I of the key from which they were borrowed.

Four part Harmony Writing

Four-part harmony writing is when a progression is developed and the notes in the chords of that progression are assigned to four voices, the bass, tenor, alto, and soprano. Each voice has to stay within a particular range and is limited to the distance which the voice can move between chords. Each note has rules for motion and each chord has rules for its resolution. Because most of the progression consists of triads, and there are four voices, a member of the chord has to be doubled, which there are also rules for.

Rules of Doubling

  • I, ii, IV, iii, and vi : The root and third can be doubled.
  • V and V/x : The root can be doubled.
  • V7 and V7/x : no doubling is needed or the root can be doubled and the fifth omitted.
  • viio : The third can be doubled.
  • vii07 : No doubling is needed.
  • N6 : The third can be doubled.
  • Fr+6 : No doubling is needed.

Rules of Resolution

  • I, ii, IV, and vi : None.
  • iii : If the fifth is in the soprano or bass it must resolve up by a m2. If the fifth is in the tenor or alto it must either resolve up a m2 or move down a M3.
  • V and V/x : If the third is in the soprano or bass it must resolve up by a m2. If the third is in the tenor or alto it must either resolve up a m2 or move down a M3.
  • V7 and V7/x : The third must resolve as the same as in the V and V/x and the seventh must move down by a m2.
  • viio : If the root is in the soprano or bass it must resolve up by a m2. If the root is in the tenor or alto it must either resolve up a m2 or move down a M3. If the chord which the viio is going to is a V, then the leading tone (the seventh degree of the scale) is already present and there are no rules of resolution.
  • vii07 : The root must resolve the same as in the viio, and even if the next chord is V, the seventh must move down by a M2.
  • N6 : The root must move down by a D3.
  • Fr+6 : The A6 (or its inversion d3) interval in the chord must resolve to P8 or P1.

Rules of Individual Voice Motion

  • Bass: The bass generally skips around more than all of the other voices and does not have any specific restrictions.
  • Tenor and Alto: Both these voices generally move very little and rarely move more then the interval of a third.
  • Soprano: This voice moves more then the tenor and alto but is usually limited to skips of less then a P4.

Range of Voices in Relation to Each Other

The distance between the tenor and the bass can never exceed a P8+ M3, while the distance between all other voices must remain within a P8.

Cross-Voicing

All voices must always be in order from lowest to highest: bass, tenor, alto, and soprano. If a voice is ever out of that order it is considered cross-voicing, which is not allowed. Cross-voicing can also occur in the motion between two chords.

Illegal Parallel Motion

If two voices move the same interval in the same direction they are in parallel motion. The parallel motions of a P8, P5, or P1 are illegal parallel motions. These are called parallel octaves, fifths, and unisons.

Programming the Progression

The progression for the program can be created two ways, either the user can select the individual chords, or the computer can generate the progression itself. Harmonic progression can be represented as an unweighted graph, in order to determine whether a specified progression is valid. The main problem with this approach is that if the computer were to generate its own progression, it would have to end at a specific point with a cadence. (A cadence is a series of chords which end a musical idea or statement). The computer is able to do two of the most common cadences, an imperfect authentic cadence (IAC), and a perfect authentic cadence (PAC). Each of the cadences consists of a V then I, but in the PAC the soprano shares the root with the bass. The problem is that if you have n chords and start with a random chord, and randomly move through the graph until n-2, then cadence, there is a good chance that the chord at n-3 will not be able to go to the chord at n-2. This would mean that the progression would have to be re-done. To prevent this problem, I built the graph backwards, and the progression is formed backwards as well. This way the progression never runs itself into a dead-end because it always starts with I, V, and then the random chords, so when the progression reaches n, the chord at n will always exist because there are no verticies in the graph that do not have at least one adjacent vertex.

Turning the Progression into Musical Notes

After the progression has been created, the computer needs a way to assign notes of all chords in the progression to the four voices. To assign notes to the four voices the computer must first generate all possible notes each voice can have. This is done by using a recursive method that when given the starting pitch of a chord, and the symbol of that chord, builds a chord consisting of every possible note in that chord. Because rules of voice leading depend on individual chords and the position of individual notes within that chord, each note in a chord maintains whether a given note is a root, third, fifth, or seventh.

The Chord as a Data Structure

The main object of this program is the chord, which consists of four voices and a chord symbol, so the obvious main data structure for the program is a Chord object, consisting of four voices and a chord symbol. A Chord object also contains methods which allow the individual voices to be viewed as either the note or the position of that note within the chord, as well as a method to obtain the chord symbol. This allows other methods to deal with notes arithmetically and as the position of that note within the chord for voice leading purposes.

Creating All The Chords

Because of all the rules associated with four-part harmony writing, sometimes the voicing of a progression leads to a dead-end where the progression is correct, but because of the way a chord is voiced, it may not be able to correctly move to the next chord. This is why for the computer to be able to voice a progression, it must have available all the possible combinations of four note chords. The combinations have restrictions though, for each voice the note must be within that voice’s range, and may not overlap that of another voice.

Checking the Motion Between Chords

To check the motion between chords, the motion itself becomes a Motion object and contains methods to compare the motion with other Motion objects, thus being able to check for all the rules of motion. A Motion object is created with the same voice in two adjacent chords, for all four voices. The object basically takes the two notes, calculates the distance between the two notes, and determined the direction that they are moving. Besides the Motion object, which checks for invalid parallel motion, the program also checks for cross-voicing within chords, cross-voicing between chords, and invalid interval skips. Within the same method which checks for all the previous mentioned motions, there are a series of nested Boolean statements which check for proper resolutions of chords based on chord symbols. Each chord is unique in its resolution, with the exception of when a member of a chord has to resolve in the same direction and distance as another. In this case a method was developed to be used for checking the resolution on chords that resolve using only step-wise motion. Otherwise if there is resolution, it directly involves the leading tone or has a completely unique resolution, and must be dealt with individually (Fr+6 and N6 chords).

Choosing the Most Correct Chord

After all the chords have been created and checked for doubling and motion, the chords that remain are all correct, but there is always a most optimal chord to choose. This is done by first looking at the inversion of the chord because chords are generally preferred to be in root position, with the exception of chords which cannot exist in root position. If there is more than one chord in root position, the chord which has the least motion to its already created adjacent chord is chosen. In the case where a chord does not exist in root position, the chord with the least motion to its already created adjacent chord is chosen.

Contributors