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.
To hear the result play the MIDI file below.
Let's have a closer look.
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'.
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.
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;.
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!
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.
The secondInversion() method is identical to the rootPosition() method except that the note pitches are differently organised in relation to the base pitch.
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
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
Chords and Bass