-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainPage.xaml.cs
62 lines (50 loc) · 1.97 KB
/
MainPage.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
using Magellanic.Speech.Recognition;
using System.Diagnostics;
using Windows.Media.SpeechRecognition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace SpeechRecognitionExample
{
public sealed partial class MainPage : Page
{
private const string grammarFile = "grammar.xml";
private SpeechRecognitionManager recognitionManager;
public MainPage()
{
this.InitializeComponent();
Loaded += MainPage_Loaded;
Unloaded += MainPage_Unloaded;
}
private async void MainPage_Unloaded(object sender, RoutedEventArgs e)
{
await recognitionManager.Dispose();
}
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// initialise the speech recognition manager
recognitionManager = new SpeechRecognitionManager(grammarFile);
// register the event for when speech is detected
recognitionManager
.SpeechRecognizer
.ContinuousRecognitionSession
.ResultGenerated += RecognizerResultGenerated;
// compile the grammar file
await recognitionManager.CompileGrammar();
}
private void RecognizerResultGenerated(
SpeechContinuousRecognitionSession session,
SpeechContinuousRecognitionResultGeneratedEventArgs args)
{
// only act if the speech is recognised with high confidence
if (!args.Result.IsRecognisedWithHighConfidence())
{
return;
}
// interpret key individual parts of the grammar specification
string command = args.Result.SemanticInterpretation.GetInterpretation("command");
string direction = args.Result.SemanticInterpretation.GetInterpretation("direction");
// write to debug
Debug.WriteLine($"Command: {command}, Direction: {direction}");
}
}
}