forked from microsoft/Cognitive-Samples-IntelligentKiosk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeechToTextControl.xaml.cs
302 lines (266 loc) · 12.8 KB
/
SpeechToTextControl.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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Cognitive Services: http://www.microsoft.com/cognitive
//
// Microsoft Cognitive Services Github:
// https://github.com/Microsoft/Cognitive
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using ServiceHelpers;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Media.SpeechRecognition;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
namespace IntelligentKioskSample.Controls
{
public class SpeechRecognitionAndSentimentResult
{
public string SpeechRecognitionText { get; set; }
public double TextAnalysisSentiment { get; set; }
}
public sealed partial class SpeechToTextControl : UserControl
{
public event EventHandler<SpeechRecognitionAndSentimentResult> SpeechRecognitionAndSentimentProcessed;
private SpeechRecognizer speechRecognizer;
private bool isCapturingSpeech;
private static uint HResultPrivacyStatementDeclined = 0x80045509;
// Keep track of existing text that we've accepted in ContinuousRecognitionSession_ResultGenerated(), so
// that we can combine it and Hypothesized results to show in-progress dictation mid-sentence.
private StringBuilder dictatedTextBuilder;
public SpeechToTextControl()
{
this.InitializeComponent();
}
#region Speech Recognizer and Text Analytics
public async Task InitializeSpeechRecognizerAsync()
{
if (this.speechRecognizer != null)
{
this.DisposeSpeechRecognizer();
}
this.dictatedTextBuilder = new StringBuilder();
this.speechRecognizer = new SpeechRecognizer();
var dictationConstraint = new SpeechRecognitionTopicConstraint(SpeechRecognitionScenario.Dictation, "dictation");
speechRecognizer.Constraints.Add(dictationConstraint);
SpeechRecognitionCompilationResult result = await speechRecognizer.CompileConstraintsAsync();
if (result.Status != SpeechRecognitionResultStatus.Success)
{
await new MessageDialog("CompileConstraintsAsync returned " + result.Status, "Error initializing SpeechRecognizer").ShowAsync();
return;
}
this.speechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated; ;
this.speechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
this.speechRecognizer.HypothesisGenerated += SpeechRecognizer_HypothesisGenerated;
}
private async void ContinuousRecognitionSession_ResultGenerated(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionResultGeneratedEventArgs args)
{
if (args.Result.Confidence == SpeechRecognitionConfidence.Medium ||
args.Result.Confidence == SpeechRecognitionConfidence.High)
{
dictatedTextBuilder.Append(args.Result.Text + " ");
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
this.speechRecognitionTextBox.Text = dictatedTextBuilder.ToString();
});
}
}
private async void ContinuousRecognitionSession_Completed(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionCompletedEventArgs args)
{
if (args.Status != SpeechRecognitionResultStatus.Success)
{
// If TimeoutExceeded occurs, the user has been silent for too long. We can use this to
// cancel recognition if the user in dictation mode and walks away from their device, etc.
// In a global-command type scenario, this timeout won't apply automatically.
// With dictation (no grammar in place) modes, the default timeout is 20 seconds.
if (args.Status == SpeechRecognitionResultStatus.TimeoutExceeded)
{
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
this.speechRecognitionControlButtonSymbol.Symbol = Symbol.Refresh;
this.speechRecognitionTextBox.PlaceholderText = "";
this.speechRecognitionTextBox.Text = dictatedTextBuilder.ToString();
this.isCapturingSpeech = false;
});
}
else
{
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
this.speechRecognitionControlButtonSymbol.Symbol = Symbol.Refresh;
this.speechRecognitionTextBox.PlaceholderText = "";
this.isCapturingSpeech = false;
});
}
}
}
public void DisposeSpeechRecognizer()
{
if (this.speechRecognizer != null)
{
try
{
this.speechRecognizer.ContinuousRecognitionSession.ResultGenerated -= ContinuousRecognitionSession_ResultGenerated;
this.speechRecognizer.ContinuousRecognitionSession.Completed -= ContinuousRecognitionSession_Completed;
this.speechRecognizer.HypothesisGenerated -= SpeechRecognizer_HypothesisGenerated;
this.speechRecognizer.Dispose();
this.speechRecognizer = null;
}
catch (Exception) { }
}
}
private async void SpeechRecognizer_HypothesisGenerated(SpeechRecognizer sender, SpeechRecognitionHypothesisGeneratedEventArgs args)
{
string hypothesis = args.Hypothesis.Text;
// Update the textbox with the currently confirmed text, and the hypothesis combined.
string textboxContent = dictatedTextBuilder.ToString() + " " + hypothesis + " ...";
await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
this.speechRecognitionTextBox.Text = textboxContent;
});
}
private async void OnSpeechRecognitionFlyoutOpened(object sender, object e)
{
try
{
this.speechRecognitionControlButton.Focus(FocusState.Programmatic);
await StartSpeechRecognition();
} catch (Exception ex)
{
if ((uint)ex.HResult == HResultPrivacyStatementDeclined)
{
await Util.ConfirmActionAndExecute(
"The Speech Privacy settings need to be enabled. Under 'Settings->Privacy->Speech, inking and typing', ensure you have viewed the privacy policy, and 'Get To Know You' is enabled. Want to open the settings now?",
async () =>
{
// Open the privacy/speech, inking, and typing settings page.
await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-speechtyping"));
});
}
else
{
await Util.GenericApiCallExceptionHandler(ex, "Error starting SpeechRecognizer.");
}
}
}
private async void OnSpeechRecognitionFlyoutClosed(object sender, object e)
{
try
{
if (this.speechRecognizer.State != SpeechRecognizerState.Idle)
{
await speechRecognizer.ContinuousRecognitionSession.StopAsync();
}
}
catch (Exception)
{
}
}
private async Task StartSpeechRecognition()
{
this.isCapturingSpeech = true;
this.speechRecognitionControlButtonSymbol.Symbol = Symbol.Stop;
if (this.speechRecognizer == null)
{
await this.InitializeSpeechRecognizerAsync();
}
this.speechRecognitionTextBox.Text = "";
this.speechRecognitionTextBox.PlaceholderText = "Listening...";
this.dictatedTextBuilder.Clear();
this.sentimentControl.Sentiment = 0.5;
await this.speechRecognizer.ContinuousRecognitionSession.StartAsync();
}
private async void SpeechRecognitionButtonClick(object sender, RoutedEventArgs e)
{
if (this.isCapturingSpeech)
{
this.isCapturingSpeech = false;
this.speechRecognitionControlButtonSymbol.Symbol = Symbol.Refresh;
this.speechRecognitionTextBox.PlaceholderText = "";
if (speechRecognizer.State != SpeechRecognizerState.Idle)
{
// Cancelling recognition prevents any currently recognized speech from
// generating a ResultGenerated event. StopAsync() will allow the final session to
// complete.
try
{
await speechRecognizer.ContinuousRecognitionSession.StopAsync();
string dictatedTextAfterStop = dictatedTextBuilder.ToString();
// Ensure we don't leave any hypothesis text behind
if (!string.IsNullOrEmpty(dictatedTextAfterStop))
{
this.speechRecognitionTextBox.Text = dictatedTextAfterStop;
}
else if (!string.IsNullOrEmpty(this.speechRecognitionTextBox.Text) && this.speechRecognitionTextBox.Text.EndsWith(" ..."))
{
this.speechRecognitionTextBox.Text = this.speechRecognitionTextBox.Text.Replace(" ...", ".");
}
}
catch (Exception exception)
{
await Util.GenericApiCallExceptionHandler(exception, "Error stopping SpeechRecognizer.");
}
}
await this.AnalyzeTextAsync();
}
else
{
await this.StartSpeechRecognition();
}
}
private async Task AnalyzeTextAsync()
{
try
{
if (!string.IsNullOrEmpty(this.speechRecognitionTextBox.Text))
{
SentimentResult textAnalysisResult = await TextAnalyticsHelper.GetSentimentAsync(new string[] { this.speechRecognitionTextBox.Text });
double score = textAnalysisResult.Scores.ElementAt(0);
this.sentimentControl.Sentiment = score;
}
else
{
this.sentimentControl.Sentiment = 0.5;
}
this.OnSpeechRecognitionAndSentimentProcessed(new SpeechRecognitionAndSentimentResult { SpeechRecognitionText = this.speechRecognitionTextBox.Text, TextAnalysisSentiment = this.sentimentControl.Sentiment });
}
catch (Exception ex)
{
await Util.GenericApiCallExceptionHandler(ex, "Error during Text Analytics call.");
}
}
private void OnSpeechRecognitionAndSentimentProcessed(SpeechRecognitionAndSentimentResult result)
{
this.SpeechRecognitionAndSentimentProcessed?.Invoke(this, result);
}
#endregion
}
}