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
| Imports System
Imports System.Diagnostics
Imports System.Linq
Imports System.Windows
Imports System.Windows.Media.Animation
Imports System.Windows.Threading
Imports Microsoft.Speech.AudioFormat
Imports Microsoft.Speech.Recognition
Imports Microsoft.Kinect
Namespace KinectSpeechRecognize
Public Partial Class MainWindow
Inherits Window
Private _kinect As KinectSensor
Private _speechRecognizer As SpeechRecognitionEngine
Private _readyTimer As DispatcherTimer
Public Sub New()
InitializeComponent()
End Sub
Private Sub InitializeSpeechEngine()
_speechRecognizer = CreateSpeechRecognizer()
InitializeKinect()
txtKinectStatus.Text = "kinect ISN't connected, default mic will be used"
If _speechRecognizer <> Nothing Then
_readyTimer = New DispatcherTimer()
_readyTimer.Tick += ReadyTimerTick
_readyTimer.Interval = New TimeSpan(0, 0, 4)
_readyTimer.Start()
ReportSpeechStatus("initializing audio stream...")
End If
End Sub
Private Sub UninitializeSpeechEngine()
If _kinect <> Nothing Then
_kinect.AudioSource.[Stop]()
_kinect.[Stop]()
End If
If _speechRecognizer <> Nothing Then
_speechRecognizer.RecognizeAsyncCancel()
_speechRecognizer.RecognizeAsyncStop()
End If
If _readyTimer <> Nothing Then
_readyTimer.[Stop]()
_readyTimer = Nothing
End If
End Sub
<Conditional("KINECT")> _
Private Sub InitializeKinect()
_kinect = KinectSensor.KinectSensors.Where(Function(s As ) s.Status = KinectStatus.Connected).FirstOrDefault()
Try
_kinect.Start()
txtKinectStatus.Text = If((_kinect <> Nothing), "kinect is connected", "kinect ISN't connected, default mic will be used")
Catch generatedExceptionName As Exception
txtKinectStatus.Text = "kinect ISN't connected, default mic will be used"
End Try
End Sub
Private Function CreateSpeechRecognizer() As SpeechRecognitionEngine
Dim ri As RecognizerInfo = GetSpeechRecognizerInfo()
If ri = Nothing Then
MessageBox.Show("There was a problem initializing Speech Recognition. Ensure you have the Microsoft Speech SDK installed.", "Failed to load Speech SDK", MessageBoxButton.OK, MessageBoxImage.[Error])
Me.Close()
Return Nothing
End If
Dim sre As SpeechRecognitionEngine
Try
sre = New SpeechRecognitionEngine(ri.Id)
Catch
MessageBox.Show("There was a problem initializing Speech Recognition. Ensure you have the Microsoft Speech SDK installed and configured.", "Failed to load Speech SDK", MessageBoxButton.OK, MessageBoxImage.[Error])
Me.Close()
Return Nothing
End Try
Dim commands As var = New Choices()
commands.Add("up")
commands.Add("down")
commands.Add("left")
commands.Add("right")
commands.Add("exit")
Dim gb As var = New GrammarBuilder(commands) With { _
.Culture = ri.Culture _
}
sre.LoadGrammar(New Grammar(gb))
sre.SpeechRecognized += SreSpeechRecognized
sre.SpeechHypothesized += SreSpeechHypothesized
sre.SpeechRecognitionRejected += SreSpeechRecognitionRejected
Return sre
End Function
Private Shared Function GetSpeechRecognizerInfo() As RecognizerInfo
Return SpeechRecognitionEngine.InstalledRecognizers().Where(Function(ri As ) String.Equals(ri.Culture.Name, "en-US", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault()
End Function
Private Sub ReadyTimerTick(sender As Object, e As EventArgs)
Start()
ReportSpeechStatus("ready to recognize speech!")
_readyTimer.[Stop]()
_readyTimer = Nothing
End Sub
Private Sub Start()
If _kinect <> Nothing Then
Dim audioSource As var = _kinect.AudioSource
audioSource.BeamAngleMode = BeamAngleMode.Adaptive
Dim kinectStream As var = audioSource.Start()
_speechRecognizer.SetInputToAudioStream(kinectStream, New SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, _
Nothing))
Else
_speechRecognizer.SetInputToDefaultAudioDevice()
End If
_speechRecognizer.SetInputToDefaultAudioDevice()
_speechRecognizer.RecognizeAsync(RecognizeMode.Multiple)
End Sub
Private Sub ReportSpeechStatus(format As String, ParamArray args As Object())
ReportSpeechStatus(String.Format(format, args))
End Sub
Private Sub ReportSpeechStatus(status As String)
Dispatcher.BeginInvoke(New Action(Function() Do
txtCommand.Text = status
End Function), DispatcherPriority.Normal)
End Sub
Private Sub RejectSpeech(result As RecognitionResult)
ReportSpeechStatus("rejected: {0} {1:0.000}", If(result = Nothing, String.Empty, result.Text.ToUpper()), result.Confidence)
End Sub
Private Sub ActivateStoryboard(storyboardName As String)
Dim sb As var = TryCast(Me.Resources(storyboardName), Storyboard)
If sb = Nothing Then
Debug.WriteLine("Storyboard with name '{0}' not found in window's resources.", storyboardName)
Return
End If
sb.Begin(Me)
End Sub
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
InitializeSpeechEngine()
End Sub
Private Sub Window_Unloaded(sender As Object, e As RoutedEventArgs)
UninitializeSpeechEngine()
End Sub
Private Sub SreSpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs)
If e.Result.Confidence < 0.7 Then
RejectSpeech(e.Result)
Return
End If
Dim handler As Action = Nothing
Select Case e.Result.Text.ToUpperInvariant()
Case "UP", "DOWN", "LEFT", "RIGHT"
handler = Function() Do
ActivateStoryboard(e.Result.Text.ToUpperInvariant())
End Function
Exit Select
Case "EXIT"
handler = Function() Do
Me.Close()
End Function
Exit Select
Case Else
Exit Select
End Select
ReportSpeechStatus(String.Format("recognized: {0} {1:0.000}", e.Result.Text.ToUpper(), e.Result.Confidence))
If handler <> Nothing Then
Dispatcher.BeginInvoke(handler, DispatcherPriority.Normal)
End If
End Sub
Private Sub SreSpeechHypothesized(sender As Object, e As SpeechHypothesizedEventArgs)
ReportSpeechStatus("hypothesized: {0} {1:0.000}", e.Result.Text.ToUpper(), e.Result.Confidence)
End Sub
Private Sub SreSpeechRecognitionRejected(sender As Object, e As SpeechRecognitionRejectedEventArgs)
RejectSpeech(e.Result)
End Sub
End Class
End Namespace |