-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathSpeechManager.cs
852 lines (742 loc) · 32.5 KB
/
SpeechManager.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
// ---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// 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.
// ---------------------------------------------------------------------------------
//#define VERBOSE_DEBUG
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Resources.Core;
using Windows.Globalization;
using Windows.Media.Capture;
using Windows.Media.SpeechRecognition;
using Windows.Media.SpeechSynthesis;
using Windows.System;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;
namespace FamilyNotes
{
/// <summary>
/// Provides speech recognition and speech synthesis services
/// for the FamilyNotes app.
/// </summary>
public class SpeechManager
{
/// <summary>
/// Initializes a new instance of the <see cref="SpeechManager"/> class.
/// </summary>
/// <param name="model">The app's data model, which has a collection
/// of <see cref="Person"/> instances.</param>
public SpeechManager(Model model)
{
if (model != null)
{
Family = model.Family;
Family.CollectionChanged += Family_CollectionChanged;
}
else
{
throw new ArgumentNullException("model", "Model can't be null");
}
}
private void InitializeRecognizer()
{
try
{
// Initialize resource map to retrieve localized speech strings.
Language speechLanguage = SpeechRecognizer.SystemSpeechLanguage;
string langTag = speechLanguage.LanguageTag;
_speechContext = ResourceContext.GetForCurrentView();
_speechContext.Languages = new string[] { langTag };
// Create the speech recognizer instance.
_speechRecognizer = new SpeechRecognizer(SpeechRecognizer.SystemSpeechLanguage);
// Be aware of state changes in the speech recognizer instance.
_speechRecognizer.StateChanged += SpeechRecognizer_StateChanged;
}
catch (Exception ex)
{
if ((uint)ex.HResult == RecognizerNotFoundHResult)
{
Debug.WriteLine("SpeechManager: The speech language pack for selected language isn't installed.");
}
else
{
Debug.WriteLine(ex.ToString());
}
}
}
/// <summary>
/// Gets or sets the kind of speech that the SpeechRecognizer listens for.
/// </summary>
/// <remarks>
/// <para>Currently supported modes are command-list and dictation.</para>
/// <para>When the recognition mode changes, the grammar is re-compiled.</para>
/// </remarks>
public SpeechRecognitionMode RecognitionMode { get; private set; }
/// <summary>
/// Gets the <see cref="Person"/> collection from the FamilyNotes app's data model.
/// </summary>
public ObservableCollection<Person> Family { get; private set; }
/// <summary>
/// Assigns the kind of speech that the <see cref="SpeechManager"/> listens
/// for: commands or dictation.
/// </summary>
/// <param name="mode">The recognition mode.</param>
/// <returns>Void</returns>
public async Task<bool> SetRecognitionMode(SpeechRecognitionMode mode)
{
bool success = true;
if (mode != RecognitionMode)
{
RecognitionMode = mode;
if (mode == SpeechRecognitionMode.Paused)
{
await EndRecognitionSession();
}
else
{
if (!await StartContinuousRecognition())
{
success = false;
mode = SpeechRecognitionMode.Default;
}
}
OnModeChanged(new EventArgs());
}
return success;
}
public async Task<bool> StartContinuousRecognition()
{
// Compiling a new grammar is potentially a high-latency operation,
// and it's easy for various threads to call this method concurrently,
// so use a sempahore to serialize access to this method. The semaphore
// allows only one thread at a time to execute this code path.
await Mutex.WaitAsync();
if (IsInRecognitionSession)
{
// End the previous speech recognition session.
await EndRecognitionSession();
}
#if VERBOSE_DEBUG
Debug.WriteLine(
"SpeechManager: Starting recognition session: {0}",
RecognitionMode );
#endif
try
{
// If no mic is available, notify the user and reset mode to default.
if (!await IsMicrophoneAvailable())
{
if (RecognitionMode == SpeechRecognitionMode.CommandPhrases ||
RecognitionMode == SpeechRecognitionMode.Dictation)
{
var messageDialog = new Windows.UI.Popups.MessageDialog("Microphone is not available.");
messageDialog.Commands.Add(new UICommand("Go to settings...", async (command) =>
{
bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-microphone"));
}));
messageDialog.Commands.Add(new UICommand("Close", (command) => { }));
await messageDialog.ShowAsync();
}
RecognitionMode = SpeechRecognitionMode.Default;
Mutex.Release();
return false;
}
// Compile the grammar, based on the value of the RecognitionMode property.
await CompileGrammar();
// You can attach these event handlers only after the grammar is compiled.
SpeechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
SpeechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;
// Start the recognition session.
await SpeechRecognizer.ContinuousRecognitionSession.StartAsync();
// Keep track of the the recognition session's state.
IsInRecognitionSession = true;
#if VERBOSE_DEBUG
Debug.WriteLine( "SpeechManager: Continuous recognition session started" );
#endif
}
catch (Exception ex)
{
Debug.WriteLine("SpeechManager: Failed to start continuous recognition session.");
var messageDialog = new Windows.UI.Popups.MessageDialog(
$"{ex.Message}",
"Failed to start continuous recognition session");
messageDialog.Commands.Add(new UICommand("Go to settings...", async (command) =>
{
bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-speech"));
}));
messageDialog.Commands.Add(new UICommand("Close", (command) => { }));
await messageDialog.ShowAsync();
Mutex.Release();
return false;
}
finally
{
Mutex.Release();
}
return true;
}
/// <summary>
/// Reads the specified phrase, with the default text-to-speech voice.
/// </summary>
/// <param name="phrase">The text to say.</param>
/// <param name="media">The <see cref="MediaElement"/> that plays the speech.</param>
/// <remarks><para>This method is awaitable, because in the case of a speech prompt,
/// the speech recognizer can hear the prompt and may process it, along with
/// the user's speech. Avoid this bug by awaiting the call to the <see cref="SpeakAsync"/> method
/// and then setting <see cref="RecognitionMode"/> to <see cref="SpeechRecognitionMode.Dictation"/>
/// after it completes. This way, the speech prompt ends before recognition begins.</para>
/// <para>Also, the <see cref="SpeakAsync"/> method stops the current recognition session,
/// so the user and any spoken prompts don't trigger speech commands.</para>
/// <para>The <see cref="SpeakAsync"/> method uses the <see cref="SemaphoreSlim"/> class to implement
/// a signal from the <see cref="MediaElement.MediaEnded"/> event handler to this method.
/// </para>
/// </remarks>
public async Task SpeakAsync(string phrase, MediaElement media)
{
if (!String.IsNullOrEmpty(phrase))
{
var previousMode = RecognitionMode;
// Turn off speech recognition while speech synthesis is happening.
await SetRecognitionMode(SpeechRecognitionMode.Paused);
MediaPlayerElement = media;
SpeechSynthesisStream synthesisStream = await SpeechSynth.SynthesizeTextToStreamAsync(phrase);
// The Play call starts the sound stream playback and immediately returns,
// so a semaphore is required to make the SpeakAsync method awaitable.
media.AutoPlay = true;
media.SetSource(synthesisStream, synthesisStream.ContentType);
media.Play();
// Wait until the MediaEnded event on MediaElement is raised,
// before turning on speech recognition again. The semaphore
// is signaled in the mediaElement_MediaEnded event handler.
await Semaphore.WaitAsync();
// Turn on speech recognition and listen for commands.
//await SetRecognitionMode(SpeechRecognitionMode.CommandPhrases);
await SetRecognitionMode(previousMode);
}
}
/// <summary>Raised when the recognition session produces a result.</summary>
/// <remarks>The handler for the <see cref="SpeechRecognizer.ContinuousRecognitionSession.ResultGenerated"/> event
/// raises this event.
/// </remarks>
public event EventHandler<PhraseRecognizedEventArgs> PhraseRecognized;
public delegate void PhraseRecognizedEventHandler(object sender, PhraseRecognizedEventArgs e);
protected virtual void OnPhraseRecognized(PhraseRecognizedEventArgs e)
{
PhraseRecognized?.Invoke(this, e);
}
/// <summary>
/// Raised when the state of the <see cref="Windows.Media.SpeechRecognition.SpeechRecognizer"/> changes.
/// </summary>
/// <remarks>The handler for the <see cref="SpeechRecognizer.StateChanged"/> event
/// raises this event.</remarks>
public event EventHandler<StateChangedEventArgs> StateChanged;
public delegate void StateChangedEventHandler(object sender, StateChangedEventArgs e);
protected virtual void OnStateChanged(StateChangedEventArgs e)
{
StateChanged?.Invoke(this, e);
}
/// <summary>
/// Raised when the state of the <see cref="Windows.Media.SpeechRecognition.SpeechRecognizer"/> changes.
/// </summary>
/// <remarks>The handler for the <see cref="SpeechRecognizer.StateChanged"/> event
/// raises this event.</remarks>
public event EventHandler<EventArgs> ModeChanged;
public delegate void ModeChangedEventHandler(object sender, EventArgs e);
protected virtual void OnModeChanged(EventArgs e)
{
ModeChanged?.Invoke(this, e);
}
#region Implementation for speech recognition
public bool IsInRecognitionSession { get; set; }
/// <summary>
/// Queries a <see cref="MediaCapture"/> instance for an audio device controller.
/// </summary>
/// <returns>True, if a microphone is found, otherwise false.</returns>
/// <remarks>TBD: is this the best/only way to test for a mic?</remarks>
private async Task<bool> IsMicrophoneAvailable()
{
bool isMicrophoneAvailable = false;
try
{
var captureDevice = new MediaCapture();
await captureDevice.InitializeAsync();
// Throws if no device is available.
var audioDevice = captureDevice.AudioDeviceController;
if (audioDevice != null)
{
#if VERBOSE_DEBUG
Debug.WriteLine( "SpeechManager: AudioDeviceController found" );
#endif
isMicrophoneAvailable = true;
}
else
{
Debug.WriteLine("SpeechManager: No AudioDeviceController found");
}
}
catch (COMException ex)
{
Debug.WriteLine(ex.Message);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
return isMicrophoneAvailable;
}
private async void Family_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// Re-compile the grammar for family members and restart the recognition
// session, but only if voice recognition is already active.
if (IsInRecognitionSession)
{
await StartContinuousRecognition();
}
}
private SpeechRecognizer SpeechRecognizer
{
get
{
if (_speechRecognizer == null)
{
InitializeRecognizer();
}
return _speechRecognizer;
}
}
private ResourceMap SpeechResourceMap
{
get
{
if (_speechResourceMap == null)
{
_speechResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("SpeechResources");
}
return _speechResourceMap;
}
}
private List<string> AvailablePhrases { get; set; }
private Dictionary<string, Person> PhraseToPersonDictionary
{
get
{
if (_phraseToPersonDictionary == null)
{
_phraseToPersonDictionary = new Dictionary<string, Person>();
}
return _phraseToPersonDictionary;
}
}
private void PopulatePhrases()
{
AvailablePhrases = new List<string>();
if (Family != null && Family.Count > 0)
{
PhraseToPersonDictionary.Clear();
var familyList = Family.ToList();
familyList.ForEach(person =>
{
var phrases = GetPhrasesForPerson(person);
AvailablePhrases = AvailablePhrases.Concat(phrases).ToList();
phrases.ForEach(phrase =>
{
if (!PhraseToPersonDictionary.Keys.Contains(phrase))
{
PhraseToPersonDictionary.Add(phrase, person);
}
});
});
}
AvailablePhrases.Add(GetGrammarResourceString("GrammarHelp"));
AvailablePhrases.Add(GetGrammarResourceString("GrammarWhatCanISay"));
AvailablePhrases.Add(GetGrammarResourceString("GrammarReadNote"));
AvailablePhrases.Add(GetGrammarResourceString("GrammarDeleteNote"));
AvailablePhrases.Add(GetGrammarResourceString("GrammarEditNote"));
AvailablePhrases.Add(GetGrammarResourceString("GrammarShowAllNotes"));
AvailablePhrases.Add(GetGrammarResourceString("GrammarShowMyNotes"));
AvailablePhrases.Add(GetGrammarResourceString("GrammarShowAllNotesToMe"));
AvailablePhrases.Add(GetGrammarResourceString("GrammarShowAllNotesForMe"));
AvailablePhrases.Add(GetGrammarResourceString("GrammarShowNotesToMe"));
AvailablePhrases.Add(GetGrammarResourceString("GrammarShowNotesForMe"));
}
private List<string> GetPhrasesForPerson(Person person)
{
List<string> phrases = new List<string>();
phrases.Add(GetGrammarResourceStringAndAppendName("GrammarAddNoteTo", person.FriendlyName));
phrases.Add(GetGrammarResourceStringAndAppendName("GrammarAddNoteFor", person.FriendlyName));
phrases.Add(GetGrammarResourceStringAndAppendName("GrammarCreateNoteTo", person.FriendlyName));
phrases.Add(GetGrammarResourceStringAndAppendName("GrammarCreateNoteFor", person.FriendlyName));
phrases.Add(GetGrammarResourceStringAndAppendName("GrammarNewNoteTo", person.FriendlyName));
phrases.Add(GetGrammarResourceStringAndAppendName("GrammarNewNoteFor", person.FriendlyName));
phrases.Add(GetGrammarResourceStringAndAppendName("GrammarShowNotesTo", person.FriendlyName));
phrases.Add(GetGrammarResourceStringAndAppendName("GrammarShowNotesFor", person.FriendlyName));
// Handle "Show <user>'s notes" command.
var showUsersTemplate = GetGrammarResourceString("GrammarShowUsersNotes");
string showUsersString = String.Format(showUsersTemplate, person.FriendlyName);
phrases.Add(showUsersString);
return phrases;
}
private string GetGrammarResourceString(string resource)
{
return SpeechResourceMap.GetValue(resource, _speechContext).ValueAsString;
}
private string GetGrammarResourceStringAndAppendName(string resource, string personName)
{
string resourceString = GetGrammarResourceString(resource);
string resourceStringWithName = $"{resourceString} {personName}";
return resourceStringWithName;
}
private async Task CompileGrammar()
{
if (RecognitionMode == SpeechRecognitionMode.Dictation)
{
await CompileDictationConstraint();
}
else
{
await CompilePhraseConstraints();
}
}
private async Task CompilePhrases()
{
#if VERBOSE_DEBUG
Debug.WriteLine( "SpeechManager: Compiling command phrase constraints" );
#endif
try
{
SpeechRecognizer.Constraints.Clear();
AvailablePhrases.ForEach(p =>
{
string phraseNoSpaces = p.Replace(" ", String.Empty);
SpeechRecognizer.Constraints.Add(
new SpeechRecognitionListConstraint(
new List<string>() { p },
phraseNoSpaces));
});
var result = await SpeechRecognizer.CompileConstraintsAsync();
if (result.Status != SpeechRecognitionResultStatus.Success)
{
Debug.WriteLine("SpeechManager: CompileConstraintsAsync failed for phrases");
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
private async Task CompileDictationConstraint()
{
#if VERBOSE_DEBUG
Debug.WriteLine( "SpeechManager: Compiling dictation constraint" );
#endif
SpeechRecognizer.Constraints.Clear();
// Apply the dictation topic constraint to optimize for dictated freeform speech.
var dictationConstraint = new SpeechRecognitionTopicConstraint(SpeechRecognitionScenario.Dictation, "dictation");
SpeechRecognizer.Constraints.Add(dictationConstraint);
var result = await SpeechRecognizer.CompileConstraintsAsync();
if (result.Status != SpeechRecognitionResultStatus.Success)
{
Debug.WriteLine("SpeechRecognizer.CompileConstraintsAsync failed for dictation");
}
}
private async Task CompilePhraseConstraints()
{
try
{
PopulatePhrases();
await CompilePhrases();
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
private void ContinuousRecognitionSession_Completed(
SpeechContinuousRecognitionSession sender,
SpeechContinuousRecognitionCompletedEventArgs args)
{
IsInRecognitionSession = false;
StateChangedEventArgs e = new StateChangedEventArgs(args);
OnStateChanged(e);
}
/// <summary>
/// Handle events fired when a result is generated. This may include a garbage rule that fires when general room noise
/// or side-talk is captured (this will have a confidence of Rejected typically, but may occasionally match a rule with
/// low confidence).
/// </summary>
/// <param name="sender">The Recognition session that generated this result</param>
/// <param name="args">Details about the recognized speech</param>
/// <remarks>
/// <para> This method raises the PhraseRecognized event. Keep in mind that the
/// ContinuousRecognitionSession.ResultGenerated event is raised on an arbitrary thread
/// from the thread pool. If a <see cref="SpeechManager"/> client has thread affinity,
/// like in a XAML-based UI, you need to marshal the call to the client's thread.
/// </para>
/// <para>In a UWP app, use the <see cref="CoreDispatcher"/> to execute the call
/// on the main UI thread.</para>
/// </remarks>
private void ContinuousRecognitionSession_ResultGenerated(
SpeechContinuousRecognitionSession sender,
SpeechContinuousRecognitionResultGeneratedEventArgs args)
{
if (args.Result.Status != SpeechRecognitionResultStatus.Success)
{
#if VERBOSE_DEBUG
Debug.WriteLine( "SpeechManager: ResultGenerated: {0}", args.Result.Status );
#endif
return;
}
// Unpack event arg data.
bool hasConstraint = args.Result.Constraint != null;
var confidence = args.Result.Confidence;
string phrase = args.Result.Text;
// The garbage rule doesn't have a tag associated with it, and
// the other rules return a string matching the tag provided
// when the grammar was compiled.
string tag = hasConstraint ? args.Result.Constraint.Tag : "unknown";
if (tag == "unknown")
{
#if VERBOSE_DEBUG
Debug.WriteLine( "SpeechManager: ResultGenerated: garbage rule hit" );
#endif
return;
}
else
{
#if VERBOSE_DEBUG
string msg = String.Format( "SpeechManager: ResultGenerated: {0}", phrase );
Debug.WriteLine( msg );
#endif
}
if (hasConstraint && args.Result.Constraint.Type == SpeechRecognitionConstraintType.List)
{
// The List constraint type represents speech from
// a compiled grammar of commands.
CommandVerb verb = GetPhraseIntent(phrase);
// You may decide to use per-phrase confidence levels in order to
// tune the behavior of your grammar based on testing.
if (confidence == SpeechRecognitionConfidence.Medium ||
confidence == SpeechRecognitionConfidence.High)
{
Person person = null;
if (PhraseToPersonDictionary.ContainsKey(phrase))
{
person = PhraseToPersonDictionary[phrase];
}
// Raise the PhraseRecognized event. Clients with thread affinity,
// like in a XAML-based UI, need to marshal the call to the
// client's thread.
PhraseRecognizedEventArgs eventArgs = new PhraseRecognizedEventArgs(
person,
phrase,
verb,
args);
OnPhraseRecognized(eventArgs);
}
}
else if (hasConstraint && args.Result.Constraint.Type == SpeechRecognitionConstraintType.Topic)
{
// The Topic constraint type represents speech from dictation.
// Raise the PhraseRecognized event. Clients with thread affinity,
// like in a XAML-based UI, need to marshal the call to the
// client's thread.
PhraseRecognizedEventArgs eventArgs = new PhraseRecognizedEventArgs(
null,
phrase,
CommandVerb.Dictation,
args);
OnPhraseRecognized(eventArgs);
}
}
private CommandVerb GetPhraseIntent(string phrase)
{
CommandVerb verb = CommandVerb.None;
if (phrase.StartsWith("Add") || phrase.StartsWith("Create") || phrase.StartsWith("New"))
{
verb = CommandVerb.Create;
}
else if (phrase.StartsWith("Read"))
{
verb = CommandVerb.Read;
}
else if (phrase.StartsWith("Edit"))
{
verb = CommandVerb.Edit;
}
else if (phrase.StartsWith("Delete"))
{
verb = CommandVerb.Delete;
}
else if (phrase.StartsWith("Help") || phrase.StartsWith("What can I say"))
{
verb = CommandVerb.Help;
}
else if (phrase.StartsWith("Show"))
{
verb = CommandVerb.Show;
}
else
{
Debug.WriteLine("Phrase intent not recognized: {0}", phrase);
}
return verb;
}
/// <summary>
/// Provides feedback to client code based on whether the recognizer is receiving speech input.
/// </summary>
/// <param name="sender">The recognizer that is currently running.</param>
/// <param name="args">The current state of the recognizer.</param>
private void SpeechRecognizer_StateChanged(SpeechRecognizer sender, SpeechRecognizerStateChangedEventArgs args)
{
StateChangedEventArgs e = new StateChangedEventArgs(args);
OnStateChanged(e);
}
/// <summary>
/// Stop the current speech recognition session.
/// </summary>
/// <returns>Void</returns>
/// <remarks>Ensure that calls to this method are protected by
/// a mutex or other thread-access object. Currently, the
/// <see cref="StartContinuousRecognition"/> method is the only
/// caller of this method, and the call is protected by the
/// <see cref="SpeechManager.Mutex"/> property.</remarks>
private async Task EndRecognitionSession()
{
// Detach event handlers.
SpeechRecognizer.ContinuousRecognitionSession.Completed -= ContinuousRecognitionSession_Completed;
SpeechRecognizer.ContinuousRecognitionSession.ResultGenerated -= ContinuousRecognitionSession_ResultGenerated;
// Stop the recognition session, if it's in progress.
if (IsInRecognitionSession)
{
#if VERBOSE_DEBUG
Debug.WriteLine( "SpeechManager: Ending continuous recognition session" );
#endif
try
{
if (SpeechRecognizer.State != SpeechRecognizerState.Idle)
{
await SpeechRecognizer.ContinuousRecognitionSession.CancelAsync();
}
else
{
await SpeechRecognizer.ContinuousRecognitionSession.StopAsync();
}
IsInRecognitionSession = false;
#if VERBOSE_DEBUG
Debug.WriteLine( "SpeechManager: Continuous recognition session ended" );
#endif
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
}
private SemaphoreSlim Mutex
{
get
{
if (_mutex == null)
{
// Initialize the semaphore to allow execution
// by one thread thread at a time.
_mutex = new SemaphoreSlim(1);
}
return _mutex;
}
}
#endregion
#region Implementation for speech synthesis
private SpeechSynthesizer SpeechSynth
{
get
{
if (_speechSynthesizer == null)
{
_speechSynthesizer = new SpeechSynthesizer();
}
return _speechSynthesizer;
}
}
private MediaElement MediaPlayerElement
{
get
{
return _mediaElement;
}
set
{
if (_mediaElement != value)
{
if (_mediaElement != null)
{
_mediaElement.MediaEnded -= mediaElement_MediaEnded;
}
_mediaElement = value;
_mediaElement.MediaEnded += mediaElement_MediaEnded;
}
}
}
private void mediaElement_MediaEnded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
// Signal the SpeakAsync method.
Semaphore.Release();
}
private SemaphoreSlim Semaphore
{
get
{
if (_semaphore == null)
{
_semaphore = new SemaphoreSlim(0,1);
}
return _semaphore;
}
}
private WaitHandle WaitHandle { get; set; }
#endregion
#region Private fields for speech recognition
private SpeechRecognizer _speechRecognizer;
private ResourceContext _speechContext;
private ResourceMap _speechResourceMap;
private Dictionary<string, Person> _phraseToPersonDictionary;
private static uint RecognizerNotFoundHResult = 0x8004503a;
// Synchronizes access to the StartContinuousRecognition method.
private SemaphoreSlim _mutex;
#endregion
#region Private fields for speech synthesis
// Creates speech for prompts and for reading notes to the user.
private SpeechSynthesizer _speechSynthesizer;
// Plays synthesized speech.
private MediaElement _mediaElement;
// Used to make the SpeakAsync method awaitable.
private SemaphoreSlim _semaphore;
#endregion
}
}