JMUSIC»My Favorite Melody Java

My Favorite Melody Java

This program demonstrates how to create more interesting music.

import jm.JMC;
import jm.util.*;

 * Plays a melody as a round in three parts
 * @author Andrew Sorensen and Andrew Brown (modified by Bill Manaris)

public final class MyFavoriteMelody implements JMC
   public static void main(String[] args)
      // Create the data objects we want to use
      Score score = new Score("Row Your Boat");

      // Parts can have a name, instrument, and channel.
      Part flute = new Part("Flute", FLUTE, 0);
      Part trumpet = new Part("Trumpet", TRUMPET, 1);
      Part clarinet = new Part("Clarinet", CLARINET, 2);

      // Let's write the music in a convenient way...
      // "Row, row, row your boat gently down the stream"
      int[] pitches1 =    {C4, C4, C4, D4, E4, E4, D4, E4, F4, G4};
      double[] rhythms1 = {QN, QN, EN, EN, QN, EN, EN, EN, EN, HN};
      // "merrily, merrily, merrily, merrily"
      int[] pitches2 =    {C5, C5, G4, G4, E4, E4, C4, C4};
      double[] rhythms2 = {EN, EN, EN, EN, EN, EN, EN, EN};
      // "life is but a dream."
      int[] pitches3 =    {G4, F4, E4, D4, C4};
      double[] rhythms3 = {EN, EN, EN, EN, HN};

      // add the notes to a phrase
      Phrase phrase1 = new Phrase(0.0);
      phrase1.addNoteList(pitches1, rhythms1);
      phrase1.addNoteList(pitches2, rhythms2);
      phrase1.addNoteList(pitches3, rhythms3);

      // make two new phrases and change start times to make a round
      Phrase phrase2 = phrase1.copy();
      phrase2.setStartTime(4.0);        // four beats into the piece
      Phrase phrase3 = phrase1.copy();
      phrase3.setStartTime(8.0);        // eight beats into the piece

      // play different parts in different octaves
      Mod.transpose(phrase1, 12);
      Mod.transpose(phrase3, -12);

      // repeat each phrase a second time
      Mod.repeat(phrase1, 2);
      Mod.repeat(phrase2, 2);
      Mod.repeat(phrase3, 2);

      // add phrases to the parts

      // add parts to the score

      // play the score as MIDI (and exit, since JavaSound does not
      // shut down properly)
      //Play.midi(score, true);

      // display the score in a music notation (CPN) window

      // write the score to a MIDI file
      Write.midi(score, "rowboat.mid");