1
2
3
4
5
6
7 import locale
8 from collections import OrderedDict
9 import time
10 import os
11 import comtypes.client
12 from comtypes import COMError
13 import _winreg
14 import globalVars
15 import speech
16 from synthDriverHandler import SynthDriver,VoiceInfo
17 import config
18 import nvwave
19 from logHandler import log
25
27 supportedSettings=(SynthDriver.VoiceSetting(),SynthDriver.RateSetting(),SynthDriver.PitchSetting(),SynthDriver.VolumeSetting())
28
29 COM_CLASS = "SAPI.SPVoice"
30
31 name="sapi5"
32 description="Microsoft Speech API version 5"
33
34 @classmethod
36 try:
37 r=_winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,cls.COM_CLASS)
38 r.Close()
39 return True
40 except:
41 return False
42
46
49
51 voices=OrderedDict()
52 v=self.tts.GetVoices()
53 for i in range(len(v)):
54 try:
55 ID=v[i].Id
56 name=v[i].GetDescription()
57 try:
58 language=locale.windows_locale[int(v[i].getattribute('language').split(';')[0],16)]
59 except KeyError:
60 language=None
61 except COMError:
62 log.warning("Could not get the voice info. Skipping...")
63 voices[ID]=VoiceInfo(ID,name,language)
64 return voices
65
67 return (self.tts.rate*5)+50
68
71
73 return self.tts.volume
74
76 return self.tts.voice.Id
77
79 bookmark=self.tts.status.LastBookmark
80 if bookmark!="" and bookmark is not None:
81 return int(bookmark)
82 else:
83 return None
84
86 self.tts.Rate = (rate-50)/5
87
91
93 self.tts.Volume = value
94
96 self.tts=comtypes.client.CreateObject(self.COM_CLASS)
97 outputDeviceID=nvwave.outputDeviceNameToID(config.conf["speech"]["outputDevice"], True)
98 if outputDeviceID>=0:
99 self.tts.audioOutput=self.tts.getAudioOutputs()[outputDeviceID]
100
102 v=self.tts.GetVoices()
103 for i in range(len(v)):
104 if value==v[i].Id:
105 break
106 else:
107
108 return
109 self._initTts()
110 self.tts.voice=v[i]
111
112 - def speak(self,speechSequence):
113 textList=[]
114 for item in speechSequence:
115 if isinstance(item,basestring):
116 textList.append(item.replace("<","<"))
117 elif isinstance(item,speech.IndexCommand):
118 textList.append("<Bookmark Mark=\"%d\" />"%item.index)
119 elif isinstance(item,speech.CharacterModeCommand):
120 textList.append("<spell>" if item.state else "</spell>")
121 elif isinstance(item,speech.SpeechCommand):
122 log.debugWarning("Unsupported speech command: %s"%item)
123 else:
124 log.error("Unknown speech: %s"%item)
125 text="".join(textList)
126
127 pitch=(self._pitch/2)-25
128 text="<pitch absmiddle=\"%s\">%s</pitch>"%(pitch,text)
129 flags=constants.SVSFIsXML|constants.SVSFlagsAsync
130 self.tts.Speak(text,flags)
131
135
137 if switch:
138 self.cancel()
139