Midishare and jMusic

Midishare is an open source MIDI input and output library developed by Grame in France.  At the moment, Midishare is perhaps the best option for realtime MIDI in jMusic.  As a result the jmms (jmusic to midishare) package has been created in jMusic, so that it is easy to utilise the features of Midishare from jMusic.

For information on downloading and installing the Midishare development kit see the midishare website.  Both the readme and setup.html have important information on the way midishare works.  The easy way is to just copy the relevant drivers etc into your working directory (rather than putting them in any system folder).  You will also need to put grame.jar in your class path to access the midishare libraries.

Once you have that done that, make sure you test the Midishare applications to see if they are working on their own.

Once Midishare is working, it is a relatively simple step to get it working with jMusic.  Just to get some sound happening, you can download this test file which should emit c4 (middle c) every quarter of a second.

Click here to view source.

Lets have a closer look.

/*
* JMMSTester.java
*
* Created on 22 March 2004, 15:20
*/


import jmms.*;
import jm.music.data.*;

/**
*
* @author Rene Wooller
*/


public class MPlayerTester extends MPlayer {

/** Creates a new instance of JMMSTester */
public MPlayerTester() {
}

public Score nextScore() {
System.out.println("making score");
return new Score(new Part(new Phrase(new Note(60, 0.25, 120))));
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
MPlayerTester jt = new MPlayerTester();
jt.go();
}

}


Pretty simple huh?
The important method here is the nextScore() method.  You see how it returns a "Score" object?  That score object is taken away and played via Midishare every 0.25 of a beat.  So, just say you want d4 to play instead of c4, can you guess what bit to change?  Answer is below (62 instead of 60):

    public Score nextScore() {
System.out.println("making score");
return new Score(new Part(new Phrase(new Note(62, 0.25, 120))));
}

Give it a try.

The other important bit is the "extends MPlayer".  This mean that the class inherits some of the features of the MPlayer.  In fact, the nextScore() method is already defined in MPlayer, but we have overwritten it for our own ends.  It was designed like this - if you don't include the nextScore() method, it will actually print out "define the music to play by overwriting the nextScore method".

There are also other methods that you might want to use, such as: getTempo(), setTempo(Tempo t) and setResolution(double d).  Of course because these are methods in MPlayer, you will acess them as this.getTempo(), or simply getTempo() rather than defining an external object.

Ideally you would want to create a graphical user interface to control the formation of the score.  For example, if you want a slider to control the pitch, "just" create a GUI with a slider in it and use the changeListener feature to control a variable that controls the pitch.  You can work out how to do GUIs from the java website, using the forums and the documentation.  If makeing your own GUIs is a bit too much for you at the moment (GUI = Graphical User Interface by the way), there is a jMusic tutorial called helperGUI that makes it fairly easy.  Be careful if you plan to use both HelperGUI and MPlayer at the same time - you can only extend one class at a time in java.  So what you would need to do is copy the contents of MPlayer and make it extend HelperGUI.  You could call it MPlayerHelperGUI.  Then when you extend this one, you are getting the features of both.