[ Video ]
[ About ]
久々にLeap Motionを引っぱり出してきました。指の加速度を使ってピアノ演奏(もどき)
Piano Play by LEAP Motion.
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#pragma once #include "ofMain.h" // Leap Motion SDK #include "Leap.h" #pragma comment(lib, "Leap.lib") class ofApp : public ofBaseApp { public: void setup(); void update(); void draw(); void keyPressed(int key) {}; void keyReleased(int key) {}; void mouseMoved(int x, int y) {}; void mouseDragged(int x, int y, int button) {}; void mousePressed(int x, int y, int button) {}; void mouseReleased(int x, int y, int button) {}; void mouseEntered(int x, int y) {}; void mouseExited(int x, int y) {}; void windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; // Leap Motion Leap::Controller leap; vector<ofSoundPlayer> sounds; vector<float> waves; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofSetWindowTitle("Insta"); ofSetColor(239); ofBackground(39); ofNoFill(); ofSetCircleResolution(36); string sound_path_list[] = { "sound/pianoC2.mp3", "sound/pianoB.mp3", "sound/pianoA.mp3", "sound/pianoG.mp3", "sound/pianoF.mp3", "sound/pianoE.mp3", "sound/pianoD.mp3","sound/pianoC.mp3" }; for (int i = 0; i < 8; i++) { ofSoundPlayer sound; sound.load(sound_path_list[i]); sound.setVolume(0.2); sound.setMultiPlay(true); this->sounds.push_back(sound); } } //-------------------------------------------------------------- void ofApp::update() { Leap::Frame frame = leap.frame(); for (Leap::Hand hand : frame.hands()) { for (Leap::Finger finger : hand.fingers()) { ofVec2f velocity = ofVec2f(finger.tipVelocity().x, -finger.tipVelocity().y); if (velocity.length() > 512) { this->sounds[ofGetFrameNum() % 8].play(); this->waves.push_back(0); } } } for (int i = this->waves.size() - 1; i >= 0; i--) { this->waves[i] += 5; if (this->waves[i] > 250) { this->waves.erase(this->waves.begin() + i); } } ofSoundUpdate(); } //-------------------------------------------------------------- void ofApp::draw() { ofTranslate(ofGetWidth() * 0.5, ofGetHeight() * 0.5); for (int i = 0; i < this->waves.size(); i++) { ofSetColor(239, ofMap(this->waves[i], 0, 250, 255, 0)); ofDrawCircle(ofPoint(), this->waves[i]); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |