Repeat

Repetition is one of the most fundamental of musical elaborations. In this tutorial we will introduce the repeat() method of the Mod class and show how it can be used to repeat whole phrases or sections of phrases.

Here is an example of the output from the Repeat class.

Below is the source code for the last example.

Click here to view source.

Lets have a closer look.

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

public class RepeatTest implements JMC {
public static void main(String[] args) {
Phrase phr = new Phrase();
// create a random walk melody
int pitch = 60;
for(int i = 0; i< 12; i++) {
Note n = new Note(pitch+= (int)(Math.random() * 8 - 4), SQ *
(int)(Math.random() * 2 + 1) );
phr.addNote(n);
}

// repeat the whole thing three times
Mod.repeat(phr, 3);

// see the result at this stage
View.show(phr);

// repeat beats 2 - 4 three times
Mod.repeat(phr, 3 ,2.0, 4.0);

// see the result of this change
View.show(phr, 50, 50);

// hear the result
Part p = new Part();
Score s = new Score();
p.addPhrase(phr);
s.addPart(p);
s.setTempo(140.0);

Write.midi(s, "Repeat.mid");

Play.midi(s, false);
// false so that it doesn't close everything (the View.show() window
// in particular)


}
}
After creating a phrase from pitches chosen as a random walk - that is, the intervals from one pitch to the next are chosen at random within a range, rather than the pitches themselves being chosen at random. This provides a slightly disorganised melody, which means the role of repetition is more important to creating cohesion to the music.

The phrase is then repeated in total three more more times:

Mod.repeat(phr, 3);

All jMusic data types can be repeated in the same way with the Mod.repeat() methods. Next we display the phrase at that point. This allows you to visualise the repetitions and to compare with the later version of the melody.

A section of the phrase is then repeated. In this example any notes between beats two and four are repeated three more times.

Mod.repeat(phr, 3 ,2.0, 4.0);

This form of the repeat method can be understood to act like repeat marks in a score, where the start repeat mark is at beat 2.0 and the end repeat mark is at beat 4.0. Notice that the method arguments in this method call are an extension of the ones in the more simple repeat call above.

The rest of the code packs the phrase into a score, sets an appropriate tempo, and plays the score using JavaSound.

With Play.midi:  parsing the false boolean stops System.exit(0) from being called.  System.exit(0) closes everything, including window created by View.show().  Play is imported from jm.util.