-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMainWindow.xaml.cs
438 lines (367 loc) · 15.1 KB
/
MainWindow.xaml.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// <description>
// Demonstrates usage of the Kinect Tooling APIs, including basic record/playback functionality
// </description>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.RecordAndPlaybackBasics
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using Microsoft.Kinect;
using Microsoft.Kinect.Tools;
/// <summary>
/// Interaction logic for the MainWindow
/// </summary>
public sealed partial class MainWindow : Window, INotifyPropertyChanged, IDisposable
{
/// <summary> Indicates if a recording is currently in progress </summary>
private bool isRecording = false;
/// <summary> Indicates if a playback is currently in progress </summary>
private bool isPlaying = false;
private string lastFile = string.Empty;
/// <summary> Recording duration </summary>
private TimeSpan duration = TimeSpan.FromSeconds(5);
/// <summary> Number of playback iterations </summary>
private uint loopCount = 0;
/// <summary> Delegate to use for placing a job with no arguments onto the Dispatcher </summary>
private delegate void NoArgDelegate();
/// <summary>
/// Delegate to use for placing a job with a single string argument onto the Dispatcher
/// </summary>
/// <param name="arg">string argument</param>
private delegate void OneArgDelegate(string arg);
/// <summary> Active Kinect sensor </summary>
private KinectSensor kinectSensor = null;
/// <summary> Current kinect sesnor status text to display </summary>
private string kinectStatusText = string.Empty;
/// <summary>
/// Current record/playback status text to display
/// </summary>
private string recordPlayStatusText = string.Empty;
/// <summary>
/// Infrared visualizer
/// </summary>
private KinectIRView kinectIRView = null;
/// <summary>
/// Depth visualizer
/// </summary>
private KinectDepthView kinectDepthView = null;
/// <summary>
/// BodyIndex visualizer
/// </summary>
private KinectBodyIndexView kinectBodyIndexView = null;
/// <summary>
/// Body visualizer
/// </summary>
private KinectBodyView kinectBodyView = null;
/// <summary>
/// Initializes a new instance of the MainWindow class.
/// </summary>
public MainWindow()
{
// initialize the components (controls) of the window
this.InitializeComponent();
// get the kinectSensor object
this.kinectSensor = KinectSensor.GetDefault();
// set IsAvailableChanged event notifier
this.kinectSensor.IsAvailableChanged += this.Sensor_IsAvailableChanged;
// open the sensor
this.kinectSensor.Open();
// set the status text
this.KinectStatusText = this.kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText
: Properties.Resources.NoSensorStatusText;
// create the IR visualizer
this.kinectIRView = new KinectIRView(this.kinectSensor);
// create the Depth visualizer
this.kinectDepthView = new KinectDepthView(this.kinectSensor);
// create the BodyIndex visualizer
this.kinectBodyIndexView = new KinectBodyIndexView(this.kinectSensor);
// create the Body visualizer
this.kinectBodyView = new KinectBodyView(this.kinectSensor);
// set data context for display in UI
this.DataContext = this;
this.kinectIRViewbox.DataContext = this.kinectIRView;
this.kinectDepthViewbox.DataContext = this.kinectDepthView;
this.kinectBodyIndexViewbox.DataContext = this.kinectBodyIndexView;
this.kinectBodyViewbox.DataContext = this.kinectBodyView;
}
/// <summary>
/// INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Gets or sets the current status text to display
/// </summary>
public string KinectStatusText
{
get
{
return this.kinectStatusText;
}
set
{
if (this.kinectStatusText != value)
{
this.kinectStatusText = value;
// notify any bound elements that the text has changed
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("KinectStatusText"));
}
}
}
}
/// <summary>
/// Gets or sets the current status text to display for the record/playback features
/// </summary>
public string RecordPlaybackStatusText
{
get
{
return this.recordPlayStatusText;
}
set
{
if (this.recordPlayStatusText != value)
{
this.recordPlayStatusText = value;
// notify any bound elements that the text has changed
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("RecordPlaybackStatusText"));
}
}
}
}
/// <summary>
/// Disposes all unmanaged resources for the class
/// </summary>
public void Dispose()
{
if (this.kinectIRView != null)
{
this.kinectIRView.Dispose();
this.kinectIRView = null;
}
if (this.kinectDepthView != null)
{
this.kinectDepthView.Dispose();
this.kinectDepthView = null;
}
if (this.kinectBodyIndexView != null)
{
this.kinectBodyIndexView.Dispose();
this.kinectBodyIndexView = null;
}
if (this.kinectBodyView != null)
{
this.kinectBodyView.Dispose();
this.kinectBodyView = null;
}
}
/// <summary>
/// Execute shutdown tasks
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
if (this.kinectIRView != null)
{
this.kinectIRView.Dispose();
this.kinectIRView = null;
}
if (this.kinectDepthView != null)
{
this.kinectDepthView.Dispose();
this.kinectDepthView = null;
}
if (this.kinectBodyIndexView != null)
{
this.kinectBodyIndexView.Dispose();
this.kinectBodyIndexView = null;
}
if (this.kinectBodyView != null)
{
this.kinectBodyView.Dispose();
this.kinectBodyView = null;
}
if (this.kinectSensor != null)
{
this.kinectSensor.Close();
this.kinectSensor = null;
}
}
/// <summary>
/// Handles the event in which the sensor becomes unavailable (E.g. paused, closed, unplugged).
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void Sensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs e)
{
// set the status text
this.KinectStatusText = this.kinectSensor.IsAvailable ? Properties.Resources.RunningStatusText
: Properties.Resources.SensorNotAvailableStatusText;
}
/// <summary>
/// Handles the user clicking on the Record button
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void RecordButton_Click(object sender, RoutedEventArgs e)
{
string filePath = this.SaveRecordingAs();
if (!string.IsNullOrEmpty(filePath))
{
this.lastFile = filePath;
this.isRecording = true;
this.RecordPlaybackStatusText = Properties.Resources.RecordingInProgressText;
this.UpdateState();
// Start running the recording asynchronously
OneArgDelegate recording = new OneArgDelegate(this.RecordClip);
recording.BeginInvoke(filePath, null, null);
}
}
/// <summary>
/// Handles the user clicking on the Play button
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void PlayButton_Click(object sender, RoutedEventArgs e)
{
string filePath = this.OpenFileForPlayback();
if (!string.IsNullOrEmpty(filePath))
{
this.lastFile = filePath;
this.isPlaying = true;
this.RecordPlaybackStatusText = Properties.Resources.PlaybackInProgressText;
this.UpdateState();
// Start running the playback asynchronously
OneArgDelegate playback = new OneArgDelegate(this.PlaybackClip);
playback.BeginInvoke(filePath, null, null);
}
}
/// <summary>
/// Records a new .xef file
/// </summary>
/// <param name="filePath">Full path to where the file should be saved to</param>
private void RecordClip(string filePath)
{
using (KStudioClient client = KStudio.CreateClient())
{
client.ConnectToService();
// Specify which streams should be recorded
KStudioEventStreamSelectorCollection streamCollection = new KStudioEventStreamSelectorCollection();
streamCollection.Add(KStudioEventStreamDataTypeIds.Ir);
streamCollection.Add(KStudioEventStreamDataTypeIds.Depth);
streamCollection.Add(KStudioEventStreamDataTypeIds.Body);
streamCollection.Add(KStudioEventStreamDataTypeIds.BodyIndex);
// Create the recording object
using (KStudioRecording recording = client.CreateRecording(filePath, streamCollection))
{
recording.StartTimed(this.duration);
while (recording.State == KStudioRecordingState.Recording)
{
Thread.Sleep(500);
}
}
client.DisconnectFromService();
}
// Update UI after the background recording task has completed
this.isRecording = false;
this.Dispatcher.BeginInvoke(new NoArgDelegate(UpdateState));
}
/// <summary>
/// Plays back a .xef file to the Kinect sensor
/// </summary>
/// <param name="filePath">Full path to the .xef file that should be played back to the sensor</param>
private void PlaybackClip(string filePath)
{
using (KStudioClient client = KStudio.CreateClient())
{
client.ConnectToService();
// Create the playback object
using (KStudioPlayback playback = client.CreatePlayback(filePath))
{
playback.LoopCount = this.loopCount;
playback.Start();
while (playback.State == KStudioPlaybackState.Playing)
{
Thread.Sleep(500);
}
}
client.DisconnectFromService();
}
// Update the UI after the background playback task has completed
this.isPlaying = false;
this.Dispatcher.BeginInvoke(new NoArgDelegate(UpdateState));
}
/// <summary>
/// Enables/Disables the record and playback buttons in the UI
/// </summary>
private void UpdateState()
{
if (this.isPlaying || this.isRecording)
{
this.PlayButton.IsEnabled = false;
this.RecordButton.IsEnabled = false;
}
else
{
this.RecordPlaybackStatusText = string.Empty;
this.PlayButton.IsEnabled = true;
this.RecordButton.IsEnabled = true;
}
}
/// <summary>
/// Launches the OpenFileDialog window to help user find/select an event file for playback
/// </summary>
/// <returns>Path to the event file selected by the user</returns>
private string OpenFileForPlayback()
{
string fileName = string.Empty;
OpenFileDialog dlg = new OpenFileDialog();
dlg.FileName = this.lastFile;
dlg.DefaultExt = Properties.Resources.XefExtension; // Default file extension
dlg.Filter = Properties.Resources.EventFileDescription + " " + Properties.Resources.EventFileFilter; // Filter files by extension
bool? result = dlg.ShowDialog();
if (result == true)
{
fileName = dlg.FileName;
}
return fileName;
}
/// <summary>
/// Launches the SaveFileDialog window to help user create a new recording file
/// </summary>
/// <returns>File path to use when recording a new event file</returns>
private string SaveRecordingAs()
{
string fileName = string.Empty;
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "recordAndPlaybackBasics.xef";
dlg.DefaultExt = Properties.Resources.XefExtension;
dlg.AddExtension = true;
dlg.Filter = Properties.Resources.EventFileDescription + " " + Properties.Resources.EventFileFilter;
dlg.CheckPathExists = true;
bool? result = dlg.ShowDialog();
if (result == true)
{
fileName = dlg.FileName;
}
return fileName;
}
}
}