forked from pvagner/backends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sapi.py
363 lines (320 loc) · 12 KB
/
sapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# -*- coding: utf-8 -*-
import os, sys, wave, array, io
try:
import importlib
importHelper = importlib.import_module
except ImportError:
importHelper = __import__
from .base import SimpleTTSBackendBase
from lib import util
from xml.sax import saxutils
def lookupGenericComError(com_error):
try:
errno = '0x%08X' % (com_error.hresult & 0xffffffff)
with open(os.path.join(util.backendsDirectory(),'comerrors.txt'),'r') as f:
lines = f.read().splitlines()
for l1,l2,l3 in zip(lines[0::3],lines[1::3],lines[2::3]):
if errno in l2:
return l1,l3
except:
pass
return None
class SAPI():
DEFAULT = 0
ASYNC = 1
PURGE_BEFORE_SPEAK = 2
IS_FILENAME = 4
IS_XML = 8
IS_NOT_XML = 16
PERSIST_XML = 32
SPEAK_PUNC = 64
PARSE_SAPI = 128
def __init__(self):
self.SpVoice = None
self.comtypesClient = None
self.valid = False
self._voiceName = None
self.interrupt = False
try:
self.reset()
except:
util.ERROR('SAPI: Initialization failed: retrying...')
util.sleep(1000) #May not be necessary, but here it is
try:
self.reset()
except:
util.ERROR('SAPI: Initialization failed: Giving up.')
return
self.valid = True
self.COMError = importHelper('_ctypes').COMError
self.setStreamFlags()
def importComtypes(self):
#Remove all (hopefully) refrences to comtypes import...
del self.comtypesClient
self.comtypesClient = None
for m in list(sys.modules.keys()):
if m.startswith('comtypes'): del sys.modules[m]
import gc
gc.collect()
#and then import
self.comtypesClient = importHelper('comtypes.client')
def reset(self):
del self.SpVoice
self.SpVoice = None
self.cleanComtypes()
self.importComtypes()
self.resetSpVoice()
def resetSpVoice(self):
self.SpVoice = self.comtypesClient.CreateObject("SAPI.SpVoice")
voice = self._getVoice()
if voice: self.SpVoice.Voice = voice
def setStreamFlags(self):
self.flags = self.PARSE_SAPI | self.IS_XML | self.ASYNC
self.streamFlags = self.PARSE_SAPI | self.IS_XML | self.ASYNC
try:
self.SpVoice.Speak('',self.flags)
except self.COMError as e:
if util.DEBUG:
self.logSAPIError(e)
util.LOG('SAPI: XP Detected - changing flags')
self.flags = self.ASYNC
self.streamFlags = self.ASYNC
def cleanComtypes(self): #TODO: Make this SAPI specific?
try:
gen = os.path.join(util.backendsDirectory(),'comtypes','gen')
import stat, shutil
os.chmod(gen,stat.S_IWRITE)
shutil.rmtree(gen,ignore_errors=True)
if not os.path.exists(gen): os.makedirs(gen)
except:
util.ERROR('SAPI: Failed to empty comtypes gen dir')
def logSAPIError(self,com_error,extra=''):
try:
errno = str(com_error.hresult)
with open(os.path.join(util.backendsDirectory(),'sapi_comerrors.txt'),'r') as f:
lines = f.read().splitlines()
for l1,l2 in zip(lines[0::2],lines[1::2]):
bits = l1.split()
if errno in bits:
util.LOG('SAPI specific COM error ({0})[{1}]: {2}'.format(errno,bits[0],l2 or '?'))
break
else:
error = lookupGenericComError(com_error)
if error:
util.LOG('SAPI generic COM error ({0})[{1}]: {2}'.format(errno,error[0],error[1] or '?'))
else:
util.LOG('Failed to lookup SAPI/COM error: {0}'.format(com_error))
except:
util.ERROR('Error looking up SAPI error: {0}'.format(com_error))
util.LOG('Line: {1} In: {0}{2}'.format(sys.exc_info()[2].tb_frame.f_code.co_name, sys.exc_info()[2].tb_lineno, extra and ' ({0})'.format(extra) or ''))
if util.DEBUG: util.ERROR('Debug:')
def _getVoice(self,voice_name=None):
voice_name = voice_name or self._voiceName
if voice_name:
v = self.SpVoice.getVoices() or []
for i in range(len(v)):
voice=v[i]
if voice_name==voice.GetDescription():
return voice
return None
def checkSAPI(func):
def checker(self,*args,**kwargs):
if not self.valid:
util.LOG('SAPI: Broken - ignoring {0}'.format(func.__name__))
return None
try:
return func(self,*args,**kwargs)
except self.COMError as e:
self.logSAPIError(e,func.__name__)
except:
util.ERROR('SAPI: {0} error'.format(func.__name__))
self.valid = False
util.LOG('SAPI: Resetting...')
util.sleep(1000)
try:
self.reset()
self.valid = True
util.LOG('SAPI: Resetting succeded.')
return func(self,*args,**kwargs)
except self.COMError as e:
self.valid = False
self.logSAPIError(e,func.__name__)
except:
self.valid = False
util.ERROR('SAPI: {0} error'.format(func.__name__))
return checker
#Wrapped SAPI methods
@checkSAPI
def SpVoice_Speak(self,ssml,flags):
return self.SpVoice.Speak(ssml,flags)
@checkSAPI
def SpVoice_GetVoices(self):
return self.SpVoice.getVoices()
@checkSAPI
def stopSpeech(self):
self.SpVoice.Speak('',self.ASYNC | self.PURGE_BEFORE_SPEAK)
@checkSAPI
def SpFileStream(self):
return self.comtypesClient.CreateObject("SAPI.SpFileStream")
@checkSAPI
def SpAudioFormat(self):
return self.comtypesClient.CreateObject("SAPI.SpAudioFormat")
@checkSAPI
def SpMemoryStream(self):
return self.comtypesClient.CreateObject("SAPI.SpMemoryStream")
def validCheck(func):
def checker(self,*args,**kwargs):
if not self.valid:
util.LOG('SAPI: Broken - ignoring {0}'.format(func.__name__))
return
return func(self,*args,**kwargs)
return checker
@validCheck
def set_SpVoice_Voice(self,voice_name):
self._voiceName = voice_name
voice = self._getVoice(voice_name)
self.SpVoice.Voice = voice
@validCheck
def set_SpVoice_AudioOutputStream(self,stream):
self.SpVoice.AudioOutputStream = stream
class SAPITTSBackend(SimpleTTSBackendBase):
provider = 'SAPI'
displayName = 'SAPI (Windows Internal)'
settings = { 'speak_via_xbmc':True,
'voice':'',
'speed':0,
'pitch':0,
'volume':100
}
canStreamWav = True
speedConstraints = (-10,0,10,True)
pitchConstraints = (-10,0,10,True)
volumeConstraints = (0,100,100,True)
volumeExternalEndpoints = (0,100)
volumeStep = 5
volumeSuffix = '%'
baseSSML = '''<?xml version="1.0"?>
<speak version="1.0"
xmlns="http://www.w3.org/2001/10/synthesis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/10/synthesis
http://www.w3.org/TR/speech-synthesis/synthesis.xsd"
xml:lang="en-US">
<volume level="{volume}" />
<pitch absmiddle="{pitch}" />
<rate absspeed="{speed}" />
<p>{text}</p>
</speak>'''
def init(self):
self.sapi = SAPI()
if not self.sapi.valid:
self.flagAsDead('RESET')
return
self.update()
def sapiValidCheck(func):
def checker(self,*args,**kwargs):
if not self.sapi or not self.sapi.valid:
return self.flagAsDead('RESET')
else:
return func(self,*args,**kwargs)
return checker
@sapiValidCheck
def runCommand(self,text,outFile):
stream = self.sapi.SpFileStream()
if not stream: return False
try:
stream.Open(outFile, 3) #3=SSFMCreateForWrite
except self.sapi.COMError as e:
self.sapi.logSAPIError(e)
return False
ssml = self.ssml.format(text=saxutils.escape(text))
self.sapi.SpVoice_Speak(ssml,self.sapi.streamFlags)
stream.close()
return True
@sapiValidCheck
def runCommandAndSpeak(self,text):
ssml = self.ssml.format(text=saxutils.escape(text))
self.sapi.SpVoice_Speak(ssml,self.sapi.flags)
@sapiValidCheck
def getWavStream(self,text):
fmt = self.sapi.SpAudioFormat()
if not fmt: return None
fmt.Type = 22
stream = self.sapi.SpMemoryStream()
if not stream: return None
stream.Format = fmt
self.sapi.set_SpVoice_AudioOutputStream(stream)
ssml = self.ssml.format(text=saxutils.escape(text))
self.sapi.SpVoice_Speak(ssml,self.streamFlags)
wavIO = io.StringIO()
self.createWavFileObject(wavIO,stream)
return wavIO
def createWavFileObject(self,wavIO,stream):
#Write wave via the wave module
wavFileObj = wave.open(wavIO,'wb')
wavFileObj.setparams((1, 2, 22050, 0, 'NONE', 'not compressed'))
wavFileObj.writeframes(array.array('B',stream.GetData()).tostring())
wavFileObj.close()
def stop(self):
if not self.sapi: return
if not self.inWavStreamMode:
self.sapi.stopSpeech()
def update(self):
self.setMode(self.getMode())
self.ssml = self.baseSSML.format(text='{text}',volume=self.setting('volume'),speed=self.setting('speed'),pitch=self.setting('pitch'))
voice_name = self.setting('voice')
self.sapi.set_SpVoice_Voice(voice_name)
def getMode(self):
if self.setting('speak_via_xbmc'):
return SimpleTTSBackendBase.WAVOUT
else:
if self.sapi: self.sapi.set_SpVoice_AudioOutputStream(None)
return SimpleTTSBackendBase.ENGINESPEAK
@classmethod
def settingList(cls,setting,*args):
sapi = SAPI()
if setting == 'voice':
voices=[]
v=sapi.SpVoice_GetVoices()
if not v: return voices
for i in range(len(v)):
try:
name=v[i].GetDescription()
except COMError as e: #analysis:ignore
sapi.logSAPIError(e)
voices.append((name,name))
return voices
@staticmethod
def available():
return sys.platform.lower().startswith('win')
# def getWavStream(self,text):
# #Have SAPI write to file
# stream = self.sapi.SpFileStream()
# fpath = os.path.join(util.getTmpfs(),'speech.wav')
# open(fpath,'w').close()
# stream.Open(fpath,3)
# self.sapi.set_SpVoice_AudioOutputStream(stream)
# self.sapi.SpVoice_Speak(text,0)
# stream.close()
# return open(fpath,'rb')
# def createWavFileObject(self,wavIO,stream):
# #Write wave headers manually
# import struct
# data = array.array('B',stream.GetData()).tostring()
# dlen = len(data)
# header = struct.pack( '4sl8slhhllhh4sl',
# 'RIFF',
# dlen+36,
# 'WAVEfmt ',
# 16, #Bits
# 1, #Mode
# 1, #Channels
# 22050, #Samplerate
# 22050*16/8, #Samplerate*Bits/8
# 1*16/8, #Channels*Bits/8
# 16,
# 'data',
# dlen
# )
# wavIO.write(header)
# wavIO.write(data)