-
Notifications
You must be signed in to change notification settings - Fork 1
/
Source.cpp
205 lines (174 loc) · 6.16 KB
/
Source.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Testing this shit out to see if i can get this to work
*/
#include <iostream>
#include <cstring>
// looks like we absolutely need this here. oh well
#include <Windows.h>
#include <mmsystem.h>
#include "Leap.h"
#pragma comment(lib, "winmm.lib")
using namespace Leap;
// inherting from the Listener Class
class MusicByTouchListener : public Listener{
public:
virtual void onInit(const Controller&);
virtual void onConnect(const Controller&);
virtual void onDisconnect(const Controller&);
virtual void onExit(const Controller&);
virtual void onFrame(const Controller&);
virtual void onFocusGained(const Controller&);
virtual void onFocusLost(const Controller&);
virtual void onDeviceChange(const Controller&);
virtual void onServiceConnect(const Controller&);
virtual void onServiceDisconnect(const Controller&);
private:
void playNote(const std::string hand);
};
const std::string fingerNames[] = { "Thumb", "Index", "Middle", "Ring", "Pinky" };
const std::string boneNames[] = { "Metacarpal", "Proximal", "Middle", "Distal" };
const std::string stateNames[] = { "STATE_INVALID", "STATE_START", "STATE_UPDATE", "STATE_END" };
// creating bodies for the virtual functions in SampleListener
void MusicByTouchListener::onInit(const Controller& controller){
std::cout << "Initialized" << std::endl;
}
// for now i'm just gonna add a bunch of fake functions that print stuff out
void MusicByTouchListener::onConnect(const Controller& controller){
std::cout << "Connected" << std::endl;
// making sure to enable gesture reading
controller.enableGesture(Gesture::TYPE_CIRCLE);
controller.enableGesture(Gesture::TYPE_KEY_TAP);
controller.enableGesture(Gesture::TYPE_SCREEN_TAP);
controller.enableGesture(Gesture::TYPE_SWIPE);
//gesture config settings
controller.config().setFloat("Gesture.Swipe.MinLength", 25.0);
controller.config().setFloat("Gesture.Swipe.MinVelocity", 7);
controller.config().save();
}
void MusicByTouchListener::onDisconnect(const Controller& controller) {
std::cout << "Disconnecting" << std::endl;
}
void MusicByTouchListener::onExit(const Controller& controller) {
std::cout << "Exiting" << std::endl;
}
void MusicByTouchListener::onFrame(const Controller& controller) {
// ouputting frame info at each frame capture
//playNote("Left");
//playNote("right");
const Frame frame = controller.frame();
SwipeGesture swipeGesture = Gesture::invalid();
GestureList gestures = frame.gestures();
Gesture goodGesture;
int count = 1;
bool exitFor = false;
for (GestureList::const_iterator g1 = gestures.begin(); g1 != gestures.end(); ++g1) {
if (exitFor) break;
const Gesture gesture = *g1;
//std::cout << count << std::endl;
count++;
switch ((*g1).state()) {
case Leap::Gesture::STATE_STOP:
//Handle ending gestures
if (gesture.type() == Gesture::Type::TYPE_SWIPE) {
swipeGesture = SwipeGesture(gesture);
Vector swipeVector = swipeGesture.direction();
if (swipeVector.y < 0) {
std::cout << swipeVector.y << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
goodGesture = gesture;
exitFor = true;
}
}
break;
default:
//Handle unrecognized states
break;
}
}
HandList hands = goodGesture.hands();
// iterates through the list and calls the object h1
for (HandList::const_iterator h1 = hands.begin(); h1 != hands.end(); ++h1) {
// getting the hand from the list address
const Hand hand = *h1;
std::string handtype;
Vector handPosition;
float height;
// this means both hands can't be detected from the same gesture
if (hand.isLeft()) {
handtype = "Left";
handPosition = hand.palmPosition();
height = handPosition.y;
playNote(handtype);
//std::cout << height << std::endl;
}
if (hand.isRight()) {
handtype = "Right";
handPosition = hand.palmPosition();
height = handPosition.y;
playNote(handtype);
//std::cout << height << std::endl;
}
}
// a bunch of random info
/*
// Commenting out for now while I try to only detect fingers
std::cout << "Frame id: " << frame.id()
<< ", timestamp: " << frame.timestamp()
<< ", hands: " << frame.hands().count()
<< ", extended fingers: " << frame.fingers().extended().count()
<< ", tools: " << frame.tools().count()
<< ", gestures: " << frame.gestures().count() << std::endl;
//std::cout << std::string(4, ' ') << handtype << std::endl;
// gettings the fingers
/*const FingerList fingers = hand.fingers();
for (FingerList::const_iterator f1 = fingers.begin(); f1 != fingers.end(); ++f1) {
const Finger finger = *f1;
std::string extendedFinger = "None";
if (finger.isExtended())
{
extendedFinger = fingerNames[finger.type()];
std::cout << extendedFinger;
}
std::cout << std::endl;
}*/
//}*/
}
void MusicByTouchListener::onFocusGained(const Controller& controller) {
std::cout << "Focusing" << std::endl;
}
void MusicByTouchListener::onFocusLost(const Controller& controller) {
std::cout << "Losing Focus" << std::endl;
}
void MusicByTouchListener::onDeviceChange(const Controller& controller) {
std::cout << "Changing Devices" << std::endl;
}
void MusicByTouchListener::onServiceConnect(const Controller& controller) {
std::cout << "Connecting to Service" << std::endl;
}
void MusicByTouchListener::onServiceDisconnect(const Controller& controller) {
std::cout << "Disconnecting from Service" << std::endl;
}
void MusicByTouchListener::playNote(const std::string hand) {
//PlaySound((LPCSTR)"VEC5 Jump Kicks 30.wav", NULL, SND_FILENAME);
// | SND_ASYNC
if(hand == "Left") sndPlaySound("left.wav", SND_FILENAME);
else sndPlaySound("right.wav", SND_FILENAME);
}
int main(int argc, char** argv){
using namespace std;
// Creating the sample listener and controller
MusicByTouchListener list;
Controller controller;
// connecting the listener to the controller
controller.addListener(list);
// i don't know what this is doing
// seems like its some commandline argument thing
if (argc > 1 && strcmp(argv[1], "--bg") == 0) {
controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);
}
// press enter to quit the program
cin.get();
// remove the sample listener to free up the controller
controller.removeListener(list);
return 0;
}