Skip to content

Instantly share code, notes, and snippets.

@schollz
Last active July 27, 2017 12:48
Show Gist options
  • Save schollz/f8ec8687e7de784aee6831fb2ca24078 to your computer and use it in GitHub Desktop.
Save schollz/f8ec8687e7de784aee6831fb2ca24078 to your computer and use it in GitHub Desktop.
import themidibus.*; //Import the library
MidiBus myBus; // The MidiBus
void setup() {
size(400, 400);
background(0);
MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
// Parent In Out
// | | |
//myBus = new MidiBus(this, "IncomingDeviceName", "OutgoingDeviceName"); // Create a new MidiBus using the device names to select the Midi input and output devices respectively.
myBus = new MidiBus(this, 0, 3); // Create a new MidiBus with no input device and the default Java Sound Synthesizer as the output device.
}
void draw() {
//myBus.sendControllerChange(0, 0, 0); // Send a controllerChange
//delay(2000);
}
class PressNote implements Runnable {
private int channel;
private int pitch;
private int velocity;
private boolean on;
public PressNote(int c, int p, int v, boolean on) {
this.channel = c;
this.pitch = p;
this.velocity = v;
this.on = on;
}
public void run() {
println();
if (on == true) {
println("Note On:");
} else {
println("Note Off:");
}
println("--------");
println("Channel:"+channel);
println("Pitch:"+pitch);
println("Velocity:"+velocity);
// Play with delay!
delay(100);
// Play with pitch symmetry!
pitch = 57 - (pitch - 60);
if (pitch < 0) {
pitch = pitch + 127;
}
if (on == true) {
myBus.sendNoteOn(channel, pitch, velocity);
} else {
myBus.sendNoteOff(channel, pitch, velocity);
}
}
}
void noteOn(int channel, int pitch, int velocity) {
// Receive a noteOn
PressNote myRunnable = new PressNote(channel, pitch, velocity, true);
new Thread(myRunnable).start();
}
void noteOff(int channel, int pitch, int velocity) {
// Receive a noteOff
PressNote myRunnable = new PressNote(channel, pitch, velocity, false);
new Thread(myRunnable).start();
}
void controllerChange(int channel, int number, int value) {
// Receive a controllerChange
println();
println("Controller Change:");
println("--------");
println("Channel:"+channel);
println("Number:"+number);
println("Value:"+value);
}
void delay(int time) {
int current = millis();
while (millis () < current+time) Thread.yield();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment