Advertisement
Guest User

Speech Library (Ashley M Begley / ambcoder)

a guest
May 31st, 2012
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.60 KB | None | 0 0
  1. 'Copyrighted By Ashley M Begley
  2.  
  3. 'This library is introducing the use of Microsoft's Speech Recognition feature. In this library you can do the following:
  4. ' Use text to speech (make the computer speak)
  5. ' Also you make the program react in different ways depending on what you say
  6.  
  7. ' - - - - - - MUST READ - - - - - -
  8. ' MAKE SURE YOU IMPORT SYSTEM.SPEECH FROM REFERENCES!!
  9. ' ,otherwise this won't work!
  10.  
  11. Imports SpeechLib 'Needed for the speech recognition to work
  12. Module SpeechLibrary
  13.  
  14.     'DECLARING THE VARIABLES THAT ARE NEEDED FOR VOICE RECOGNITION
  15.  
  16.     Dim WithEvents RecoContext As SpSharedRecoContext      'Used to store the charectar of each letter spoken
  17.     Dim Grammar As ISpeechRecoGrammar                      'Used to store a dictionary of words and letters so the program can recognise them
  18.     Dim CharCount As Integer 'Counts the amount of letters used at a time
  19.  
  20.     Public SpeechLibrary_ResultToString As String 'All the results from the text will be made here
  21.  
  22.     'This is the sub which activates once the command has been recognised
  23.     'Use this sub to say what happens if certain words are used.
  24.     Private Sub SpeechLibrary_COMMAND_LIST()
  25.  
  26.         '- - - - - Example Use - - - - -
  27.         'If SpeechLibrary_ResultToString = "whatever you have said" Then
  28.         ' DO THIS
  29.         'End If
  30.  
  31.         If SpeechLibrary_ResultToString = "hello" Then
  32.             SpeechLibrary_TextToSpeech("Hello Sir..")
  33.         Else
  34.             SpeechLibrary_TextToSpeech("This command is not recognised.. Please speak more clearly..")
  35.         End If
  36.     End Sub
  37.  
  38.     'This function will convert any text entered into it into speech!
  39.     Public Sub SpeechLibrary_TextToSpeech(ByVal TextToSpeak)
  40.         Dim SAPI
  41.         SAPI = CreateObject("sapi.spvoice") 'sapi.spvoice' meaning the Speech engine
  42.         SAPI.Speak(TextToSpeak) 'Speak what the user has requested.
  43.     End Sub
  44.  
  45.     'This function will stop the speech recognition feature.
  46.     Public Sub SpeechLibrary_StopSpeechRecognition()
  47.         Grammar.DictationSetState(SpeechRuleState.SGDSInactive)   'Turns off the Recognition  
  48.     End Sub
  49.  
  50.     'This function will start the speech recognition feature.
  51.     'NOTE: This must be turned on before you can start to use speech recognition
  52.     Public Sub SpeechLibrary_StartSpeechRecognition()
  53.         'First check to see if reco has been loaded before. If not lets load it.  
  54.         If (RecoContext Is Nothing) Then
  55.             RecoContext = New SpSharedRecoContextClass        'Create a new RecoContextClass  
  56.             Grammar = RecoContext.CreateGrammar(1)            'Setup the Grammar  
  57.             Grammar.DictationLoad()                          'Load the Grammar  
  58.         End If
  59.  
  60.         Beep()                'Make the computer beep so the user knows that speech recognition has started
  61.         Grammar.DictationSetState(SpeechRuleState.SGDSActive)   'Turns on the Recognition  
  62.     End Sub
  63.  
  64.     'This function is here for when the speech is recognised.
  65.     Private Sub OnReco(ByVal StreamNumber As Integer, ByVal StreamPosition As Object, ByVal RecognitionType As SpeechRecognitionType, ByVal Result As ISpeechRecoResult) Handles RecoContext.Recognition
  66.         Dim recoResult As String = Result.PhraseInfo.GetText 'Create a new string, and assign the recognized text to it.  
  67.         Dim txt As New Windows.Forms.TextBox
  68.         txt.SelectionStart = CharCount
  69.         txt.SelectedText = recoResult.ToLower
  70.         CharCount = CharCount + 1 + Len(recoResult)
  71.  
  72.         SpeechLibrary_ResultToString = txt.Text
  73.         SpeechLibrary_COMMAND_LIST()
  74.         txt.Clear()
  75.     End Sub
  76. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement