audio MIDI Music Algorithms Interfaces Programming Acoustics Context
> Audio > Synthesis > Rendering with multiple Instruments    
 
   

Rendering with multiple Instruments     

This class extenmds the scoe to use three instruments and uses two contrapuntal lines.

To hear the result download the MP3 file below.

WaveformExample7.mp3 [248K]

View / Download source

Lets have a closer look. 
import jm.music.data.*;
import jm.JMC;
import jm.audio.*;
import jm.util.*;
 
// this class introduces three instruments at a time
 
public class WaveformExample7 implements JMC {
	
	public static void main(String[] args) {
		new WaveformExample7();
	}
	
	public WaveformExample7() {
 
		// make the first jmusic score
		Phrase melody = new Phrase();
		for(int i = 0; i < 24; i++) {
			Note n = new Note(
				(int)(Math.random() * 12) * 2 + 50, 
				(int)(Math.random()* 4) * 0.5 + 0.5,
				(int)(Math.random() * 60 + 60));
			n.setPan(Math.random());
			melody.addNote(n);
		}
		Score score = new Score(new Part(melody));
		
		// add a second part
		Part part2 = new Part("Unison", 1);
		Phrase phrase2 = new Phrase();
		phrase2 = melody.copy();
		part2.addPhrase(phrase2);
		score.addPart(part2);
		
		// Create a counterpoint
		Phrase phrase3 = new Phrase();
		int[] intervals = {3, 5, 7, 9, 12};
		double rhythmValue;
		int pitch;
		for (int i = 0; i < melody.size(); i++) {
			rhythmValue = melody.getNote(i).getRhythmValue();
			rhythmValue = rhythmValue / 2.0;
			pitch = melody.getNote(i).getPitch() + 
				intervals[(int)(Math.random() * intervals.length)];
			phrase3.addNote(new Note(pitch, rhythmValue));
			if (Math.random() > 0.5) { // passing note direction?
				pitch += 2;
			} else pitch -= 2;
			phrase3.addNote(new Note(pitch, rhythmValue));
		}
 
		Part part3 = new Part("Counterpoint", 2);
		part3.addPhrase(phrase3);
		score.addPart(part3);
		
		// set up audio instrument
		Instrument sine = new SineInst(44100);
		Instrument chiff = new ChiffInst(44100);
		Instrument tri = new TriangleInst(44100);
	
 	         // add instruments to an instrument array
		Instrument[] ensemble = {sine, chiff, tri};
		
		// render audio file of the score
		Write.au(score, "WaveformExample7.au", ensemble);
	}
}

This class extends the use of multiple instruments introduced in the previous tutorial.

First a three part score is created. The firat two parts play the same melody in unision and the third part contains a counter melody.
Each part is assigned a different instrument number; for example part3 is assigned instrument number 2 (below).

		Part part3 = new Part("Counterpoint", 2);

Three instruments are declared.

		Instrument sine = new SineInst(44100);
		Instrument chiff = new ChiffInst(44100);
		Instrument tri = new TriangleInst(44100);

The instruments are put into an instrument array called "ensemble".

		Instrument[] ensemble = {sine, chiff, tri};

This array is passed as an argument to the Write.au() method for rendering.

		Write.au(score, "WaveformExample7.au", ensemble);

While the melody code is similar to previous examples in this tutoial series, the counter melody code may be of interest (or require some deciphering).
It looks at each note of the melody and generates two notes each of half the rhythmValue of the melodic note.
The pitch of the first of the counter melody note pairs is set to either a minor 3rd, fourth, fifth, major 6th or octave above the melody note.
The pitch of the second note pair is one tone either side of the first note.
The result is an active and fragmented descant above the original part, creating a predominatly tonal but occasionaly dissonant harmonic texture.

 

jMusic Australia Council Queensland University of Technology Sitemap Contact Home Home http://www.qut.com http://explodingart.com/jmusic http://www.ozco.gov.au

Digital Instrument making Home