Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

jordanorelli commented Jan 23, 2013

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

@FrozenFury

This comment has been minimized.

Copy link

FrozenFury commented Jan 23, 2013

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

@jordanorelli

This comment has been minimized.

Copy link
Owner Author

jordanorelli commented May 30, 2013

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
You can’t perform that action at this time.