This repository was archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwatKinect.pde
More file actions
246 lines (181 loc) · 6.33 KB
/
SwatKinect.pde
File metadata and controls
246 lines (181 loc) · 6.33 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import SimpleOpenNI.*;
SimpleOpenNI kinect;
import rwmidi.*;
MidiOutput output;
// variables for note generation from keystrike
// variables for note generation from keystrike
int aChannel, sChannel, dChannel, fChannel;
int aNote, sNote, dNote, fNote;
int aVelocity, sVelocity, dVelocity, fVelocity;
int aSuccess, sSuccess, dSuccess, fSuccess;
boolean isClapping = false;
boolean wasClapping = false;
boolean beatOn = false;
boolean wasInEffectBox = false;
void setup() {
kinect = new SimpleOpenNI(this);
kinect.enableDepth();
kinect.setMirror(true);
// turn on user tracking
kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);
size(640, 480);
// just like serial ports, you can list the devices if you like
println("output devices");
println(RWMidi.getOutputDevices());
println("\ninput devices");
println(RWMidi.getInputDevices());
// creates a connection to IAC as an output
output = RWMidi.getOutputDevices()[0].createOutput(); // talks to MIDI
}
void draw() {
kinect.update();
PImage depth = kinect.depthImage();
image(depth, 0, 0);
// make a vector of ints to store the list of users
IntVector userList = new IntVector();
// write the list of detected users
// into our vector
kinect.getUsers(userList);
// if we found any users
if (userList.size() > 0) {
// get the first user
int userId = userList.get(0);
// if we're successfully calibrated
if ( kinect.isTrackingSkeleton(userId)) {
// make a vector to store the left hand
PVector leftHand = new PVector();
// put the position of the left hand into that vector
kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, leftHand);
// convert the detected hand position
// to "projective" coordinates
// that will match the depth image
PVector convertedLeftHand = new PVector();
kinect.convertRealWorldToProjective(leftHand, convertedLeftHand);
// and display it
fill(255,0,0);
ellipse(convertedLeftHand.x, convertedLeftHand.y, 10, 10);
// make a vector to store the left hand
PVector rightHand = new PVector();
// put the position of the left hand into that vector
kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, rightHand);
// convert the detected hand position
// to "projective" coordinates
// that will match the depth image
PVector convertedRightHand = new PVector();
kinect.convertRealWorldToProjective(rightHand, convertedRightHand);
// and display it
fill(0,255,0);
ellipse(convertedRightHand.x, convertedRightHand.y, 10, 10);
stroke(0,0,255);
line(convertedRightHand.x, convertedRightHand.y, convertedLeftHand.x, convertedLeftHand.y);
float distance_between_hands = sqrt( pow(convertedRightHand.x - convertedLeftHand.x, 2) + pow(convertedRightHand.y - convertedLeftHand.y,2));
println(convertedRightHand.x + " " + convertedRightHand.y);
// Draw toggle box
noFill();
strokeWeight(3);
stroke(0,0,255);
rect(590,10,40,40);
// println(distance_between_hands);
// Figure out if a clap just occurred
if ((!wasClapping && distance_between_hands < 60) || (wasClapping && distance_between_hands < 100)) {
isClapping =true;
} else {
isClapping = false;
}
// If this is a new clap, toggle the state of the beat
if (!wasClapping && isClapping){
if (beatOn)
{
// Turn off beat
output.sendNoteOff(0, 3, 3);
beatOn = false;
println("Beat turned off");
} else {
// Turn beat on
output.sendNoteOn(0, 3, 3);
beatOn = true;
println("Beat turned on");
}
}
// If the beat is on, control the volume
if (beatOn)
{
// Control the volume
int volume = min(int(map(max(distance_between_hands,40), 0, 500, 0, 127)), 127);
output.sendController(1, 1, volume);
println("Volume is: " + volume);
// Calculate angle of hands
leftHand.sub(rightHand);
leftHand.normalize();
PVector handOrientation = new PVector(1,0,0);
float angle = degrees(acos(handOrientation.dot(leftHand)));
println("Angle: " + angle);
if (convertedRightHand.x >= 590 && convertedRightHand.x <= 630 && convertedRightHand.y <= 50 && convertedRightHand.y >= 10)
{
if (!wasInEffectBox)
{
output.sendNoteOn(2, 1, 1);
println("Just entered effect box");
}
wasInEffectBox = true;
} else {
wasInEffectBox = false;
}
// Show the indicator
stroke(255,0,0);
fill(255,0,0);
rect(10,10,40,40);
} else {
// Show an empty indicator -- beat is off
noFill();
strokeWeight(3);
stroke(255,0,0);
rect(10,10,40,40);
}
wasClapping = isClapping;
}
}
}
void keyPressed(){
if (key == 'a' || key == 'A' ) {
// Toggle effect
output.sendNoteOn(2, 1, 1);
}
if (key == 's' || key == 'S' ) {
sChannel = 1;
sNote = 61;
sVelocity = 127;
sSuccess = output.sendNoteOn(sChannel, sNote, sVelocity); // note return a variabel of type INT
}
if (key == 'd' || key == 'D' ) {
dChannel = 1;
dNote = 62;
dVelocity = 127;
dSuccess = output.sendNoteOn(dChannel, dNote, dVelocity); // note return a variabel of type INT
}
if (key == 'f' || key == 'F' ) {
fChannel = 1;
fNote = 63;
fVelocity = 127;
fSuccess = output.sendNoteOn(fChannel, fNote, fVelocity); // note return a variabel of type INT
}
}
// user-tracking callbacks!
void onNewUser(int userId) {
println("start pose detection");
kinect.startPoseDetection("Psi", userId);
}
void onEndCalibration(int userId, boolean successful) {
if (successful) {
println(" User calibrated !!!");
kinect.startTrackingSkeleton(userId);
} else {
println(" Failed to calibrate user !!!");
kinect.startPoseDetection("Psi", userId);
}
}
void onStartPose(String pose, int userId) {
println("Started pose for user");
kinect.stopPoseDetection(userId);
kinect.requestCalibrationSkeleton(userId, true);
}