// Spatial Music Game // Copyright 2008 Les Hall // This software is protected by the GNU General Public License // parameters 1::ms => dur time_step; // game time step duration 0.660::ms => dur max_delay; // maximum time delay between ears 10::second => dur rotation_time; // time for one complete rotation of the sound source 0.8 => float volume; // nominal volume level 0.2 => float volume_delta; // delta of volume level pi/4 => float accuracy; // how accurate a shot must be 500::ms => dur shot_delay; // delay between shots // variables int t; // time counter float theta; // angle of sound source float phi; // angle of player int shoot; // true if a shot has been fired int score; // player score // the patch adc => DelayA right_delay => dac.right; adc => DelayA left_delay => dac.left; 4 * max_delay => right_delay.max => left_delay.max; 2 * max_delay => right_delay.delay => left_delay.delay; volume => right_delay.gain => left_delay.gain; Impulse shot_sound => BPF ping => dac; 10 => ping.Q; 1000 => ping.freq; // mouse initialization 0 => int mouse_device; Hid mouse_hid; HidMsg mouse_msg; if (!mouse_hid.openMouse(mouse_device)) me.exit(); <<< "Mouse '" + mouse_hid.name() + "' ready", "">>>; spork ~ mouse_watcher(); // keyboard initialization 0 => int kbd_device; Hid kbd_hid; HidMsg kbd_msg; if (!kbd_hid.openKeyboard(kbd_device)) me.exit(); <<< "Keyboard '" + kbd_hid.name() + "' ready", "">>>; spork ~ keyboard_watcher(); // time loop <<< "score:", 0 >>>; while(true) { // calculate angle of sound source t * time_step / rotation_time * 2 * pi => theta; if (theta < 0) { 2 * pi +=> theta; } if (theta > 2 * pi) { 2 * pi -=> theta; } // adjust the left delay line duration 2 * max_delay + max_delay * Math.sin(theta - phi) => left_delay.delay; // adjust the left delay line gain volume - volume_delta * Math.sin(theta - phi) => left_delay.gain; // handle shooting if (shoot) { if (Math.fabs(theta - phi) < accuracy) { 1 +=> score; <<< "score:", score >>>; } 0 => shoot; } // advance time 1 +=> t; time_step => now; } // mouse watcher function void mouse_watcher() { while(true) { mouse_hid => now; while(mouse_hid.recv(mouse_msg) ) { if(mouse_msg.isMouseMotion() ) { mouse_msg.deltaX * 0.01 +=> phi; if (phi < 0) { 2 * pi +=> phi; } if (phi > 2 * pi) { 2 * pi -=> phi; } } } } } // keyboaord watcher function void keyboard_watcher() { while(true) { kbd_hid => now; while(kbd_hid.recv(kbd_msg) ) { if(kbd_msg.isButtonDown() ) { if(kbd_msg.which == 44) { // spacebar 1 => shoot; 1000 => shot_sound.next; } } } if (shoot) { shot_delay => now; } } }