Sinewave Melodic
Contour
This tutorial shows how mathematical functions can be used
to create melodies.
Click here to view source.
Here is the sound output
Want to hear it go faster? with more range? Well you should
be able to work out how to do it pretty easily.
The GUI will look similar to this (uncanny isn't it!):
Let's have a closer look at how its done.
import jm.JMC; import jm.music.data.*; import qt.QTPlayer; import jm.util.View; import jm.util.Write;
|
This section covers the importing of the usual jMusic classes.
The show() method that
is responsible for the picture above is found within jm.util.View
class.
public final class SineMelody implements JMC{ public static void main(String[] args){ Score s = new Score("JMDemo - Sine melody"); Part p = new Part("Melody", MARIMBA, 0); Phrase phr = new Phrase(); double density = 25.0;
|
This section creates the SineMelody class, and starts off its main method
by creating an empty Score, Part and Phrase.
The density variable is also set to define the resolution of notes.
for(int i=0;i<(2*3.14*density);i++){ int pitch = (int)(Math.sin(i/density)*50+60); Note note = new Note(pitch, DSQ); phr.addNote(note); }
|
OK, so here is the fun maths bit that does all the hard work.
Look complicated? Not if you paid attention in high school maths!
Remember, the formula for the circumference of a circle is 2*pi*r (pronounced
"two pie arrrr" :-). Taking the density variable as r (the circle's radius),
this formula is entered into the for() statement. Then, a pitch is calculated
using Java's inbuilt Math.sin() method, and a new note is created using
this pitch and added into the phrase.
p.addPhrase(phr); s.addPart(p); View.show(s); Play.midi(s, false); Write.midi(s, "SineMelody.mid"); } }
|
The end of the SineMelody class should look very familiar.
Here, we finish up by adding the new phrase containing our Sine Melody
into a Part, and added this to the Score.
Show is used to pop up a GUI so we can see the fruits of our efforts,
playQT is called to pup up a player to hear the music, and the MIDI file
"SineMelody.mid" is created. Now imagine what a tan wave would sound like!
|