-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbodysynth.pde
220 lines (159 loc) · 6.45 KB
/
bodysynth.pde
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
import SimpleOpenNI.*;
SimpleOpenNI kinect;
//MIDI libraries
import themidibus.*; //Import the library
MidiBus myBus; // The MidiBus
boolean lefthand_UP = false;
void setup() {
kinect = new SimpleOpenNI(this);
kinect.enableDepth();
kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);
size(640, 480);
fill(255, 0, 0);
MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
myBus = new MidiBus(this, 0, 0); // Create a new MidiBus using the device index to select the Midi input and output devices respectively.
}
void draw() {
kinect.update();
image(kinect.depthImage(), 0, 0);
IntVector userList = new IntVector();
kinect.getUsers(userList);
if (userList.size() > 0) {
int userId = userList.get(0);
if ( kinect.isTrackingSkeleton(userId)) {
drawSkeleton(userId);
}
}
}
void drawSkeleton(int userId) {
//set up the MIDI channel
int channel = 1;
stroke(0);
strokeWeight(15);
stroke(57,206,224);
fill(57,206,224);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
noStroke();
fill(255, 0, 0);
// drawJoint(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER);
drawJoint(userId, SimpleOpenNI.SKEL_LEFT_ELBOW);
drawJoint(userId, SimpleOpenNI.SKEL_LEFT_HAND);
//drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW);
drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_HAND);
drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_HIP);
drawJoint(userId, SimpleOpenNI.SKEL_LEFT_HIP);
drawJoint(userId, SimpleOpenNI.SKEL_HEAD);
float leftHandY = check_positiony_JOIN(userId,SimpleOpenNI.SKEL_LEFT_HAND);
float rightHandY = check_positiony_JOIN(userId,SimpleOpenNI.SKEL_RIGHT_HAND);
float leftElbow = check_positiony_JOIN(userId,SimpleOpenNI.SKEL_LEFT_ELBOW);
float rightElbow = check_positiony_JOIN(userId,SimpleOpenNI.SKEL_RIGHT_ELBOW);
float leftHip = check_positionx_JOIN(userId,SimpleOpenNI.SKEL_LEFT_HIP);
float rightHip = check_positionx_JOIN(userId,SimpleOpenNI.SKEL_RIGHT_HIP);
float head = check_positionx_JOIN(userId,SimpleOpenNI.SKEL_HEAD);
//send CHANNEL 1, CONTROL NUM1
myBus.sendControllerChange(1,1,int((leftHandY/height)*127));
//send CHANNEL 1, CONTROL NUM2
myBus.sendControllerChange(2,2,int((rightHandY/height)*127));
//send CHANNEL 1, CONTROL NUM3
myBus.sendControllerChange(3,3,int((leftElbow/height)*127));
//send CHANNEL 1, CONTROL NUM4
myBus.sendControllerChange(4,4,int((rightElbow/height)*127));
// //send CHANNEL 1, CONTROL NUM5
myBus.sendControllerChange(5,5,int((leftHip/width)*127));
// //send CHANNEL 1, CONTROL NUM6
myBus.sendControllerChange(6,6,int((rightHip/width)*127));
// //send CHANNEL 1, CONTROL NUM4
myBus.sendControllerChange(7,7,int((head/width)*127));
}
void drawJoint(int userId, int jointID) {
PVector joint = new PVector();
float confidence = kinect.getJointPositionSkeleton(userId, jointID, joint);
if (confidence < 0.5) {
return;
}
PVector convertedJoint = new PVector();
kinect.convertRealWorldToProjective(joint, convertedJoint);
ellipse(convertedJoint.x, convertedJoint.y, 10, 10);
}
void check_position_LEFT_HAND(int userId, int jointID){
PVector joint_check = new PVector();
float confidence_check = kinect.getJointPositionSkeleton(userId, jointID, joint_check);
if (confidence_check < 0.5) {
return;
}
PVector convertedJoint_check = new PVector();
kinect.convertRealWorldToProjective(joint_check, convertedJoint_check);
if (convertedJoint_check.y <50){
println("LADO IZQUIERDO!!");
lefthand_UP = !lefthand_UP;
}
}
float check_positiony_JOIN(int userId, int jointID) {
PVector joint_check = new PVector();
float confidence_check = kinect.getJointPositionSkeleton(userId, jointID, joint_check);
if (confidence_check < 0.5) {
return 0;
}
PVector convertedJoint_check = new PVector();
kinect.convertRealWorldToProjective(joint_check, convertedJoint_check);
return convertedJoint_check.y;
}
float check_positionx_JOIN(int userId, int jointID) {
PVector joint_check = new PVector();
float confidence_check = kinect.getJointPositionSkeleton(userId, jointID, joint_check);
if (confidence_check < 0.5) {
return 0;
}
PVector convertedJoint_check = new PVector();
kinect.convertRealWorldToProjective(joint_check, convertedJoint_check);
return convertedJoint_check.x;
}
float calculateAngle(int userId, int jointID_origin, int jointID_end) {
PVector joint_origin = new PVector();
float confidence = kinect.getJointPositionSkeleton(userId, jointID_origin, joint_origin);
if (confidence < 0.5) {
return 0;
}
PVector convertedJoint_origin = new PVector();
kinect.convertRealWorldToProjective(joint_origin, convertedJoint_origin);
// println("coordenada x_origin " + convertedJoint_origin.x);
// println("coordenada y_origin " + convertedJoint_origin.y);
PVector joint_end = new PVector();
float confidence2 = kinect.getJointPositionSkeleton(userId, jointID_end, joint_end);
if (confidence2 < 0.5) {
return 0;
}
PVector convertedJoint_end = new PVector();
kinect.convertRealWorldToProjective(joint_end, convertedJoint_end);
// println("coordenada x_end " + convertedJoint_end.x);
// println("coordenada y_end " + convertedJoint_end.y);
//
float angle_rad = atan(abs(convertedJoint_end.x - convertedJoint_origin.x)/abs(convertedJoint_end.y - convertedJoint_origin.y));
float angle_deg = (angle_rad*360 /( 2*PI));
//println("angulo " + angle_deg);
return angle_deg;
}
// 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);
}