/
TtsService.java
349 lines (300 loc) · 12.6 KB
/
TtsService.java
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
/*
* Copyright (C) 2022 Beka Gozalishvili
* Copyright (C) 2012-2015 Reece H. Dunn
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This file implements the Android Text-to-Speech engine for eSpeak.
*
* Android Version: 4.0 (Ice Cream Sandwich)
* API Version: 14
*/
package com.reecedunn.espeak;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioTrack;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.speech.tts.SynthesisCallback;
import android.speech.tts.SynthesisRequest;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeechService;
import android.util.Log;
import android.util.Pair;
import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
/**
* Implements the eSpeak engine as a {@link TextToSpeechService}.
*
* @author msclrhd@gmail.com (Reece H. Dunn)
* @author alanv@google.com (Alan Viverette)
*/
@SuppressLint("NewApi")
public class TtsService extends TextToSpeechService {
public static final String ESPEAK_INITIALIZED = "com.reecedunn.espeak.ESPEAK_INITIALIZED";
private static final String TAG = TtsService.class.getSimpleName();
private static Context storageContext;
private static final boolean DEBUG = BuildConfig.DEBUG;
private SpeechSynthesis mEngine;
private SynthesisCallback mCallback;
private final Map<String, Voice> mAvailableVoices = new HashMap<String, Voice>();
protected Voice mMatchingVoice = null;
private BroadcastReceiver mOnLanguagesDownloaded = null;
@Override
public void onCreate() {
storageContext = EspeakApp.getStorageContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
storageContext.moveSharedPreferencesFrom(this, this.getPackageName() + "_preferences");
initializeTtsEngine();
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
if (mOnLanguagesDownloaded != null) {
unregisterReceiver(mOnLanguagesDownloaded);
}
}
/**
* Sets up the native eSpeak engine.
*/
private void initializeTtsEngine() {
if (mEngine != null) {
mEngine.stop();
mEngine = null;
}
mEngine = new SpeechSynthesis(storageContext, mSynthCallback);
mAvailableVoices.clear();
for (Voice voice : mEngine.getAvailableVoices()) {
mAvailableVoices.put(voice.name, voice);
}
final Intent intent = new Intent(ESPEAK_INITIALIZED);
sendBroadcast(intent);
}
@Override
protected String[] onGetLanguage() {
// This is used to specify the language requested from GetSampleText.
if (mMatchingVoice == null) {
return new String[] { "eng", "GBR", "" };
}
return new String[] {
mMatchingVoice.locale.getISO3Language(),
mMatchingVoice.locale.getISO3Country(),
mMatchingVoice.locale.getVariant()
};
}
private Pair<Voice, Integer> findVoice(String language, String country, String variant) {
if (!CheckVoiceData.hasBaseResources(storageContext) || CheckVoiceData.canUpgradeResources(storageContext)) {
if (mOnLanguagesDownloaded == null) {
mOnLanguagesDownloaded = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
initializeTtsEngine();
}
};
final IntentFilter filter = new IntentFilter(DownloadVoiceData.BROADCAST_LANGUAGES_UPDATED);
registerReceiver(mOnLanguagesDownloaded, filter);
}
final Intent intent = new Intent(storageContext, DownloadVoiceData.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return new Pair<>(null, TextToSpeech.LANG_MISSING_DATA);
}
final Locale query = new Locale(language, country, variant);
Voice languageVoice = null;
Voice countryVoice = null;
synchronized (mAvailableVoices) {
for (Voice voice : mAvailableVoices.values()) {
switch (voice.match(query)) {
case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
return new Pair<>(voice, TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE);
case TextToSpeech.LANG_COUNTRY_AVAILABLE:
countryVoice = voice;
case TextToSpeech.LANG_AVAILABLE:
languageVoice = voice;
break;
}
}
}
if (languageVoice == null) {
return new Pair<>(null, TextToSpeech.LANG_NOT_SUPPORTED);
} else if (countryVoice == null) {
return new Pair<>(languageVoice, TextToSpeech.LANG_AVAILABLE);
} else {
return new Pair<>(countryVoice, TextToSpeech.LANG_COUNTRY_AVAILABLE);
}
}
private Pair<Voice, Integer> getDefaultVoiceFor(String language, String country, String variant) {
final Pair<Voice, Integer> match = findVoice(language, country, variant);
switch (match.second) {
case TextToSpeech.LANG_AVAILABLE:
if (language.equals("fr") || language.equals("fra")) {
return new Pair<>(findVoice(language, "FRA", "").first, match.second);
}
if (language.equals("pt") || language.equals("por")) {
return new Pair<>(findVoice(language, "PRT", "").first, match.second);
}
return new Pair<>(findVoice(language, "", "").first, match.second);
case TextToSpeech.LANG_COUNTRY_AVAILABLE:
if ((language.equals("vi") || language.equals("vie")) && (country.equals("VN") || country.equals("VNM"))) {
return new Pair<>(findVoice(language, country, "hue").first, match.second);
}
return new Pair<>(findVoice(language, country, "").first, match.second);
default:
return match;
}
}
@Override
protected int onIsLanguageAvailable(String language, String country, String variant) {
return findVoice(language, country, variant).second;
}
@Override
protected int onLoadLanguage(String language, String country, String variant) {
final Pair<Voice, Integer> match = getDefaultVoiceFor(language, country, variant);
if (match.first != null) {
mMatchingVoice = match.first;
}
return match.second;
}
@Override
protected Set<String> onGetFeaturesForLanguage(String lang, String country, String variant) {
return new HashSet<String>();
}
@Override
public String onGetDefaultVoiceNameFor(String language, String country, String variant) {
final Voice match = getDefaultVoiceFor(language, country, variant).first;
return (match == null) ? null : match.name;
}
@Override
public List<android.speech.tts.Voice> onGetVoices() {
List<android.speech.tts.Voice> voices = new ArrayList<android.speech.tts.Voice>();
for (Voice voice : mAvailableVoices.values()) {
int quality = android.speech.tts.Voice.QUALITY_NORMAL;
int latency = android.speech.tts.Voice.LATENCY_VERY_LOW;
Locale locale = new Locale(voice.locale.getISO3Language(), voice.locale.getISO3Country(), voice.locale.getVariant());
Set<String> features = onGetFeaturesForLanguage(locale.getLanguage(), locale.getCountry(), locale.getVariant());
voices.add(new android.speech.tts.Voice(voice.name, voice.locale, quality, latency, false, features));
}
return voices;
}
@Override
public int onIsValidVoiceName(String name) {
Voice voice = mAvailableVoices.get(name);
return (voice == null) ? TextToSpeech.ERROR : TextToSpeech.SUCCESS;
}
@Override
public int onLoadVoice(String name) {
Voice voice = mAvailableVoices.get(name);
if (voice == null) {
return TextToSpeech.ERROR;
}
mMatchingVoice = voice;
return TextToSpeech.SUCCESS;
}
@Override
protected void onStop() {
Log.i(TAG, "Received stop request.");
mEngine.stop();
}
@SuppressWarnings("deprecation")
private String getRequestString(SynthesisRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return request.getCharSequenceText().toString();
} else {
return request.getText();
}
}
private int selectVoice(SynthesisRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final String name = request.getVoiceName();
if (name != null && !name.isEmpty()) {
return onLoadVoice(name);
}
}
final int result = onLoadLanguage(request.getLanguage(), request.getCountry(), request.getVariant());
switch (result) {
case TextToSpeech.LANG_MISSING_DATA:
case TextToSpeech.LANG_NOT_SUPPORTED:
return TextToSpeech.ERROR;
}
return TextToSpeech.SUCCESS;
}
@Override
protected synchronized void onSynthesizeText(SynthesisRequest request, SynthesisCallback callback) {
if (mMatchingVoice == null)
return;
String text = getRequestString(request);
if (text == null)
return;
if (DEBUG) {
Log.i(TAG, "Received synthesis request: {language=\"" + mMatchingVoice.name + "\"}");
final Bundle params = request.getParams();
for (String key : params.keySet()) {
Log.v(TAG,
"Synthesis request contained param {" + key + ", " + params.get(key) + "}");
}
}
if (text.startsWith("<?xml"))
{
// eSpeak does not recognise/skip "<?...?>" preprocessing tags,
// so need to remove these before passing to synthesize.
text = text.substring(text.indexOf("?>") + 2).trim();
}
mCallback = callback;
mCallback.start(mEngine.getSampleRate(), mEngine.getAudioFormat(), mEngine.getChannelCount());
final VoiceSettings settings = new VoiceSettings(PreferenceManager.getDefaultSharedPreferences(storageContext), mEngine);
mEngine.setVoice(mMatchingVoice, settings.getVoiceVariant());
mEngine.Rate.setValue(settings.getRate(), request.getSpeechRate());
mEngine.Pitch.setValue(settings.getPitch(), request.getPitch());
mEngine.PitchRange.setValue(settings.getPitchRange());
mEngine.Volume.setValue(settings.getVolume());
mEngine.Punctuation.setValue(settings.getPunctuationLevel());
mEngine.setPunctuationCharacters(settings.getPunctuationCharacters());
mEngine.synthesize(text, text.startsWith("<speak"));
}
/**
* Pipes synthesizer output from native eSpeak to an {@link AudioTrack}.
*/
private final SpeechSynthesis.SynthReadyCallback mSynthCallback = new SynthReadyCallback() {
@Override
public void onSynthDataReady(byte[] audioData) {
if ((audioData == null) || (audioData.length == 0)) {
onSynthDataComplete();
return;
}
final int maxBytesToCopy = mCallback.getMaxBufferSize();
int offset = 0;
while (offset < audioData.length) {
final int bytesToWrite = Math.min(maxBytesToCopy, (audioData.length - offset));
mCallback.audioAvailable(audioData, offset, bytesToWrite);
offset += bytesToWrite;
}
}
@Override
public void onSynthDataComplete() {
mCallback.done();
}
};
}