Japanese

MDPW for Java

Install

Note

Sequence

Chord

Music piece

Visitor


Download
mdpw30.zip

Back

Install

Download mdpw30.zip and extract it. You will find mdpw.jar. Add the jar file to your Java development environment. There are at least three ways.
  • Use java, javac classpath argument.
  • Set CLASSPATH Environmental variable.
  • Put the jar file in jre/lib/ext folder(directory).

Top of this page| Next step

How to make a Note

I will show you an example of only one note first. You will understand how to use "Player Class". The arguments of :mn represent pitch(0-127), duration(positive double), velocity(-64 - 63) and program number (0 - 127, 129) respectively.
import com.cwbp.mdpw.*;
public class tNote {
  public static void main(String[] a){
    // Create Player class
    Player p = new MIDIFilePlayer();
    // file name to output
    p.init("tNote.mid");
    // Set tempo
    p.setTempo(120);
    // Create one note and play
    p.play(new DataNote(60, 1, 64, 1));
    // Do write to the file.
    p.end();
  }
}

Top of this page| Next step

How to make a Sequence

You can make a sequnece of notes by DataSequence class. Create a instance and add notes to it. If you omitt arguments of DataNote, the valeus of the note before are applied.
import com.cwbp.mdpw.*;
public class tSeq {
  public static void main(String[] a){
    Player p = new MIDIFilePlayer();
    // Create Sequence
    DataSequence s = new DataSequence();
    // Add notes to the sequence
    s.addElement(new DataNote(60, 1, 64, 0));//C
    s.addElement(new DataNote(62, 1));       //D
    s.addElement(new DataNote(64, 1));       //E
    // Write to a file
    p.init("tSeq.mid");
    p.setTempo(120);
    p.play(s);
    p.end();
  }
}

Top of this page| Next step

How to make a Chord

You can make a chord by DataChord class. If you omit arguments of DataNote, the valeus of the note before are applied.
import com.cwbp.mdpw.*;
public class tChord {
  public static void main(String[] a){
    Player p = new MIDIFilePlayer();
    DataChord c = new DataChord();
    c.addElement(new DataNote(60, 1, 64, 0));//C
    c.addElement(new DataNote(64, 1));       //E
    c.addElement(new DataNote(67, 1));       //G
    p.init("tChord.mid");
    p.setTempo(120);
    p.play(c);
    p.end();
  }
}

Top of this page| Next step

How to make a musical piece

You can make a musical piece by sequences and chords. Both can contain both. You can make a seqence as the melody, a sequence of chords as the accompaniment and a chord of those two. And you will find how to repeat by repeated invocation of play method of Player class.
import com.cwbp.mdpw.*;
public class tSample {
  public static void main(String[] a){
    Player p = new MIDIFilePlayer();
    DataChord c = new DataChord();           //Chord:C
    c.addElement(new DataNote(60, 2, 64, 0));
    c.addElement(new DataNote(64, 2));
    c.addElement(new DataNote(67, 2));
    DataSequence b = new DataSequence();     //Chord part
    b.addElement(c);
    b.addElement(c);
    DataSequence s = new DataSequence();     //Melody
    s.addElement(new DataNote(60, 1, 80, 0));
    s.addElement(new DataNote(62, 1));
    s.addElement(new DataNote(64, 1));
    s.addElement(new DataNote(64, 1, 0));    //Rest
    DataChord m = new DataChord();           //Phrase 1
    m.addElement(s);
    m.addElement(b);
    p.init("tSample.mid");
    p.setTempo(120);
    p.play(m);                              //Repeat 2 times
    p.play(m);
    p.end();
  }
}

Top of this page| Next step

Visitor

This sample implements the "Crescendo" visitor. You can implement how to proccess the notes at the method "proccess". The first argument is the note to be proccessed and the second argument is the timing the note in the sequence.
import com.cwbp.mdpw.*;
public class tVisitorCrescendo implements Visitor {
	double duration;
	int startVelocity;
	int endVelocity;
	public tVisitorCrescendo(double d, int s, int e){
	  duration = d;
	  startVelocity = s;
	  endVelocity = e;
	}
   public void process(DataNote n, double t){
   	  n.setVelocity((int)((endVelocity-startVelocity)*t/duration+startVelocity));
   	  System.out.println("tVisitorCrescendo:"+n.getVelocity());
   }

  public static void main(String[] a){
    Player p = new MIDIFilePlayer();
    DataSequence s = new DataSequence();
    s.addElement(new DataNote(60, 2, 32, 0));
    s.addElement(new DataNote(62));
    s.addElement(new DataNote(64));
    s.addElement(new DataNote(65));
  	tVisitorCrescendo v = new tVisitorCrescendo(s.getDuration(), 16, 89);
  	s.visit(v);
  	
    p.init("tVisitor.mid");
    p.setTempo(120);
    p.play(s);
    p.end();
  }
}
Are you inspired? Enjoy Music Programing.
Top of this page