Midishare Input

Midishare is an open source MIDI input and output library developed by Grame in France. It works on many platforms. 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.

This tutorial shows how to recieve MIDi input into jMusic using Midishare.

Click here to view source.

Lets have a closer look below. The code is extensively commented - so have a read through.

import grame.midishare.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;

/**
*
* @author Rene Wooller
*/
public class MSRecieveTest extends MidiAppl {

/** Creates a new instance of MSRecieveTest */
public MSRecieveTest() {
// initialise everything in the super class
super();

//opening this MidiAppl requires the catching of possible MidiExceptions
try{
//give it the name "msTest". This can be use to identify the app
this.Open("msTest");
} catch(MidiException e) {
//here we print out the stack trace if this.Open throws an exception.
// (a stack is an object which allows objects to be put into it and
// taken out of it, but you can only take out what was most recently
// put in.
// the Java Virtual Machine has a stack to manage all of the objects
// that are running. When an object is finished, it comes off the stack.
// when you print the stacktrace, you can see all of the objects that were
// existing at the time of the error - vey useful for debugging.
e.printStackTrace();
}

// in order for the application to connect Midishare as a Midi Application,
// we need to do this:
Midi.Connect(this.refnum, 0, 1);
Midi.Connect(0, this.refnum, 1);

makeWindow();

}

/*
* this is the special method. You need to put the stuff from the Midishare
* tute in here. I've put a print statement to deomonstrate wether information
* is getting here.
*/
public void ReceiveAlarm(int event) {
System.out.println("event " + event);
}

// make these class variables so that the inner class (action listener) can see them
private MSRecieveTest thisMidiAppl = this;
private JFrame window;
private void makeWindow() {
window = new JFrame();
// an easy way of getting a window to shut down on closing it
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// add a window listener to shut Midishare down if the window is closed
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Midi.Close(thisMidiAppl.refnum);
}
});

// make a button and name it
JButton but = new JButton("close");

//add an actionListener that is executed when it clicks
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Midi.Close(thisMidiAppl.refnum);
window.dispose();
System.exit(0);
}
});

//put it together
JPanel panel = new JPanel();
panel.add(but);
window.getContentPane().add(panel);

//invoke the layoutManager
window.pack();

// make it visible
window.setVisible(true);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new MSRecieveTest();
}

}

More info to come...


Give it a try.