Chorale - Using addNoteList()

As well as adding notes one by one to a jMusic Phrase it is possible to add a list of notes using the addNoteList() method. This example shows how this can be done, by re-creating a section of a Bach Chorale.

The process for using addNoteList() is to create two arrays for each phrase, one which consists of the pitch values (as integers), the other of rhythmic values (as doubles). The addNoteList() method is called for the phrase with the pitch and rhythmic value arrays as arguments.

 

The final MIDI file will sound like this:

Click here to view source.

Lets have a closer look.
import jm.JMC;
import jm.music.data.*;
import jm.util.*
	 
Here are the by now familiar imports of the jMusic packages required by this class.

public final class Chorale implements JMC{
	public static void main(String[] args){
		// set up the pitches and durations of each line
		int[] pitchSop = {C5,G4,E4,D4,G4,A4,C4,D4,E4,D4,F4,E4,A4,G4,E4};
		double[] rhythmSop = {C,C,DC,Q,C,C,C,C,C,C,C,C,M,C,C};
		int[] pitchAlto = {E4,D4,C4,A3,B3,C4,D4,C4,D4,CS4};
		double[] rhythmAlto = {C,C,SB,C,C,C,C,SB,C,C};
		int[] pitchTenor = {G3,C4,B3,A3,G3,F3,E3,G3,G3,C4,B3,A3,B3,A3};
		double[] rhythmTenor = {M,C,Q,Q,C,C,C,C,M,C,C,M,C,C};
		int[] pitchBass = {C3,B2,A2,G2,F2,E2,F2,A2,G2,C3,B2,A2,G2,F2,F3,E3,A3};
		double[] rhythmBass = {C,C,C,Q,Q,C,C,C,C,C,C,C,C,C,C,C,C};
The class is decalred and then the main() method is decalred.

Next is a series of data arrays that hold jMusic constant values for pitches and rhythm values. There is a pitch array and rhythm value array fro each phrase.

 
		//create the jMusic phrase objects
		Phrase soprano = new Phrase();
		Phrase alto = new Phrase();
		Phrase tenor = new Phrase();
		Phrase bass = new Phrase();
The phrases are created.

 
		// add the notes to each phrase
		soprano.addNoteList(pitchSop, rhythmSop);
		alto.addNoteList(pitchAlto, rhythmAlto);
		tenor.addNoteList(pitchTenor, rhythmTenor);
		bass.addNoteList(pitchBass, rhythmBass);
In these lines the addNoteList() method takes the pitch and rhythm value arrays as arguments and creates note objects and adds them to the phrases.

 
		// create jMusic parts
		Part s = new Part("Soprano", OHH, 1);
		Part a = new Part("Alto", AHH, 2);
		Part t = new Part("Tenor", OHH, 3);
		Part b = new Part("Bass", AHH, 4);
four parts are created, one for each phrase. Each is on a different channel (1-4) but only two of the General MIDI instruments are used across the four parts.

 
		// add the phrases to the parts
		s.addPhrase(soprano);
		a.addPhrase(alto);
		t.addPhrase(tenor);
		b.addPhrase(bass);
Each phrase is added to a comensurate part.

 
		//create a score
		Score score = new Score("Chorale");
		
		//add the parts to the score
		score.addPart(s);
		score.addPart(a);
		score.addPart(t);
		score.addPart(b);
A score is created. It is called score (lower case) and has the title "Chorale". Each of the parts is added to the Score. Our packing of the jMusic data structure is complete.

 
		//display the result for the world to see
		View.show(score);
		
		// save score as a MIDI file
		Write.midi(score, "Chorale.mid");
	}
}
	
We can now do things with the score. First we display the music using the show score GUI. The View class has a methods called show() dedicated to this task.

Second, we translate the jMusic Score named score into a standard MIDI file and save it to disk with the name "Chorale.mid".


jMusic Tutorial Index