JavaSound MIDI Playback
The playback of MIDi files has been
possible by saving the file and using any MIDI player, or by
using the QuickTime player. Version 1.3 of Java comes with the
JavaSound libraies that inlcude MIDI playback. jMusic can now
playback directly using these libraries thanks to Mark Elston's
MidiSynth class.
This tutorial shows how simply you
can access this playback option by using the Play.midi() method in the jm.util package.
Click here to view
source.
Lets have a closer look.
import jm.JMC; import jm.util.*; import jm.music.data.*; import jm.music.tools.Mod; import jm.midi.MidiSynth; public final class MidiSynthTest implements JMC{ public static void main(String[] args){ Score s = new Score(); s.setTempo(130.0); for(int i=0; i<3;i++) { Part p = new Part("part",i*5,i); p.setTempo(120.0 + (double)i * 0.5); for(int j=0; j<1;j++) { Phrase phrase = new Phrase(); phrase = makePhrase(0.0, 50); p.addPhrase(phrase); } s.addPart(p); } Play.midi(s); }
|
Above is the whole file, apart
from the composition method (shown below). Starting from the
bottom the Play.midi(s); line does all the work of interest to
this tutorial. It plays the score "s" via the JavaSound General
MIDI sound set - as simple as that.
The rest of the code is dedicated to
building a score of 3 parts each with one phrase.
private static Phrase makePhrase(double startTime, int length) { Phrase phr = new Phrase(startTime); int pitch = (int)(Math.random()*60+30); for(int i=0; i < length; i++) { pitch += (int)(Math.random()*10-5); Note n = new Note(pitch, CROTCHET, (int)(Math.random()*70 + 30)); phr.addNote(n); } return phr; } }
|
The score to be played is made
up of phrases created by the method above.
|