This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
/
LooperOSC.cs
153 lines (140 loc) · 5.2 KB
/
LooperOSC.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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 LooperOSC
{
private bool isRecording = false;
private bool isOverdubbing = false;
private bool isPlaying = false;
private bool isStopped = false;
private static UdpWriter oscLoop;
private double barrier = 0.2;
public LooperOSC()
{
Console.WriteLine("Starting looper...");
ResetOsc();
}
public static void ResetOsc()
{
oscLoop = new UdpWriter(MainWindow.oscHost, MainWindow.looperOscPort);
}
public String GetState()
{
if (isRecording) return "RECD";
if (isOverdubbing) return "ODUB";
if (isPlaying) return "PLAY";
if (isStopped) return "STOP";
return "STOP";
}
public void record()
{
if (!isRecording)
{
OscElement o = new OscElement("/Looper/0/State", "Record");
oscLoop.Send(o);
isRecording = true;
isOverdubbing = false;
isPlaying = false;
isStopped = false;
}
else
return;
}
public void overdub()
{
if (!isOverdubbing)
{
OscElement o = new OscElement("/Looper/0/State", "Overdub");
oscLoop.Send(o);
isRecording = false;
isOverdubbing = true;
isPlaying = false;
isStopped = false;
}
else
return;
}
public void play()
{
if (!isPlaying)
{
OscElement o = new OscElement("/Looper/0/State", "Play");
oscLoop.Send(o);
isRecording = false;
isOverdubbing = false;
isPlaying = true;
isStopped = false;
}
}
public void stop()
{
if (!isStopped)
{
OscElement o = new OscElement("/Looper/0/State", "Stop");
oscLoop.Send(o);
isRecording = false;
isOverdubbing = false;
isPlaying = false;
isStopped = true;
}
}
/// <summary>
/// Determines if looper data should be sent, given the body skeleton.
/// </summary>
/// <param name="body"></param>
/// <returns></returns>
public bool SendLooperData(Body body)
{
try
{
if ((body.HandRightState == HandState.Lasso && body.HandLeftState == HandState.Open) &&
(body.Joints[JointType.WristRight].Position.Y < body.Joints[JointType.SpineShoulder].Position.Y))
{
if ((body.Joints[JointType.AnkleRight].Position.Z > body.Joints[JointType.SpineBase].Position.Z + barrier) &&
(Math.Abs(body.Joints[JointType.AnkleRight].Position.Z - body.Joints[JointType.SpineBase].Position.Z) >
Math.Abs(body.Joints[JointType.AnkleLeft].Position.Z - body.Joints[JointType.SpineBase].Position.Z)))
{
this.record();
return true;
}
else if ((body.Joints[JointType.AnkleRight].Position.Z < body.Joints[JointType.SpineBase].Position.Z + barrier) &&
(Math.Abs(body.Joints[JointType.AnkleRight].Position.Z - body.Joints[JointType.SpineBase].Position.Z) >
Math.Abs(body.Joints[JointType.AnkleLeft].Position.Z - body.Joints[JointType.SpineBase].Position.Z)))
{
this.overdub();
return true;
}
else if ((body.Joints[JointType.AnkleLeft].Position.Z < body.Joints[JointType.SpineBase].Position.Z + barrier) &&
(Math.Abs(body.Joints[JointType.AnkleRight].Position.Z - body.Joints[JointType.SpineBase].Position.Z) <
Math.Abs(body.Joints[JointType.AnkleLeft].Position.Z - body.Joints[JointType.SpineBase].Position.Z)))
{
this.play();
return true;
}
else if ((body.Joints[JointType.AnkleLeft].Position.Z > body.Joints[JointType.SpineBase].Position.Z + barrier) &&
(Math.Abs(body.Joints[JointType.AnkleRight].Position.Z - body.Joints[JointType.SpineBase].Position.Z) <
Math.Abs(body.Joints[JointType.AnkleLeft].Position.Z - body.Joints[JointType.SpineBase].Position.Z)))
{
this.stop();
return true;
}
else
{
return false;
}
}
}
catch (Exception)
{
return false;
}
return false;
}
}
}