Forms: Arranging phrases

This class shows how you can experiment with different arrangements of phrases. The same material can be reorganised into different forms so that you can see which works best for you. The program creates two MIDI files with different arrangements of the same phrase material.

To hear the results play the MIDI files below. Binary and Ternary form respectively.

 

Click here to view source

Lets have a closer look at what just happened.
import jm.JMC;
import jm.music.data.*;
import jm.midi.*;
The basic jm libraries used by the class are imported first. The JMC has the note constants and so on. The jm/music/data directory has the jm data structure classes, such as phrase. The jm/midi directory has the classes used to write a MIDI file of the score.

 /**
* An example which generates two different
* score arrangements from the same phrase material
*/
This is a comment. Important to read, but has no impact on the way the program runs.

	public static void main(String[] args){
Score s = new Score();
Part p = new Part();

System.out.println("Starting part length is "+p.getEndTime());
The main() method is declared and an empty Score called 's' and an empty Part called 'p' are created. To confirm the part is empty its length is printed to the screen. Further printouts follow which trace the change in size of the part as phrases are added to it. Notice that Score and Part declarations need not take any arguments. This is important for the Part in this example, as it defaults to a start time of -1.0. This makes no sense, but is used as a flag by the Part to add it to the end of the Part. In this way you don't need to calculate the start time yourself - jMusic will simply add the Phrases (with a start time of -1.0) to the Part in sequential order (like notes in a phrase).

	    //create the first score form
		p.addPhrase(makePhrase1());
		p.addPhrase(makePhrase2());
		s.addPart(p);
				
		System.out.println("Form1 part length is  "+p.getEndTime());
This first form is a simple binary form - phrase 1 followed by phrase 2. The first line in this section is a comment. The next two add new phrases to the part 'p'. They do so by calling the makePhrase1() and makePhrase2() methods which are described in detail below. The part now has two phrases in it. The part is added to the score 's'. The last line prints out the current length of the part 'p' which will be equal to the combined lengths of the two phrases within it.

	    Write.midi(s, "Form1.mid", s);
This line converts the jm score called 's' into a midi file called 'Form1.mid'. All jm scores have the ability to write themselves as MIDI files in this way, which is why the line starts with the name of the score ('s' in this case).

	    p.empty();
s.empty();
Before creating the next form we clear out the contents of the part 'p' and score 's' so we can reuse them. An alternative would be to create a new score and part for the second form but this is just as neat, and shows you the empty() methods as well :)

	    p.addPhrase(makePhrase2());
p.addPhrase(makePhrase1());
p.addPhrase(makePhrase2());
s.addPart(p);

System.out.println("Form2 part length is "+p.getEndTime());
//write a MIDI file to disk Write.midi(s, "Form2.mid");
This section of code creates the second form in the same way as above. The second form is ternary - phrase 2 followed by phrase 1 followed by phrase 2 again. After being created the length of the part is printed and the score is written to a MIDI file.

	   private static Phrase makePhrase1() {
Phrase phrase = new Phrase();
for(short i=0;i<12;i++){
Note n = new Note(c5+i, Q);
phrase.addNote(n);
}
return phrase;
}
There are two more methods (apart from main()) in this class, the first of these is above. The makePhrase1() method simply creates an ascending chromatic scale. Because we reuse the phrase several times it is efficient to have it as a separate method which can be called whenever required. Notice that the method returns a Phrase object, which is why the first line of the method is private static Phrase ... rather than private static void ...(the void key word indicates that no value is returned by the method).

	   private static Phrase makePhrase2() {
Phrase phrase = new Phrase();
for(short i=12;i>0;i--){
Note n = new Note(c5+i, Q);
phrase.addNote(n);
}
return phrase;
}
The last method creates a descending chromatic scale.

Try modifying the class to produce a third score with a different form structure.



jMusic Tutorial Index