-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsTTS.pas
157 lines (136 loc) · 3.63 KB
/
jsTTS.pas
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
unit jstts;
interface
uses
windows, messages, sysutils, classes, system.hash, mod_tts, ActiveX, wmipc;
procedure renderText(handle: dword; text, character, path, id: string);
implementation
const
timeout = 9000;
var
rendering: Boolean;
gvoices: Tstringlist;
function _md5(str: string): string;
begin
Result := THashMD5.GetHashString(str);
end;
function getvoice(vname: string): string;
var
i: integer;
begin
Result := '';
vname := trim((vname));
if gvoices.Count > 0 then
begin
for i := 0 to gvoices.Count - 1 do
begin
if ansipos(vname, trim(gvoices.Strings[i])) > 0 then
begin
Result := gvoices.Strings[i];
break;
end;
end;
end;
end;
function gethash(sp, text: string): string;
begin
Result := lowercase(_md5(getvoice(sp) + text));
end;
function makePath(path: string): string;
begin
Result := stringreplace(path, '\', '/', [rfReplaceAll, rfIgnoreCase]);
end;
procedure debug(str: string);
begin
OutputDebugStringA(Pansichar(AnsiString(str)));
end;
procedure renderText(handle: dword; text, character, path, id: string);
var
T: TThread;
f: string;
S: TRTLCriticalSection;
begin
// debug('tts render');
if gvoices.Count > 0 then
begin
f := path + gethash(character, text) + '.wav';
if fileexists(f) then
begin
// debug('tts cache hit: ' + text);
wm_sendstringex(handle, handle, pwidechar('~eval=' + id + '(' + quotedstr(f) + ',0);' + id + '=undefined;'));
exit;
end;
end;
InitializeCriticalSection(S);
T := TThread.CreateAnonymousThread(
procedure
var
filename: string;
tts: Tsapi;
start: int64;
begin
// debug('tts thread start');
start := gettickcount64;
while rendering = true do
begin
if gettickcount64 - start > timeout then
begin
rendering := False;
break;
end;
sleep(1);
end;
rendering := true;
EnterCriticalSection(S);
coInitialize(nil);
tts := Tsapi.create;
if gvoices.Count <= 0 then
tts.enumvoices(gvoices);
tts.SetVoice(character);
OutputDebugString(pchar(character));
filename := makePath(tts.render(text, path));
wm_sendstringex(handle, handle, pwidechar('~eval=' + id + '(' + quotedstr(filename) + ',' + inttostr(gettickcount - start) + ');' + id + '=undefined;'));
sleep(1);
tts.free;
LeaveCriticalSection(S);
rendering := False;
end);
T.FreeOnTerminate := true;
T.start;
end;
procedure speakFast(handle: dword; text, character, path, id: string);
var
T: TThread;
f: string;
S: TRTLCriticalSection;
begin
debug('tts start');
InitializeCriticalSection(S);
T := TThread.CreateAnonymousThread(
procedure
var
filename: string;
tts: Tsapi;
start: int64;
begin
debug('tts thread start');
start := gettickcount;
EnterCriticalSection(S);
coInitialize(nil);
tts := Tsapi.create;
if gvoices.Count <= 0 then
tts.enumvoices(gvoices);
tts.SetVoice(character);
OutputDebugString(pchar(character));
filename := makePath(tts.render(text, path));
wm_sendstringex(handle, handle, pwidechar('~eval=' + id + '(' + quotedstr(filename) + ',' + inttostr(gettickcount - start) + ');' + id + '=undefined;'));
sleep(1);
tts.free;
LeaveCriticalSection(S);
rendering := False;
end);
T.FreeOnTerminate := true;
T.start;
end;
initialization
gvoices := Tstringlist.create;
end.