JMUSIC.StochasticCMajorJava History

Hide minor edits - Show changes to output

Changed line 16 from:
public final class Stochastic implements JMC
to:
public final class StochasticCMajor implements JMC
Changed lines 21-22 from:
to:
final int NUM_NOTES = 32; // length of melody
Deleted lines 26-27:
final int NUM_NOTES = 32; // length of melody
Changed line 32 from:
// Use C major scale
to:
// Use C major scale
Changed line 34 from:
final int scale[] = MAJOR_SCALE;
to:
final int scale[] = MAJOR_SCALE;
Changed lines 51-52 from:
Double datum = Math.min(Math.random(), max);
to:
double datum = Math.min(Math.random(), max);
Changed line 61 from:
int octave = (int)datum / scale.length;
to:
int octave = (int)datum / scale.length;
Changed lines 64-65 from:
int pitch = tone + (octave * 12);
to:
int pitch = tone + (octave * 12);
Changed lines 67-68 from:
int pitch = pitch + (LOW_OCTAVE * 12);
to:
pitch += (LOW_OCTAVE * 12);
Added lines 3-7:
import jm.JMC;
import jm.music.data.*;
import jm.midi.*;
import jm.util.*;
Added lines 1-78:
(: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 :)

/**
* This short example generates a random melody in C major scale
* and writes to a MIDI file called 'stochy.C-Major.mid'. It demonstrates
* how to map a continuous range of real values to the corresponding
* scale notes within a specified octave range.
*
* @author Andrew Brown (modified by Bill Manaris)
*/
public final class Stochastic implements JMC
{
public static void main(String[] args)
{
final double TEMPO = 80; // the higher, the faster the melody

Score stochScore = new Score("Stochastic melody in C major", TEMPO);
Part inst = new Part("Piano", PIANO, 0);
Phrase phr = new Phrase(0.0);

final int NUM_NOTES = 32; // length of melody

// Specify octave range for sonification
// (Note: there are 11 MIDI octaves, i.e., 0 to 10)
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)

// Use C major scale
// (see http://www.docjar.com/html/api/jm/constants/Scales.java.html)
final int scale[] = MAJOR_SCALE;

// number of distinct notes available for mapping data values
final int MAX_NOTES = scale.length * NUM_OCTAVES;

// 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
// scale note within the specified range.
for (int i = 0; i < NUM_NOTES; i++)
{
// Map a real value from the range 'min' to 'max'
// to a scale note within the specified octave range.

// first, get a data value (random) 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]
datum = (datum - min) * (MAX_NOTES - 1) / (max - min);
// now, datum is a multiple of an index into scale[]

// get corresponding chromatic tone from the scale
int tone = scale[(int)datum % scale.length];

// get octave of datum
int octave = (int)datum / scale.length;

// adjust pitch for octave (12 notes in an octave)
int pitch = tone + (octave * 12);

// shift pitch by specified offset (12 notes in an octave)
int pitch = pitch + (LOW_OCTAVE * 12);

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.C-Major.mid");
}
}


(:sourcend:)