Chords in the cycle of fifths (CPhrase example)

A CPhrase enables jMusic to work with chords.

The normal Phrase type in jMusic is monophonic. This is fine for melodies, polyphonic musics, and even chords can be obtained through multiple phrases. However, the CPhrase (an abbreviation for Chord Phrase) provides a convenient way to deal with chords. Many contemporary music styles such as jazz and rock music rely heavily on the concept of a chord. The CPhrase is great for music with these chordal structures.

In a CPhrase notes can be organized vertically. A chord is considered here as a group of notes which play together, they share the same attributes - start time, duration, and so on. A CPhrase can hold a series of chords, just as a Phrase holds a series of notes. In this example each CPhrase only contains one chord.

All of the methods which manipulate Phrases are also availible in CPhrases, so think of a CPhrase as a homophonic Phrase.

This example plays a progression of dominate seventh chords each a fifth away from the previous chord. In this way each of the 12 notes of the chromatic scale are used once as the root of a chord. This chord progression is common in western musical styles from Baroque to Jazz, and is often called the Cycle of Fifths.

Click here to view source.

To hear the result play the MIDI file below.

Let's have a closer look.
import jm.JMC;
import jm.music.data.*;
import jm.midi.*;
import jm.music.tools.*;
import jm.util.*;

First the class imports the jm packages which are required. The JMC is used for the constants, music.data for the CPhrase and other jMusic types, the midi package allows us to import/export a MIDI file, and the music.tools package includes the ShowScore method used to display the score as 'notation'.

public final class Chords implements JMC{

private static Score s = new Score("CPhrase class example");
private static Part p = new Part("Piano",0,0);

public static void main(String[] args){
//Let us know things have started
System.out.println("Creating chord progression . . .");

The class is declared in the first line of this section. Then the Score and Part are declared. As you can see in this class, the score and part variables are outside the main() method. This enables all the methods within the class to "see" them (they become class variables, not confined to one method). This class is 'static' - it will not be instantiated, and the static main() method does most of the work. That is why these variables are declared static. Making the variables private is not necessary (but a tidy Java practice because no other classes need to 'see' those variables).

The main() method is declared in the normal way, and includes a comment and a println statement to make clear what the programmer intends.

 //choose root notes around the cycle of fifths
int rootPitch = c4;
//set start note to middle C
for (int i = 0; i < 6; i++) {
secondInversion(rootPitch);
rootPitch -= 7;
rootPosition(rootPitch);
rootPitch += 5;
}

This code section does most of the music theory work. This for loop works its way around the cycle of fifths, two chords at a time. In order to keep the note range relatively stable the root note alternates between going up and down from its previous value. It loops six times because there are two chords made per loop. To add musical interest, two chord inversions are used; rootPosition and secondInversion. There are methods below which create chords of these types from the root note. The += and -= signs are short cut syntax. So rootPitch += 5; is a shorter way of writing rootPitch = rootPitch + 5;.

 //add a final chord
ending(rootPitch);

//pack the part into a score
s.addPart(p);

//display the music
new ShowScore(s);

// write the score to a MIDIfile
Write.midi(s, "Chords.mid");
}

This is the rest of the main() method. An ending chord method is added to resolve the progression - it is a tonic rather than dominant chord. The part is then added to the score which is shown and written as a SMF. Lastly, the user is notified that the program has completed.

Now lets look at the chord stuff!

 private static void rootPosition(int rootPitch) {
// build the chord from the rootPitch
int[] pitchArray = new int[4];
pitchArray[0] = rootPitch;
pitchArray[1] = rootPitch + 4;
pitchArray[2] = rootPitch + 7;
pitchArray[3] = rootPitch + 10;
//add chord to the part
CPhrase chord = new CPhrase();
chord.addChord(pitchArray, C);
p.addCPhrase(chord);
}

In this method the root Pitch is used to calculate the other notes of the chord. First an array is declared&emdash;pitchArray. The rootpitch is inserted in the zeroth place, then a note a third above the root, a fifth above the root, and the minor seventh above the root. A CPhrase is declared, and called chord. Its addChord() method uses the array of pitches and a rhythmic value (Crotchet in this case) to create a chord. The CPhrase is added to part 'p'. This process is repeated each time the rootPosition() method is called. Part 'p' ends up containing a series of CPhrases, each with one chord in them.

 private static void secondInversion(int rootPitch) {
// build the chord from the rootPitch
int[] pitchArray = new int[4];
pitchArray[0] = rootPitch;
pitchArray[1] = rootPitch + 4;
pitchArray[2] = rootPitch - 2;
pitchArray[3] = rootPitch - 5;
//add chord to the part
CPhrase chord = new CPhrase();
chord.addChord(pitchArray, C);
p.addCPhrase(chord);
}

The secondInversion() method is identical to the rootPosition() method except that the note pitches are differently organised in relation to the base pitch.

 private static void ending(int rootPitch) {
// build the chord from the rootPitch
int[] pitchArray = new int[4];
pitchArray[0] = rootPitch;
pitchArray[1] = rootPitch + 4;
pitchArray[2] = rootPitch + 7;
pitchArray[3] = rootPitch + 12;
//add chord to the part
CPhrase chord = new CPhrase();
chord.addChord(pitchArray, SB);
p.addCPhrase(chord);
}

This ending() method is called once only. It creates a CPhrase with the fourth note positioned an octave above the root. The SB constant stands for SEMIBREVE or a numerical value of 4.0

With this way of creating a different method for each chord type, you can begin to see that a simple change of root pitch organisation in the main() method could result in a quite different progression. Perhaps you could give it a try :)


Chords and Bass

This variation on the Chords program by Tara Simmonds and Andrew Brown adds a varied rhythm to the chord selection and also a monophonic bass part. The results can sound quite dramatic at times.

Click here to view source.

To hear the result play the MIDI file below.



jMusic Tutorial Index