Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Speech!

Question posted by: Dekudude (Newbie) on July 2nd, 2008 09:23 PM
Hi there!

I'm working on a simple script, and I was wondering if there is some way to use the Microsoft built-in speech SDKs (to voice, and text FROM voice)? I'd like to capture raw_input() data from my microphone whenever the program recognizes that something went through the microphone. I'd also like to have the program use the default text-to-voice synthesizer I have installed... currently Microsoft Mary.

Is this possible? If so, how might I go about doing it? Are there any simple modules with functions such as, "say()" or "listen()"? Thanks a lot!
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Dekudude's Avatar
Dekudude
Newbie
9 Posts
July 3rd, 2008
04:45 PM
#2

Re: Speech!
Hmm, okay. I figured out text to voice. Is there some way to do voice to text, though? I already have the Microsoft speech API, SAPI, but I don't know how to make that work with Python...

Reply
heiro's Avatar
heiro
Member
53 Posts
July 3rd, 2008
05:52 PM
#3

Re: Speech!
posting again with code tags

Last edited by heiro : July 3rd, 2008 at 06:00 PM. Reason: no code tags
Reply
heiro's Avatar
heiro
Member
53 Posts
July 3rd, 2008
05:58 PM
#4

Re: Speech!
Code: ( text )
  1. from win32com.client import constants
  2. import win32com.client
  3. import pythoncom
  4. class SpeechRecognition:
  5.     """ Initialize the speech recognition with the passed in list of words """
  6.     def __init__(self, wordsToAdd):
  7.         # For text-to-speech
  8.         self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
  9.         # For speech recognition - first create a listener
  10.         self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
  11.         # Then a recognition context
  12.         self.context = self.listener.CreateRecoContext()
  13.         # which has an associated grammar
  14.         self.grammar = self.context.CreateGrammar()
  15.         # Do not allow free word recognition - only command and control
  16.         # recognizing the words in the grammar only
  17.         self.grammar.DictationSetState(0)
  18.         # Create a new rule for the grammar, that is top level (so it begins
  19.         # a recognition) and dynamic (ie we can change it at runtime)
  20.         self.wordsRule = self.grammar.Rules.Add("wordsRule",
  21.                         constants.SRATopLevel + constants.SRADynamic, 0)
  22.         # Clear the rule (not necessary first time, but if we're changing it
  23.         # dynamically then it's useful)
  24.         self.wordsRule.Clear()
  25.         # And go through the list of words, adding each to the rule
  26.         [ self.wordsRule.InitialState.AddWordTransition(None  , word) for word in wordsToAdd ]
  27.         # Set the wordsRule to be active
  28.         self.grammar.Rules.Commit()
  29.         self.grammar.CmdSetRuleState("wordsRule", 1)
  30.         # Commit the changes to the grammar
  31.         self.grammar.Rules.Commit()
  32.         # And add an event handler that's called back when recognition occurs
  33.         self.eventHandler = ContextEvents(self.context)
  34.         # Announce we've started
  35.         self.say("Started successfully")
  36.     def say(self, phrase):
  37.         self.speaker.Speak(phrase)
  38.  
  39. class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
  40.     def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
  41.         newResult = win32com.client.Dispatch(Result)
  42.         print "You said: ",newResult.PhraseInfo.GetText()
  43. if __name__=='__main__':
  44.     wordsToAdd = [ "One", "Two", "Three", "Four" ]
  45.     speechReco = SpeechRecognition(wordsToAdd)
  46.     while 1:
  47.         pythoncom.PumpWaitingMessages()

##### for text to speech################
Code: ( text )
  1. import sys
  2. from win32com.client import constants
  3. import win32com.client
  4.  
  5. speaker = win32com.client.Dispatch("SAPI.SpVoice")
  6. while 1:
  7.    try:
  8.       s = raw_input('Type word or phrase: ')
  9.       speaker.Speak(s)
  10.    except:
  11.       if sys.exc_type is EOFError:
  12.          sys.exit()

Reply
Dekudude's Avatar
Dekudude
Newbie
9 Posts
July 3rd, 2008
06:04 PM
#5

Re: Speech!
Thanks for your response:

However, that is not exactly what I want. For starters, I already got the text-to-voice working. As for voice-to-text, I already tried that example, but it doesn't work how I want it to. As far as my beginner eyes can see, there's no way to replicate raw_input() using it, where if I said, "This is a test, hello world", it would enter that.

Do you understand what I'm saying? If you can show me how to use that code example, that would be great, but I just want a raw_input() style function that enters data via voice-to-text.

Thanks!

Reply
Reply
Not the answer you were looking for? Post your question . . .
183,968 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Python Forum Contributors