<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import jm.music.data.*;
import jm.gui.helper.HelperGUI;

/*
* An elementary use of the markov process for music.
* @author Andrew Sorensen.
*/
public final class BasicMarkov extends HelperGUI{
	public static void main(String[] args){
            new BasicMarkov();
        }
        
        public BasicMarkov() {}
        
        public Score compose() {
		//A map for our musical pitches
		int[] map = {60,62,64,67};
		//A multidimensional array representing our markov matrix	
		double[][] markovMatrix = {{0.0,1.0,0.0,0.0},
			{0.3,0.3,0.4,0.0},
			{0.0,0.45454545,0.45454545,0.09090909},
			{0.0,0.0,0.5,0.5}};
		//The seed for starting output
		int seed = 2; // row 2 of the seed collumn
		//The output as an index value 
		int output = 0;  //there are no outputs yet
		//Score, part and phrase to contain the output
		Score scr = new Score(120.0);
		Part part = new Part("Piano", PIANO, 0);
		Phrase phrase = new Phrase("New melody");

		//Add the seed note to the phrase first
		Note n = new Note();
		n.setPitch(map[seed]);
		phrase.addNote(n);
		
		//Generate notes and add them to a phrase
		for(int i=0;i&lt;30;i++){
			//Retrieve a random number between 0.0 and 1.0
			double choice = Math.random();
			//The current sum of weightings left to right
			double currentSum = 0.0;
			//Check matrix left to right
			for(;output&lt;markovMatrix.length;output++){
				currentSum += markovMatrix[seed][output];
				if(choice &lt;= currentSum){
					break; //break when we have chosen the right number
				}
			}
			Note note = new Note();
			note.setPitch(map[output]);
			phrase.addNote(note);
			//Change the seed to equal the output
			seed = output;
			//Reset the output for the next pass 
			output = 0;
		}
		part.addPhrase(phrase);
		scr.addPart(part);
                return scr;
	}
}



</pre></body></html>