Added pitch-bending capabilities.

This commit is contained in:
Sam Archer 2012-03-08 23:27:04 -08:00
parent 942f0b1656
commit 3e9bf6a078

View File

@ -3,6 +3,7 @@ package moppydesk;
import gnu.io.SerialPort;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.Receiver;
/**
*
* @author Sammy1Am
@ -33,6 +34,11 @@ public class MoppyPlayer implements Receiver {
*/
public static int ARDUINO_RESOLUTION = 40;
/**
* Current period of each MIDI channel (zero is off) as set
* by the NOTE ON message; for pitch-bending.
*/
private int[] currentPeriod = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
MoppyBridge mb;
SerialPort com;
@ -54,8 +60,8 @@ public class MoppyPlayer implements Receiver {
//System.out.println("Got note OFF on pin: " + (pin & 0xFF));
mb.sendEvent(pin, 0);
}
else if (message.getStatus() > 143 && message.getStatus() < 160){ // Note ON
currentPeriod[message.getStatus() - 128] = 0;
} else if (message.getStatus() > 143 && message.getStatus() < 160) { // Note ON
//Convert the MIDI channel being used to the controller pin on the
//Arduino by multipying by 2.
byte pin = (byte) (2 * (message.getStatus() - 143));
@ -76,9 +82,25 @@ public class MoppyPlayer implements Receiver {
//Zero velocity events turn off the pin.
if (message.getMessage()[2] == 0) {
mb.sendEvent(pin, 0);
currentPeriod[message.getStatus() - 144] = 0;
} else {
mb.sendEvent(pin, period);
currentPeriod[message.getStatus() - 144] = period;
}
} else if (message.getStatus() > 223 && message.getStatus() < 240) { //Pitch bends
//Only proceed if the note is on (otherwise, no pitch bending)
if (currentPeriod[message.getStatus() - 224] != 0) {
//Convert the MIDI channel being used to the controller pin on the
//Arduino by multipying by 2.
byte pin = (byte) (2 * (message.getStatus() - 223));
double pitchBend = ((message.getMessage()[2] & 0xff) << 8) + (message.getMessage()[1] & 0xff);
int period = (int) (currentPeriod[message.getStatus() - 224] / Math.pow(2.0, (pitchBend - 8192) / 49152));
//System.out.println(currentPeriod[message.getStatus() - 224] + "-" + period);
mb.sendEvent(pin, period);
}
}
}
}