JMUSIC.StochasticJava History

Hide minor edits - Show changes to output

Changed line 9 from:
* This short example generates a random melody and writes to a MIDI file
to:
* This short example generates a random melody and writes it to a MIDI file
Changed lines 20-21 from:
to:
final int NUM_NOTES = 32; // length of melody
Deleted lines 25-26:
final int NUM_NOTES = 32; // length of melody
Changed line 21 from:
Score stochScore = new Score("Stochastic melody in C major", TEMPO);
to:
Score stochScore = new Score("Stochastic melody", TEMPO);
Changed lines 1-2 from:
(:source lang=Java header="This program demonstrates how to create a random melody in the C major scale within the 4th, 5th, and 6th octave." tabwidth=3 -trim :)
to:
(:source lang=Java header="This program demonstrates how to create a random melody within the 4th, 5th, and 6th octave." tabwidth=3 -trim :)
Changed lines 9-13 from:
* This short example generates a random melody in C major scale
* and writes to a MIDI file called
'stochy.mid'. It demonstrates
* how to map a continuous range of real values to the corresponding
* scale
notes within a specified octave range.
*
to:
* This short example generates a random melody and writes to a MIDI file
* called
'stochy.mid'. It demonstrates how to map a continuous range of
* real values to the corresponding discrete
notes within a specified range.
*
Changed lines 19-21 from:
Score stochScore = new Score("Stochastic melody in C major");
to:
final double TEMPO = 80; // the higher, the faster the melody

Score stochScore = new Score(
"Stochastic melody in C major", TEMPO);
Changed lines 23-26 from:
Phrase phr = new Phrase(0.0);

final int scale[] = MAJOR_SCALE; // C major scale
to:
Phrase phr = new Phrase(0.0);
Changed lines 27-32 from:
// There are 11 MIDI octaves (0 to 10)
final int LOW_OCTAVE = 4; // start at 4th octave, and extend
final int NUM_OCTAVES = 3; // ...three octaves (e.g., C4, C5, C6)

// create a phrase of NUM_NOTES eigth notes picking a random
// scale note within the specified range.
to:
// Specify octave range for sonification
// (Note: there are 11 MIDI octaves, i.e., 0 to 10
// see http://www.midimountain.com/midi/midi_note_numbers.html)
final int LOW_OCTAVE = 4; // start at 4th octave, and extend
final int NUM_OCTAVES = 3; // ...three octaves (i.e., C4..B4, C5..B5, C6..B6)

// number of distinct notes available for mapping data values
// (12 notes in an octave)
final int MAX_NOTES = NUM_OCTAVES * 12;

// specify range of data values (i.e., min and max)
Double min = 0.0; // minimum value in data
Double max = 0.5; // maximum value in data

// create a phrase of NUM_NOTES eigth notes picking a random
// data values
within the specified range.
Changed lines 45-62 from:
/** NOTE: The following maps a real value
* from the range [0.0 to 0.99999...]
* to a scale note within the specified range.
*/

// get a random value between 0 and (number of scales notes - 1)
int j = (int)(Math.random() * scale.length);
int pitch = scale[j]; // get a random pitch from the scale
//System.out.println(" Scale degree = " + pitch);

// get a random octave from given range
int octave = (int)(Math.random() * NUM_OCTAVES);
//System.out.println(" Octave = " + octave);

// map pitch to the corresponding note
pitch += (octave + LOW_OCTAVE + 1) * 12;
//System.out.println(" Pitch = " + pitch);
to:
// Map a data value from range ['min' to 'max']
// to the corresponding note

// get a random data value (within the specified data range)
Double datum = Math.min(Math.random(), max);

// map data value to the range of available notes [0 to MAX_NOTES - 1]
int pitch = (int) ((datum - min) * (MAX_NOTES - 1) / (max - min));

// shift pitch by specified offset (12 notes in an octave)
pitch += (LOW_OCTAVE * 12);
Changed line 60 from:
to:
Changed line 63 from:
to:
Changed line 50 from:
// map pitch to the coresponding note
to:
// map pitch to the corresponding note
Changed lines 36-39 from:
/** NOTE: The following maps a real value from the range
* [0.0 to 0.99999...] to a scale note
* within the specified range.
*/
to:
/** NOTE: The following maps a real value
* from the range [0.0 to 0.99999...]
* to a scale note within the specified range.
*/
Changed lines 37-38 from:
* [0.0 to 0.99999...] to a scale note within the specified range.
to:
* [0.0 to 0.99999...] to a scale note
*
within the specified range.
Changed lines 32-33 from:
// create a phrase of NUM_NOTES eigth notes picking a random scale note
// within the specified range.
to:
// create a phrase of NUM_NOTES eigth notes picking a random
// scale note within the specified range.
Changed lines 5-6 from:
import jm.util.Write;
to:
import jm.midi.*;
import jm.util.*
;
Changed lines 9-10 from:
* This short example generates a random melody in the C major scale
* and writes to a MIDI file called 'stochy.mid'.
to:
* This short example generates a random melody in C major scale
* and writes to a MIDI file called 'stochy.mid'. It demonstrates
* how to map a continuous range of real values to the corresponding
* scale notes within a specified octave range.
*
Changed line 16 from:
public final class Stochastic implements JMC, Scales
to:
public final class Stochastic implements JMC
Changed lines 23-29 from:
final int START_OCTAVE = 4; // holds lowest octave of melody
final int OCTAVE_RANGE = 3; // holds octave range of melody

// create a phrase of 24 randomly pitched eigth notes in C major
// over the full MIDI range.
for (int i = 0; i < 24; i++)
to:

final int scale[] = MAJOR_SCALE; // C major scale

final int NUM_NOTES = 32; // length of melody

// There are 11 MIDI octaves (0 to 10)
final int LOW_OCTAVE = 4; // start at 4th octave, and extend
final int NUM_OCTAVES = 3; // ...three octaves (e.g., C4, C5, C6)

// create a phrase of NUM_NOTES eigth notes picking a random scale note
// within the specified range.
for (int i = 0; i < NUM_NOTES
; i++)
Changed lines 36-40 from:
// there are seven pitches in the C major scale, so
// get a random value between 0 and 6
int j = (int)(Math.random() * 7);
int pitch = MAJOR_SCALE(j); // get a random C major pitch
to:
/** NOTE: The following maps a real value from the range
* [0.0 to 0.99999...] to a scale note within the specified range.
*/

// get a random value between 0 and (number of scales notes - 1)
int j = (int)(Math.random() * scale.length);
int
pitch = scale[j]; // get a random pitch from the scale
//System.out.println(" Scale degree = " + pitch);
Changed lines 46-52 from:
int octave = (int)(Math.random() * OCTAVE_RANGE);

// map pitch to the desired range
pitch = (pitch * (12 * octave)) + START_OCTAVE;

// System.out.println(" Pitch = ", pitch);
to:
int octave = (int)(Math.random() * NUM_OCTAVES);
//System.out.println(" Octave = " + octave);

// map pitch to the coresponding note
pitch += (octave + LOW_OCTAVE + 1) * 12;
//System.out.println(" Pitch = " +
pitch);
Added line 65:
Added lines 1-52:
(:source lang=Java header="This program demonstrates how to create a random melody in the C major scale within the 4th, 5th, and 6th octave." tabwidth=3 -trim :)

import jm.JMC;
import jm.music.data.*;
import jm.util.Write;

/**
* This short example generates a random melody in the C major scale
* and writes to a MIDI file called 'stochy.mid'.
* @author Andrew Brown (modified by Bill Manaris)
*/
public final class Stochastic implements JMC, Scales
{
public static void main(String[] args)
{
Score stochScore = new Score("Stochastic melody in C major");
Part inst = new Part("Piano", PIANO, 0);
Phrase phr = new Phrase(0.0);

final int START_OCTAVE = 4; // holds lowest octave of melody
final int OCTAVE_RANGE = 3; // holds octave range of melody

// create a phrase of 24 randomly pitched eigth notes in C major
// over the full MIDI range.
for (int i = 0; i < 24; i++)
{
// there are seven pitches in the C major scale, so
// get a random value between 0 and 6
int j = (int)(Math.random() * 7);
int pitch = MAJOR_SCALE(j); // get a random C major pitch

// get a random octave from given range
int octave = (int)(Math.random() * OCTAVE_RANGE);

// map pitch to the desired range
pitch = (pitch * (12 * octave)) + START_OCTAVE;

// System.out.println(" Pitch = ", pitch);

Note note = new Note(pitch, EN); // create a note object
phr.addNote(note); // add it to the phrase
}

inst.addPhrase(phr); // add the phrase to an instrument (part)
stochScore.addPart(inst); // add the instrument to the score

// create a MIDI file of the score
Write.midi(stochScore, "stochy.mid");
}
}

(:sourcend:)