// Guitar Tuner: press 1 thru 6 to select string, then tune for 100% value, easy! // Copyright 2008 Les Hall // This software is protected by the GNU General Public License // parameters 1 => int string_number; // 1 thru 6, 1 is high E string, 6 is low E string [0.0, 329.63, 246.94, 196.00, 146.83, 110.00, 82.407] @=> float string_freq[]; 0.005 => float noise_threshold; // only detect notes with this amplitude or greater 0 => int keyboard_device; // keyboard device number // variables float frequency; // the patch adc => BPF bpf => FullRect fwr => LPF lpf => blackhole; // the patch parameters string_freq[string_number] => bpf.freq; 100 => bpf.Q; 10 => lpf.freq; // ChucK it up, spork down spork ~ freq_meas (); // instantiate the class 123456 Keyboard_Interface KI; // ti-i-i-ime is on my side, yes it is while (true) { if (lpf.last () > noise_threshold) { <<<"frequency percentage:", 100 * frequency / string_freq[string_number]>>>; } second => now; } // measure the frequency function void freq_meas () { float previous, current; time previous_time; dur delta_time; while (true) { current => previous; bpf.last () => current; if ( (previous < 0) & (current > 0) ) { now - previous_time => delta_time; now => previous_time; second / delta_time => frequency; } samp => now; } } // Interface to the keyboard class Keyboard_Interface { // variables int kbd_value; // value of key pressed int string_index; // index of string selected // hid initialization Hid hid; HidMsg hidmsg; if (!hid.openKeyboard (keyboard_device)) { me.exit(); } // launch the time loop spork ~ time_loop (); // time loop fun void time_loop () { while (true) { hid => now; // wait for a key press while (hid.recv (hidmsg)) { // while new keys are in queue if (hidmsg.isButtonDown ()) { // check for button down message hidmsg.which => kbd_value; // save button value kbd_value - 29 => string_index; // should be 1 thru 6 only if ( (string_index >= 1) & (string_index <= 6) ) { <<<"string", string_index, "frequency:", string_freq[string_number]>>>; string_index => string_number; string_freq[string_number] => bpf.freq; } } } } } }