First up, hello everybody. I'm new, both to the forum, and to processing, but despite my very limited experience, what an awesome program - It's so exciting to tinker with!
I'm using processing 3.2.3, and for now, I'm learning the basics through modifying example code provided via example librarys, which is proving to be a slow but valuable learning experience.
I've now got a little project where I want to combine 2 parts of example code, and I've hit a road block - I simply can't seem to get it to work.
There is a nice SineWave demo in the official sound library (libraries/sound/examples/Oscillators/SineWave) which generates a tone, that tracks your mouse position and changes frequency, amplitude and panning based on your mouse position.
The second is from an example provided as part of the Library item: Leapmotion | Forwards Leap Motion controller events to a processing sketch by Michael Heuer, specifically, the leap_position_example. This example tracks finger tip position provided by the leap, and displays them in a processing window as circular vectors of a random colour.
What I would like to do, is to combine both parts of code - I'd like to bring the SineWave code into the Leap example and then map one of the values provided by the leap (Wrist/palm position ideally - anybody know how to visually display this also?) to SineWave code so that you are able to control the frequency etc. of the sound with you hard movements rather than the mouse. Like a simple theremin!
I'm clearly missing something fundamental, because no matter how I try to format it, I can't get both examples of working in the same processing window. As such, any help would be greatly appreciated - Thank you!
Rather than provide my numerous examples butchered code for you to untangle, here are the two examples of untouched code that I am hoping to use:
SOUND:
import processing.sound.*;
SinOsc sine;
float freq=400;
float amp=0.5;
float pos;
void setup() {
size(800, 600);
background(255);
// Create and start the sine oscillator.
sine = new SinOsc(this);
//Start the Sine Oscillator.
sine.play();
}
void draw() {
// Map mouseY from 0.0 to 1.0 for amplitude
amp=map(mouseY, 0, height, 1.0, 0.0);
sine.amp(amp);
// Map mouseX from 20Hz to 1000Hz for frequency
freq=map(mouseX, 0, width, 80.0, 1000.0);
sine.freq(freq);
// Map mouseX from -1.0 to 1.0 for left to right
pos=map(mouseX, 0, width, -1.0, 1.0);
sine.pan(pos);
}
LEAP:
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;
LeapMotion leapMotion;
ConcurrentMap<Integer, Integer> fingerColors;
ConcurrentMap<Integer, Integer> toolColors;
ConcurrentMap<Integer, Vector> fingerPositions;
ConcurrentMap<Integer, Vector> toolPositions;
void setup()
{
size(800, 600);
background(20);
frameRate(60);
ellipseMode(CENTER);
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()
{
fill(20);
rect(0, 0, width, height);
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(0, 255), random(0, 255), random(0, 255));
fingerColors.putIfAbsent(fingerId, c);
fingerPositions.put(fingerId, finger.tipPosition());
}
toolPositions.clear();
for (Tool tool : frame.tools())
{
int toolId = tool.id();
color c = color(random(0, 255), random(0, 255), random(0, 255));
toolColors.
(toolId, c); toolPositions.put(toolId, tool.tipPosition()); } }