Dots and Dashes 1

This is the first in a series of tutorials that introduces composing in jMusic. Musical notes are often talked about as 'dots' and when displayed on a time line they can appear as 'dashes' which indicate their length. jMusic is a kind of code for describing music, in the same way that the dots and dashes that comprise semaphore is a code for describing letters of the alphabet. This series of tutorials will introduce you to both the musical and programming concepts behind jMusic in alternating tutorials.

Here is the source file for this class. Save this file to disk to compile and run.

Dot01.java

This program is very simple. It creates one jMusic note and displays it as common practice notation.

Compile and run the class before reading on so you can see what it does.

Let's have a closer look.
import jm.music.data.*;
import jm.JMC;
import jm.util.*;
 
public class Dot01 implements JMC {
public static void main(String[] args) {
Note n;
n = new Note(C4, QUARTER_NOTE);
Phrase phr = new Phrase();
phr.addNote(n);

View.notate(phr);
}
}

A brief description:

The main method of this class contains four lines. The first three declare a note and phrase, then add the note to the phrase. The fourth line calls the jMusic notation display to show the phrase.

More detail:

Note n;

n = new Note(C4, QUARTER_NOTE);

Programming langauges classsify objects by 'type', for example all whole numbers are of type integer. In jMusic we declare some musical types. An object of type Note is being declared here. The object is called 'n.' In the first line n is declared as being of type Note (an instance of the Note class). In the second line n is instantiated (created) and given values. Objects are created with the Java keyword new, as in new Note(). The Note constructor takes two arguments, a pitch and a rhythm value. In this case jMusic constants are used for both; C4 is middle C or MIDI note value 60, and QUARTER_NOTE equals the value 1.0 beats. All jMusic constants are in upper case. Constants make the code easier to read but the values can be replaced by valid numbers if you prefer.

Phrase phrase = new Phrase();

As with the previous line an object is being created. In this case jMusic Phrase object named 'phr'. The two lines above are combined into a more convenienet one line statement that both declares and instantiates the Phrase object. Notice that Java is case senstive, that is, Phrase with a capital P is different from phrase. It is a convention is Java to name classes with a capital letter, as with the Phrase class, and to name variables starting with a lower case letter, as with phr. The new keyword is again used and the Phrase constructor takes no arguments in this example. This means that it will use default values.

phr.addNote(n);

Now that we have a note and a phrase, we can add the note to the phrase. The musical phrase now has just one note - trivial but important to understand. A jMusic Phrase can contain any number of notes and they are stored, displayed and played in the order in which they are added, one after the other. In jMusic a Phrase is monophonic. In this line of code the Phrase object we created, phr, uses its addNote() method. A method is a function or procedure. Each object inherits the methods of its class, so the 'phr' object inherits the addNote() method from the Phrase class. Notice that we access the method using 'dot' notation (not to be confused with the name of this tutorial!). As in phr[DOT]addNote(n). Which you can read as, "run the addNote() method of the phr object and use the object n as an argument."

View.notate(phr);

This line displays the note as manuscript. The View class is one of the jMusic utility classes and is used to invoke one of the many and varied visualisations of music in jMusic. In this case its notate() method is used to display CPN and the Phrase object 'phr' is passed to the method for displaying.

Notice in this last line that we can use the View class directly rather than having to create a instance of it, as we had to for Note and Phrase. That is because the View class is a static class, there is only ever one copy of it, the class is directly called itself. Classes that we need lots of instances of, like Note that we need lots of, are not static (most classes are like this) but other classes that perform specific functions can be static. This difference may seem subtle or unclear at present but will become more familiar over time working with Java - fear not :)


jMusic Tutorial Index