# Created by Leo from: C:\Python22\Tom\Speech.leo # << speachtest1 declarations >> from win32com.client import constants import win32com.client import pythoncom import sys """Sample code for using the Microsoft Speech SDK 5.1 via COM in Python. Requires that the SDK be installed; it's a free download from http://microsoft.com/speech and that MakePy has been used on it (in PythonWin, select Tools | COM MakePy Utility | Microsoft Speech Object Library 5.1). After running this, then saying "One", "Two", "Three" or "Four" should display "You said One" etc on the console. The recognition can be a bit shaky at first until you've trained it (via the Speech entry in the Windows Control Panel.""" # -- end -- << speachtest1 declarations >> # << speachtest1 methods >> (1 of 2) class SpeechRecognition: # << class SpeechRecognition declarations >> """ Initialize the speech recognition with the passed in list of words """ # -- end -- << class SpeechRecognition declarations >> # << class SpeechRecognition methods >> (1 of 2) def __init__(self, wordsToAdd): # For text-to-speech self.speaker = win32com.client.Dispatch("SAPI.SpVoice") # For speech recognition - first create a listener self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer") # Then a recognition context self.context = self.listener.CreateRecoContext() # which has an associated grammar self.grammar = self.context.CreateGrammar() # Do not allow free word recognition - only command and control # recognizing the words in the grammar only self.grammar.DictationSetState(0) # Create a new rule for the grammar, that is top level (so it begins # a recognition) and dynamic (ie we can change it at runtime) self.wordsRule = self.grammar.Rules.Add("wordsRule",33, 0)#constants.SRATopLevel + constants.SRADynamic # Clear the rule (not necessary first time, but if we're changing it # dynamically then it's useful) self.wordsRule.Clear() # And go through the list of words, adding each to the rule [ self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd ] # Set the wordsRule to be active self.grammar.Rules.Commit() self.grammar.CmdSetRuleState("wordsRule", 1) # Commit the changes to the grammar self.grammar.Rules.Commit() # And add an event handler that's called back when recognition occurs self.eventHandler = ContextEvents(self.context) # Announce we've started using speech synthesis self.say("Started successfully") # << class SpeechRecognition methods >> (2 of 2) """Speak a word or phrase""" def say(self, phrase): self.speaker.Speak(phrase) # -- end -- << class SpeechRecognition methods >> # << speachtest1 methods >> (2 of 2) """The callback class that handles the events raised by the speech object. See "Automation | SpSharedRecoContext (Events)" in the MS Speech SDK online help for documentation of the other events supported. """ class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")): # << class ContextEvents declarations >> """Called when a word/phrase is successfully recognized - ie it is found in a currently open grammar with a sufficiently high confidence""" # -- end -- << class ContextEvents declarations >> # << class ContextEvents methods >> def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result): newResult = win32com.client.Dispatch(Result) speaker = win32com.client.Dispatch("SAPI.SpVoice") shell = win32com.client.Dispatch("WScript.Shell") if newResult.PhraseInfo.GetText() == "computer": speaker.Speak('working') elif newResult.PhraseInfo.GetText() == "turn on tee vee": shell.SendKeys("^(%z)") elif newResult.PhraseInfo.GetText() == "channel thirty eight": shell.SendKeys("38") elif newResult.PhraseInfo.GetText() == "channel thirty six": shell.SendKeys("36") elif newResult.PhraseInfo.GetText() == "channel thirty four": shell.SendKeys("34") elif newResult.PhraseInfo.GetText() == "channel seven": shell.SendKeys("7") elif newResult.PhraseInfo.GetText() == "channel forty": shell.SendKeys("40") elif newResult.PhraseInfo.GetText() == "channel forty two": shell.SendKeys("42") elif newResult.PhraseInfo.GetText() == "channel sixteen": shell.SendKeys("16") elif newResult.PhraseInfo.GetText() == "channel forty four": shell.SendKeys("44") elif newResult.PhraseInfo.GetText() == "close program": shell.SendKeys("%( )c") elif newResult.PhraseInfo.GetText() == "check mail": pass print "You said: ",newResult.PhraseInfo.GetText() # -- end -- << class ContextEvents methods >> # -- end -- << speachtest1 methods >> if __name__=='__main__': wordsToAdd = ["computer" , "turn on tee vee", "channel forty", "channel forty two", "channel forty four", "channel sixteen", "close program", "check mail", "channel thirty eight", "channel thirty six", "channel thirty four", "channel seven"] speechReco = SpeechRecognition(wordsToAdd) while 1: pythoncom.PumpWaitingMessages()