Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created January 5, 2012 05:35
Show Gist options
  • Save jordanorelli/1563870 to your computer and use it in GitHub Desktop.
Save jordanorelli/1563870 to your computer and use it in GitHub Desktop.
bass tuner in ChucK
[30.868, 41.204, 55, 73.416, 97.99, 130.813] @=> float pitches[];
SinOsc o => dac;
0.02 => o.gain; // you... will probably want to adjust this depending on your setup.
Hid hi;
HidMsg msg;
1 => int octave;
0 => int device;
2 => int currentFreq;
pitches[currentFreq] => o.freq;
if(!hi.openKeyboard(device)) {
me.exit();
}
<<< "keyboard '" + hi.name() + "' ready", "" >>>;
pitches[1] => o.freq;
fun void kbListener() {
while(true) {
hi => now;
while(hi.recv(msg)) {
if(msg.which == 79 && msg.type == 1) { // right to decrease pitch
freqIncrease();
} else if (msg.which == 80 && msg.type == 1) { // left to decrease pitch
freqDecrease();
} else if (msg.which == 82 && msg.type == 1) { // down to decrease octave
1 +=> octave;
setPitch();
} else if (msg.which == 81 && msg.type == 1) { // up to increase octave
if(octave > 0) {
1 -=> octave;
setPitch();
}
} else if (msg.which == 44 && msg.type == 1) { // space to mute
toggleMute();
}
}
}
}
fun void toggleMute() {
if(o.gain() == 0) {
0.02 => o.gain;
} else {
0 => o.gain;
}
}
fun void setPitch() {
pitches[currentFreq] * Math.pow(2, octave) => o.freq;
<<< o.freq() >>>;
}
fun void freqIncrease() {
if(currentFreq == pitches.cap() - 1) {
0 => currentFreq;
} else {
1 +=> currentFreq;
}
setPitch();
}
fun void freqDecrease() {
if(currentFreq == 0) {
pitches.cap() - 1 => currentFreq;
} else {
1 -=> currentFreq;
}
setPitch();
}
spork ~ kbListener();
while(true) {
100::ms => now;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment