Letter Melody
This is an example of rule based
composition. The class produces a melody where the note pitch
is based on text typed into the program. It simply maps lower
case 'a' to the note A above middle C and so on for all letters
between a - g. A comma (,) is used to indicate a rest. In this
way different melodies are generated by different sentences.
This is what the result of this
tutorial file can sound like. The sentence typed was 'every good
boy deserves fish and chips':
Click here to view
the source file..
The Guido Word Music tutorial is based upon this idea but is
much more comprehensive.
Let's have a closer look:
import jm.JMC; import jm.music.data.*; import jm.music.tools.*; import jm.midi.*; import jm.util.*;
|
The classes needed for jMusic
are imported for use.
public class LetterMelody implements JMC{ public static void main(String[] args){ Phrase phr = new Phrase(); Part p = new Part(); Score s = new Score();
|
The class is declared and the
main method started. The jMusic data types of score, part, and
phrase are declared using default values (no arguments) for
convenience. Often the String[] array called args is not used,
but in this example it is. When the program is run a sentence
is typed as an argument. That sentence is used by the program
to generate the melody.
for(int i=0; i<args.length;i++) { System.out.println("Word "+i+" is "+args[i]); String word = args[i]; for(int j=0;j<word.length();j++) { System.out.println("Letter "+j+" is "+ word.charAt(j)); if(word.charAt(j) == 'a') { Note n = new Note(a4,Q); phr.addNote(n); } if(word.charAt(j) == 'b') { Note n = new Note(b4,Q); phr.addNote(n); } if(word.charAt(j) == 'c') { Note n = new Note(c4,Q); phr.addNote(n); } if(word.charAt(j) == 'd') { Note n = new Note(d4,Q); phr.addNote(n); } if(word.charAt(j) == 'e') { Note n = new Note(e4,Q); phr.addNote(n); } if(word.charAt(j) == 'f') { Note n = new Note(f4,Q); phr.addNote(n); } if(word.charAt(j) == 'a') { Note n = new Note(g4,Q); phr.addNote(n); } if(word.charAt(j) == ',') { Note n = new Note(REST,Q); phr.addNote(n); } } }
|
This is the heart of the program. The
string array (args) is cycled through, word by word. Each word is cycled
through character by character. To ensure that all is working there are
System.out.println statements outputting the words and letters.
Each letter is checked by a series
of 'if' statements. this is the basis of rule-based composition. Each
'if' statement represents a rule, and each rule is checked. You can
easily add additional rules. In this case lower case characters
between a and g result in a new note being added to the phrase.
The last rule adds a rest if the
comma is found.
p.addPhrase(phr); s.addPart(p); Write.midi(s, "LetterMelody.mid"); View.show(s); } }
|
The phrase is packed in to a
part, which is packed into a score. The score is written to a
MIDI file and displayed on screen.
Take a break (on jMusic)
Congratulations on getting this far.
At this stage of the tutorial you know enough to work out this
next demo on your own. have a listen, then download the java
file and look through it yourself. We've made the MIDI file
below loop so you can have the soothing tones of stochastic music sooth
you while you examine the code.
To hear the result play the MIDI
file below.
To keep it playing while you read,
open the link below in a new browser window.
Click here to view
source
|