Stochastic Melody

This demo creates a melody where each note's pitch is randomly determined. This is an example of Stochastic music - that is, music which is not predetermined but has elements that are set at random, or by some probability function. Each time a stochastic piece is 'run' (performed) the output will be different in an unpredictable way.

To hear the result play the MIDI file below.

View source

Download source

When music is performed some aspects are determined in advance, such as the note pitches and rhythms in the score, and other aspects vary at each performance, such as tempo, dynamic, and phrasing. When generating music in jMusic it is also possible to make some aspects predictable (deterministic) and other aspects unpredictable (stochastic or aleatoric). In this demo the note pitches are selected at random. Randomness is one of the simplest (but not most musical!) stochastic functions to implement. All other note attributes, such as duration and loudness are constantly set to the jMusic defaults.

For a more comprehensive discussion of composition with aleatoric processes read chapter 11 of "Computer Music" (2nd Ed. 1997, Schirmer Books) by Charles Dodge and Thomas Jerse.

Lets have a closer look. 
 23     import jm.JMC;
 24     import jm.music.data.*;
 25     import jm.midi.*;
 26     import jm.util.*;
Lines 1-4 import useful packages that we need to use in the program. The statements import the jMusic classes (to take another look at the package documentation or work out what gets imported from where look at the -> PACKAGES).
 33     public final class Stochastic implements JMC {
 34             public static void main(String[] args){
 35                     Score stochScore = new Score("JMDemo - Stochastic");
 36                     Part inst = new Part("Piano", PIANO , 0);
 37                     Phrase phr = new Phrase(); 
This section declares the class and sets class variables for most levels of the jMusic data structure. A Score object called "stochScore" is created, as is a part object called "inst" and a phrase object to hold the melody called "phr" which (by default) is set to start at the beginning of the piece &emdash; beat 0.
 39                     // create a phrase of randomly pitched quavers over the full MIDI range.
 40                     for(int i=0;i<32;i++){
 41                             Note note = new Note((int)(Math.random()*127), Q);
 42                             phr.addNote(note);
 43                     }
A phrase of 32 notes is created. Each note is generated by the Math.random() method which returns a value between 0 and 1. In order to scale this value across the set of chromatic note pitches used by jMusic (and MIDI) the random number is multiplied by 127. The (int) command just before the random number converts the result of the random calculation to the type 'int' as required by jMusic for pitches. This also rounds the calculation in the process. In this code class, the note constructor is used, which generically has default values for all aspects of the note except its pitch.
 46                     inst.addPhrase(phr);
 47                     stochScore.addPart(inst);
The phrase is added to a part, which in turn is allocated to the score. A score object is required for passing to the MIDI file conversion class. The addPhrase and addPart methods do the work.
 50                     Write.midi(stochScore, "stochy.mid");
 51             }
 52     }
As with other jMusic demo files, this code creates a MIDI file from the score. Once created the stochy.mid file can be played by any MIDI file player and will sound correctly using a General MIDI sound source, such as most PC sound cards, or Quicktime.


jMusic Tutorial Index