> Music Algorithms > Generating > Stochastic Melody | ||||||||
Stochastic MelodyThis demo creates a melody where each note's pitch is randomly determined.
To hear the result play the MIDI file below.
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 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.
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).
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.
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.
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.
|
||||||||