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 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.*;
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;
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 }
61 inst.addPhrase(phr); 62 modeScore.addPart(inst);
65 Write.midi(modeScore, "modeDrunk.mid"); 66 } 67 }