This demo extends the Random Patterns demo in creating polyphonic random patterns. It is essentially the same process repeated three times using three different percussion voices. However, a number of interesting structural programming changes in the class are worth noting.
To hear the result play this MIDI file
Lets have a closer look.
Lines 1-4 import useful packages that we need to use in the program. The first import statement gets a standard Java class. The rest of the statements import the jMusic classes required by this class.
In this section Score, Part, and Phrase objects are created. As well, arrays of duration are set up, each one 4 beats long. The array called 'pattern' is later used to store one of the randomly selected patterns 0 through 3. A phrase is created for each of the three percussion voices. Notice that the start times for each phrase is offset by a sixteenth note (semiquaver) to add musical interest and excitement.
Here one of the four patterns is randomly selected eight times over. Each time through the snare, hi hat, and clap patterns are added to their respective phrases. Notice that a new random pattern is selected before each part is rendered using the 'setPattern()' method - see below. The percussive sounds (clap etc.) are selected by choosing the appropriate pitch (39 for clap, and so on).
Just for musical coherence a cymbal crash is added to the end of the phrase, otherwise the score does not finish on a down beat.
Each phrase is added to the same Part and the part to a Score. The score is printed to the screen when the class file is run. This allows the user to see the score data and to enable error checking.
As with other jMusic tutorial files, this code creates a MIDI file from the score. Once created the polyPatterns.mid file can be played by any MIDI file player and will sound correctly using a General MIDI sound source, such as most PC sound cards, or Quicktime. The final line provides feedback during execution that the MIDI file has been successfully written.
This class contains a second method which chooses one of the patterns at random. It is efficient to isolate this routine as a separate method because it is called many times during the program execution and would otherwise need to be repeated within the main method. The pattern arrays are declared earlier as class variables to allow access from both the main and setPattern methods.
|J Music Tutorial index|