-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
630 lines (524 loc) · 22.4 KB
/
__init__.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# -*- coding: utf-8 -*-
#
"""<rst>
Plugin for using **eSpeak** text to speach program.
eSpeak installation does not contain **libespeak.dll** which is required for this plugin.
Portable 32-bit libespeak.dll for eSpeak version 1.48 is supplied with this plugin
(in its directory) -- you are free to use and relocate it as eSpeak licence permits.
If you did not run eSpeak installation program, system has no eSpeak **registry** record
and no environment variable **ESPEAK_DATA_PATH** with the path of the directory which contains
the **espeak-data** directory. You can configure plugin to you use eSpeak without them,
just locate espeak-data directory containing data of metioned eSpeak version.
The configuration failures will be printed to EventGhost log.
**Links:**
`eSpeak <http://espeak.sourceforge.net>`_
`eSpeak Documentation <http://espeak.sourceforge.net/docindex.html>`_
`Search Path Used by Windows to Locate a DLL <https://docs.microsoft.com/en-us/windows/desktop/Dlls/dynamic-link-library-search-order>`_
"""
import eg
eg.RegisterPlugin(
name = "eSpeak",
author = "obermann",
version = "1.48.15.01",
kind = "program",
canMultiLoad = False,
description = __doc__,
guid = "{B4ABA767-CE75-4012-863E-0C9C3994591C}",
url = eg.folderPath.ProgramFiles + r"\eSpeak\docs\index.html",
icon = (
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAADdEREZXxxOAAAAAXRS"
"TlMAQObYZgAAADdJREFUeF4VwjENACAMAMFPOrCBgQaQ0aGiKgFpSEECY0fC5fgmEpRLS7rjlRik"
"YoYG7fxlIwt4xzMI/RonXVkAAAAASUVORK5CYII="
)
)
import wx
import os
import locale
import _winreg
from ctypes import c_int, c_uint, c_ubyte, c_size_t, c_char_p, c_void_p
from ctypes import POINTER, CFUNCTYPE, CDLL, Structure, sizeof, create_string_buffer
AUDIO_OUTPUT_PLAYBACK = 0 # PLAYBACK mode: plays the audio data, supplies events to the calling program
AUDIO_OUTPUT_RETRIEVAL = 1 # RETRIEVAL mode: supplies audio data and events to the calling program
AUDIO_OUTPUT_SYNCHRONOUS = 2 # SYNCHRONOUS mode: as RETRIEVAL but doesn't return until synthesis is completed
AUDIO_OUTPUT_SYNCH_PLAYBACK = 3 # Synchronous playback
espeakINITIALIZE_PHONEME_EVENTS = 0x0001
espeakINITIALIZE_PHONEME_IPA = 0x0002
espeakINITIALIZE_DONT_EXIT = 0x8000
EE_OK = 0
EE_INTERNAL_ERROR = -1
EE_BUFFER_FULL = 1
EE_NOT_FOUND = 2
espeakSSML = 0x10
espeakPHONEMES = 0x100
espeakENDPAUSE = 0x1000
espeakKEEP_NAMEDATA = 0x2000
espeakCHARS_UTF8 = 1
# enums:
espeak_POSITION_TYPE = c_int
espeak_AUDIO_OUTPUT = c_int
espeak_ERROR = c_int
espeak_PARAMETER = c_int
class espeak_VOICE(Structure):
_fields_ = [('name', c_char_p),
('languages', c_char_p),
('identifier', c_char_p),
('gender', c_ubyte),
('age', c_ubyte),
('variant', c_ubyte),
('xx1', c_ubyte),
('score', c_int),
('spare', c_void_p)
]
OK_RATE = 8000
class STuple(tuple):
def __new__ (cls, *args):
return super(STuple, cls).__new__(cls, args)
def seek(self, key):
try:
return self.index(key)
except ValueError:
return len(self) if key else 0
def search(self, key):
try:
return self.index(key)
except ValueError:
return 0
PARAMETERS = STuple(
"Ignore", # internal use
"Rate",
"Volume",
"Pitch",
"Range",
"Punctuation",
"Capitals",
"Wordgap",
"Options", # reserved for misc. options. not yet used
"Intonation",
)
class Text:
e01 = "Error loading DLL: %s"
e02 = "Error. Not found: %s"
e03 = "Error. eSpeak installation not found."
e04 = "eSpeak is not intialized."
e05 = "Error. eSpeak initialization failure (returned value = %i). Check plugin configuration for espeak-data."
e06 = "Error. Path to espeak-data cannot not contain foreign characters."
n01 = "libespeak.dll successfully loaded from: %s"
n02 = "eSpeak successfully initialized with sample rate: %i"
dlg01 = "Location of espeak-data:"
dlg02 = "Location of libespeak.dll:"
dlg05 = "Select the directory which contains the espeak-data directory:"
dlg06 = "Select the directory which contains libespeak.dll:"
c01 = "Set by eSpeak Installation"
c02 = "Same as espeak-data"
c03 = "On System's Seach Path"
c04 = "Directory of this Plugin"
c05 = "Custom:"
c10 = "Use EventGhost Result Value"
c11 = "Set Relative"
c12 = "Request Default"
c21 = "{...} At First Evaluate Text for EventGhost Python Scripts"
c22 = "<_>...</_> Interpret SSML Markup"
c23 = "[[...]] Interpret Phoneme Codes"
c24 = "Add a Sentence Pause at the End of the Text"
c25 = "Synchronize - Wait for a Speach to End"
class eSpeak(eg.PluginClass):
text = Text()
def _load(self):
path = "libespeak" if self.espeak_dll is None else os.path.join(self.espeak_dll, "libespeak")
try:
self.libespeak = CDLL(path)
except:
eg.PrintError(self.text.e01 % path)
self.libespeak = None
return
self.espeak_Initialize = self.libespeak.espeak_Initialize
self.espeak_Initialize.restype = espeak_ERROR
self.espeak_Initialize.argtypes = [espeak_AUDIO_OUTPUT, c_int, c_char_p, c_int]
self.espeak_Synth = self.libespeak.espeak_Synth
self.espeak_Synth.restype = espeak_ERROR
self.espeak_Synth.argtypes = [c_void_p, c_size_t, c_uint, espeak_POSITION_TYPE, c_uint, c_uint, POINTER(c_uint), c_void_p]
self.espeak_SetParameter = self.libespeak.espeak_SetParameter
self.espeak_SetParameter.restype = espeak_ERROR
self.espeak_SetParameter.argtypes = [espeak_PARAMETER, c_int, c_int]
self.espeak_GetParameter = self.libespeak.espeak_GetParameter
self.espeak_GetParameter.restype = c_int
self.espeak_GetParameter.argtypes = [espeak_PARAMETER, c_int]
self.espeak_ListVoices = self.libespeak.espeak_ListVoices
self.espeak_ListVoices.restype = POINTER(POINTER(espeak_VOICE))
self.espeak_ListVoices.argtypes = [POINTER(espeak_VOICE)]
self.espeak_SetVoiceByName = self.libespeak.espeak_SetVoiceByName
self.espeak_SetVoiceByName.restype = espeak_ERROR
self.espeak_SetVoiceByName.argtypes = [c_char_p]
self.espeak_SetVoiceByProperties = self.libespeak.espeak_SetVoiceByProperties
self.espeak_SetVoiceByProperties.restype = espeak_ERROR
self.espeak_SetVoiceByProperties.argtypes = [POINTER(espeak_VOICE)]
self.espeak_GetCurrentVoice = self.libespeak.espeak_GetCurrentVoice
self.espeak_GetCurrentVoice.restype = POINTER(espeak_VOICE)
self.espeak_GetCurrentVoice.argtypes = []
self.espeak_Cancel = self.libespeak.espeak_Cancel
self.espeak_Cancel.restype = espeak_ERROR
self.espeak_Cancel.argtypes = []
self.espeak_Synchronize = self.libespeak.espeak_Synchronize
self.espeak_Synchronize.restype = espeak_ERROR
self.espeak_Synchronize.argtypes = []
self.espeak_Terminate = self.libespeak.espeak_Terminate
self.espeak_Terminate.restype = espeak_ERROR
self.espeak_Terminate.argtypes = []
print self.text.n01 % self.espeak_dll
def __init__(self):
self.libespeak = None
self.rate = -1000
self.AddAction(SetVoice)
self.AddAction(GetVoice)
self.AddAction(SetParameter)
self.AddAction(GetParameter)
self.AddAction(Text)
self.AddAction(Cancel)
def __start__(self, espeak_data="installation", espeak_dll="plugin"):
self.espeak_data = None
if espeak_data == "installation":
pass
elif espeak_data == "plugin":
self.espeak_data = os.path.dirname(os.path.abspath(__file__))
else:
self.espeak_data = espeak_data
if self.espeak_data is not None and not os.path.isdir(os.path.join(self.espeak_data, "espeak-data")):
eg.PrintError(self.text.e02 % os.path.join(self.espeak_data, "espeak-data"))
return
self.espeak_dll = None
if espeak_dll == "system":
pass
elif espeak_dll == "same":
if espeak_data == "installation":
# immitate speak_lib.cpp
# pass over eg.folderPath.ProgramFiles + r"\eSpeak"
try:
if os.path.isdir(os.path.join(os.environ["ESPEAK_DATA_PATH"], "espeak-data")):
self.espeak_dll = os.environ["ESPEAK_DATA_PATH"]
except KeyError:
pass
if self.espeak_dll is None:
try:
with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
r"SOFTWARE\Microsoft\Speech\Voices\Tokens\eSpeak") as key:
self.espeak_dll = _winreg.QueryValueEx(key, "Path")[0]
except:
pass
if self.espeak_dll is None:
eg.PrintError(self.text.e03)
return
else:
self.espeak_dll = self.espeak_data
elif espeak_dll == "plugin":
self.espeak_dll = os.path.dirname(os.path.abspath(__file__))
else:
self.espeak_dll = espeak_dll
if self.espeak_dll is not None and not os.path.isfile(os.path.join(self.espeak_dll, "libespeak.dll")):
eg.PrintError(self.text.e02 % os.path.join(self.espeak_dll, "libespeak.dll"))
return
if self.libespeak is None:
self._load()
if self.libespeak is None:
return
data = None
if self.espeak_data is not None:
try:
# DLL use fopen() for espeak_data
data = self.espeak_data.encode(locale.getdefaultlocale()[1])
except UnicodeEncodeError:
eg.PrintError(self.text.e06)
return
self.rate = self.espeak_Initialize(
AUDIO_OUTPUT_PLAYBACK,
0,
data,
espeakINITIALIZE_DONT_EXIT
)
# None of these checks will work with standard build of 1.48.15!
# Suplied DLL is patched and will work with both.
#if self.rate == EE_INTERNAL_ERROR:
if self.rate < OK_RATE:
#eg.PrintError("Error with returned sample rate", self.rate, "by", "espeak_Initialize")
eg.PrintError(self.text.e05 % self.rate)
else:
print self.text.n02 % self.rate
def __stop__(self):
eg.PrintDebugNotice("eSpeak __stop__()")
if self.libespeak is None or self.rate < OK_RATE:
return
result = self.espeak_Terminate()
if result != EE_OK:
eg.PrintError("Error No.", result, "by", "espeak_Terminate")
def is_off(self):
"""
Pattern for every action check:
if self.plugin.is_off(): return
"""
if self.libespeak is None or self.rate < OK_RATE:
eg.PrintError(self.text.e04)
return True
else:
return False
def Configure(self, espeak_data="installation", espeak_dll="plugin"):
data_list = STuple(
"installation",
"plugin"
)
dll_list = STuple(
"same",
"system",
"plugin"
)
panel = eg.ConfigPanel()
panel.AddCtrl(wx.StaticText(panel, -1, self.text.dlg01))
dataRadio = panel.RadioBox(data_list.seek(espeak_data), (self.text.c01, self.text.c04, self.text.c05))
panel.AddCtrl(dataRadio)
# dataControl = eg.DirBrowseButton(
# panel,
# startDirectory=espeak_data,
# buttonText=eg.text.General.browse,
# dialogTitle=self.text.dlg05
# )
dataControl = wx.DirPickerCtrl(
panel,
-1,
path=espeak_data,
message=self.text.dlg05,
style=wx.DIRP_USE_TEXTCTRL,
)
def dataEevent(evt):
if len(data_list) == evt.GetSelection():
dataControl.Enable()
else:
dataControl.Enable(False)
dataControl.SetPath("")
dataEevent(dataRadio)
dataRadio.Bind(wx.EVT_RADIOBOX, dataEevent)
panel.sizer.Add(dataControl, 0, wx.EXPAND)
panel.sizer.Add(wx.StaticLine(panel), 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
panel.AddCtrl(wx.StaticText(panel, -1, self.text.dlg02))
dllRadio = panel.RadioBox(dll_list.seek(espeak_dll), (self.text.c02, self.text.c03, self.text.c04, self.text.c05))
panel.AddCtrl(dllRadio)
dllControl = wx.DirPickerCtrl(
panel,
-1,
path=espeak_dll,
message=self.text.dlg06,
style=wx.DIRP_USE_TEXTCTRL,
)
# dllControl = wx.FilePickerCtrl(
# panel,
# -1,
# path=espeak_dll,
# wildcard="libespeak.dll",
# message=self.text.dlg06,
# style=wx.FLP_OPEN | wx.FLP_USE_TEXTCTRL,
# )
def dllEevent(evt):
if len(dll_list) == evt.GetSelection():
dllControl.Enable()
else:
dllControl.Enable(False)
dllControl.SetPath("")
dllEevent(dllRadio)
dllRadio.Bind(wx.EVT_RADIOBOX, dllEevent)
panel.sizer.Add(dllControl, 0, wx.EXPAND)
while panel.Affirmed():
global data
global dll
# eg's DirBrowseButton.IsEnabled() is buggy
if len(data_list) == dataRadio.GetValue():
data = dataControl.GetPath()
if data in data_list:
data = ".\\" + data
else:
data = data_list[dataRadio.GetValue()]
if len(dll_list) == dllRadio.GetValue():
dll = dllControl.GetPath()
if dll in dll_list:
dll = ".\\" + dll
else:
dll = dll_list[dllRadio.GetValue()]
panel.SetResult(data, dll)
class SetParameter(eg.ActionBase):
"""
Set the relative or absolute value of the specified parameter.
"""
name = "Set Parameter"
with open(os.path.join(os.path.dirname(__file__), "parameters.html"), "r") as file:
description = __doc__ + "\n\n" + file.read().decode("utf8")
def __call__(self, type, value, relative=False, from_eg_result=False):
if self.plugin.is_off() or not PARAMETERS.search(type.capitalize()):
return
if from_eg_result:
# ValueError expected from users, its OK
value = int(eg.result)
result = self.plugin.espeak_SetParameter(PARAMETERS.search(type.capitalize()), value, relative)
if result != EE_OK:
eg.PrintError("Error No.", result, "by", "espeak_SetParameter", "with value:", value)
def Configure(self, type="Ignore", value=0, relative=False, from_eg_result=False):
panel = eg.ConfigPanel()
typeControl = panel.Choice(PARAMETERS.search(type.capitalize()), PARAMETERS)
relativeControl = panel.CheckBox(relative, self.plugin.text.c11)
valueControl = panel.SpinIntCtrl(value, min=-10000, max=10000)
valueControl.Enable(not from_eg_result)
fromControl = panel.CheckBox(from_eg_result, self.plugin.text.c10)
def egresultOn(evt):
if evt.IsChecked():
valueControl.Enable(False)
valueControl.SetValue(0)
else:
valueControl.Enable()
fromControl.Bind(wx.EVT_CHECKBOX, egresultOn)
panel.AddCtrl(typeControl)
panel.AddCtrl(relativeControl)
panel.AddCtrl(valueControl)
panel.AddCtrl(fromControl)
while panel.Affirmed():
panel.SetResult(
PARAMETERS[typeControl.GetValue()],
valueControl.GetValue(),
relativeControl.GetValue(),
fromControl.GetValue(),
)
class GetParameter(eg.ActionBase):
"""
Returns the current or default value of the specified parameter.
"""
name = "Get Parameter"
with open(os.path.join(os.path.dirname(__file__), "parameters.html"), "r") as file:
description = __doc__ + "\n\n" + file.read().decode("utf8")
def __call__(self, type, default=False):
if not self.plugin.is_off() and PARAMETERS.search(type.capitalize()):
return self.plugin.espeak_GetParameter(PARAMETERS.search(type.capitalize()), int(not default))
def Configure(self, type="Ignore", default=False):
panel = eg.ConfigPanel()
typeControl = panel.Choice(PARAMETERS.search(type.capitalize()), PARAMETERS)
defaultControl = panel.CheckBox(default, self.plugin.text.c12)
panel.AddCtrl(typeControl)
panel.AddCtrl(defaultControl)
while panel.Affirmed():
panel.SetResult(
PARAMETERS[typeControl.GetValue()],
defaultControl.GetValue(),
)
class SetVoice(eg.ActionBase):
name = "Set Voice"
description = "Select the voice by name."
def __call__(self, voice, from_eg_result=False):
if self.plugin.is_off(): return
if from_eg_result:
voice = eg.result
if voice:
result = self.plugin.espeak_SetVoiceByName(voice.encode("utf-8"))
if result != EE_OK:
eg.PrintError("Error No.", result, "by", "espeak_SetVoiceByName", "with value:", voice)
else:
eg.PrintError("Error. None voice was not set.")
def Configure(self, voice="default", from_eg_result=False):
selection = 0
list = []
result = None
if not self.plugin.is_off():
result = self.plugin.espeak_ListVoices(None)
if result:
for pVoice in result:
if pVoice:
#list.append(pVoice.contents.identifier.decode())
list.append(pVoice.contents.name.decode("utf-8"))
else:
break # important
try:
selection = list.index(voice)
except ValueError:
pass
panel = eg.ConfigPanel()
voiceControl = panel.Choice(selection, list)
voiceControl.Enable(not from_eg_result)
fromControl = panel.CheckBox(from_eg_result, self.plugin.text.c10)
def egresultOn(evt):
if evt.IsChecked():
voiceControl.Enable(False)
else:
voiceControl.Enable()
fromControl.Bind(wx.EVT_CHECKBOX, egresultOn)
panel.AddCtrl(voiceControl)
panel.AddCtrl(fromControl)
while panel.Affirmed():
panel.SetResult(
list[voiceControl.GetValue()] if list else "",
fromControl.GetValue()
)
class GetVoice(eg.ActionBase):
"""
Returns the name for the currently selected voice.
This is not affected by temporary voice changes caused by SSML elements such as <voice> or <s>.
"""
name = "Get Voice"
description = __doc__
def __call__(self):
if self.plugin.is_off(): return
result = self.plugin.espeak_GetCurrentVoice()
if result:
return result.contents.name.decode("utf-8")
else:
eg.PrintError("Error by", "espeak_GetCurrentVoice")
class Text(eg.ActionBase):
name = "Text"
with open(os.path.join(os.path.dirname(__file__), "text.html"), "r") as file:
description = file.read().decode("utf8")
def __call__(self, text, flags=0, eval=False, sync=True):
if self.plugin.is_off(): return
if eval:
text = eg.ParseString(text)
if not text:
return
#pText = c_char_p(text.encode("utf-8"))
pText = create_string_buffer(text.encode("utf-8"))
result = self.plugin.espeak_Synth(pText, sizeof(pText), 0, 0, 0, espeakCHARS_UTF8 | flags, None, None)
del pText
if result != EE_OK:
eg.PrintError("Error No.", result, "by", "espeak_Synth")
return
if sync:
result = self.plugin.espeak_Synchronize()
if result != EE_OK:
eg.PrintError("Error No.", result, "by", "espeak_Synchronize")
def Configure(self, text="", flags=0, eval=False, sync=True):
panel = eg.ConfigPanel()
textControl = panel.TextCtrl(text)
evalControl = panel.CheckBox(eval, self.plugin.text.c21)
ssmlControl = panel.CheckBox(flags & espeakSSML, self.plugin.text.c22)
phonemesControl = panel.CheckBox(flags & espeakPHONEMES, self.plugin.text.c23)
endpauseControl = panel.CheckBox(flags & espeakENDPAUSE, self.plugin.text.c24)
syncControl = panel.CheckBox(sync, self.plugin.text.c25)
panel.sizer.Add(textControl, 1, wx.EXPAND | wx.BOTTOM, 10)
panel.AddCtrl(evalControl)
panel.AddCtrl(ssmlControl)
panel.AddCtrl(phonemesControl)
panel.AddCtrl(endpauseControl)
panel.AddCtrl(syncControl)
while panel.Affirmed():
panel.SetResult(
textControl.GetValue(),
(
(espeakSSML if ssmlControl.GetValue() else 0) |
(espeakPHONEMES if phonemesControl.GetValue() else 0) |
(espeakENDPAUSE if endpauseControl.GetValue() else 0)
),
evalControl.GetValue(),
syncControl.GetValue(),
)
class Cancel(eg.ActionBase):
"""
Stop immediately synthesis and audio output of the current text.
When this function returns, the audio output is fully stopped
and the synthesizer is ready to synthesize a new message.
"""
name = "Cancel"
description = __doc__
def __call__(self):
if self.plugin.is_off(): return
result = self.plugin.espeak_Cancel()
if result != EE_OK:
eg.PrintError("Error No.", result, "by", "espeak_Cancel")