Skip to content

Instantly share code, notes, and snippets.

@bonar
Created March 22, 2009 04:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonar/83068 to your computer and use it in GitHub Desktop.
Save bonar/83068 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.ArrayList;
import javax.sound.midi.*;
class SimpleInput {
static final int DEVICE_IN = 0;
static final int DEVICE_OUT = 3;
public static void main (String[] args) {
dumpDeviceInfo();
ArrayList<MidiDevice> devices = getDevices();
MidiDevice device_input = devices.get(DEVICE_IN);
MidiDevice device_output = devices.get(DEVICE_OUT);
if (!(device_output instanceof Synthesizer)) {
throw new IllegalArgumentException("not a Synthesizer!");
}
try { // connet nanoKEY transmitter to Software synthesizer
Transmitter trans // nanoKEY
= device_input.getTransmitter();
Receiver recv // Java Sound Synthesizer
= device_output.getReceiver();
if (!device_output.isOpen()) {
device_output.open();
}
trans.setReceiver(recv);
} catch (MidiUnavailableException e) {
System.err.println(e.getMessage());
System.exit(0);
}
}
public static ArrayList<MidiDevice> getDevices() {
ArrayList<MidiDevice> devices = new ArrayList<MidiDevice>();
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for (int i = 0; i < infos.length; i++) {
MidiDevice.Info info = infos[i];
MidiDevice dev = null;
try {
dev = MidiSystem.getMidiDevice(info);
devices.add(dev);
} catch (SecurityException e) {
System.err.println(e.getMessage());
} catch (MidiUnavailableException e) {
System.err.println(e.getMessage());
}
}
return devices;
}
public static void dumpDeviceInfo() {
ArrayList<MidiDevice> devices = getDevices();
for (int i = 0; i < devices.size(); i++) {
MidiDevice device = devices.get(i);
MidiDevice.Info info = device.getDeviceInfo();
System.out.println("[" + i + "] devinfo: " + info.toString());
System.out.println(" name:" + info.getName());
System.out.println(" vendor:" + info.getVendor());
System.out.println(" version:" + info.getVersion());
System.out.println(" description:" + info.getDescription());
if (device instanceof Synthesizer) {
System.out.println(" SYNTHESIZER");
}
if (device instanceof Sequencer) {
System.out.println(" SEQUENCER");
}
System.out.println("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment