|
| 1 | +# Text To Speech using SAPI (Windows) and Python module pyTTS by Peter Parente |
| 2 | +# download installer file pyTTS-3.0.win32-py2.4.exe |
| 3 | +# from: http://sourceforge.net/projects/uncassist |
| 4 | +# also needs: http://www.cs.unc.edu/Research/assist/packages/SAPI5SpeechInstaller.msi |
| 5 | +# and pywin32-204.win32-py2.4.exe at this date the latest version of win32com |
| 6 | +# from: http://sourceforge.net/projects/pywin32/ |
| 7 | +# tested with Python24 on a Windows XP computer vagaseat 15jun2005 |
| 8 | +import pyTTS |
| 9 | +import time |
| 10 | +tts = pyTTS.Create() |
| 11 | +# set the speech rate, higher value = faster |
| 12 | +# just for fun try values of -10 to 10 |
| 13 | +tts.Rate = 1 |
| 14 | +print "Speech rate =", tts.Rate |
| 15 | +# set the speech volume percentage (0-100%) |
| 16 | +tts.Volume = 90 |
| 17 | +print "Speech volume =", tts.Volume |
| 18 | +# get a list of all the available voices |
| 19 | +print "List of voices =", tts.GetVoiceNames() |
| 20 | +# explicitly set a voice |
| 21 | +tts.SetVoiceByName('MSMary') |
| 22 | +print "Voice is set ot MSMary" |
| 23 | +print |
| 24 | +# announce the date and time, does a good job |
| 25 | +timeStr = "The date and time is " + time.asctime() |
| 26 | +print timeStr |
| 27 | +tts.Speak(timeStr) |
| 28 | +print |
| 29 | +str1 = """ |
| 30 | +A young executive was leaving the office at 6 pm when he found |
| 31 | +the CEO standing in front of a shredder with a piece of paper in hand. |
| 32 | +"Listen," said the CEO, "this is important, and my secretary has left. |
| 33 | +Can you make this thing work?" |
| 34 | +"Certainly," said the young executive. He turned the machine on, |
| 35 | +inserted the paper, and pressed the start button. |
| 36 | +"Excellent, excellent!" said the CEO as his paper disappeared inside |
| 37 | +the machine. "I just need one copy." |
| 38 | +""" |
| 39 | +print str1 |
| 40 | +tts.Speak(str1) |
| 41 | +tts.Speak('Haah haa haah haa') |
| 42 | +print |
| 43 | +str2 = """ |
| 44 | +Finagle's fourth law: |
| 45 | + Once a job is fouled up, anything done to improve it only makes it worse. |
| 46 | +""" |
| 47 | +print str2 |
| 48 | +print |
| 49 | +print "The spoken text above has been written to a wave file (.wav)" |
| 50 | +tts.SpeakToWave('Finagle4.wav', str2) |
| 51 | +print "The wave file is loaded back and spoken ..." |
| 52 | +tts.SpeakFromWave('Finagle4.wav') |
| 53 | +print |
| 54 | +print "Substitute a hard to pronounce word like Ctrl key ..." |
| 55 | +#create an instance of the pronunciation corrector |
| 56 | +p = pyTTS.Pronounce() |
| 57 | +# replace words that are hard to pronounce with something that |
| 58 | +# is spelled out or misspelled, but at least sounds like it |
| 59 | +p.AddMisspelled('Ctrl', 'Control') |
| 60 | +str3 = p.Correct('Please press the Ctrl key!') |
| 61 | +tts.Speak(str3) |
| 62 | +print |
| 63 | +print "2 * 3 = 6" |
| 64 | +tts.Speak('2 * 3 = 6') |
| 65 | +print |
| 66 | +tts.Speak("sounds goofy, let's replace * with times") |
| 67 | +print "Substitute * with times" |
| 68 | +# ' * ' needs the spaces |
| 69 | +p.AddMisspelled(' * ', 'times') |
| 70 | +str4 = p.Correct('2 * 3 = 6') |
| 71 | +tts.Speak(str4) |
| 72 | +print |
| 73 | +print "Say that real fast a few times!" |
| 74 | +str5 = "The sinking steamer sunk!" |
| 75 | +tts.Rate = 3 |
| 76 | +for k in range(7): |
| 77 | + print str5 |
| 78 | + tts.Speak(str5) |
| 79 | + time.sleep(0.3) |
| 80 | +tts.Rate = 0 |
| 81 | +tts.Speak("Wow, not one mispronounced word!") |
0 commit comments