手部位置未按预期更新 - 处理中的跳跃运动

Hand Position Not Updating As Expected - Leap Motion on Processing

我正在尝试通过处理 (Java) 使用 Leap Motion 控制器制作特雷门。到目前为止,我已经非常接近了。我可以通过将一只手放在控制器上保持静止,同时另一只手移动来改变音高和振幅。我希望做到这一点,以便一只手能够做到这一点,或者左手控制振幅而右手控制音高。

事实上,你必须保持一只手完全静止,而另一只手移动。如果你只用一只手,它只会在手完全离开并重新进入控制器视野时更新音调。我想知道是否有人可以告诉我为什么会这样,以及我可以做些什么来让它随着手的位置不断更新。

代码如下:

import processing.sound.*;
import de.voidplus.leapmotion.*;

LeapMotion leap;

ArrayList<PVector> points; 
PVector fp;

float freq;
float amp;

TriOsc tri;
SinOsc sin;
SqrOsc sqr;
SawOsc saw;

void setup () {

  size(640,800);

  leap = new LeapMotion(this);
  points = new ArrayList<PVector>();

  tri = new TriOsc(this);    
  sin = new SinOsc(this);
  sqr = new SqrOsc(this);
  saw = new SawOsc(this);

}

void leapOnInit() {
  // println("Leap Motion Init");
}
void leapOnConnect() {
  // println("Leap Motion Connect");
}
void leapOnFrame() {
  // println("Leap Motion Frame");
}
void leapOnDisconnect() {
  // println("Leap Motion Disconnect");
}
void leapOnExit() {
  // println("Leap Motion Exit");
}

void draw() {

  tri.freq(freq);
  tri.amp(amp);
  tri.play();

    for (Hand hand : leap.getHands()) {

       fp   = hand.getPosition(); 
       if (fp.z <= 30) {
       points = new ArrayList<PVector>();
    }

    else if (fp.z > 30) {
       points.add(new PVector(fp.x, fp.y));
    }
  }

  for (int i = points.size()-1; i >= 0; i--) {
     PVector p = points.get(i);
     amp = map(width-p.x, 0, width, 1, .01); //Volume based on x-axis
     freq = map(height-p.y, 0, height, 40, 880); //Pitch based on y-axis
  }
}

好的,我知道了。顺便说一下,Leap Motion 的 SDK 文档使用的库与您在 select "Sketch>Import Library...> Leap Motion for Processing" 上处理时实际导入的库完全不同。我花了半天时间试图弄清楚为什么他们的代码示例使用了 类 而没有告诉你其中的内容...

该文档使用起来令人沮丧,所以我没有使用它,也没有制作自己的交互框。相反,我仔细研究了我的代码,发现有一个 "else" 语句扰乱了我的阅读。一旦我摆脱了它,该死的东西就像一个魅力。我什至把"amp" 和"freq" 分别放在我的左手和右手上!

这是它的样子:

import processing.sound.*;
import de.voidplus.leapmotion.*;

LeapMotion leap;
Theremin leapTheremin;

ArrayList<PVector> points; 
PVector handPos;

TriOsc tri;

void setup () {

  size(640,800);

  leap = new LeapMotion(this);
  points = new ArrayList<PVector>();

  tri = new TriOsc(this);    

  leapTheremin = new Theremin(tri);

}

void draw() {

  leapTheremin.renderSound();

  for (Hand hand : leap.getHands()) {

      handPos = hand.getPosition(); 
      boolean handIsLeft = hand.isLeft();
      boolean handIsRight = hand.isRight();

      if (handPos.z <= 75) {
       points = new ArrayList<PVector>();
       points.add(new PVector(handPos.x, handPos.y));
       background((handPos.x)/2.5,(width-handPos.x)/3,handPos.y-(height/3.5));
      }

       if (hand.isRight()) {
        leapTheremin.setPitch();
   }

      if (hand.isLeft()) {
        leapTheremin.setVolume();
      }   
   }
}

class Theremin {

float freq;
float amp;
int sound;

Theremin (TriOsc tri_g) {

  setPitch();

  sound = 1;

  tri = tri_g;

}

  void setPitch () {

    for (int i = points.size()-1; i >= 0; i--) {
    PVector p = points.get(i);
    freq = map((height-handPos.y)+10, 0, height, 40, 880); //"upright antenna", aka "the right one that controls pitch"
    // To match the colors with the moaods of the pitches   

    }   
  }

  void setVolume() {

    for (int i = points.size()-1; i >= 0; i--) {
    PVector p = points.get(i);
    amp = map(width-p.x, 0, width, 1, .01); //"loop antenna", aka "the left one that controls volume" 

    }
  }

  void renderSound() {
    tri.freq(freq);
    tri.amp(amp);
    tri.play();

  }
}