-
Notifications
You must be signed in to change notification settings - Fork 0
/
KinectAudioViewer.xaml.cs
185 lines (159 loc) · 12.5 KB
/
KinectAudioViewer.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
//------------------------------------------------------------------------------
// <copyright file="KinectAudioViewer.xaml.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.WpfViewers
{
using System;
using System.Windows.Controls;
using Microsoft.Kinect;
/// <summary>
/// Interaction logic for KinectAudioViewer.xaml
/// </summary>
public partial class KinectAudioViewer : ImageViewer
{
private double angle;
private double soundSourceAngle;
public KinectAudioViewer()
{
InitializeComponent();
this.MarkWidth = 0.05;
this.SoundSourceWidth = 0.05;
this.BeamAngleInDegrees = 0;
this.BeamDisplayText = null;
this.SoundSourceAngleInDegrees = 0;
this.SoundSourceDisplayText = null;
}
/// <summary>
/// Gets or sets string overlayed on beam indicator
/// </summary>
public string BeamDisplayText
{
get
{
return txtDisplayBeam.Text;
}
set
{
txtDisplayBeam.Text = value;
}
}
/// <summary>
/// Gets or sets string overlayed on sound source indicator
/// </summary>
public string SoundSourceDisplayText
{
get
{
return txtDisplaySource.Text;
}
set
{
txtDisplaySource.Text = value;
}
}
/// <summary>
/// Gets or sets width of the beam mark, in the 0-0.5 range
/// </summary>
public double MarkWidth { get; set; }
/// <summary>
/// Gets or sets audio beam angle, in degrees
/// </summary>
public double BeamAngleInDegrees
{
get
{
return this.angle;
}
set
{
// save RAW sensor value
this.angle = value;
// Angle is in Degrees, so map the MinBeamAngle..MaxBeamAngle range to 0..1
// and clamp
double gradientOffset = (value / (KinectAudioSource.MaxBeamAngle - KinectAudioSource.MinBeamAngle)) + 0.5;
if (gradientOffset > 1.0)
{
gradientOffset = 1.0;
}
if (gradientOffset < 0.0)
{
gradientOffset = 0.0;
}
// Move the gradient stops together
this.gsPre.Offset = Math.Max(gradientOffset - this.MarkWidth, 0);
gsIt.Offset = gradientOffset;
this.gsPos.Offset = Math.Min(gradientOffset + this.MarkWidth, 1);
}
}
/// <summary>
/// Gets or sets width of the sound source mark, in the 0-0.5 range
/// </summary>
public double SoundSourceWidth { get; set; }
/// <summary>
/// Gets or sets sound direction angle, in degrees
/// </summary>
public double SoundSourceAngleInDegrees
{
get
{
return this.soundSourceAngle;
}
set
{
// save RAW sensor value
this.soundSourceAngle = value;
// Angle is in Degrees, so map the MinSoundSourceAngle..MaxSoundSourceAngle range to 0..1
// and clamp
double gradientOffset = (value
/
(KinectAudioSource.MaxSoundSourceAngle - KinectAudioSource.MinSoundSourceAngle))
+ 0.5;
if (gradientOffset > 1.0)
{
gradientOffset = 1.0;
}
if (gradientOffset < 0.0)
{
gradientOffset = 0.0;
}
// Move the gradient stops together
this.gsPreS.Offset = Math.Max(gradientOffset - this.SoundSourceWidth, 0);
gsItS.Offset = gradientOffset;
this.gsPosS.Offset = Math.Min(gradientOffset + this.SoundSourceWidth, 1);
}
}
protected override void OnKinectChanged(KinectSensor oldKinectSensor, KinectSensor newKinectSensor)
{
if (oldKinectSensor != null && oldKinectSensor.AudioSource != null)
{
// remove old handlers
oldKinectSensor.AudioSource.BeamAngleChanged -= this.AudioSourceBeamChanged;
oldKinectSensor.AudioSource.SoundSourceAngleChanged -= this.AudioSourceSoundSourceAngleChanged;
}
if (newKinectSensor != null && newKinectSensor.AudioSource != null)
{
// add new handlers
newKinectSensor.AudioSource.BeamAngleChanged += this.AudioSourceBeamChanged;
newKinectSensor.AudioSource.SoundSourceAngleChanged += this.AudioSourceSoundSourceAngleChanged;
}
}
private void AudioSourceSoundSourceAngleChanged(object sender, SoundSourceAngleChangedEventArgs e)
{
// Set width of mark based on confidence
this.SoundSourceWidth = Math.Max(((1 - e.ConfidenceLevel) / 2), 0.02);
// Move indicator
this.SoundSourceAngleInDegrees = e.Angle;
// Update text
this.SoundSourceDisplayText = " Sound source angle = " + this.SoundSourceAngleInDegrees.ToString("0.00") + " deg Confidence level=" + e.ConfidenceLevel.ToString("0.00");
}
private void AudioSourceBeamChanged(object sender, BeamAngleChangedEventArgs e)
{
// Move our indicator
this.BeamAngleInDegrees = e.Angle;
// Update Text
this.BeamDisplayText = " Audio beam angle = " + this.BeamAngleInDegrees.ToString("0.00") + " deg";
}
}
}