Package synthDrivers :: Module sapi5
[hide private]
[frames] | no frames]

Source Code for Module synthDrivers.sapi5

  1  #synthDrivers/sapi5.py 
  2  #A part of NonVisual Desktop Access (NVDA) 
  3  #Copyright (C) 2006-2011 NVDA Contributors <http://www.nvda-project.org/> 
  4  #This file is covered by the GNU General Public License. 
  5  #See the file COPYING for more details. 
  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 
20 21 -class constants:
22 SVSFlagsAsync = 1 23 SVSFPurgeBeforeSpeak = 2 24 SVSFIsXML = 8
25
26 -class SynthDriver(SynthDriver):
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
35 - def check(cls):
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
43 - def __init__(self):
44 self._pitch=50 45 self._initTts()
46
47 - def terminate(self):
48 del self.tts
49
50 - def _getAvailableVoices(self):
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
66 - def _get_rate(self):
67 return (self.tts.rate*5)+50
68
69 - def _get_pitch(self):
70 return self._pitch
71
72 - def _get_volume(self):
73 return self.tts.volume
74
75 - def _get_voice(self):
76 return self.tts.voice.Id
77
78 - def _get_lastIndex(self):
79 bookmark=self.tts.status.LastBookmark 80 if bookmark!="" and bookmark is not None: 81 return int(bookmark) 82 else: 83 return None
84
85 - def _set_rate(self,rate):
86 self.tts.Rate = (rate-50)/5
87
88 - def _set_pitch(self,value):
89 #pitch is really controled with xml around speak commands 90 self._pitch=value
91
92 - def _set_volume(self,value):
93 self.tts.Volume = value
94
95 - def _initTts(self):
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
101 - def _set_voice(self,value):
102 v=self.tts.GetVoices() 103 for i in range(len(v)): 104 if value==v[i].Id: 105 break 106 else: 107 # Voice not found. 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("<","&lt;")) 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 #Pitch must always be hardcoded 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
132 - def cancel(self):
133 #if self.tts.Status.RunningState == 2: 134 self.tts.Speak(None, 1|constants.SVSFPurgeBeforeSpeak)
135
136 - def pause(self,switch):
137 if switch: 138 self.cancel()
139