> Music Algorithms > Generating > Modal Melody | ||||||||
Modal MelodyThis demo extends the random melody demo such that notes are constrained to the C major scale. To hear the result play the midi file below. Music in western culture is usually organised around regular patterns or within limited boundaries. One of common constraint is the restricting of pitches to particular modes, the most common of which are Major and Minor. In this example notes are generated at random and then checked to see if they are notes in the C major scale (white notes on the piano). If the generated pitch is within the constraints it is added to the melody. This process is repeated until 24 notes pass the test and are added to the phrase. Charles Dodge and Thomas Jerse discuss the constraining of stochastic methods in music composition in chapter 11 of "Computer Music" (2nd Ed. 1997, Schirmer Books). They deal with statistical constraints while this demo deals with constraints based on music theory. 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 temporary random
number while it is checked to see if it is within the constraints.
Once the random number is generated and multiplied by 127 to spread it across the pitch range it is checked to see if it is the C major scale. The temp%12 code generates the modulus (remainder) of the number divided by 12. This is a useful technique to remember for musical applications where you want to disregard octave position and focus on pitch class set. Each 'successful' temporary pitch is added to the 'phr' phrase, and the counter incremented.
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 modeMelody.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. As an exercise you might try to constrain notes to a minor mode, or to limit notes to a smaller range than the full 127 notes. |
||||||||