Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created January 17, 2012 16:50
Show Gist options
  • Save jordanorelli/1627453 to your computer and use it in GitHub Desktop.
Save jordanorelli/1627453 to your computer and use it in GitHub Desktop.
an early ChucK synth
MidiIn padIn;
MidiOut padOut;
MidiMsg msg;
1.059463094359295 => float halfStepRatio;
TriOsc mrNote => dac;
0 => mrNote.gain;
55.0 => float lowestFreq;
8 => int rowStep; // number of halfsteps between launchpad rows.
// set to 8 for a chromatic layout. Anything above
// 8 and you'll miss notes. Anything below and
// notes will have multiple keys.
if(!padIn.open(0))
me.exit();
if(!padOut.open(0))
me.exit();
while(true) {
padIn => now;
while(padIn.recv(msg)) {
<<< msg.data1, msg.data2, msg.data3, keyToFreq(msg.data2) >>>;
if(msg.data3 == 127) {
bounce(msg);
noteOn(keyToFreq(msg.data2));
} else {
bounce(msg);
noteOff(keyToFreq(msg.data2));
}
}
}
fun float keyToFreq(int key) {
key % 16 => int column;
key / 16 => int row;
return lowestFreq * max(1, exp(halfStepRatio, ((7 - row) * rowStep + column)));
}
fun void noteOn(float freq) {
freq => mrNote.freq;
0.8 => mrNote.gain;
}
fun void noteOff(float freq) {
0 => mrNote.gain;
}
fun void bounce(MidiMsg msg) {
padOut.send(msg);
}
fun float max(float a, float b) {
if (a > b)
return a;
else
return b;
}
// now this is truly awful.
fun float exp(float base, int power) {
base => float ret;
if(power < 0)
me.exit();
else if(power == 0)
return 1.0;
else {
while(power > 1) {
ret * base => ret;
power - 1 => power;
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment