c++audioopenframeworksleap-motion

Leap motion play sound in update function (openframework)


I use leap motion to play instrument notes, and I use palmPosition to triggler the sound to play, but it is not what I want, the palmPosition is in update function, the sound will play base on the frame rates over and over again. too fast, I want the leap motion just like the mouse up and mouse down...Play the sounds when the palmPosition is changed. Here is my code,

    void ofApp::update(){
        ...
    if (hands[i].palmPosition().z < 50) {
                    isTouch = true;
                        if ((hands[i].palmPosition().x > -240)&& (hands[i].palmPosition().x < -160)) {
                                pianoE.setVolume(1);
                                pianoE.play();
                        }

                        if ((hands[i].palmPosition().x >-120)&& (hands[i].palmPosition().x <-60)) {
                            pianoE.setVolume(1);
                            pianoE.play();
                        }

                        if ((hands[i].palmPosition().x >-20)&& (hands[i].palmPosition().x < 20)) {
                            pianoC.setVolume(1);
                            pianoC.play();
                        }

                        if ((hands[i].palmPosition().x > 90)&& (hands[i].palmPosition().x < 120)) {
                            pianoC.setVolume(1);
                            pianoC.play();
                        }

                        if ((hands[i].palmPosition().x >180)&& (hands[i].palmPosition().x < 240)) {
                            pianoC.setVolume(1);
                            pianoC.play();
                        }
                    }else{
                    isTouch = false;

                }
...
    }

Solution

  • You check if the sound isPlaying() and only play it if it's not already doing that.

    e.g.

    if (hands[i].palmPosition().z < 50) {
                        isTouch = true;
                            if ((hands[i].palmPosition().x > -240)&& (hands[i].palmPosition().x < -160)) {
                                    pianoE.setVolume(1);
                                    if(!pianoE.isPlaying()) pianoE.play();
                            }
    
                            if ((hands[i].palmPosition().x >-120)&& (hands[i].palmPosition().x <-60)) {
                                pianoE.setVolume(1);
                                if(!pianoE.isPlaying()) pianoE.play();
                            }
    
                            if ((hands[i].palmPosition().x >-20)&& (hands[i].palmPosition().x < 20)) {
                                pianoC.setVolume(1);
                                if(!pianoC.isPlaying()) pianoC.play();
                            }
    
                            if ((hands[i].palmPosition().x > 90)&& (hands[i].palmPosition().x < 120)) {
                                pianoC.setVolume(1);
                                if(!pianoC.isPlaying()) pianoC.play();
                            }
    
                            if ((hands[i].palmPosition().x >180)&& (hands[i].palmPosition().x < 240)) {
                                pianoC.setVolume(1);
                                if(!pianoC.isPlaying()) pianoC.play();
                            }
                        }else{
                        isTouch = false;
    
                    }