Inputting Music as Notation

It is often comfortable for musicians to work in common practice notation (CPN) on a staff. For this reason jMusic provides a simple CPN implementation. This tutorial will show a class that is dedicated to using the CPN feature to inout some music for use in jMusic. It displays a stave window allowing CPN input, uses the Play class to play it back, and the music can be saved to disk as a MIDi or jm file for later import into another jMusic class.

Click here to view source.

Let's have a closer look.

import jm.JMC;
import jm.music.data.*;
import jm.midi.*;
import jm.util.*;

The usual suspects are imported. The CPN display and the Play class is in the jm.util package.

public class CPNInput implements JMC{

public static void main(String[] args){
new CPNInput();
}

public CPNInput() {
Score score = new Score();
Part part = new Part();
Phrase phrase = new Phrase();
part.addPhrase(phrase);
score.addPart(part);

View.notate(phrase);

Play.midi(score, false);
}
 
}

The class is declared and a main method is written to call the constructor.

The constructor does all the work in this class, first declaring a Score, Part and Phrase then packing one inside the other as required by the jMusic data structure. The notation window is displayed using the phrase just declared.

Play.midi() is used to playback the score (which contains the part which contains the phrase which contains the notes added by the user!). The 'false' parameter specifies that the System.exit(0) command should NOT be called once the score has finished playing.   (Whenever the System.exit(0) command is called the entire program exits, no matter where the call was made from.)

To save a phrase to disk select "Save as MIDI..." from the CPN menu. You will be prompted for a name and location for the MIDI file. That MIDI file can them be imported into a future jMusic program as part of a composition.

When you get confident, try writing a jMusic composition program that incorporates both notation input and saving as in this class, along side code that reads MIDI files and creates a piece with them.



J Music Tutorial Index