JMSL Tutorial: JScore
Adding algorithmically generated notes to a score

JScore's programmability breaks down into two areas:
  1. Adding algorithmically generated notes to a score
  2. Transforming selected notes in a score

This tutorial demonstrates how to add algorithmically generated notes to a score.

Comment: Here we explicitly add software-generated note events one at a time, specifying their duration.  But JMSL also has a powerful transcriber which quantizes and notates musical events with arbitrary timestamps.  See the transcribe tutorial.

Adding a note to a Score with addNote()

The Score method for adding notes to a score is addNote(). Every call to addNote() appends a new Note onto the end of the current staff of the current measure. Measures are added as needed.

The method signature of addNote() is as follows: public Note addNote(double dur, double pitch, double amp, double hold)

addNote() creates a Note, adds it to the Score, and returns the new Note.

For example:

score.addNote(1.0, NoteFactory.MIDDLE_C, 0.5, 0.8);         // add C qtr note
score.addNote(0.5, NoteFactory.MIDDLE_C+1, 0.5, 0.4);       // add C# 8th note
score.addNote(0.33333, NoteFactory.MIDDLE_C+2, 0.5, 0.2);   // add D 8th note triplet
score.addNote(0.33333, 0, 0, 0.2);                        // add 8th note triplet rest


Setting the insertion point of added notes

Measures are numbered 0..(numMeasures-1). Staves are numbered 0..(numStaves-1). So once you've filled up staff 0 with notes, you can rewind to the beginning of the score and add notes to staff 1, like so:
Notice that you can add as many notes as you like, and measures will automatically get added as needed (inheriting the time sig from the last specified measure). You may also jump to any measure and add notes into it with a call to score.setCurrentMeasureNumber(int n); However it is your responsibility that this measure exists (call score.size() to see how many measures are there).

Manipulating the properties of an added Note: beaming and chords

Since you can get a hold of the note added to the score by addNote(), you can manipulate it. You may set the beaming, for example, or add an interval.
Note note = score.addNote(0.5, NoteFactory.MIDDLE_C+1, 0.5, 0.4);
note.setBeamedOut(true);    // beam to next note
In this demo, we set the beamed out flag on all even numbered notes, so that the notes entered are beamed in pairs. This will save the composer editing time.

You may also build a chord with a call to:

In this demo, we add a random interval above notes, deciding to do so or not with a 1/4 probability. The following line chooses a random interval from a predefined array of int[], and adds it as an interval above the last added note.
int[] possibleIntervals = {3, 4, 7, 9, 12};
note.addInterval(possibleIntervals[JMSLRandom.choose(possibleIntervals.length)] + score.getLastAddedNote().getPitchData());

You need a Java-enabled browser to view this applet.
View the complete source here.
Previous Tutorial Index Tutorial Contents Next


  (C) 2000 Nick Didkovsky and Phil Burk, All Rights Reserved
  JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom.