Imports System.Speech.Recognition Imports System.Speech.Synthesis Namespace SimpleSpeech Public Class SpeechHandle #Region "Declarations" Private _Commands As New List(Of String) Private _Voice As String Private _Volume As Integer Private spoken_text As String Private spoken_sentence As String Private speak As New System.Speech.Synthesis.SpeechSynthesizer Private recognizer As New SpeechRecognitionEngine() Private LastSpokenText As String Private allowrec As Boolean = True Private Dictrecognizer As New SpeechRecognitionEngine #End Region #Region "Events" Public Event Recognized(sender As Object, e As SpeechRecognizedEventArgs) Public Event TextSpoken(sender As Object, e As TextSpokenEventArgs) Public Event CommandsLoaded(sender As Object, e As CommandEventArgs) Public Event CommandsUnloaded(sender As Object, e As CommandEventArgs) Public Event DictationRecognized(sender As Object, e As DictationSpeechRecognizedEventArgs) #End Region Public Sub New() AddHandler recognizer.LoadGrammarCompleted, AddressOf recognizer_LoadGrammarCompleted AddHandler speak.SpeakCompleted, AddressOf speak_completed AddHandler recognizer.SpeechRecognized, AddressOf recognizer_SpeechRecognized AddHandler Dictrecognizer.SpeechRecognized, AddressOf dictationrecognizer_SpeechRecognized End Sub Public Property Volume As String Get Return _Volume End Get Set(Volume As String) _Volume = Volume speak.Volume = Volume End Set End Property Public Property Voice As String Get Return _Voice End Get Set(Name As String) _Voice = Name End Set End Property Public Function GetVoices() Dim voices As New List(Of String) For Each instVoice In speak.GetInstalledVoices() voices.Add(instVoice.VoiceInfo.Name) Next Return voices End Function Public Sub LoadCommands(ByVal Commands As List(Of String)) _Commands = Commands recognizer.UnloadAllGrammars() recognizer.LoadGrammar(CreateGrammar(_Commands)) End Sub Public Sub UnloadCommands() _Commands.Clear() recognizer.UnloadAllGrammars() End Sub Public Function StartRecognize() As Boolean Try recognizer.SetInputToDefaultAudioDevice() recognizer.RecognizeAsync(RecognizeMode.Multiple) Return True Catch ex As Exception Return False End Try End Function Public Function StopRecognize() As Boolean Try recognizer.RecognizeAsyncCancel() recognizer.SetInputToNull() Return True Catch ex As Exception Return False End Try End Function Public Sub SpeakText(ByVal Text As String) allowrec = False If _Voice = "" Then speak.SelectVoice(speak.GetInstalledVoices().Item(0).VoiceInfo.Name) Else speak.SelectVoice(_Voice) End If speak.SpeakAsync(Text) LastSpokenText = Text End Sub Public Sub StopSpeak() speak.SpeakAsyncCancelAll() End Sub Private Function CreateGrammar(ByVal Words As List(Of String)) As Grammar Dim builder As New GrammarBuilder() Dim Text As New Choices(Words.ToArray) builder.Append(Text) Dim Grammar As New Grammar(builder) Grammar.Name = "Grammar" Return Grammar End Function Private Sub recognizer_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechRecognizedEventArgs) If allowrec = True Then spoken_text = e.Result.Text For Each com In _Commands If e.Result.Text = com Then RaiseEvent Recognized(Me, New SpeechRecognizedEventArgs(com)) End If Next End If End Sub Private Sub speak_completed(ByVal sender As Object, ByVal e As SpeakCompletedEventArgs) RaiseEvent TextSpoken(Me, New TextSpokenEventArgs(LastSpokenText)) allowrec = True End Sub Private Sub recognizer_LoadGrammarCompleted(sender As Object, e As System.Speech.Recognition.LoadGrammarCompletedEventArgs) If e.Grammar.Loaded = True Then RaiseEvent CommandsLoaded(Me, New CommandEventArgs(_Commands)) Else RaiseEvent CommandsUnloaded(Me, New CommandEventArgs(_Commands)) End If End Sub 'dictation Dim standardvalue As New List(Of String) Public Function StartdictationRecognize(Optional ByVal ExtraCommands As List(Of String) = Nothing) As Boolean Try Dictrecognizer.SetInputToDefaultAudioDevice() Dim dictation As Grammar = New DictationGrammar() dictation.Name = "Dictation" If ExtraCommands IsNot Nothing Then Dictrecognizer.LoadGrammar(CreateGrammar(ExtraCommands)) End If Dictrecognizer.LoadGrammarAsync(dictation) Dictrecognizer.RecognizeAsync(RecognizeMode.Multiple) Return True Catch ex As Exception Return False End Try End Function Public Function StopdictationRecognize() As Boolean Try Dictrecognizer.RecognizeAsyncCancel() Dictrecognizer.SetInputToNull() Return True Catch ex As Exception Return False End Try End Function Private Sub dictationrecognizer_SpeechRecognized(sender As Object, e As System.Speech.Recognition.SpeechRecognizedEventArgs) If allowrec = True Then spoken_sentence = e.Result.Text RaiseEvent DictationRecognized(Me, New DictationSpeechRecognizedEventArgs(e.Result.Text)) End If End Sub End Class Public NotInheritable Class DictationSpeechRecognizedEventArgs Inherits EventArgs Private _Sent As String Friend Sub New(Sentence As String) _Sent = Sentence End Sub Public ReadOnly Property Sentence As String Get Return _Sent End Get End Property End Class Public NotInheritable Class SpeechRecognizedEventArgs Inherits EventArgs Private _Com As String Friend Sub New(Command As String) _Com = Command End Sub Public ReadOnly Property Command As String Get Return _Com End Get End Property End Class Public NotInheritable Class TextSpokenEventArgs Inherits EventArgs Private _Text As String Friend Sub New(Text As String) _Text = Text End Sub Public ReadOnly Property Text As String Get Return _Text End Get End Property End Class Public NotInheritable Class CommandEventArgs Inherits EventArgs Private _Commands As New List(Of String) Friend Sub New(Commands As List(Of String)) _Commands = Commands End Sub Public ReadOnly Property Commands As List(Of String) Get Return _Commands End Get End Property End Class End Namespace