📄 midioutdevice.java
字号:
/* MidiOutDevice.java
A Java class to handle MIDI output.
*/
package midi;
/**
MIDI Out interface.
Defines a consumer of MIDI messages.
*/
interface MidiOut {
/** Consume a standard short (i.e., non-SysEx) MIDI message */
public abstract void Out(int message) throws MidiException;
/** Consume a long (i.e., SysEx) MIDI message */
public abstract void SysExOut(byte buf[], int buflen) throws MidiException;
}
/**
Handle MIDI output.
*/
public class MidiOutDevice extends MidiDevice implements MidiOut {
static boolean interfaceLoaded = false;
static {
/* try to load the DLL containing the MIDI interface */
try {
System.loadLibrary("javamidi");
interfaceLoaded = true;
} catch (UnsatisfiedLinkError e) {
String errmsg = "javamidi.dll not found";
System.err.println(errmsg);
}
}
/** return the number of installed devices */
public static int GetNumDevices() {
if (interfaceLoaded) {
return MidiOutGetNumDevs();
} else {
return -1;
}
}
/*------------- Native Methods -------------*/
/** query the system regarding the number of MIDI output devices */
protected static native int MidiOutGetNumDevs();
/* The following native methods return MMSYSERR_* or MIDIERR_* return codes */
/** open the device; if successful, the instance variable deviceHandle will contain
a valid handle */
protected native int MidiOutOpen();
/** reset the device to its default state */
protected native int MidiOutReset();
/** close the device */
protected native int MidiOutClose();
/** send a non-sysex MIDI message to the device */
protected native int MidiOutShortMsg(int message);
/*--------- End of Native Methods ----------*/
/** Default constructor: open device 0 */
public MidiOutDevice() throws MidiException {
this(0);
}
/** General constructor: open a particular device */
public MidiOutDevice(int id) throws MidiException {
super(id);
}
/** Open the device for midi out. */
public void Open() throws MidiException {
// only try to open if it is not already open
if (deviceHandle != 0) {
return;
}
// if an attempt to open more than then max number of devices
// return bad ID error
if ((MidiOutGetNumDevs() <= deviceId) && (deviceId != MIDI_MAPPER)) {
throw new MidiBadDeviceException();
}
// attempt to open the device
int rc = MidiOutOpen();
// if there is an error opening midi out, throw an exception
if (rc != MMSYSERR_NOERROR) {
// eventually switch on error code
throw new MidiException(rc);
}
}
/**
* Reset the MIDI out device. This would also be the logical place to turn
* notes off if we add note tracking.
*/
public void Reset() {
if (interfaceLoaded) {
MidiOutReset();
}
}
/**
* Close the midi device.
*/
public synchronized void Close() throws MidiException {
if (deviceHandle == 0) { // not open; nothing to do
return;
}
// reset and close midi out
Reset();
int rc = MidiOutClose();
if (rc == MMSYSERR_NOERROR) {
deviceHandle = 0;
} else {
throw new MidiException(rc);
}
}
/**
* Output a midi event
*/
public synchronized void Out(int message) throws MidiException {
if (deviceHandle == 0) {
throw new MidiDeviceNotOpenException();
}
if ((message & 0xF0) == MidiSysEx) {
throw new MidiException("SysEx messages not supported in this version");
}
int rc = MidiOutShortMsg(message);
if (rc != MMSYSERR_NOERROR) {
throw new MidiException(rc);
}
}
/** Output a SysEx message */
public void SysExOut(byte buf[], int buflen) throws MidiException {
throw new MidiException("SysExOut not implemented yet");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -