Original Link: http://www.codeproject.com/Articles/...xt-to-speech-a
I have tried to make this Source work with VBNET2010. But its not working. After Importing the Speech Imports I tried a few times but It wont start. But on vB2012 it works fine. When Starting the Program it will automatically start the "Console" WriteLine, and nothing showes up on VB2010 at all. I hope I could get some help making this work with VB2010.. Thanks a lot in advance..
I have tried to make this Source work with VBNET2010. But its not working. After Importing the Speech Imports I tried a few times but It wont start. But on vB2012 it works fine. When Starting the Program it will automatically start the "Console" WriteLine, and nothing showes up on VB2010 at all. I hope I could get some help making this work with VB2010.. Thanks a lot in advance..
Code:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading
Imports System.Speech.Recognition
Imports System.Speech.Synthesis
Namespace StartingWithSpeechRecognition
Class Program
Shared _recognizer As SpeechRecognitionEngine = Nothing
Shared manualResetEvent As ManualResetEvent = Nothing
Public Shared Sub Main(ByVal args As String())
manualResetEvent = New ManualResetEvent(False)
Console.WriteLine("To recognize speech, and write 'test' to the console, press 0")
Console.WriteLine("To recognize speech and make sure the computer speaks to you, press 1")
Console.WriteLine("To emulate speech recognition, press 2")
Console.WriteLine("To recognize speech using Choices and GrammarBuilder.Append, press 3")
Console.WriteLine("To recognize speech using a DictationGrammar, press 4")
Console.WriteLine("To get a prompt building example, press 5")
Dim pressedKey As ConsoleKeyInfo = Console.ReadKey(True)
Dim keychar As Char = pressedKey.KeyChar
Console.WriteLine("You pressed '{0}'", keychar)
Select Case keychar
Case "0"c
RecognizeSpeechAndWriteToConsole()
Exit Select
Case "1"c
RecognizeSpeechAndMakeSureTheComputerSpeaksToYou()
Exit Select
Case "2"c
EmulateRecognize()
Exit Select
Case "3"c
SpeechRecognitionWithChoices()
Exit Select
Case "4"c
SpeechRecognitionWithDictationGrammar()
Exit Select
Case "5"c
PromptBuilding()
Exit Select
Case Else
Console.WriteLine("You didn't press 0, 1, 2, 3, 4 or 5!")
Console.WriteLine("Press any key to continue . . .")
Console.ReadKey(True)
Environment.[Exit](0)
Exit Select
End Select
If keychar <> "5"c Then
manualResetEvent.WaitOne()
End If
If _recognizer IsNot Nothing Then
_recognizer.Dispose()
End If
Console.WriteLine("Press any key to continue . . .")
Console.ReadKey(True)
End Sub
#Region "Recognize speech and write to console"
Private Shared Sub RecognizeSpeechAndWriteToConsole()
_recognizer = New SpeechRecognitionEngine()
_recognizer.RequestRecognizerUpdate()
' request for recognizer update
_recognizer.LoadGrammar(New Grammar(New GrammarBuilder("test")))
' load a "test" grammar
_recognizer.RequestRecognizerUpdate()
' request for recognizer update
_recognizer.LoadGrammar(New Grammar(New GrammarBuilder("exit")))
' load a "exit" grammar
_recognizer.RequestRecognizerUpdate()
' request for recognizer update
AddHandler _recognizer.SpeechRecognized, AddressOf _recognizeSpeechAndWriteToConsole_SpeechRecognized
' if speech is recognized, call the specified method
AddHandler _recognizer.SpeechRecognitionRejected, AddressOf _recognizeSpeechAndWriteToConsole_SpeechRecognitionRejected
' if recognized speech is rejected, call the specified method
_recognizer.SetInputToDefaultAudioDevice()
' set the input to the default audio device
_recognizer.RecognizeAsync(RecognizeMode.Multiple)
' recognize speech asynchronous
End Sub
Private Shared Sub _recognizeSpeechAndWriteToConsole_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs)
If e.Result.Text = "test" Then
Console.WriteLine("test")
ElseIf e.Result.Text = "exit" Then
manualResetEvent.[Set]()
End If
End Sub
Private Shared Sub _recognizeSpeechAndWriteToConsole_SpeechRecognitionRejected(sender As Object, e As SpeechRecognitionRejectedEventArgs)
Console.WriteLine("Speech rejected. Did you mean:")
For Each r As RecognizedPhrase In e.Result.Alternates
Console.WriteLine(" " + r.Text)
Next
End Sub
#End Region
#Region "Recognize speech and make sure the computer speaks to you"
Private Shared Sub RecognizeSpeechAndMakeSureTheComputerSpeaksToYou()
_recognizer = New SpeechRecognitionEngine()
_recognizer.RequestRecognizerUpdate()
' request for recognizer update
_recognizer.LoadGrammar(New Grammar(New GrammarBuilder("hello computer")))
' load a "hello computer" grammar
AddHandler _recognizer.SpeechRecognized, AddressOf _recognizeSpeechAndMakeSureTheComputerSpeaksToYou_SpeechRecognized
' if speech is recognized, call the specified method
AddHandler _recognizer.SpeechRecognitionRejected, AddressOf _recognizeSpeechAndMakeSureTheComputerSpeaksToYou_SpeechRecognitionRejected
_recognizer.SetInputToDefaultAudioDevice()
' set the input to the default audio device
_recognizer.RecognizeAsync(RecognizeMode.Multiple)
' recognize speech asynchronous
End Sub
Private Shared Sub _recognizeSpeechAndMakeSureTheComputerSpeaksToYou_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs)
If e.Result.Text = "hello computer" Then
Dim speechSynthesizer As New SpeechSynthesizer()
speechSynthesizer.Speak("hello user")
speechSynthesizer.Dispose()
End If
manualResetEvent.[Set]()
End Sub
Private Shared Sub _recognizeSpeechAndMakeSureTheComputerSpeaksToYou_SpeechRecognitionRejected(sender As Object, e As SpeechRecognitionRejectedEventArgs)
If e.Result.Alternates.Count = 0 Then
Console.WriteLine("No candidate phrases found.")
Return
End If
Console.WriteLine("Speech rejected. Did you mean:")
For Each r As RecognizedPhrase In e.Result.Alternates
Console.WriteLine(" " + r.Text)
Next
End Sub
#End Region
#Region "Emulate speech recognition"
Private Shared Sub EmulateRecognize()
_recognizer = New SpeechRecognitionEngine()
_recognizer.RequestRecognizerUpdate()
' request for recognizer update
_recognizer.LoadGrammar(New Grammar(New GrammarBuilder("emulate speech")))
' load "emulate speech" grammar
AddHandler _recognizer.SpeechRecognized, AddressOf _emulateRecognize_SpeechRecognized
_recognizer.EmulateRecognize("emulate speech")
End Sub
Private Shared Sub _emulateRecognize_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs)
If e.Result.Text = "emulate speech" Then
Console.WriteLine("Speech was emulated!")
End If
manualResetEvent.[Set]()
End Sub
#End Region
#Region "Speech recognition with Choices and GrammarBuilder.Append"
Private Shared Sub SpeechRecognitionWithChoices()
_recognizer = New SpeechRecognitionEngine()
Dim grammarBuilder As New GrammarBuilder()
grammarBuilder.Append("I")
' add "I"
grammarBuilder.Append(New Choices("like", "dislike"))
' load "like" & "dislike"
grammarBuilder.Append(New Choices("dogs", "cats", "birds", "snakes", "fishes", "tigers", _
"lions", "snails", "elephants"))
' add animals
_recognizer.RequestRecognizerUpdate()
_recognizer.LoadGrammar(New Grammar(grammarBuilder))
' load grammar
AddHandler _recognizer.SpeechRecognized, AddressOf speechRecognitionWithChoices_SpeechRecognized
_recognizer.SetInputToDefaultAudioDevice()
' set input to default audio device
_recognizer.RecognizeAsync(RecognizeMode.Multiple)
' recognize speech
End Sub
Private Shared Sub speechRecognitionWithChoices_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs)
Console.WriteLine(("Do you really " + e.Result.Words(1).Text & " ") + e.Result.Words(2).Text & "?")
manualResetEvent.[Set]()
End Sub
#End Region
#Region "Speech recognition with DictationGrammar"
Private Shared Sub SpeechRecognitionWithDictationGrammar()
_recognizer = New SpeechRecognitionEngine()
_recognizer.RequestRecognizerUpdate()
_recognizer.LoadGrammar(New Grammar(New GrammarBuilder("exit")))
_recognizer.RequestRecognizerUpdate()
_recognizer.LoadGrammar(New DictationGrammar())
AddHandler _recognizer.SpeechRecognized, AddressOf speechRecognitionWithDictationGrammar_SpeechRecognized
_recognizer.SetInputToDefaultAudioDevice()
_recognizer.RecognizeAsync(RecognizeMode.Multiple)
End Sub
Private Shared Sub speechRecognitionWithDictationGrammar_SpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs)
If e.Result.Text = "exit" Then
manualResetEvent.[Set]()
Return
End If
Console.WriteLine("You said: " & Convert.ToString(e.Result.Text))
End Sub
#End Region
#Region "Prompt Building"
Private Shared Sub PromptBuilding()
Dim builder As New PromptBuilder()
builder.StartSentence()
builder.AppendText("This is a prompt building example.")
builder.EndSentence()
builder.StartSentence()
builder.AppendText("Now, there will be a break of 2 seconds.")
builder.EndSentence()
builder.AppendBreak(New TimeSpan(0, 0, 2))
builder.StartStyle(New PromptStyle(PromptVolume.ExtraSoft))
builder.AppendText("This text is spoken extra soft.")
builder.EndStyle()
builder.StartStyle(New PromptStyle(PromptRate.Fast))
builder.AppendText("This text is spoken fast.")
builder.EndStyle()
Dim synthesizer As New SpeechSynthesizer()
synthesizer.Speak(builder)
synthesizer.Dispose()
End Sub
#End Region
End Class
End Namespace