audio MIDI Music Algorithms Interfaces Programming Acoustics Context
> Music Algorithms > Generating > Drunk (Random Walk) Melody    
 
   

Drunk (Random Walk) Melody

This 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.

View / download source

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.
This type of pattern is called a random walk and is common in computer music compositional techniques.

Charles Dodge and Thomas Jerse discuss random walks on pages 365-368 of "Computer Music" (2nd Ed. 1997, Schirmer Books).
They deal also with Markov processes in these pages, a more detailed version of which is availible in further jMusic demos.

In this demo two constraints on the randomness of the pitch selection are used.
First the limiting of pitch intervalic voice leading, and secondly the restriction of pitches to the C major scale (mode).

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 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).

 34     public final class ModeDrunk implements JMC {
 35         public static void main(String[] args){
 36             Score modeScore = new Score("Drunk walk demo");
 37             Part inst = new Part("Bass", SYNTH_BASS, 0);
 38             Phrase phr = new Phrase(); 
 39             int pitch = C3; // variable to store the calculated pitch 
// (initialized with a start pitch value)  40             int offset;

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.
 42             // create a phrase of 32 quavers following 
// a random walk within C minor.  43             for(short i=0;i<32;) {  44             // next note within plus or minus a 5th.  45             offset = 0;  46                 while(offset == 0) {  47                 offset = (int)((Math.random()*14) - 7);   48                 }
// add the offset to the pitch to find the next pitch:
 49                 pitch += offset;  50                               51                 // check that it is a note in the mode using the isScale method.  52                 // several other scale constants are available in the JMC  53                 Note note = new Note(pitch, Q);  54                 if(note.isScale(MINOR_SCALE)) {  55                 phr.addNote(note);  56                     i++;  57                 }  58             }

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.

 61             inst.addPhrase(phr);
 62             modeScore.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.

 65             Write.midi(modeScore, "modeDrunk.mid");
 66         }
 67     }

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.

 

jMusic Australia Council Queensland University of Technology Sitemap Contact Home Home http://www.qut.com http://explodingart.com/jmusic http://www.ozco.gov.au

Digital Instrument making Home