Created
March 22, 2009 06:00
-
-
Save bonar/83095 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.ArrayList; | |
import javax.sound.midi.*; | |
class FilteredInput { | |
static final int DEVICE_IN = 0; | |
static final int DEVICE_OUT = 3; | |
public static void main (String[] args) { | |
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 = device_input.getTransmitter(); | |
if (!device_output.isOpen()) { | |
device_output.open(); | |
} | |
// create custom receiver | |
Receiver myrecv = new MyReceiver(device_output); | |
trans.setReceiver(myrecv); | |
} 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; | |
} | |
} | |
class MyReceiver | |
implements javax.sound.midi.Receiver { | |
private Synthesizer synth; | |
private MidiChannel defaultChannel; | |
public MyReceiver(MidiDevice device_out) { | |
if (!(device_out instanceof Synthesizer)) { | |
throw new IllegalArgumentException( | |
"device is not a Synthesizer"); | |
} | |
this.synth = (Synthesizer)device_out; | |
// get first channel of the device | |
MidiChannel[] channels = this.synth.getChannels(); | |
if (0 == channels.length) { | |
throw new IllegalStateException("no channels available"); | |
} | |
this.defaultChannel = channels[0]; | |
} | |
public void send(MidiMessage message, long timeStamp) { | |
if (message instanceof ShortMessage) { | |
ShortMessage sm = ((ShortMessage)message); | |
switch(sm.getCommand()) { | |
case ShortMessage.NOTE_ON: | |
this.defaultChannel.noteOn( | |
minorize(sm.getData1()), sm.getData2()); | |
break; | |
case ShortMessage.NOTE_OFF: | |
this.defaultChannel.noteOff( | |
minorize(sm.getData1()), sm.getData2()); | |
break; | |
} | |
} | |
} | |
public void close() { } | |
private int minorize(int origin) { | |
int minorized = origin; | |
int note = (origin % 12); | |
if (note == 4 || note == 9 || note == 11) { | |
minorized--; | |
} | |
return minorized; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment