# << simple declarations >> import wx import win32event, pythoncom from win32com.client import constants import win32con import win32api import win32com.client listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer") #listener = win32com.client.Dispatch("SAPI.SpInprocRecognizer") #Recognizer = New SpInprocRecognizer #ObjectTokenCat = win32com.client.Dispatch("SAPI.SpObjectTokenCategory") #ObjectTokenCat.SetId('HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\AudioInput') #enumtoken = [] #for i in ObjectTokenCat.EnumerateTokens(): # enumtoken.append(i) #listener.AudioInput = enumtoken[0] #listener.AudioInput = ObjectTokenCat.EnumerateTokens().Item('0') #listener.Recognizer = listener.GetRecognizers().Item('0') #Set Recognizer.Recognizer = Recognizer.GetRecognizers().Item(SREngines.ListIndex) context = listener.CreateRecoContext() speaker = win32com.client.Dispatch("SAPI.SpVoice") # -- end -- << simple declarations >> # << simple methods >> class VoiceDialog(win32com.client.getevents("SAPI.SpSharedRecoContext"),wx.Dialog): # << class VoiceDialog methods >> (1 of 3) def __init__(self, parent, title,YesP =('yep','yea','yes','O.K.'),NoP =('na','no','nope'),StopP = ('stop stop','stop stops','stop top','stops stop'), pos = wx.DefaultPosition, size=(500,300), style = wx.DEFAULT_DIALOG_STYLE): global speaker,context,listener win32com.client.getevents("SAPI.SpSharedRecoContext").__init__(self,context) wx.Dialog.__init__(self, parent, -1, title, pos, size, style) #start Voice self.YesP = YesP self.NoP = NoP self.StopP = StopP self.grammar = context.CreateGrammar(1) self.grammar.DictationLoad() self.rtext = '' self.parent = parent self.yesno = False self.result = '' x, y = pos if x == -1 and y == -1: self.CenterOnScreen(wx.BOTH) self.text = wx.TextCtrl(self, -1,'',size=(200, 100), style=wx.TE_MULTILINE) ok = wx.Button(self, wx.ID_OK, "OK") ok.SetDefault() cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") dlgsizer = wx.BoxSizer(wx.VERTICAL) dlgsizer.Add(self.text, 1, wx.EXPAND | wx.ALL, 4) btnsizer = wx.StdDialogButtonSizer() btnsizer.AddButton(ok) btnsizer.AddButton(cancel) btnsizer.Realize() dlgsizer.Add(btnsizer, 0, wx.ALL | wx.ALIGN_RIGHT, 4) self.SetSizer(dlgsizer) self.Layout() self.grammar.DictationSetState(1) #Public WithEvents RC As SpSharedRecoContext 'The main shared Recognizer Context #Attribute RC.VB_VarHelpID = -1 #Public Grammar As ISpeechRecoGrammar 'Command and Control interface #Dim indent As Integer 'Sets indent level for small output window #Dim fRecoEnabled As Boolean 'Is recognition enabled #Dim fGrammarLoaded As Boolean 'Is a grammar loaded #Dim RecoResult As ISpeechRecoResult 'Recognition result interface #' This subroutine destroys the Shared RecoContext and creates and Inproc RecoContext #Private Sub Inproc_Click() #Dim Recognizer As ISpeechRecognizer # ##Destroy Shared RecoContext #Set RC = Nothing ##Create Inproc Recognizer which we will use to create the Inproc RecoContext. #win32com.client.getevents("SAPI.SpInprocRecognizer") ##Set Recognizer = New SpInprocRecognizer ##To create an Inproc RecoContext we must set an Audio Input. To do this we create ##an SpObjectTokenCategory object with the category of AudioIn. This object enumerates ##the registry to see what types of audio input devices are available. #Dim ObjectTokenCat As ISpeechObjectTokenCategory #Set ObjectTokenCat = New SpObjectTokenCategory #ObjectTokenCat.SetId SpeechCategoryAudioIn ##Set the default AudioInput device which is typically the first item and is usually ##the microphone. #Set Recognizer.AudioInput = ObjectTokenCat.EnumerateTokens.Item(0) ##Set the Recognizer to the one selected in the drop down box. #Set Recognizer.Recognizer = Recognizer.GetRecognizers().Item("Microsoft English Recognizer v5.1") ##Now go ahead and actually create the Inproc RecoContext. ##Note - in VB even though the global "RC" object is declaired as a ##SpSharedRecoContext, it is still possible to set it to a SpInprocRecoContext. #Set RC = Recognizer.CreateRecoContext #Call the InitEventInterestCheckBoxes subroutine which uses the SR engine #default event interests to initialize the event interest checkboxes. #InitEventInterestCheckBoxes ##Create grammar objects #LoadGrammarObj ##Attempt to load the default .xml file and set the RuleId State to Inactive until ##the user starts recognition. #LoadDefaultCnCGrammar ##Enable the engine selection drop down box #SREngines.Enabled = True #End Sub # << class VoiceDialog methods >> (2 of 3) def GetValue(self): return self.result # << class VoiceDialog methods >> (3 of 3) def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result): global speaker newResult = win32com.client.Dispatch(Result) print(newResult.PhraseInfo.GetText()) if self.yesno: if newResult.PhraseInfo.GetText() in self.YesP: self.result = self.text.GetValue() self.yesno = False self.grammar.DictationSetState(0) self.EndModal(wx.ID_OK) elif newResult.PhraseInfo.GetText() in self.NoP: #speaker.Speak('Please Restate Phrase') result = '' self.text.SetValue('') self.yesno = False else: if newResult.PhraseInfo.GetText() in self.StopP: speaker.Speak('You said ' + self.text.GetValue()) speaker.Speak('Is this correct') self.yesno = True else: self.rtext = newResult.PhraseInfo.GetText() self.text.SetValue(self.text.GetValue() + self.rtext + ' ') # -- end -- << class VoiceDialog methods >> # -- end -- << simple methods >>