How can I run this java program? Please help.
I have jmf installed, sunone running , the file may be in the wrong directory.
It is in some directory nowhere near the java lang dir.
When I try to compile or build i cant. I'm off on a basic thing.
here is the code.
importjavax.sound.midi.InvalidMidiDataException;
importjavax.sound.midi.MidiDevice;
importjavax.sound.midi.MidiSystem;
importjavax.sound.midi.MidiUnavailableException;
importjavax.sound.midi.Receiver;
importjavax.sound.midi.MidiMessage;
importjavax.sound.midi.ShortMessage;
importjavax.sound.midi.SysexMessage;
public class MidiNote
{
/**Flag for debugging messages.
If true, some messages are dumped to the console
during operation.
*/
private static booleanDEBUG = false;
public static void main(String[] args)
{
try {
// TODO: make settable via command line
intnChannel = 0;
intnKey = 0;// MIDI key number
intnVelocity = 0;
/*
*Time between note on and note off event in
*milliseconds. Note that on most systems, the
*best resolution you can expect are 10 ms.
*/
intnDuration = 0;
intnArgumentIndexOffset = 0;
StringstrDeviceName = null;
if (args.length == 4)
{
strDeviceName = args[0];
nArgumentIndexOffset = 1;
}
else if (args.length == 3)
{
nArgumentIndexOffset = 0;
}
else
{
printUsageAndExit();
}
nKey = Integer.parseInt(args[0 + nArgumentIndexOffset]);
nKey = Math.min(127, Math.max(0, nKey));
nVelocity = Integer.parseInt(args[1 + nArgumentIndexOffset]);
nVelocity = Math.min(127, Math.max(0, nVelocity));
nDuration = Integer.parseInt(args[2 + nArgumentIndexOffset]);
nDuration = Math.max(0, nDuration);
MidiDeviceoutputDevice = null;
Receiverreceiver = null;
if (strDeviceName != null)
{
MidiDevice.Infoinfo = getMidiDeviceInfo(strDeviceName, true);
if (info == null)
{
out("no device info found for name " + strDeviceName);
System.exit(1);
}
try
{
outputDevice = MidiSystem.getMidiDevice(info);
outputDevice.open();
}
catch (MidiUnavailableException e)
{
if (DEBUG) { out(e); }
}
if (outputDevice == null)
{
out("wasn't able to retrieve MidiDevice");
System.exit(1);
}
try
{
receiver = outputDevice.getReceiver();
}
catch (MidiUnavailableException e)
{
if (DEBUG) { out(e); }
}
}
else
{
/*We retrieve a Receiver for the default
MidiDevice.
*/
try
{
receiver = MidiSystem.getReceiver();
}
catch (MidiUnavailableException e)
{
if (DEBUG) { out(e); }
}
}
if (receiver == null)
{
out("wasn't able to retrieve Receiver");
System.exit(1);
}
/*Here, we prepare the MIDI messages to send.
Obviously, one is for turning the key on and
one for turning it off.
*/
MidiMessageonMessage = null;
MidiMessageoffMessage = null;
try
{
onMessage = new ShortMessage();
offMessage = new ShortMessage();
((ShortMessage) onMessage).setMessage(ShortMessage.NOTE_ON, nChannel, nKey, nVelocity);
((ShortMessage) offMessage).setMessage(ShortMessage.NOTE_OFF, nChannel, nKey);
/* test for SysEx messages */
//byte[] data = { (byte) 0xF0, (byte) 0xF7, (byte) 0x99, 0x40, 0x7F, 0x40, 0x00 };
//onMessage = new SysexMessage();
//offMessage = new SysexMessage();
//onMessage.setMessage(data, data.length);
//offMessage = (SysexMessage) onMessage.clone();
}
catch (InvalidMidiDataException e)
{
if (DEBUG) { out(e); }
}
/*
*Turn the note on
*/
receiver.send(onMessage, -1);
/*
*Wait for the specified amount of time
*(the duration of the note).
*/
try
{
Thread.sleep(nDuration);
}
catch (InterruptedException e)
{
if (DEBUG) { out(e); }
}
/*
*Turn the note off.
*/
receiver.send(offMessage, -1);
/*
*Clean up.
*/
receiver.close();
if (outputDevice != null)
{
outputDevice.close();
}
} catch (Throwable t) {
out(t);
}
System.exit(0);
}
private static void printUsageAndExit()
{
out("MidiNote: usage:");
out(" java MidiNote [<device name>] <note number> <velocity> <duration>");
out("<device name>\toutput to named device");
out("-D\tenables debugging output");
System.exit(1);
}
private static void listDevicesAndExit(boolean forInput, boolean forOutput) {
if (forInput && !forOutput) {
out("Available MIDI IN Devices:");
}
else if (!forInput && forOutput) {
out("Available MIDI OUT Devices:");
} else {
out("Available MIDI Devices:");
}
MidiDevice.Info[]aInfos = MidiSystem.getMidiDeviceInfo();
for (int i = 0; i < aInfos.length; i++) {
try {
MidiDevicedevice = MidiSystem.getMidiDevice(aInfos);
booleanbAllowsInput = (device.getMaxTransmitters() != 0);
booleanbAllowsOutput = (device.getMaxReceivers() != 0);
if ((bAllowsInput && forInput) || (bAllowsOutput && forOutput)) {
out(""+i+" "
+(bAllowsInput?"IN ":"")
+(bAllowsOutput?"OUT ":"")
+aInfos.getName()+", "
+aInfos.getVendor()+", "
+aInfos.getVersion()+", "
+aInfos.getDescription());
}
}
catch (MidiUnavailableException e) {
// device is obviously not available...
}
}
if (aInfos.length == 0) {
out("[No devices available]");
}
System.exit(0);
}
e
/*
*This method tries to return a MidiDevice.Info whose name
*matches the passed name. If no matching MidiDevice.Info is
*found, null is returned.
*If forOutput is true, then only output devices are searched,
*otherwise only input devices.
*/
private static MidiDevice.Info getMidiDeviceInfo(String strDeviceName, boolean forOutput) {
MidiDevice.Info[]aInfos = MidiSystem.getMidiDeviceInfo();
for (int i = 0; i < aInfos.length; i++) {
if (aInfos.getName().equals(strDeviceName)) {
try {
MidiDevice device = MidiSystem.getMidiDevice(aInfos);
booleanbAllowsInput = (device.getMaxTransmitters() != 0);
booleanbAllowsOutput = (device.getMaxReceivers() != 0);
if ((bAllowsOutput && forOutput) || (bAllowsInput && !forOutput)) {
return aInfos;
}
} catch (MidiUnavailableException mue) {}
}
}
return null;
}
private static void out(String strMessage)
{
System.out.println(strMessage);
}
private static void out(Throwable t)
{
t.printStackTrace();
}
}

