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

时间:2018-12-10 05:37:11

标签: java processing leap-motion

我正在尝试通过处理(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
  }
}

1 个答案:

答案 0 :(得分:0)

好的,我明白了。顺便说一句,让我们知道,Leap Motion SDK的文档使用的是完全不同的库,而在“处理”中选择“ Sketch>导入库...> Leap Motion用于处理”时,该库实际上就是导入的。我花了半天的时间来弄清楚为什么他们的编码示例使用类而不告诉您它们中包含什么...

文档使用起来令人沮丧,因此我没有使用它或自己创建了交互框。取而代之的是,我玩弄了我已有的代码,发现有一个“ 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();

  }
}