'Copyrighted By Ashley M Begley
'This library is introducing the use of Microsoft's Speech Recognition feature. In this library you can do the following:
' Use text to speech (make the computer speak)
' Also you make the program react in different ways depending on what you say
' - - - - - - MUST READ - - - - - -
' MAKE SURE YOU IMPORT SYSTEM.SPEECH FROM REFERENCES!!
' ,otherwise this won't work!
Imports SpeechLib 'Needed for the speech recognition to work
Module SpeechLibrary
'DECLARING THE VARIABLES THAT ARE NEEDED FOR VOICE RECOGNITION
Dim WithEvents RecoContext As SpSharedRecoContext 'Used to store the charectar of each letter spoken
Dim Grammar As ISpeechRecoGrammar 'Used to store a dictionary of words and letters so the program can recognise them
Dim CharCount As Integer 'Counts the amount of letters used at a time
Public SpeechLibrary_ResultToString As String 'All the results from the text will be made here
'This is the sub which activates once the command has been recognised
'Use this sub to say what happens if certain words are used.
Private Sub SpeechLibrary_COMMAND_LIST()
'- - - - - Example Use - - - - -
'If SpeechLibrary_ResultToString = "whatever you have said" Then
' DO THIS
'End If
If SpeechLibrary_ResultToString = "hello" Then
SpeechLibrary_TextToSpeech("Hello Sir..")
Else
SpeechLibrary_TextToSpeech("This command is not recognised.. Please speak more clearly..")
End If
End Sub
'This function will convert any text entered into it into speech!
Public Sub SpeechLibrary_TextToSpeech(ByVal TextToSpeak)
Dim SAPI
SAPI = CreateObject("sapi.spvoice") 'sapi.spvoice' meaning the Speech engine
SAPI.Speak(TextToSpeak) 'Speak what the user has requested.
End Sub
'This function will stop the speech recognition feature.
Public Sub SpeechLibrary_StopSpeechRecognition()
Grammar.DictationSetState(SpeechRuleState.SGDSInactive) 'Turns off the Recognition
End Sub
'This function will start the speech recognition feature.
'NOTE: This must be turned on before you can start to use speech recognition
Public Sub SpeechLibrary_StartSpeechRecognition()
'First check to see if reco has been loaded before. If not lets load it.
If (RecoContext Is Nothing) Then
RecoContext = New SpSharedRecoContextClass 'Create a new RecoContextClass
Grammar = RecoContext.CreateGrammar(1) 'Setup the Grammar
Grammar.DictationLoad() 'Load the Grammar
End If
Beep() 'Make the computer beep so the user knows that speech recognition has started
Grammar.DictationSetState(SpeechRuleState.SGDSActive) 'Turns on the Recognition
End Sub
'This function is here for when the speech is recognised.
Private Sub OnReco(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal RecognitionType As SpeechRecognitionType, ByVal Result As ISpeechRecoResult) Handles RecoContext.Recognition
Dim recoResult As String = Result.PhraseInfo.GetText 'Create a new string, and assign the recognized text to it.
Dim txt As New Windows.Forms.TextBox
txt.SelectionStart = CharCount
txt.SelectedText = recoResult.ToLower
CharCount = CharCount + 1 + Len(recoResult)
SpeechLibrary_ResultToString = txt.Text
SpeechLibrary_COMMAND_LIST()
txt.Clear()
End Sub
End Module