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
|
/**************************************************************************/
/* GodotTTS.java */
/**************************************************************************/
/* This file is part of: */
/* REDOT ENGINE */
/* https://redotengine.org */
/**************************************************************************/
/* Copyright (c) 2024-present Redot Engine contributors */
/* (see REDOT_AUTHORS.md) */
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
package org.godotengine.godot.tts;
import org.godotengine.godot.GodotLib;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.speech.tts.Voice;
import androidx.annotation.Keep;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
/**
* Wrapper for Android Text to Speech API and custom utterance query implementation.
* <p>
* A [GodotTTS] provides the following features:
* <p>
* <ul>
* <li>Access to the Android Text to Speech API.
* <li>Utterance pause / resume functions, unsupported by Android TTS API.
* </ul>
*/
@Keep
public class GodotTTS extends UtteranceProgressListener {
// Note: These constants must be in sync with DisplayServer::TTSUtteranceEvent enum from "servers/display_server.h".
final private static int EVENT_START = 0;
final private static int EVENT_END = 1;
final private static int EVENT_CANCEL = 2;
final private static int EVENT_BOUNDARY = 3;
private final Context context;
private TextToSpeech synth;
private LinkedList<GodotUtterance> queue;
final private Object lock = new Object();
private GodotUtterance lastUtterance;
private boolean speaking;
private boolean paused;
public GodotTTS(Context context) {
this.context = context;
}
private void updateTTS() {
if (!speaking && queue.size() > 0) {
int mode = TextToSpeech.QUEUE_FLUSH;
GodotUtterance message = queue.pollFirst();
Set<Voice> voices = synth.getVoices();
for (Voice v : voices) {
if (v.getName().equals(message.voice)) {
synth.setVoice(v);
break;
}
}
synth.setPitch(message.pitch);
synth.setSpeechRate(message.rate);
Bundle params = new Bundle();
params.putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME, message.volume / 100.f);
lastUtterance = message;
lastUtterance.start = 0;
lastUtterance.offset = 0;
paused = false;
synth.speak(message.text, mode, params, String.valueOf(message.id));
speaking = true;
}
}
/**
* Called by TTS engine when the TTS service is about to speak the specified range.
*/
@Override
public void onRangeStart(String utteranceId, int start, int end, int frame) {
synchronized (lock) {
if (lastUtterance != null && Integer.parseInt(utteranceId) == lastUtterance.id) {
lastUtterance.offset = start;
GodotLib.ttsCallback(EVENT_BOUNDARY, lastUtterance.id, start + lastUtterance.start);
}
}
}
/**
* Called by TTS engine when an utterance was canceled in progress.
*/
@Override
public void onStop(String utteranceId, boolean interrupted) {
synchronized (lock) {
if (lastUtterance != null && !paused && Integer.parseInt(utteranceId) == lastUtterance.id) {
GodotLib.ttsCallback(EVENT_CANCEL, lastUtterance.id, 0);
speaking = false;
updateTTS();
}
}
}
/**
* Called by TTS engine when an utterance has begun to be spoken..
*/
@Override
public void onStart(String utteranceId) {
synchronized (lock) {
if (lastUtterance != null && lastUtterance.start == 0 && Integer.parseInt(utteranceId) == lastUtterance.id) {
GodotLib.ttsCallback(EVENT_START, lastUtterance.id, 0);
}
}
}
/**
* Called by TTS engine when an utterance was successfully finished.
*/
@Override
public void onDone(String utteranceId) {
synchronized (lock) {
if (lastUtterance != null && !paused && Integer.parseInt(utteranceId) == lastUtterance.id) {
GodotLib.ttsCallback(EVENT_END, lastUtterance.id, 0);
speaking = false;
updateTTS();
}
}
}
/**
* Called by TTS engine when an error has occurred during processing.
*/
@Override
public void onError(String utteranceId, int errorCode) {
synchronized (lock) {
if (lastUtterance != null && !paused && Integer.parseInt(utteranceId) == lastUtterance.id) {
GodotLib.ttsCallback(EVENT_CANCEL, lastUtterance.id, 0);
speaking = false;
updateTTS();
}
}
}
/**
* Called by TTS engine when an error has occurred during processing (pre API level 21 version).
*/
@Override
public void onError(String utteranceId) {
synchronized (lock) {
if (lastUtterance != null && !paused && Integer.parseInt(utteranceId) == lastUtterance.id) {
GodotLib.ttsCallback(EVENT_CANCEL, lastUtterance.id, 0);
speaking = false;
updateTTS();
}
}
}
/**
* Initialize synth and query.
*/
public void init() {
synth = new TextToSpeech(context, null);
queue = new LinkedList<GodotUtterance>();
synth.setOnUtteranceProgressListener(this);
}
/**
* Adds an utterance to the queue.
*/
public void speak(String text, String voice, int volume, float pitch, float rate, int utterance_id, boolean interrupt) {
synchronized (lock) {
GodotUtterance message = new GodotUtterance(text, voice, volume, pitch, rate, utterance_id);
queue.addLast(message);
if (isPaused()) {
resumeSpeaking();
} else {
updateTTS();
}
}
}
/**
* Puts the synthesizer into a paused state.
*/
public void pauseSpeaking() {
synchronized (lock) {
if (!paused) {
paused = true;
synth.stop();
}
}
}
/**
* Resumes the synthesizer if it was paused.
*/
public void resumeSpeaking() {
synchronized (lock) {
if (lastUtterance != null && paused) {
int mode = TextToSpeech.QUEUE_FLUSH;
Set<Voice> voices = synth.getVoices();
for (Voice v : voices) {
if (v.getName().equals(lastUtterance.voice)) {
synth.setVoice(v);
break;
}
}
synth.setPitch(lastUtterance.pitch);
synth.setSpeechRate(lastUtterance.rate);
Bundle params = new Bundle();
params.putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME, lastUtterance.volume / 100.f);
lastUtterance.start = lastUtterance.offset;
lastUtterance.offset = 0;
paused = false;
synth.speak(lastUtterance.text.substring(lastUtterance.start), mode, params, String.valueOf(lastUtterance.id));
speaking = true;
} else {
paused = false;
}
}
}
/**
* Stops synthesis in progress and removes all utterances from the queue.
*/
public void stopSpeaking() {
synchronized (lock) {
for (GodotUtterance u : queue) {
GodotLib.ttsCallback(EVENT_CANCEL, u.id, 0);
}
queue.clear();
if (lastUtterance != null) {
GodotLib.ttsCallback(EVENT_CANCEL, lastUtterance.id, 0);
}
lastUtterance = null;
paused = false;
speaking = false;
synth.stop();
}
}
/**
* Returns voice information.
*/
public String[] getVoices() {
Set<Voice> voices = synth.getVoices();
String[] list = new String[voices.size()];
int i = 0;
for (Voice v : voices) {
list[i++] = v.getLocale().toString() + ";" + v.getName();
}
return list;
}
/**
* Returns true if the synthesizer is generating speech, or have utterance waiting in the queue.
*/
public boolean isSpeaking() {
return speaking;
}
/**
* Returns true if the synthesizer is in a paused state.
*/
public boolean isPaused() {
return paused;
}
}
|