audio MIDI Music Algorithms Interfaces Programming Acoustics Context
> Music Algorithms > Generating > Sinewave Melodic Contour    
 
   

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!):

Sine Melody

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.

/**
* A short example which generates a chromatic melody
* which follows the curve of a sine wave
* and writes to a MIDI file
* @author Andrew R. Brown and Rene Wooller
*/

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();
// the density is the resolution of notes over the sine wave function
// a lower density produces a shorter melody - more course resolution.

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.

               	// create a phrase of randomly pitched quavers over the full MIDI range.
for(int i=0;i<(2*3.14*density);i++){ /* 3.14 is an approximate
value for pi */

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.

               	// add the phrase to an instrument and that to a score
p.addPhrase(phr);
s.addPart(p);
//display the output

View.show(s);

// listen to the output using javaSound - and don't exit

Play.midi(s, false);

// create a MIDI file of the score

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!

 

jMusic Australia Council Queensland University of Technology Sitemap Contact Home Home http://www.qut.com http://explodingart.com/jmusic http://www.ozco.gov.au

Digital Instrument making Home