Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created May 18, 2011 19:32
Show Gist options
  • Save jordanorelli/979344 to your computer and use it in GitHub Desktop.
Save jordanorelli/979344 to your computer and use it in GitHub Desktop.
Controlling shreds with Midi messages
MidiIn min;
MidiMsg msg;
false => int activeShred; // store the id of the currently active shred.
// always be mindful of your starting conditions;
// in this case, the starting condition is that we
// have not yet launched any child shreds.
if(!min.open(0)) me.exit();
fun int reShred(string shredFileName, int currentShred) {
// temporarily save the id of the last working shred.
currentShred => int previousShred;
if(currentShred) {
Machine.replace(currentShred, shredFileName) => currentShred;
} else {
// starting condition; this will be executed only the first time.
Machine.add(shredFileName) => currentShred;
}
// helps protect us if the file indicatd in "shredFileName" has an error
// in it.
if(!currentShred) previousShred => currentShred;
return currentShred;
}
fun void midiHandler() {
while(true) {
min => now;
while(min.recv(msg)) {
// you'll have to update this to suit your controller.
if(msg.data1 == 144 && msg.data2 == 0 && msg.data3 == 127) {
reShred("shred_1.ck", activeShred) => activeShred;
} else if (msg.data1 == 144 && msg.data2 == 1 && msg.data3 == 127) {
reShred("shred_2.ck", activeShred) => activeShred;
} else if (msg.data1 == 144 && msg.data2 == 2 && msg.data3 == 127) {
reShred("shred_3.ck", activeShred) => activeShred;
} else if (msg.data1 == 144 && msg.data2 == 3 && msg.data3 == 127) {
reShred("shred_4.ck", activeShred) => activeShred;
}
}
}
}
spork ~ midiHandler();
while(true) {
100::ms => now;
}
while(true) {
50::ms => now;
chout <= "hello from shred 1." <= IO.newline();
}
while(true) {
50::ms => now;
chout <= "hello from shred 2." <= IO.newline();
}
while(true) {
50::ms => now;
chout <= "hello from shred 3." <= IO.newline();
}
while(true) {
50::ms => now;
chout <= "hello from shred 4." <= IO.newline();
}
@jordanorelli
Copy link
Author

just run the "main.ck" file; it will spork the other scripts as necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment