Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created January 18, 2013 22:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanorelli/4569165 to your computer and use it in GitHub Desktop.
Save jordanorelli/4569165 to your computer and use it in GitHub Desktop.
polyphonic theremin for LEAP with Python and ChucK
class Voice {
int id; // id of the finger on the leapmotion
OscRecv in; // inbound communication
ADSR env => Pan2 pan => dac; // setup an ADSR envelope
Osc @ osc; // reference to the current oscillator
float x;
float y;
float z;
0 => int state;
// start in the off position
env.keyOff();
env.set(20::ms, 40::ms, 0.6, 40::ms);
5::ms => dur beat;
fun static Voice Voice(OscRecv _in, int _id) {
Voice v;
_id => v.id;
_in @=> v.in;
new SinOsc @=> v.osc => v.env;
spork ~ v.listen();
spork ~ v.play();
return v;
}
fun void listen() {
in.event("/leap/finger/" + id, "fff") @=> OscEvent e;
while(true) {
e => now;
while(e.nextMsg() != 0) {
e.getFloat() => x;
e.getFloat() => y;
e.getFloat() => z;
}
}
}
// loop continuously
fun void play() {
while(true) {
beat - (now % beat) => now;
if(x != 0 && y != 0 && z != 0) {
update();
0 => x;
0 => y;
0 => z;
} else if(state == 1) {
off();
}
}
}
fun void update() {
if(state == 0) {
on();
}
x / 256.0 => pan.pan;
y => osc.freq;
}
fun void on() {
env.keyOn();
1 => state;
}
fun void off() {
env.keyOff();
0 => state;
}
}
OscRecv recv;
9000 => recv.port;
recv.listen();
for (0 => int i; i < 30; i++) {
Voice.Voice(recv, i) @=> Voice v;
}
while(true) { 1::minute => now; }
import Leap, sys, liblo
class Listener(Leap.Listener):
def __init__(self):
super(Listener, self).__init__()
self.target = liblo.Address(9000)
def on_frame(self, controller):
frame = controller.frame()
for hand in frame.hands:
for finger in hand.fingers:
self.send_finger(finger)
def send_finger(self, finger):
msg = liblo.Message("/leap/finger/%d" % finger.id)
msg.add(
finger.tip_position.x,
finger.tip_position.y,
finger.tip_position.z,
)
liblo.send(self.target, msg)
l, c = Listener(), Leap.Controller()
c.add_listener(l)
print "Press Enter to quit..."
sys.stdin.readline()
c.remove_listener(l)
@jordanorelli
Copy link
Author

here's a video of this project in action: https://vimeo.com/57712895

@SaavanNanavati
Copy link

Hi! What is the module liblo? I tried running it, and it said no module named liblo. Help?

@jordanorelli
Copy link
Author

Hey! Sorry I didn't respond; I didn't notice your question until now. You can find liblo here. I'm pretty sure that I installed it through homebrew, though, just because it's easier and I'm on a Mac. liblo is a C library for sending and receiving OSC messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment