// Mouse Harmony // by Stefan Blixt // 2007-07-28 // sound SinOsc s1a => dac; SinOsc s1b => dac; 0.0 => s1a.gain; 0.0 => s1b.gain; 0.0 => float g1; 28.0 => float harmony_pos1; [0,4,7,11,14,17,21] @=> int harmony_steps1[]; SinOsc s2a => dac; SinOsc s2b => dac; 0.0 => s2a.gain; 0.0 => s2b.gain; 0.0 => float g2; 28.0 => float harmony_pos2; [2,5,9,12,16,19,23] @=> int harmony_steps2[]; // input Hid hi1; HidMsg msg1; 0 => int device1; Hid hi2; HidMsg msg2; 1 => int device2; if( me.args() ) { me.arg(0) => Std.atoi => device1; me.arg(1) => Std.atoi => device2; } if( !hi1.openMouse( device1 ) ) me.exit(); <<< "mouse '" + hi1.name() + "' ready...", "" >>>; if( !hi2.openMouse( device2 ) ) me.exit(); <<< "mouse '" + hi2.name() + "' ready...", "" >>>; fun void setGain1() { (harmony_pos1 + 0.0001) $ int % 2 => int flip; harmony_pos1 - ((harmony_pos1 + 0.0001) $ int) => float inbetween; if (g1 <= 0) { if (flip == 1) { g1*inbetween => s1b.gain; g1*(1.0-inbetween) => s1a.gain; } else { g1*(1.0-inbetween) => s1b.gain; g1*(inbetween) => s1a.gain; } } else { 0 => s1a.gain; 0 => s1b.gain; } } fun void setGain2() { (harmony_pos2 + 0.0001) $ int % 2 => int flip; harmony_pos2 - ((harmony_pos2 + 0.0001) $ int) => float inbetween; if (g2 <= 0) { if (flip == 1) { g2*inbetween => s2b.gain; g2*(1.0-inbetween) => s2a.gain; } else { g2*(1.0-inbetween) => s2b.gain; g2*(inbetween) => s2a.gain; } } else { 0 => s2a.gain; 0 => s2b.gain; } } fun void setPitch1() { (harmony_pos1 + 0.0001) $ int % 2 => int flip; (harmony_pos1 + 0.0001) $ int => int discretePos; if (flip == 1) { setFromHarmony(discretePos, s1a, harmony_steps1); setFromHarmony(discretePos+1, s1b, harmony_steps1); } else { setFromHarmony(discretePos+1, s1a, harmony_steps1); setFromHarmony(discretePos, s1b, harmony_steps1); } } fun void setPitch2() { (harmony_pos2 + 0.0001) $ int % 2 => int flip; (harmony_pos2 + 0.0001) $ int => int discretePos; if (flip == 1) { setFromHarmony(discretePos, s2a, harmony_steps2); setFromHarmony(discretePos+1, s2b, harmony_steps2); } else { setFromHarmony(discretePos+1, s2a, harmony_steps2); setFromHarmony(discretePos, s2b, harmony_steps2); } } fun void setFromHarmony(int pos, SinOsc ugen, int harmony_steps[]) { // <<< "sfh position=", pos >>>; ((pos) / 7) => int double_octave; harmony_steps[pos % 7] => float note; Std.mtof(double_octave*24 + note) => ugen.freq; } fun void handleMouse1() { while (true) { hi1 => now; while (hi1.recv(msg1)) { if (msg1.isMouseMotion()) { msg1.deltaY * 0.001 +=> g1; msg1.deltaX * 0.01 +=> harmony_pos1; setGain1(); setPitch1(); } } } } fun void handleMouse2() { while (true) { hi2 => now; while (hi2.recv(msg2)) { if (msg2.isMouseMotion()) { msg2.deltaY * 0.001 +=> g2; msg2.deltaX * 0.01 +=> harmony_pos2; setGain2(); setPitch2(); } } } } spork ~ handleMouse1(); spork ~ handleMouse2(); // main loop while (true) { 1::second => now; }