| 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 |
package com.reecedunn.espeak; |
| 27 |
|
| 28 |
import android.annotation.SuppressLint; |
| 29 |
import android.content.BroadcastReceiver; |
| 30 |
import android.content.Context; |
| 31 |
import android.content.Intent; |
| 32 |
import android.content.IntentFilter; |
| 33 |
import android.media.AudioTrack; |
| 34 |
import android.os.Build; |
| 35 |
import android.os.Bundle; |
| 36 |
import android.preference.PreferenceManager; |
| 37 |
import android.speech.tts.SynthesisCallback; |
| 38 |
import android.speech.tts.SynthesisRequest; |
| 39 |
import android.speech.tts.TextToSpeech; |
| 40 |
import android.speech.tts.TextToSpeechService; |
| 41 |
import android.util.Log; |
| 42 |
import android.util.Pair; |
| 43 |
|
| 44 |
import com.reecedunn.espeak.SpeechSynthesis.SynthReadyCallback; |
| 45 |
|
| 46 |
import java.util.ArrayList; |
| 47 |
import java.util.HashMap; |
| 48 |
import java.util.HashSet; |
| 49 |
import java.util.List; |
| 50 |
import java.util.Locale; |
| 51 |
import java.util.Map; |
| 52 |
import java.util.Set; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
@SuppressLint("NewApi") |
| 61 |
public class TtsService extends TextToSpeechService { |
| 62 |
public static final String ESPEAK_INITIALIZED = "com.reecedunn.espeak.ESPEAK_INITIALIZED"; |
| 63 |
|
| 64 |
private static final String TAG = TtsService.class.getSimpleName(); |
| 65 |
private static Context storageContext; |
| 66 |
private static final boolean DEBUG = BuildConfig.DEBUG; |
| 67 |
|
| 68 |
private SpeechSynthesis mEngine; |
| 69 |
private SynthesisCallback mCallback; |
| 70 |
|
| 71 |
private final Map<String, Voice> mAvailableVoices = new HashMap<String, Voice>(); |
| 72 |
protected Voice mMatchingVoice = null; |
| 73 |
|
| 74 |
private BroadcastReceiver mOnLanguagesDownloaded = null; |
| 75 |
|
| 76 |
@Override |
| 77 |
public void onCreate() { |
| 78 |
storageContext = EspeakApp.getStorageContext(); |
| 79 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) |
| 80 |
storageContext.moveSharedPreferencesFrom(this, this.getPackageName() + "_preferences"); |
| 81 |
initializeTtsEngine(); |
| 82 |
super.onCreate(); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void onDestroy() { |
| 87 |
super.onDestroy(); |
| 88 |
if (mOnLanguagesDownloaded != null) { |
| 89 |
unregisterReceiver(mOnLanguagesDownloaded); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private void initializeTtsEngine() { |
| 97 |
if (mEngine != null) { |
| 98 |
mEngine.stop(); |
| 99 |
mEngine = null; |
| 100 |
} |
| 101 |
|
| 102 |
mEngine = new SpeechSynthesis(storageContext, mSynthCallback); |
| 103 |
mAvailableVoices.clear(); |
| 104 |
for (Voice voice : mEngine.getAvailableVoices()) { |
| 105 |
mAvailableVoices.put(voice.name, voice); |
| 106 |
} |
| 107 |
|
| 108 |
final Intent intent = new Intent(ESPEAK_INITIALIZED); |
| 109 |
sendBroadcast(intent); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
protected String[] onGetLanguage() { |
| 114 |
|
| 115 |
if (mMatchingVoice == null) { |
| 116 |
return new String[] { "eng", "GBR", "" }; |
| 117 |
} |
| 118 |
return new String[] { |
| 119 |
mMatchingVoice.locale.getISO3Language(), |
| 120 |
mMatchingVoice.locale.getISO3Country(), |
| 121 |
mMatchingVoice.locale.getVariant() |
| 122 |
}; |
| 123 |
} |
| 124 |
|
| 125 |
private Pair<Voice, Integer> findVoice(String language, String country, String variant) { |
| 126 |
if (!CheckVoiceData.hasBaseResources(storageContext) || CheckVoiceData.canUpgradeResources(storageContext)) { |
| 127 |
if (mOnLanguagesDownloaded == null) { |
| 128 |
mOnLanguagesDownloaded = new BroadcastReceiver() { |
| 129 |
@Override |
| 130 |
public void onReceive(Context context, Intent intent) { |
| 131 |
initializeTtsEngine(); |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
final IntentFilter filter = new IntentFilter(DownloadVoiceData.BROADCAST_LANGUAGES_UPDATED); |
| 136 |
registerReceiver(mOnLanguagesDownloaded, filter); |
| 137 |
} |
| 138 |
|
| 139 |
final Intent intent = new Intent(storageContext, DownloadVoiceData.class); |
| 140 |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 141 |
startActivity(intent); |
| 142 |
|
| 143 |
return new Pair<>(null, TextToSpeech.LANG_MISSING_DATA); |
| 144 |
} |
| 145 |
|
| 146 |
final Locale query = new Locale(language, country, variant); |
| 147 |
|
| 148 |
Voice languageVoice = null; |
| 149 |
Voice countryVoice = null; |
| 150 |
|
| 151 |
synchronized (mAvailableVoices) { |
| 152 |
for (Voice voice : mAvailableVoices.values()) { |
| 153 |
switch (voice.match(query)) { |
| 154 |
case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE: |
| 155 |
return new Pair<>(voice, TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE); |
| 156 |
case TextToSpeech.LANG_COUNTRY_AVAILABLE: |
| 157 |
countryVoice = voice; |
| 158 |
case TextToSpeech.LANG_AVAILABLE: |
| 159 |
languageVoice = voice; |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (languageVoice == null) { |
| 166 |
return new Pair<>(null, TextToSpeech.LANG_NOT_SUPPORTED); |
| 167 |
} else if (countryVoice == null) { |
| 168 |
return new Pair<>(languageVoice, TextToSpeech.LANG_AVAILABLE); |
| 169 |
} else { |
| 170 |
return new Pair<>(countryVoice, TextToSpeech.LANG_COUNTRY_AVAILABLE); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
private Pair<Voice, Integer> getDefaultVoiceFor(String language, String country, String variant) { |
| 175 |
final Pair<Voice, Integer> match = findVoice(language, country, variant); |
| 176 |
switch (match.second) { |
| 177 |
case TextToSpeech.LANG_AVAILABLE: |
| 178 |
if (language.equals("fr") || language.equals("fra")) { |
| 179 |
return new Pair<>(findVoice(language, "FRA", "").first, match.second); |
| 180 |
} |
| 181 |
if (language.equals("pt") || language.equals("por")) { |
| 182 |
return new Pair<>(findVoice(language, "PRT", "").first, match.second); |
| 183 |
} |
| 184 |
return new Pair<>(findVoice(language, "", "").first, match.second); |
| 185 |
case TextToSpeech.LANG_COUNTRY_AVAILABLE: |
| 186 |
if ((language.equals("vi") || language.equals("vie")) && (country.equals("VN") || country.equals("VNM"))) { |
| 187 |
return new Pair<>(findVoice(language, country, "hue").first, match.second); |
| 188 |
} |
| 189 |
return new Pair<>(findVoice(language, country, "").first, match.second); |
| 190 |
default: |
| 191 |
return match; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
protected int onIsLanguageAvailable(String language, String country, String variant) { |
| 197 |
return findVoice(language, country, variant).second; |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
protected int onLoadLanguage(String language, String country, String variant) { |
| 202 |
final Pair<Voice, Integer> match = getDefaultVoiceFor(language, country, variant); |
| 203 |
if (match.first != null) { |
| 204 |
mMatchingVoice = match.first; |
| 205 |
} |
| 206 |
return match.second; |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
protected Set<String> onGetFeaturesForLanguage(String lang, String country, String variant) { |
| 211 |
return new HashSet<String>(); |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public String onGetDefaultVoiceNameFor(String language, String country, String variant) { |
| 216 |
final Voice match = getDefaultVoiceFor(language, country, variant).first; |
| 217 |
return (match == null) ? null : match.name; |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public List<android.speech.tts.Voice> onGetVoices() { |
| 222 |
List<android.speech.tts.Voice> voices = new ArrayList<android.speech.tts.Voice>(); |
| 223 |
for (Voice voice : mAvailableVoices.values()) { |
| 224 |
int quality = android.speech.tts.Voice.QUALITY_NORMAL; |
| 225 |
int latency = android.speech.tts.Voice.LATENCY_VERY_LOW; |
| 226 |
Locale locale = new Locale(voice.locale.getISO3Language(), voice.locale.getISO3Country(), voice.locale.getVariant()); |
| 227 |
Set<String> features = onGetFeaturesForLanguage(locale.getLanguage(), locale.getCountry(), locale.getVariant()); |
| 228 |
voices.add(new android.speech.tts.Voice(voice.name, voice.locale, quality, latency, false, features)); |
| 229 |
} |
| 230 |
return voices; |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
public int onIsValidVoiceName(String name) { |
| 235 |
Voice voice = mAvailableVoices.get(name); |
| 236 |
return (voice == null) ? TextToSpeech.ERROR : TextToSpeech.SUCCESS; |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public int onLoadVoice(String name) { |
| 241 |
Voice voice = mAvailableVoices.get(name); |
| 242 |
if (voice == null) { |
| 243 |
return TextToSpeech.ERROR; |
| 244 |
} |
| 245 |
mMatchingVoice = voice; |
| 246 |
return TextToSpeech.SUCCESS; |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
protected void onStop() { |
| 251 |
Log.i(TAG, "Received stop request."); |
| 252 |
|
| 253 |
mEngine.stop(); |
| 254 |
} |
| 255 |
|
| 256 |
@SuppressWarnings("deprecation") |
| 257 |
private String getRequestString(SynthesisRequest request) { |
| 258 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 259 |
return request.getCharSequenceText().toString(); |
| 260 |
} else { |
| 261 |
return request.getText(); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
private int selectVoice(SynthesisRequest request) { |
| 266 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 267 |
final String name = request.getVoiceName(); |
| 268 |
if (name != null && !name.isEmpty()) { |
| 269 |
return onLoadVoice(name); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
final int result = onLoadLanguage(request.getLanguage(), request.getCountry(), request.getVariant()); |
| 274 |
switch (result) { |
| 275 |
case TextToSpeech.LANG_MISSING_DATA: |
| 276 |
case TextToSpeech.LANG_NOT_SUPPORTED: |
| 277 |
return TextToSpeech.ERROR; |
| 278 |
} |
| 279 |
return TextToSpeech.SUCCESS; |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
protected synchronized void onSynthesizeText(SynthesisRequest request, SynthesisCallback callback) { |
| 284 |
if (mMatchingVoice == null) |
| 285 |
return; |
| 286 |
|
| 287 |
String text = getRequestString(request); |
| 288 |
if (text == null) |
| 289 |
return; |
| 290 |
|
| 291 |
if (DEBUG) { |
| 292 |
Log.i(TAG, "Received synthesis request: {language=\"" + mMatchingVoice.name + "\"}"); |
| 293 |
|
| 294 |
final Bundle params = request.getParams(); |
| 295 |
for (String key : params.keySet()) { |
| 296 |
Log.v(TAG, |
| 297 |
"Synthesis request contained param {" + key + ", " + params.get(key) + "}"); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
if (text.startsWith("<?xml")) |
| 302 |
{ |
| 303 |
|
| 304 |
|
| 305 |
text = text.substring(text.indexOf("?>") + 2).trim(); |
| 306 |
} |
| 307 |
|
| 308 |
mCallback = callback; |
| 309 |
mCallback.start(mEngine.getSampleRate(), mEngine.getAudioFormat(), mEngine.getChannelCount()); |
| 310 |
|
| 311 |
final VoiceSettings settings = new VoiceSettings(PreferenceManager.getDefaultSharedPreferences(storageContext), mEngine); |
| 312 |
mEngine.setVoice(mMatchingVoice, settings.getVoiceVariant()); |
| 313 |
mEngine.Rate.setValue(settings.getRate(), request.getSpeechRate()); |
| 314 |
mEngine.Pitch.setValue(settings.getPitch(), request.getPitch()); |
| 315 |
mEngine.PitchRange.setValue(settings.getPitchRange()); |
| 316 |
mEngine.Volume.setValue(settings.getVolume()); |
| 317 |
mEngine.Punctuation.setValue(settings.getPunctuationLevel()); |
| 318 |
mEngine.setPunctuationCharacters(settings.getPunctuationCharacters()); |
| 319 |
mEngine.synthesize(text, text.startsWith("<speak")); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
private final SpeechSynthesis.SynthReadyCallback mSynthCallback = new SynthReadyCallback() { |
| 326 |
@Override |
| 327 |
public void onSynthDataReady(byte[] audioData) { |
| 328 |
if ((audioData == null) || (audioData.length == 0)) { |
| 329 |
onSynthDataComplete(); |
| 330 |
return; |
| 331 |
} |
| 332 |
|
| 333 |
final int maxBytesToCopy = mCallback.getMaxBufferSize(); |
| 334 |
|
| 335 |
int offset = 0; |
| 336 |
|
| 337 |
while (offset < audioData.length) { |
| 338 |
final int bytesToWrite = Math.min(maxBytesToCopy, (audioData.length - offset)); |
| 339 |
mCallback.audioAvailable(audioData, offset, bytesToWrite); |
| 340 |
offset += bytesToWrite; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
@Override |
| 345 |
public void onSynthDataComplete() { |
| 346 |
mCallback.done(); |
| 347 |
} |
| 348 |
}; |
| 349 |
} |