Hi guys, first post here. 
I was wondering if any of you guys have any experience using a leap with Processing? I'm trying to get a simple(ish!) sketch to work using example functionality provided from leap motion libraries and I can't quite get it to work, the example code below has the mouse position controlling a sinewave frequency change, and the leap drawing ellipses of a random colour at tracked fingertip locations.
However, I've spent the 2 days trying to map one of the leap finger positions to the frequency shift (instead of mouseX), but I've had zero luck - the syntax is clearly beyond my current ability. I would be eternally if anybody is able to provide some advise?
Here is the code with both of them working independently within the same sketch:
import processing.sound.*;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Hand;
import com.leapmotion.leap.Tool;
import com.leapmotion.leap.Vector;
import com.leapmotion.leap.processing.LeapMotion;
SinOsc sine;
LeapMotion leapMotion;
float freq=400;
float amp=1;
float pos;
ConcurrentMap<Integer, Integer> fingerColors;
ConcurrentMap<Integer, Integer> toolColors;
ConcurrentMap<Integer, Vector> fingerPositions;
ConcurrentMap<Integer, Vector> toolPositions;
void setup() {
size(640, 360);
background(0);
frameRate(60);
// Create and start the sine oscillator.
sine = new SinOsc(this);
//Start the Sine Oscillator.
sine.play();
leapMotion = new LeapMotion(this);
fingerColors = new ConcurrentHashMap<Integer, Integer>();
toolColors = new ConcurrentHashMap<Integer, Integer>();
fingerPositions = new ConcurrentHashMap<Integer, Vector>();
toolPositions = new ConcurrentHashMap<Integer, Vector>();
}
void draw() {
// Map mouseX from 20Hz to 1000Hz for frequency
freq=map(mouseX, 0, width, 80.0, 1000.0);
sine.freq(freq);
fill(0); //This creates a fill to prevent the solitaire effect
rect(0, 0, width, height); //this controls the size of the above fill
for (Map.Entry entry : fingerPositions.entrySet())
{
Integer fingerId = (Integer) entry.getKey();
Vector position = (Vector) entry.getValue();
fill(fingerColors.get(fingerId));
noStroke();
ellipse(leapMotion.leapToSketchX(position.getX()), leapMotion.leapToSketchY(position.getY()), 24.0, 24.0);
}
for (Map.Entry entry : toolPositions.entrySet())
{
Integer toolId = (Integer) entry.getKey();
Vector position = (Vector) entry.getValue();
fill(toolColors.get(toolId));
noStroke();
ellipse(leapMotion.leapToSketchX(position.getX()), leapMotion.leapToSketchY(position.getY()), 24.0, 24.0);
}
}
void onFrame(final Controller controller)
{
Frame frame = controller.frame();
fingerPositions.clear();
for (Finger finger : frame.fingers())
{
int fingerId = finger.id();
color c = color(random(70, 255), random(70, 255), random(70, 255)); //This allows me to change colour of finger ellipses
fingerColors.putIfAbsent(fingerId, c);
fingerPositions.put(fingerId, finger.tipPosition());
}
toolPositions.clear();
for (Tool tool : frame.tools())
{
int toolId = tool.id();
color c = color(random(255, 255), random(255, 255), random(255, 255));
toolColors.putIfAbsent(toolId, c);
toolPositions.put(toolId, tool.tipPosition());
}
}