> Music Algorithms > Generating > Drunk (Random Walk) Melody | ||||||||
Drunk (Random Walk) MelodyThis demo creates a series of note pitches with small random intervals between them, notes are constrained to only those in a C major scale. To hear the result play the MIDI file below. The totally random note series is relatively uninteresting, and even
the modally constrained version does not sound musically convincing due
to the wide range of pitches which can result. The random walk is a stochastic
series but the constraint restricts overly large leaps in the melodic
voice. Charles Dodge and Thomas Jerse discuss random walks on pages 365-368
of "Computer Music" (2nd Ed. 1997, Schirmer Books). In this demo two constraints on the randomness of the pitch selection
are used. Lets have a closer look.
Lines 1-4 import useful packages that we need to use in the program. The first import statement gets a standard Java class. The rest of the statements import the jMusic classes (to take another look at the package documentation or work out what gets imported from where look here).
This section declares the class and sets class variables for most levels of the jMusic data structure. A Score object called "ModeScore" is created, as is a part object called "inst" and a phrase object to hold the melody called "phr" which is set to start at the beginning of the piece - beat 0. A variable called 'temp' is declared which will store the previous pitch value. The variable 'offset' stores the random number to be added to the previous note pitch. An array called 'mode' is declared which holds the semitone steps of the degrees of the major scale - 0 being C.
A series of 24 notes is created. Starting from middle C (note 60) a new note up to 7 semitones away from the previous note is randomly chosen. Following this the notes are checked to see if they are within the C major scale.
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.
As with other jMusic demo files, this code creates a MIDI file from the score. Once created the modeDrunk.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. The final line provides feedback during execution that the MIDI file has been successfully written. |
||||||||