-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDW.TextToSpeech.pas
244 lines (209 loc) · 6.54 KB
/
DW.TextToSpeech.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
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
unit DW.TextToSpeech;
{*******************************************************}
{ }
{ Kastri }
{ }
{ Delphi Worlds Cross-Platform Library }
{ }
{ Copyright 2020-2022 Dave Nottage under MIT license }
{ which is located in the root folder of this library }
{ }
{*******************************************************}
{$I DW.GlobalDefines.inc}
interface
uses
System.Classes;
type
TTextToSpeech = class;
TCustomPlatformTextToSpeech = class(TObject)
private
FAvailableVoices: TArray<string>;
FCanSpeak: Boolean;
FLanguage: string;
FTextToSpeech: TTextToSpeech;
FUnavailableVoices: TArray<string>;
procedure SetCanSpeak(const Value: Boolean);
protected
procedure DoCheckDataComplete;
procedure DoSpeechStarted;
procedure DoSpeechFinished;
function CheckData: Boolean; virtual;
function IsSpeaking: Boolean; virtual;
function Speak(const AText: string): Boolean; virtual;
procedure Stop; virtual;
property AvailableVoices: TArray<string> read FAvailableVoices write FAvailableVoices;
property CanSpeak: Boolean read FCanSpeak write SetCanSpeak;
property Language: string read FLanguage write FLanguage;
property UnavailableVoices: TArray<string> read FUnavailableVoices write FUnavailableVoices;
public
constructor Create(const ATextToSpeech: TTextToSpeech); virtual;
end;
TTextToSpeech = class(TObject)
private
FPlatformTextToSpeech: TCustomPlatformTextToSpeech;
FOnCanSpeakChanged: TNotifyEvent;
FOnCheckDataComplete: TNotifyEvent;
FOnSpeechStarted: TNotifyEvent;
FOnSpeechFinished: TNotifyEvent;
function GetLanguage: string;
procedure SetLanguage(const Value: string);
function GetAvailableVoices: TArray<string>;
function GetUnavailableVoices: TArray<string>;
protected
procedure DoCanSpeakChanged;
procedure DoCheckDataComplete;
procedure DoSpeechStarted;
procedure DoSpeechFinished;
public
constructor Create;
/// <summary>
/// Indicates whether or not the text to speech engine is available
/// </summary>
function CanSpeak: Boolean;
/// <summary>
/// Starts the activity responsible for checking TTS data, and if successful, starts the activity that installs the data.
/// Returns True if the activity is started.
/// </summary>
/// <remarks>
/// Applies to Android ONLY
/// </remarks>
function CheckData: Boolean;
/// <summary>
/// Indicates whether or not the text to speech engine is in the process of speaking
/// </summary>
function IsSpeaking: Boolean;
/// <summary>
/// Invokes the text to speech engine to utter the text in AText
/// </summary>
function Speak(const AText: string): Boolean;
/// <summary>
/// Stops the text to speech engine from speaking
/// </summary>
procedure Stop;
property AvailableVoices: TArray<string> read GetAvailableVoices;
property UnavailableVoices: TArray<string> read GetUnavailableVoices;
/// <summary>
/// The language being used when Speak is called
/// </summary>
property Language: string read GetLanguage write SetLanguage;
property OnCanSpeakChanged: TNotifyEvent read FOnCanSpeakChanged write FOnCanSpeakChanged;
property OnCheckDataComplete: TNotifyEvent read FOnCheckDataComplete write FOnCheckDataComplete;
property OnSpeechStarted: TNotifyEvent read FOnSpeechStarted write FOnSpeechStarted;
property OnSpeechFinished: TNotifyEvent read FOnSpeechFinished write FOnSpeechFinished;
end;
implementation
uses
{$IF Defined(IOS)}
DW.TextToSpeech.iOS;
{$ELSEIF Defined(ANDROID)}
DW.TextToSpeech.Android;
{$ELSEIF Defined(MSWINDOWS)}
DW.TextToSpeech.Win;
{$ELSEIF Defined(MACOS)}
DW.TextToSpeech.Mac;
{$ENDIF}
{ TCustomPlatformTextToSpeech }
constructor TCustomPlatformTextToSpeech.Create(const ATextToSpeech: TTextToSpeech);
begin
inherited Create;
FTextToSpeech := ATextToSpeech;
end;
procedure TCustomPlatformTextToSpeech.DoCheckDataComplete;
begin
FTextToSpeech.DoCheckDataComplete;
end;
procedure TCustomPlatformTextToSpeech.DoSpeechFinished;
begin
FTextToSpeech.DoSpeechFinished;
end;
procedure TCustomPlatformTextToSpeech.DoSpeechStarted;
begin
FTextToSpeech.DoSpeechStarted;
end;
function TCustomPlatformTextToSpeech.CheckData: Boolean;
begin
Result := False;
end;
function TCustomPlatformTextToSpeech.IsSpeaking: Boolean;
begin
Result := False;
end;
procedure TCustomPlatformTextToSpeech.SetCanSpeak(const Value: Boolean);
begin
if Value <> FCanSpeak then
begin
FCanSpeak := Value;
FTextToSpeech.DoCanSpeakChanged;
end;
end;
function TCustomPlatformTextToSpeech.Speak(const AText: String): Boolean;
begin
Result := False;
end;
procedure TCustomPlatformTextToSpeech.Stop;
begin
//
end;
{ TTextToSpeech }
constructor TTextToSpeech.Create;
begin
inherited;
FPlatformTextToSpeech := TPlatformTextToSpeech.Create(Self);
end;
function TTextToSpeech.CanSpeak: Boolean;
begin
Result := FPlatformTextToSpeech.CanSpeak;
end;
procedure TTextToSpeech.DoCanSpeakChanged;
begin
if Assigned(FOnCanSpeakChanged) then
FOnCanSpeakChanged(Self);
end;
procedure TTextToSpeech.DoCheckDataComplete;
begin
if Assigned(FOnCheckDataComplete) then
FOnCheckDataComplete(Self);
end;
procedure TTextToSpeech.DoSpeechFinished;
begin
if Assigned(FOnSpeechFinished) then
FOnSpeechFinished(Self);
end;
procedure TTextToSpeech.DoSpeechStarted;
begin
if Assigned(FOnSpeechStarted) then
FOnSpeechStarted(Self);
end;
function TTextToSpeech.GetAvailableVoices: TArray<string>;
begin
Result := FPlatformTextToSpeech.AvailableVoices;
end;
function TTextToSpeech.GetLanguage: string;
begin
Result := FPlatformTextToSpeech.Language;
end;
function TTextToSpeech.GetUnavailableVoices: TArray<string>;
begin
Result := FPlatformTextToSpeech.UnavailableVoices;
end;
function TTextToSpeech.CheckData: Boolean;
begin
Result := FPlatformTextToSpeech.CheckData;
end;
function TTextToSpeech.IsSpeaking: Boolean;
begin
Result := FPlatformTextToSpeech.IsSpeaking;
end;
procedure TTextToSpeech.SetLanguage(const Value: string);
begin
FPlatformTextToSpeech.Language := Value;
end;
function TTextToSpeech.Speak(const AText: String): Boolean;
begin
Result := FPlatformTextToSpeech.Speak(AText);
end;
procedure TTextToSpeech.Stop;
begin
FPlatformTextToSpeech.Stop;
end;
end.