<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import jm.JMC;
import jm.music.data.*;
import jm.gui.helper.HelperGUI;

/**
 * A short example which generates a chromatic melody
 * which follows the curve of a sine wave.
 * @author Andrew Brown and Rene Wooller
 */
public final class SineMelody extends HelperGUI implements JMC{
    public static void main(String[] args){
        new SineMelody();
    }
    
    public SineMelody() {}
    
    public Score compose() {
        Score score = new Score("Sine melody", 130);
        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;
        // create a phrase of randomly pitched quavers over the full MIDI range.
        //3.14 is an approximate value for pi
        for(int i = 0; i &lt; (2*3.14*density); i++) { 
                int pitch = (int)(Math.sin(i/density) * 50 + 60);
                // Uncomment below to try adding two sine waves together
                //int pitch = (int)((Math.sin(i/density) + Math.sin(i/density*2)) / 2 * 50 + 60);
                Note note = new Note(pitch, THIRTYSECOND_NOTE);
                phr.addNote(note);
        }
        // add the phrase to an instrument and that to a score
        p.addPhrase(phr);
        score.addPart(p);
        return score;
    }
}
</pre></body></html>