1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using System.Data;
|
---|
5 | using System.Drawing;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Text;
|
---|
8 | using System.Threading.Tasks;
|
---|
9 | using System.Windows.Forms;
|
---|
10 |
|
---|
11 | using Microsoft.Kinect;
|
---|
12 | using Newtonsoft.Json;
|
---|
13 | using NAudio.Wave;
|
---|
14 | using MicroLibrary;
|
---|
15 | using System.IO;
|
---|
16 |
|
---|
17 |
|
---|
18 | namespace FFDDR
|
---|
19 | {
|
---|
20 | public partial class FFDDR : Form
|
---|
21 | {
|
---|
22 | // Wavprocessor variables
|
---|
23 |
|
---|
24 | private string[] wavInfo;
|
---|
25 | private long wavFrameIncrement = 0;
|
---|
26 | private WaveFileReader reader;
|
---|
27 | private WaveOut output;
|
---|
28 | private int m = 0;
|
---|
29 | private Graphics graphics;
|
---|
30 | private MicroTimer timer;
|
---|
31 | private int beatBoxChanger = 0;
|
---|
32 | private long timeSinceLastBeat = 0;
|
---|
33 |
|
---|
34 |
|
---|
35 | // SkeletonTracker variables
|
---|
36 |
|
---|
37 | private const float RenderWidth = 640.0f;
|
---|
38 | private const float RenderHeight = 480.0f;
|
---|
39 | private KinectSensor sensor;
|
---|
40 | private Skeleton[] skeletonArray;
|
---|
41 |
|
---|
42 | private SkeletonManager skeletonManager;
|
---|
43 |
|
---|
44 | private PictureBox[] pboxShowCalibrated;
|
---|
45 |
|
---|
46 | private Vector4 centreScreen;
|
---|
47 | private bool twoPlayer = false;
|
---|
48 | private float deadspotSize = 50;
|
---|
49 |
|
---|
50 |
|
---|
51 | public FFDDR()
|
---|
52 | {
|
---|
53 | InitializeComponent();
|
---|
54 | }
|
---|
55 |
|
---|
56 | private void FFDDR_Load(object sender, EventArgs e)
|
---|
57 | {
|
---|
58 | #region Beat detect
|
---|
59 |
|
---|
60 | // The song to convert + read
|
---|
61 | reader = new WaveFileReader(@"C:\Users\Chris\Downloads\become.wav");
|
---|
62 | output = new WaveOut();
|
---|
63 | output.Init(reader);
|
---|
64 | output.Play();
|
---|
65 | graphics = mainScreen.CreateGraphics();
|
---|
66 |
|
---|
67 | this.DoubleBuffered = true;
|
---|
68 |
|
---|
69 | // The song to play
|
---|
70 | wavInfo = WavProcessor.ReadWav(@"C:\Users\Chris\Downloads\", "become", 1024, 30);
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | // Play one buffer every (songLength/temp.length(numbuffer)) milliseconds. Both values are contained in the Json string[]
|
---|
75 | WavFrame temp = (WavFrame)JsonConvert.DeserializeObject<WavFrame>(wavInfo[0]);
|
---|
76 |
|
---|
77 | timer = new MicroTimer();
|
---|
78 | timer.Interval = (int)((temp.songLength / wavInfo.Length) * 1000);
|
---|
79 |
|
---|
80 | // Have the ability to ignore late-firing timer events. Enable if drift issues exist, currently works with.
|
---|
81 | // timer.IgnoreEventIfLateBy = (int)((temp.songLength / wavInfo.Length) )/ 4;
|
---|
82 |
|
---|
83 | timer.Enabled = true;
|
---|
84 | timer.MicroTimerElapsed += new MicroTimer.MicroTimerElapsedEventHandler(timerTick);
|
---|
85 |
|
---|
86 | #endregion
|
---|
87 | #region Initialize and start Kinect
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | foreach (var potentialSensor in KinectSensor.KinectSensors)
|
---|
92 | {
|
---|
93 | if (potentialSensor.Status == KinectStatus.Connected)
|
---|
94 | {
|
---|
95 | this.sensor = potentialSensor;
|
---|
96 | break;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | if (null != this.sensor)
|
---|
102 | {
|
---|
103 | // Turn on the skeleton stream to receive skeleton frames
|
---|
104 | this.sensor.SkeletonStream.Enable();
|
---|
105 |
|
---|
106 | // Add an event handler to be called whenever there is new color frame data
|
---|
107 | this.sensor.SkeletonFrameReady += this.SensorSkeletonFrameReady;
|
---|
108 |
|
---|
109 | // Start the sensor!
|
---|
110 | try
|
---|
111 | {
|
---|
112 |
|
---|
113 | this.sensor.Start();
|
---|
114 | sensor.ElevationAngle = -5;
|
---|
115 | }
|
---|
116 | catch (IOException)
|
---|
117 | {
|
---|
118 | this.sensor = null;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | #endregion
|
---|
123 |
|
---|
124 | skeletonManager = new SkeletonManager(6);
|
---|
125 | graphics = screen.CreateGraphics();
|
---|
126 | centreScreen.X = screen.Width / 2;
|
---|
127 | centreScreen.Z = screen.Height / 2;
|
---|
128 |
|
---|
129 | #region Display pictureboxes showing calibration information
|
---|
130 | pboxShowCalibrated = new PictureBox[6];
|
---|
131 | for (int i = 0; i < pboxShowCalibrated.Length; i++)
|
---|
132 | {
|
---|
133 |
|
---|
134 | pboxShowCalibrated[i] = new PictureBox();
|
---|
135 | pboxShowCalibrated[i].Height = 40;
|
---|
136 | pboxShowCalibrated[i].Width = 40;
|
---|
137 | pboxShowCalibrated[i].Left = this.Width - (pboxShowCalibrated[i].Width + 17);
|
---|
138 | pboxShowCalibrated[i].Top = (i * 50);
|
---|
139 | pboxShowCalibrated[i].BackColor = Color.DarkGray;
|
---|
140 | pboxShowCalibrated[i].Visible = true;
|
---|
141 | this.Controls.Add(pboxShowCalibrated[i]);
|
---|
142 | }
|
---|
143 | #endregion
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void timerTick(object sender, MicroTimerEventArgs args)
|
---|
147 | {
|
---|
148 | timeSinceLastBeat++;
|
---|
149 | wavFrameIncrement++;
|
---|
150 |
|
---|
151 | if (mainScreen.InvokeRequired)
|
---|
152 | {
|
---|
153 | //mainScreen.Invoke(new MethodInvoker(delegate { mainScreen.Refresh(); }));
|
---|
154 | mainScreen.Invoke(new MethodInvoker(delegate { mainScreen.CreateGraphics().FillRectangle(new SolidBrush(Color.White), 0, 0, mainScreen.Width, mainScreen.Height); }));
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 |
|
---|
159 | // When song finished this is out of bounds. fix.
|
---|
160 | WavFrame currentFrame = (WavFrame)JsonConvert.DeserializeObject<WavFrame>(wavInfo[wavFrameIncrement]);
|
---|
161 | for (int j = 0; j < currentFrame.bandPowers.Length; j++)
|
---|
162 | {
|
---|
163 | // Calling control change drawing beat visualizer bands) from another thread.
|
---|
164 | if (mainScreen.InvokeRequired)
|
---|
165 | {
|
---|
166 | mainScreen.Invoke(new MethodInvoker(delegate { mainScreen.CreateGraphics().DrawRectangle(new Pen(Color.DarkBlue), j * 15, 0, 10, 6 * currentFrame.bandPowers[j]); }));
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | // If a beat exists on the current frame, display it via BeatBox
|
---|
171 | if (currentFrame.beatExists == true)
|
---|
172 | {
|
---|
173 | if (beatBox.InvokeRequired)
|
---|
174 | {
|
---|
175 | beatBox.Invoke(new MethodInvoker(delegate { ChangeBeatBox(); }));
|
---|
176 |
|
---|
177 | }
|
---|
178 |
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | private void ChangeBeatBox()
|
---|
183 | {
|
---|
184 |
|
---|
185 | // Alternate the box which chanhges to the beat.
|
---|
186 | graphics = mainScreen.CreateGraphics();
|
---|
187 |
|
---|
188 | if (timeSinceLastBeat >= 20)
|
---|
189 | {
|
---|
190 | beatBoxChanger++;
|
---|
191 | timeSinceLastBeat = 0;
|
---|
192 | PictureBox p;
|
---|
193 | if ((beatBoxChanger %= 2) == 1)
|
---|
194 | {
|
---|
195 | p = beatBox;
|
---|
196 | }
|
---|
197 | else p = beatBox1;
|
---|
198 |
|
---|
199 | if (beatBox.BackColor == Color.DarkBlue)
|
---|
200 | {
|
---|
201 | p.BackColor = Color.Turquoise;
|
---|
202 | }
|
---|
203 | else p.BackColor = Color.DarkBlue;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
|
---|
208 | {
|
---|
209 | skeletonArray = new Skeleton[0];
|
---|
210 |
|
---|
211 | using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
|
---|
212 | {
|
---|
213 | if (skeletonFrame != null)
|
---|
214 | {
|
---|
215 | skeletonArray = new Skeleton[skeletonFrame.SkeletonArrayLength];
|
---|
216 | skeletonFrame.CopySkeletonDataTo(skeletonArray);
|
---|
217 | skeletonManager.Update(skeletonArray);
|
---|
218 |
|
---|
219 | }
|
---|
220 | if (skeletonArray.Length != 0)
|
---|
221 | {
|
---|
222 | //screen.Refresh();
|
---|
223 | //screen2.Refresh();
|
---|
224 |
|
---|
225 |
|
---|
226 | twoPlayer = false;
|
---|
227 | for (int i = 0; i < skeletonArray.Length; i++)
|
---|
228 | {
|
---|
229 | if (skeletonManager.calibrated[i] == false) skeletonManager.Calibrate(i);
|
---|
230 |
|
---|
231 | if (skeletonManager.calibrated[i] == true)
|
---|
232 | {
|
---|
233 |
|
---|
234 | if (skeletonManager.skeletons[i].TrackingState == SkeletonTrackingState.NotTracked)
|
---|
235 | {
|
---|
236 | skeletonManager.calibrated[i] = false;
|
---|
237 | pboxShowCalibrated[i].BackColor = Color.DarkGray;
|
---|
238 | }
|
---|
239 | else
|
---|
240 | {
|
---|
241 |
|
---|
242 | pboxShowCalibrated[i].BackColor = Color.Cyan;
|
---|
243 | if (twoPlayer == false) graphics = screen.CreateGraphics();
|
---|
244 | else graphics = screen2.CreateGraphics();
|
---|
245 |
|
---|
246 | twoPlayer = true;
|
---|
247 |
|
---|
248 | int quadrentLeft = skeletonManager.FindQuadrant(skeletonManager.leftFoot[i].Z, skeletonManager.leftFoot[i].X, skeletonManager.centre[i]);
|
---|
249 | int quadrentRight = skeletonManager.FindQuadrant(skeletonManager.rightFoot[i].Z, skeletonManager.rightFoot[i].X, skeletonManager.centre[i]);
|
---|
250 |
|
---|
251 | graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, screen.Width, screen.Height);
|
---|
252 |
|
---|
253 | DrawQuadrent(quadrentLeft, graphics);
|
---|
254 | DrawQuadrent(quadrentRight, graphics);
|
---|
255 |
|
---|
256 | float xCoordLeft = centreScreen.X + (skeletonManager.leftFoot[i].X - skeletonManager.centre[i].X) * (screen.Width / 2) / 0.8f;
|
---|
257 | float yCoordLeft = centreScreen.Z + (skeletonManager.leftFoot[i].Z - skeletonManager.centre[i].Z) * (screen.Height / 2) / 0.8f;
|
---|
258 | float xCoordRight = centreScreen.X + (skeletonManager.rightFoot[i].X - skeletonManager.centre[i].X) * (screen.Width / 2) / 0.8f;
|
---|
259 | float yCoordRight = centreScreen.Z + (skeletonManager.rightFoot[i].Z - skeletonManager.centre[i].Z) * (screen.Height / 2) / 0.8f;
|
---|
260 |
|
---|
261 |
|
---|
262 |
|
---|
263 | graphics.DrawRectangle(new Pen(Color.Black),
|
---|
264 | (int)(xCoordLeft),
|
---|
265 | (int)(yCoordLeft),
|
---|
266 | 20, 20);
|
---|
267 |
|
---|
268 | graphics.DrawRectangle(new Pen(Color.Black),
|
---|
269 | (int)(xCoordRight),
|
---|
270 | (int)(yCoordRight),
|
---|
271 | 20, 20);
|
---|
272 |
|
---|
273 |
|
---|
274 |
|
---|
275 | }
|
---|
276 |
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 |
|
---|
281 |
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 |
|
---|
286 | }
|
---|
287 |
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | }
|
---|
292 |
|
---|
293 | private void DrawQuadrent(int num, Graphics graphics)
|
---|
294 | {
|
---|
295 | if (num == 1)
|
---|
296 | {
|
---|
297 | graphics.FillRectangle(new SolidBrush(Color.Red), 0, 0, screen.Width / 2, screen.Height / 2);
|
---|
298 | }
|
---|
299 | else if (num == 2)
|
---|
300 | {
|
---|
301 | graphics.FillRectangle(new SolidBrush(Color.Blue), screen.Width / 2, 0, screen.Width / 2, screen.Height / 2);
|
---|
302 | }
|
---|
303 | else if (num == 3)
|
---|
304 | {
|
---|
305 | graphics.FillRectangle(new SolidBrush(Color.Green), screen.Height / 2, screen.Width / 2, screen.Width / 2, screen.Height / 2);
|
---|
306 | }
|
---|
307 | else if (num == 4)
|
---|
308 | {
|
---|
309 | graphics.FillRectangle(new SolidBrush(Color.Yellow), 0, screen.Height / 2, screen.Width / 2, screen.Height / 2);
|
---|
310 | }
|
---|
311 | graphics.FillEllipse(new SolidBrush(Color.Black), screen.Width / 2 - (deadspotSize / 2), screen.Height / 2 - (deadspotSize / 2), deadspotSize, deadspotSize);
|
---|
312 |
|
---|
313 |
|
---|
314 |
|
---|
315 | }
|
---|
316 |
|
---|
317 | }
|
---|
318 | }
|
---|