-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
234 lines (188 loc) · 8.17 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
//------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.SpeechBasics
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using Microsoft.Kinect;
using Microsoft.Speech.AudioFormat;
using Microsoft.Speech.Recognition;
using System.Net;
using Speech.Synthesis;
using System.Windows.Controls;
using Abilities;
using System.Media;
public partial class MainWindow : Window
{
IVoiceAbility[] abilities = {
new CommuteAbility(),
new HueAbility()
};
private KinectSensor kinectSensor = null;
private KinectAudioStream convertStream = null;
private SpeechRecognitionEngine speechEngine = null;
private List<Span> recognitionSpans;
private Choices commands = new Choices();
private Dictionary<string, IVoiceAbility> commandToVoiceAbility = new Dictionary<string, IVoiceAbility>();
private List<String> allValidPhrases = new List<String>();
private Dictionary<string, string> phraseToCommand = new Dictionary<string, string>();
public MainWindow()
{
this.InitializeComponent();
foreach (IVoiceAbility ability in abilities)
{
Dictionary<string, string[]> commandsAndPhrases = ability.GetCommandsAndPhrases();
foreach (String command in commandsAndPhrases.Keys)
{
string[] phrases = commandsAndPhrases[command];
foreach (String phrase in phrases)
{
commands.Add(new SemanticResultValue(phrase, command));
allValidPhrases.Add(phrase);
phraseToCommand.Add(phrase, command);
}
commandToVoiceAbility.Add(command, ability);
}
}
}
public async void HandleCommandString(string command)
{
if (!Utils.Blocking.IsBlocking())
{
IVoiceAbility relevantAbility = commandToVoiceAbility[command];
relevantAbility.Execute(command);
}
else
{
}
}
private static RecognizerInfo TryGetKinectRecognizer()
{
IEnumerable<RecognizerInfo> recognizers;
try
{
recognizers = SpeechRecognitionEngine.InstalledRecognizers();
}
catch (COMException)
{
return null;
}
foreach (RecognizerInfo recognizer in recognizers)
{
string value;
recognizer.AdditionalInfo.TryGetValue("Kinect", out value);
if ("True".Equals(value, StringComparison.OrdinalIgnoreCase) && "en-US".Equals(recognizer.Culture.Name, StringComparison.OrdinalIgnoreCase))
{
return recognizer;
}
}
return null;
}
private void WindowLoaded(object sender, RoutedEventArgs e)
{
// Only one sensor is supported
this.kinectSensor = KinectSensor.GetDefault();
if (this.kinectSensor != null)
{
// open the sensor
this.kinectSensor.Open();
// grab the audio stream
IReadOnlyList<AudioBeam> audioBeamList = this.kinectSensor.AudioSource.AudioBeams;
System.IO.Stream audioStream = audioBeamList[0].OpenInputStream();
// create the convert stream
this.convertStream = new KinectAudioStream(audioStream);
}
else
{
// on failure, set the status text
this.statusBarText.Text = Properties.Resources.NoKinectReady;
return;
}
RecognizerInfo ri = TryGetKinectRecognizer();
if (null != ri)
{
this.speechEngine = new SpeechRecognitionEngine(ri.Id);
var gb = new GrammarBuilder { Culture = ri.Culture };
gb.Append(commands);
var g = new Grammar(gb);
this.speechEngine.LoadGrammar(g);
this.speechEngine.SpeechRecognized += this.SpeechRecognized;
this.speechEngine.SpeechRecognitionRejected += this.SpeechRejected;
this.speechEngine.SpeechHypothesized += this.SpeechHypothesized;
// let the convertStream know speech is going active
this.convertStream.SpeechActive = true;
// For long recognition sessions (a few hours or more), it may be beneficial to turn off adaptation of the acoustic model.
// This will prevent recognition accuracy from degrading over time.
speechEngine.UpdateRecognizerSetting("AdaptationOn", 0);
this.speechEngine.SetInputToAudioStream(
this.convertStream, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
this.speechEngine.RecognizeAsync(RecognizeMode.Multiple);
}
else
{
this.statusBarText.Text = Properties.Resources.NoSpeechRecognizer;
}
}
private void WindowClosing(object sender, CancelEventArgs e)
{
if (null != this.convertStream)
{
this.convertStream.SpeechActive = false;
}
if (null != this.speechEngine)
{
this.speechEngine.SpeechRecognized -= this.SpeechRecognized;
this.speechEngine.SpeechRecognitionRejected -= this.SpeechRejected;
this.speechEngine.RecognizeAsyncStop();
}
if (null != this.kinectSensor)
{
this.kinectSensor.Close();
this.kinectSensor = null;
}
}
bool alreadyHandled = false;
SoundPlayer player = new SoundPlayer("C:\\Users\\Joshua\\Documents\\SpeechBasics-WPF\\Images\\startlisten.wav");
private void SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
{
LogText.Text = e.Result.Confidence + ": " + e.Result.Text + "\n" + LogText.Text;
if (e.Result.Confidence < 0.25)
return;
if (!alreadyHandled &&
allValidPhrases.Contains(e.Result.Text.Trim()))
{
LogText.Text = "Handling..." + "\n" + LogText.Text;
if (e.Result.Semantics.Value != null)
HandleCommandString(e.Result.Semantics.Value.ToString());
else
HandleCommandString(phraseToCommand[e.Result.Text]);
alreadyHandled = true;
}
}
private void SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
LogText.Text = "> " + e.Result.Confidence + ": " + e.Result.Text + "\n" + LogText.Text;
if (!alreadyHandled) {
if (e.Result.Confidence > 0.25)
{
LogText.Text = "> Handling..." + "\n" + LogText.Text;
HandleCommandString(e.Result.Semantics.Value.ToString());
}
}
alreadyHandled = false;
}
private void SpeechRejected(object sender, SpeechRecognitionRejectedEventArgs e)
{
}
}
}