audio MIDI Music Algorithms Interfaces Programming Acoustics Context
> MIDI > Musical Data > Lets make a noise    
 
   

Lets make a noise

Enough already I hear you say, lets make some noise.
What follows is a simple program that plays a scale and saves it to a Standard MIDI File (SMF).
To try this example download the code from the link below, it results in a file named 'Scale.java'.
Don't worry if you don't understand the code, we haven't explained it to you yet! Just take a look...

Click here to view source



To hear the result play the MIDI file below


.Lets have a closer look at what just happened.



import jm.JMC;
import jm.music.data.*;
import jm.util.*;
Lines 1-3 import useful packages that we need to use in the Scale.java program.
The first import statement gets a class called the jMusic Constants (hence JMC).
The second line imports the data structure classes including Score, Part, Phrase, and Note.
The third import statement gives us access to jMusic utilities, and we will use the Write utility to save the composition to disk as a MIDI file.

 

/**
* A short example which generates a one octave c chromatic scale
* and writes to a MIDI file called ChromaticScale.mid
* @author Andrew Sorensen and Andrew R. Brown
*/
This is a comment. Important to read but has no impact on the way the program runs.
When the java file is compiled all the comments are disregarded - they have no impact on the .class file that is generated
. But they are very useful for humans like us, and it is good programming practice to include comments in your code.


public final class Scale implements JMC{

This line defines our Scale class. If you're new to Java then the 'public final' words might be mysterious.
The public statement enables this class to potentially be used by all other classes, and to be run.
The final keyword means that the class may never be subclassed. . . and sublclassed means . . . (later folks).

In this line the class implements the interface JMC
. All you need to understand about interfaces for the moment is that whenever you want to be able to use CROTCHET (or just C) to represent crotchet (instead of 1.0) or C4 to represent the pitch middle C (instead of 60) then you need your class to implement the JMC interface.

Have another look at JMC.java to see what constants are defined in there.
Constants are variables that are set once and never change. All constants in jMusic are in upper case.




public static void main(String[] args){
 
This line describes a method and is the starting point of our little program.
All Java programs start in a main() method like the one defined above, but if you didn't already know that you probably need to take a quick look at a short
java tutorial before going on.
(There are several online java tutorials that will show more than enough to understand the basics of jMusic.
Try looking here for a good place to bookmark.)

A brief description of the keywords follows: The public keyword makes this method 'visible' (able to be used) anywhere the class is visible.
The static keyword indicates that there can only be one copy of this method, and this being the 'main' method where all Java applications start running we
(of course) only want one starting place.
The void keyword indicates that this method does not return any results to a calling method (again of course in this case considering that the main() method is the first method of all to be called!).

The main() method in Java must take one argument, a string array - in this case called args (short for arguments). The [] brackets after String means that args is an array (a list) of String objects. (Strings are text objects i.e., words). F

Finally the curly bracket at the end of the line { announces the start of a block of code, which in this case will contain the rest of the method. At the end of the method, as well see below, a matching bracket marks the end of the method.

Anyway, all that may have been either trivial or confusing to you. In any case it was all standard Java, let's get onto more specific jMusic stuff.




Score s = new Score("JMDemo1 - Scale");
Part p = new Part("Flute", FLUTE, 0);
Phrase phr = new Phrase("Chromatic Scale", 0.0);
 
Now we are starting to get to the meat of the program. The three lines above are used to initialise the jMusic data types used in Scale.
Scores, Parts, and Phrases can take a string as a constructor argument which sets a title for them, this is rarely used in composing but would be useful in
notational extensions to jMusic which might use the title to display at the top of the score or beside each part in the margin.

The Score created in this program is declared as (named) 's'. So, whenever we want to refer to that score we will use its name.
For example, View.show(s);

The word new is a special Java command. It is regularly used, so get a feel for it. In this line it means than we are creating a new object that is an instance of the Score class; generating a new score. Classes can be like templates from which you can make new versions, called instances; the Score, Part and Phrase Classes are like this. So we create a new Score object, called s that has a title JMDemo1 - Scale.

The Part object also takes two more arguments after the title, an instrument (in this case using one of th JMC constants for General MIDI sounds), and the channel. Notice that we use channel zero, which corresponds to MIDI channel one, because computer programming languages always count from zero.

The Phrase object takes a title and a start time which indicates how far into the piece this Phrase should start playing. 0.0 indicates that it should start playing right from the start.



			for(int i=0;i<12;i++){
Note n = new Note(C4+i, CROTCHET);
phr.addNote(n);
}
This is the work horse section of the program, the part where we actually do something.
This is a simple loop that increments the pitch by one semitone on each iteration.

On each iteration a new note, named n, is created. The new note object has two values set; its pitch and rhythm value.
The pitch of the note is C4 plus the number of times we've been around the loop. C4 is a constant value set in JMC class which represents the MIDI pitch 60 (don't you think that C4 is easier to remember than 60!).
You could just have easily used 60 instead of C4. Because the pitch changes each cycle around the for-loop we end up with a chromatic scale starting from middle C and growing up to the B below C5 in semitone steps.

After the comma the word CROTCHET represents the note's rhythmic value.
The constant CROTCHET (equals a double value 1.0) represents a crotchet [Americans may like to use the QUARTER_NOTE constant instead].
Every note in this program will be a CROTCHET in length.

After we create each note Note n = new Note(...) we place it into the phrase object phr.addNote(n) that we created earlier.
Phrases hold notes (and rests) in the order you enter them.

The for-loop syntax can look a little odd at first. In time you will see this used so often that it will become second nature.
For now it might be useful to see the loop expressed a different way - this code has the same result but its structure is more English-like.

int i = 0;
while(i<12) {
Note n = new Note(C4+i, CROTCHET);
phr.addNote(n);
i++;
}
You can read this as: " i starts as zero. While i is less than 12 execute the loop. Make a new note with a pitch of middle C+i of a crotchet (quarter note) value. Add the note to the phr phrase. Increase the value of i by adding 1 to it. Continue."



			p.addPhrase(phr);
s.addPart(p);
Just as we added new note objects into the phrase object we must enter our newly filled phrase into a part p.addPhrase(phr) and then add the part to our score s.addPart(p) .

			Write.midi(s, "ChromaticScale.mid");
The final section of our scale program creates a MIDI file called ChromaticScale.mid from the Score s.

This line uses the Write class. The Write class has methods for saving jMusic compositions as files to disk. In this case we are using the midi() method of the Write class. This is what the 'dot' indicates; Write.midi() which we saw also in s.addPart()

The Write.midi() method takes two arguments, firstly, the score for exploring exporting to an SMF and, secondly, the name to save the file as (the file name).

The last two elements of the class (not shown) are two curly brackets. One to close the method, the second to close the class.

Whew. . . . There was quite a lot to take in for those new to Java. Following the instructions below to compile and run the application and if you need to re-read the tutorial a few times to take it all in. Then move on.

Compiling and Running

Once you have saved this code into a file called Scale.java you can compile it using the Javac application and produce a Scale.class file.
Once the program is compiled you can run the program using the Java application (on a Macintosh drop the .class file on the JBindery application icon)
and a SMF called ChromaticScale.mid should appear in your current directory. You can then load the SMF into a sequencer and play the file.
If you don't have a MIDI sequencer try using QuickTime's Movieplayer or the Microsoft media player, or on a UNIX machine try typing playmidi scale.mid on the command line.

Congratulations, you have just made your first jMusic piece. Of course jMusic is capable of generating much more complicated music than this,
as complicated as you can imagine in fact, and we shall explore some of these options in further tutorials.


If you are don't understand how everything in this program worked don't worry at all, there are plenty more easy examples to follow and a great way to learn to program is to learn by example.
Do what you can and copy the rest, that's the way we like to program! The important thing is to use the examples as templates for your own explorations. Try using the above example to make a descending scale for example.

If you are inexperienced at programming, you should have an introductory text on programming, ideally one that focuses on the Java language.
If you have a copy of the Computer Music Tutorial (Roads 1996) then read Chapter 2 starting on page 50 for an introduction to computer programming for musicians.

Welcome to jMusic.
 

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