This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MidiPad.cs
137 lines (114 loc) · 5.04 KB
/
MidiPad.cs
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
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ventuz.OSC;
namespace GesturalMusic
{
class MidiPad : Instrument
{
public new string name;
private DateTime lastTimeLPadPlayed;
private DateTime lastTimeRPadPlayed;
private int lastLPadPlayed;
private int lastRPadPlayed;
private static TimeSpan rateLimit = new TimeSpan(0, 0, 0, 0, 10);
private static TimeSpan reHitLimit = new TimeSpan(0, 0, 0, 0, 200);
// Velocity tracker
private double lWristVelocity;
private double rWristVelocity;
private Joint lWristLocationLast;
private Joint rWristLocationLast;
private DateTime lastFrame;
public MidiPad(string name) : base(name)
{
this.name = name;
lastTimeLPadPlayed = DateTime.Now;
lastTimeRPadPlayed = DateTime.Now;
lWristVelocity = 0f;
rWristVelocity = 0f;
}
new public void PlayNote(int pitch, int velocity = 127, int duration = 5000, int midiChannel = 1)
{
Console.WriteLine("Playing: " + this.name + " " + pitch + " " + velocity + " " + duration + " " + midiChannel);
OscElement elem = new OscElement("/" + this.name, pitch, velocity, duration, midiChannel);
MainWindow.osc.Send(elem);
}
new public bool CheckAndPlayNote(Body body)
{
double threshold = 0.85 * MainWindow.armLength; // Don't even look for a hit unless more than threshold out
double xThreshold = 0.4 * MainWindow.armLength; // Don't even look for a hit unless more than threshold out in X
// Set locations if first time
if (lWristLocationLast == null || rWristLocationLast == null)
{
lWristLocationLast = body.Joints[JointType.WristLeft];
rWristLocationLast = body.Joints[JointType.WristRight];
lastFrame = DateTime.Now;
lastLPadPlayed = -1;
lastRPadPlayed = -1;
return false;
}
// Set velocities
TimeSpan dt = DateTime.Now - lastFrame;
lastFrame = DateTime.Now;
// left hand
Joint lWrist = body.Joints[JointType.WristLeft];
double dLWristPos = (lWrist.Position.X < lWristLocationLast.Position.X ? -1 : 1) * Utils.Length(lWrist, lWristLocationLast);
double newLWristVelocity = dLWristPos/dt.TotalMilliseconds;
// If change in direction, and we're at least halfway extended in x and at least .85 extended overall
if (newLWristVelocity > 0 && lWristVelocity < 0 && Utils.Length(body.Joints[JointType.WristLeft], body.Joints[JointType.ShoulderLeft]) > threshold && Math.Abs(lWrist.Position.X - body.Joints[JointType.ShoulderLeft].Position.X) > xThreshold)
{
// Calculate which pad we're hitting
// 5 O 2
// 4--|--1
// 3 /\ 0
int pad = 0;
if (lWrist.Position.Y > body.Joints[JointType.ShoulderLeft].Position.Y)
{
pad = 2;
}
else if (lWrist.Position.Y > (body.Joints[JointType.SpineBase].Position.Y + 0.4 * Utils.Length(body.Joints[JointType.SpineMid], body.Joints[JointType.SpineBase])))
{
pad = 1;
}
if (lastLPadPlayed != pad || lastTimeLPadPlayed + reHitLimit <= DateTime.Now)
{
this.PlayNote(36 + pad);
lastTimeLPadPlayed = DateTime.Now;
lastLPadPlayed = pad;
}
}
// right hand
Joint rWrist = body.Joints[JointType.WristRight];
double dRWristPos = (double)Utils.LengthFloat(rWrist, rWristLocationLast);
double newRWristVelocity = dRWristPos / dt.TotalMilliseconds;
// If change in direction, and we're at least halfway extended
if (newRWristVelocity < 0 && rWristVelocity > 0 && body.Joints[JointType.WristRight].Position.X > threshold)
{
// Calculate which pad we're hitting
// 5 O 2
// 4--|--1
// 3 /\ 0
int pad = 3;
if (rWrist.Position.Y > body.Joints[JointType.ShoulderRight].Position.Y)
{
pad = 5;
}
else if (rWrist.Position.Y > body.Joints[JointType.SpineMid].Position.Y)
{
pad = 4;
}
this.PlayNote(pad);
}
// Update velocities
lWristVelocity = newLWristVelocity;
rWristVelocity = newRWristVelocity;
lWristLocationLast = lWrist;
rWristLocationLast = rWrist;
// Do we need to send a StopNote?
return false;
}
}
}