Wind Chimes

This was one of the earliest jMusic pieces, and so is considered a 'classic' in the jMusic community. It is quite simple but effective - like all good music. The class simulates the random-like polyphony of metallic wind chimes. There are several pitches (length of tube) and each employs random rhythms and dynamics.

This is what the result sounds like

Click here to view source

Let's have a closer look.

import jm.JMC;
import jm.midi.*;
import jm.util.*;
/** * A short example which generates a random rhythms at different pitches * and writes to a MIDI file called Windchimes.mid * @author Andrew Brown */
public final class WindChimes implements JMC{

public static void main(String[] args){
Score score = new Score("JMDemo - Wind Chimes");
Part one = new Part("One", 1, BELLS);
Phrase phr1 = new Phrase(0.0);
Phrase phr2 = new Phrase(1.0);
Phrase phr3 = new Phrase(3.0);
Phrase phr4 = new Phrase(5.0);

// create a phrase of random durations up to a semibreve in length.

for(short i=0;i<24;i++){
Note note1 = new Note(c6,
(int)(Math.random()*80 + 20));
Note note2 = new Note(f6,
(int)(Math.random()*80 + 20));
Note note3 = new Note(g5,
(int)(Math.random()*80 + 20));
Note note4 = new Note(d7,
(int)(Math.random()*80 + 20));


// add the phrase to a part

// add part to the score

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

Four phrases area created, each of 24 notes added with random rhythm values in one large for-loop. Notice that the pitches are set, which corresponds to a four ringer wind chime. The rhythm values and dynamic of each note is set using the Math.random() function. This function gives a result between zero and one and is scaled by multiplying as required. An important programming point here is that the arguments passed to methods (such as the Note constructor) can be computed as we go. In previous tutorials we have often used set values.

Each phrase is added to the same part - they all play on the same instrument so there is no need for separate parts.

The part is added to a score which is then translated to a MIDI file format and written to disk.

A sample output from this class is on the jMusic home page as a looped MIDI file. Because of the stochastic nature of the rhythm, little sense of repetition is perceived and the looped music sounds quite continuous. A neat psychoacoutsic trick.