User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,761 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,524 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Apr 29th, 2008
Views: 1,613
Text to speech can be implemented in its simplest form using Microsoft's Component Object Model (COM) connecting to the Speech API (SAPI). The attached Python code shows you how to do this.
python Syntax
  1. # to bring text to speech capability to your Windows computer
  2. # install SAPI5Speech (XP and Vista should have it already)
  3. # http://www.nolad.com/vt/redist/SAPI5SpeechInstaller.msi
  4. #
  5. # Windows XP has Sam, SAPI5Voice adds Mary and Mike
  6. # http://www.nolad.com/vt/redist/SAPI5VoiceInstaller.msi
  7. #
  8. # the Python for Windows extensions should be installed
  9. # for COM to work eg. pywin32-210.win32-py2.5.exe
  10. # from http://sourceforge.net/projects/pywin32/
  11. #
  12. # tested with Python25 on a Windows XP machine by vegaseat
  13.  
  14. import win32com.client
  15.  
  16. voices = {
  17. 'Sam' : 'Microsoft Sam',
  18. 'Mary' : 'Microsoft Mary',
  19. 'Mike' : 'Microsoft Mike'
  20. }
  21.  
  22. # choose voice from the voices dictionary
  23. voice = 'Sam'
  24. # range 0(low) - 100(loud)
  25. volume = 100
  26. # range -10(slow) - 10(fast)
  27. rate = -1
  28.  
  29. # some text to speak
  30. text = """\
  31. It is said, that if you line up all the cars in the world end to end,
  32. someone would be stupid enough and try to pass them.
  33. """
  34.  
  35. # initialize COM components of MS Speech API
  36. # COM is Microsoft's Component Object Model
  37. # (COM is also used by Peter Parente's pyTTS)
  38. speak = win32com.client.Dispatch('Sapi.SpVoice')
  39. # assign a voice
  40. speak.Voice = speak.GetVoices('Name='+voices[voice]).Item(0)
  41. speak.Rate = rate
  42. speak.Volume = volume
  43. # now speak out the text
  44. speak.Speak(text)
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 3:55 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC