Audio 101 - Introducing jMusic Audio

This example creates a scale and writes it out to an Audio file!

The TriangleInst class is used to create a synthesised audio waveform for each note.

The final Audio file will sound like this:

Click here to view source.

Lets have a closer look.
import jm.JMC;
import jm.util.*;
import jm.music.data.*;
import jm.audio.*;

As well as the jm.music classes, the jm.audio classes are imported. The audio.data folder contains input and output code, the audio.synth folder contains synthesis tools not used in the sample-based example, and the jmInst class contains the instrument classes including the TriangleInst class used in this tutorial.

public final class AudioScale implements JMC{
public static void main(String[] args){

The class is declared, then the main method. Structurally this class is very basic, everything happens in the main method.

		//declare an instance of the triangle wave instrument
		TriangleInst tri = new TriangleInst(8000);
		//Put the instrument(s) into an array called ensemble
		Instrument[] ensemble = {tri};

First audio priority is to declare instruments and then an instrument array. The TriangleInst takes one argument, the sampling rate. The ensemble array holds all the instruments to be used by this score, only one in this case.

		Score s = new Score("JMDemo - Audio Scale");
		// The third argument to the Part constructor
		//specifies an element in the instrument array
		Part p = new Part("Melody", 0);
		Phrase phr = new Phrase(); 

The jMusic score is created in the 'normal' way. This is the nice thing about jMusic, the same score file can be used to render audio or MIDI versions of the score. Importantly, the second argument to the part indicates which instrument in the instrument array is to be used by the part.

		for(short i=0;i<25;i++){
Note n = new Note(i+60, QUAVER, 127);
phr.addNote(n);
}

A chromatic scale is created.

		p.addPhrase(phr);
s.addPart(p);

The phrase is packed into a part, then into a score.

		Write.au(s, "AudioScale.au", ensemble);
}
}
 

This is a the magic line that renders the score as an audio file - simple isn't it! The writeAU() method takes two arguments, the file name and the Instrument array to be used.



jMusic Tutorial Index


© 2002 Andew R Brown