eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpeechSynthesis.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (C) 2012-2015 Reece H. Dunn
  3. * Copyright (C) 2011 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * This file implements the Java API to eSpeak using the JNI bindings.
  19. *
  20. * Android Version: 4.0 (Ice Cream Sandwich)
  21. * API Version: 14
  22. */
  23. package com.reecedunn.espeak;
  24. import android.content.Context;
  25. import android.content.res.Configuration;
  26. import android.content.res.Resources;
  27. import android.util.DisplayMetrics;
  28. import android.util.Log;
  29. import java.io.File;
  30. import java.util.HashMap;
  31. import java.util.LinkedList;
  32. import java.util.List;
  33. import java.util.Locale;
  34. import java.util.Map;
  35. import java.util.MissingResourceException;
  36. public class SpeechSynthesis {
  37. private static final String TAG = SpeechSynthesis.class.getSimpleName();
  38. public static final int GENDER_UNSPECIFIED = 0;
  39. public static final int GENDER_MALE = 1;
  40. public static final int GENDER_FEMALE = 2;
  41. public static final int AGE_ANY = 0;
  42. public static final int AGE_YOUNG = 12;
  43. public static final int AGE_OLD = 60;
  44. public static final int CHANNEL_COUNT_MONO = 1;
  45. public static final int FORMAT_PCM_S16 = 2;
  46. static {
  47. System.loadLibrary("ttsespeak");
  48. nativeClassInit();
  49. }
  50. private final Context mContext;
  51. private final SynthReadyCallback mCallback;
  52. private final String mDatapath;
  53. private boolean mInitialized = false;
  54. private static int mVoiceCount = 0;
  55. private int mSampleRate = 0;
  56. public SpeechSynthesis(Context context, SynthReadyCallback callback) {
  57. // First, ensure the data directory exists, otherwise init will crash.
  58. final File dataPath = CheckVoiceData.getDataPath(context);
  59. if (!dataPath.exists()) {
  60. Log.e(TAG, "Missing voice data");
  61. dataPath.mkdirs();
  62. }
  63. mContext = context;
  64. mCallback = callback;
  65. mDatapath = dataPath.getParentFile().getPath();
  66. attemptInit();
  67. }
  68. public static String getVersion() {
  69. return nativeGetVersion();
  70. }
  71. public static int getVoiceCount() {
  72. return mVoiceCount;
  73. }
  74. public int getSampleRate() {
  75. return mSampleRate;
  76. }
  77. public int getChannelCount() {
  78. return CHANNEL_COUNT_MONO;
  79. }
  80. public int getAudioFormat() {
  81. return FORMAT_PCM_S16;
  82. }
  83. private Locale getLocaleFromLanguageName(String name) {
  84. if (mLocaleFixes.containsKey(name)) {
  85. return mLocaleFixes.get(name);
  86. }
  87. String[] parts = name.split("-");
  88. switch (parts.length) {
  89. case 1: // language
  90. return new Locale(parts[0]);
  91. case 2: // language-country
  92. return new Locale(parts[0], parts[1]);
  93. case 3: // language-country-variant
  94. return new Locale(parts[0], parts[1], parts[2]);
  95. default:
  96. return null;
  97. }
  98. }
  99. public List<Voice> getAvailableVoices() {
  100. final List<Voice> voices = new LinkedList<Voice>();
  101. final String[] results = nativeGetAvailableVoices();
  102. mVoiceCount = results.length / 4;
  103. for (int i = 0; i < results.length; i += 4) {
  104. final String name = results[i];
  105. final String identifier = results[i + 1];
  106. final int gender = Integer.parseInt(results[i + 2]);
  107. final int age = Integer.parseInt(results[i + 3]);
  108. try {
  109. final Locale locale;
  110. if (identifier.equals("asia/fa-en-us")) {
  111. throw new IllegalArgumentException("Voice '" + identifier + "' is a duplicate voice.");
  112. } else {
  113. locale = getLocaleFromLanguageName(name);
  114. if (locale == null) {
  115. throw new IllegalArgumentException("Locale not supported.");
  116. }
  117. }
  118. String language = locale.getISO3Language();
  119. if (language.equals("")) {
  120. throw new IllegalArgumentException("Language '" + locale.getLanguage() + "' not supported.");
  121. }
  122. String country = locale.getISO3Country();
  123. if (country.equals("") && !locale.getCountry().equals("")) {
  124. throw new IllegalArgumentException("Country '" + locale.getCountry() + "' not supported.");
  125. }
  126. final Voice voice = new Voice(name, identifier, gender, age, locale);
  127. voices.add(voice);
  128. } catch (MissingResourceException e) {
  129. // Android 4.3 throws this exception if the 3-letter language
  130. // (e.g. nci) or country (e.g. 021) code is missing for a locale.
  131. // Earlier versions return an empty string (handled above).
  132. Log.d(TAG, "getAvailableResources: skipping " + name + " => " + e.getMessage());
  133. } catch (IllegalArgumentException e) {
  134. Log.d(TAG, "getAvailableResources: skipping " + name + " => " + e.getMessage());
  135. }
  136. }
  137. return voices;
  138. }
  139. public void setVoice(Voice voice, VoiceVariant variant) {
  140. // NOTE: espeak_SetVoiceByProperties does not support specifying the
  141. // voice variant (e.g. klatt), but espeak_SetVoiceByName does.
  142. if (variant.variant == null) {
  143. nativeSetVoiceByProperties(voice.name, variant.gender, variant.age);
  144. } else {
  145. nativeSetVoiceByName(voice.identifier + "+" + variant.variant);
  146. }
  147. }
  148. public void setPunctuationCharacters(String characters) {
  149. nativeSetPunctuationCharacters(characters);
  150. }
  151. /** Don't announce any punctuation characters. */
  152. public static final int PUNCT_NONE = 0;
  153. /** Announce every punctuation character. */
  154. public static final int PUNCT_ALL = 1;
  155. /** Announce some of the punctuation characters. */
  156. public static final int PUNCT_SOME = 2;
  157. public enum UnitType {
  158. Percentage,
  159. WordsPerMinute,
  160. /** One of the PUNCT_* constants. */
  161. Punctuation,
  162. }
  163. public class Parameter {
  164. private final int id;
  165. private final int min;
  166. private final int max;
  167. private final UnitType unitType;
  168. private Parameter(int id, int min, int max, UnitType unitType) {
  169. this.id = id;
  170. this.min = min;
  171. this.max = max;
  172. this.unitType = unitType;
  173. }
  174. public int getMinValue() {
  175. return min;
  176. }
  177. public int getMaxValue() {
  178. return max;
  179. }
  180. public int getDefaultValue() {
  181. return nativeGetParameter(id, 0);
  182. }
  183. public int getValue() {
  184. return nativeGetParameter(id, 1);
  185. }
  186. public void setValue(int value, int scale) {
  187. setValue((value * scale) / 100);
  188. }
  189. public void setValue(int value) {
  190. nativeSetParameter(id, value);
  191. }
  192. public UnitType getUnitType() {
  193. return unitType;
  194. }
  195. }
  196. /** Speech rate. */
  197. public final Parameter Rate = new Parameter(1, 80, 450, UnitType.WordsPerMinute);
  198. /** Audio volume. */
  199. public final Parameter Volume = new Parameter(2, 0, 200, UnitType.Percentage);
  200. /** Base pitch. */
  201. public final Parameter Pitch = new Parameter(3, 0, 100, UnitType.Percentage);
  202. /** Pitch range (monotone = 0). */
  203. public final Parameter PitchRange = new Parameter(4, 0, 100, UnitType.Percentage);
  204. /** Which punctuation characters to announce. */
  205. public final Parameter Punctuation = new Parameter(5, 0, 2, UnitType.Punctuation);
  206. public void synthesize(String text, boolean isSsml) {
  207. nativeSynthesize(text, isSsml);
  208. }
  209. public void stop() {
  210. nativeStop();
  211. }
  212. private void nativeSynthCallback(byte[] audioData) {
  213. if (mCallback == null)
  214. return;
  215. if (audioData == null) {
  216. mCallback.onSynthDataComplete();
  217. } else {
  218. mCallback.onSynthDataReady(audioData);
  219. }
  220. }
  221. private void attemptInit() {
  222. if (mInitialized) {
  223. return;
  224. }
  225. if (!CheckVoiceData.hasBaseResources(mContext)) {
  226. Log.e(TAG, "Missing base resources");
  227. return;
  228. }
  229. mSampleRate = nativeCreate(mDatapath);
  230. if (mSampleRate == 0) {
  231. Log.e(TAG, "Failed to initialize speech synthesis library");
  232. return;
  233. }
  234. Log.i(TAG, "Initialized synthesis library with sample rate = " + getSampleRate());
  235. mInitialized = true;
  236. }
  237. public static String getSampleText(Context context, Locale locale) {
  238. final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
  239. final Configuration config = context.getResources().getConfiguration();
  240. final String language = getIanaLanguageCode(locale.getLanguage());
  241. final String country = getIanaCountryCode(locale.getCountry());
  242. config.locale = new Locale(language, country, locale.getVariant());
  243. Resources res = new Resources(context.getAssets(), metrics, config);
  244. return res.getString(R.string.sample_text, config.locale.getDisplayName(config.locale));
  245. }
  246. private static native final boolean nativeClassInit();
  247. private native final int nativeCreate(String path);
  248. private native final static String nativeGetVersion();
  249. private native final String[] nativeGetAvailableVoices();
  250. private native final boolean nativeSetVoiceByName(String name);
  251. private native final boolean nativeSetVoiceByProperties(String language, int gender, int age);
  252. private native final boolean nativeSetParameter(int parameter, int value);
  253. private native final int nativeGetParameter(int parameter, int current);
  254. private native final boolean nativeSetPunctuationCharacters(String characters);
  255. private native final boolean nativeSynthesize(String text, boolean isSsml);
  256. private native final boolean nativeStop();
  257. public interface SynthReadyCallback {
  258. void onSynthDataReady(byte[] audioData);
  259. void onSynthDataComplete();
  260. }
  261. public static String getIanaLanguageCode(String code) {
  262. return getIanaLocaleCode(code, mJavaToIanaLanguageCode);
  263. }
  264. public static String getIanaCountryCode(String code) {
  265. return getIanaLocaleCode(code, mJavaToIanaCountryCode);
  266. }
  267. private static String getIanaLocaleCode(String code, final Map<String, String> javaToIana) {
  268. final String iana = javaToIana.get(code);
  269. if (iana != null) {
  270. return iana;
  271. }
  272. return code;
  273. }
  274. private static final Map<String, String> mJavaToIanaLanguageCode = new HashMap<String, String>();
  275. private static final Map<String, String> mJavaToIanaCountryCode = new HashMap<String, String>();
  276. private static final HashMap<String, Locale> mLocaleFixes = new HashMap<String, Locale>();
  277. static {
  278. mJavaToIanaLanguageCode.put("afr", "af");
  279. mJavaToIanaLanguageCode.put("amh", "am");
  280. mJavaToIanaLanguageCode.put("arg", "an");
  281. mJavaToIanaLanguageCode.put("asm", "as");
  282. mJavaToIanaLanguageCode.put("aze", "az");
  283. mJavaToIanaLanguageCode.put("bul", "bg");
  284. mJavaToIanaLanguageCode.put("ben", "bn");
  285. mJavaToIanaLanguageCode.put("bos", "bs");
  286. mJavaToIanaLanguageCode.put("cat", "ca");
  287. mJavaToIanaLanguageCode.put("ces", "cs");
  288. mJavaToIanaLanguageCode.put("cym", "cy");
  289. mJavaToIanaLanguageCode.put("dan", "da");
  290. mJavaToIanaLanguageCode.put("deu", "de");
  291. mJavaToIanaLanguageCode.put("ell", "el");
  292. mJavaToIanaLanguageCode.put("eng", "en");
  293. mJavaToIanaLanguageCode.put("epo", "eo");
  294. mJavaToIanaLanguageCode.put("spa", "es");
  295. mJavaToIanaLanguageCode.put("est", "et");
  296. mJavaToIanaLanguageCode.put("eus", "eu");
  297. mJavaToIanaLanguageCode.put("fas", "fa");
  298. mJavaToIanaLanguageCode.put("fin", "fi");
  299. mJavaToIanaLanguageCode.put("fra", "fr");
  300. mJavaToIanaLanguageCode.put("gle", "ga");
  301. mJavaToIanaLanguageCode.put("gla", "gd");
  302. mJavaToIanaLanguageCode.put("guj", "gu");
  303. mJavaToIanaLanguageCode.put("hin", "hi");
  304. mJavaToIanaLanguageCode.put("hrv", "hr");
  305. mJavaToIanaLanguageCode.put("hun", "hu");
  306. mJavaToIanaLanguageCode.put("hye", "hy");
  307. mJavaToIanaLanguageCode.put("ina", "ia");
  308. mJavaToIanaLanguageCode.put("ind", "in"); // NOTE: The deprecated 'in' code is used by Java/Android.
  309. mJavaToIanaLanguageCode.put("isl", "is");
  310. mJavaToIanaLanguageCode.put("ita", "it");
  311. mJavaToIanaLanguageCode.put("kat", "ka");
  312. mJavaToIanaLanguageCode.put("kal", "kl");
  313. mJavaToIanaLanguageCode.put("kan", "kn");
  314. mJavaToIanaLanguageCode.put("kor", "ko");
  315. mJavaToIanaLanguageCode.put("kur", "ku");
  316. mJavaToIanaLanguageCode.put("lat", "la");
  317. mJavaToIanaLanguageCode.put("lit", "lt");
  318. mJavaToIanaLanguageCode.put("lav", "lv");
  319. mJavaToIanaLanguageCode.put("mkd", "mk");
  320. mJavaToIanaLanguageCode.put("mal", "ml");
  321. mJavaToIanaLanguageCode.put("mar", "mr");
  322. mJavaToIanaLanguageCode.put("msa", "ms");
  323. mJavaToIanaLanguageCode.put("nep", "ne");
  324. mJavaToIanaLanguageCode.put("nld", "nl");
  325. mJavaToIanaLanguageCode.put("nob", "nb");
  326. mJavaToIanaLanguageCode.put("nor", "no");
  327. mJavaToIanaLanguageCode.put("ori", "or");
  328. mJavaToIanaLanguageCode.put("orm", "om");
  329. mJavaToIanaLanguageCode.put("pan", "pa");
  330. mJavaToIanaLanguageCode.put("pol", "pl");
  331. mJavaToIanaLanguageCode.put("por", "pt");
  332. mJavaToIanaLanguageCode.put("ron", "ro");
  333. mJavaToIanaLanguageCode.put("rus", "ru");
  334. mJavaToIanaLanguageCode.put("sin", "si");
  335. mJavaToIanaLanguageCode.put("slk", "sk");
  336. mJavaToIanaLanguageCode.put("slv", "sl");
  337. mJavaToIanaLanguageCode.put("sqi", "sq");
  338. mJavaToIanaLanguageCode.put("srp", "sr");
  339. mJavaToIanaLanguageCode.put("swe", "sv");
  340. mJavaToIanaLanguageCode.put("swa", "sw");
  341. mJavaToIanaLanguageCode.put("tam", "ta");
  342. mJavaToIanaLanguageCode.put("tel", "te");
  343. mJavaToIanaLanguageCode.put("tur", "tr");
  344. mJavaToIanaLanguageCode.put("urd", "ur");
  345. mJavaToIanaLanguageCode.put("vie", "vi");
  346. mJavaToIanaLanguageCode.put("zho", "zh");
  347. mJavaToIanaCountryCode.put("ARM", "AM");
  348. mJavaToIanaCountryCode.put("BEL", "BE");
  349. mJavaToIanaCountryCode.put("BRA", "BR");
  350. mJavaToIanaCountryCode.put("FRA", "FR");
  351. mJavaToIanaCountryCode.put("GBR", "GB");
  352. mJavaToIanaCountryCode.put("HKG", "HK");
  353. mJavaToIanaCountryCode.put("JAM", "JM");
  354. mJavaToIanaCountryCode.put("MEX", "MX");
  355. mJavaToIanaCountryCode.put("PRT", "PT");
  356. mJavaToIanaCountryCode.put("USA", "US");
  357. mJavaToIanaCountryCode.put("VNM", "VN");
  358. // Map eSpeak locales to their correct BCP47 locales supported by Android:
  359. mLocaleFixes.put("en-sc", new Locale("en", "GB", "scotland"));
  360. mLocaleFixes.put("en-uk-north", new Locale("en", "GB", "north"));
  361. mLocaleFixes.put("en-uk-rp", new Locale("en", "GB", "rp"));
  362. mLocaleFixes.put("en-uk-wmids", new Locale("en", "GB", "wmids"));
  363. mLocaleFixes.put("en-wi", new Locale("en", "JM"));
  364. mLocaleFixes.put("es-la", new Locale("es", "MX"));
  365. mLocaleFixes.put("fa-pin", null); // Script tags not supported.
  366. mLocaleFixes.put("hy-west", new Locale("hy", "AM", "arevmda")); // hy-arevmda crashes on Android 5.0
  367. mLocaleFixes.put("no", new Locale("nb")); // 'no' is valid, but the system uses the more specific 'nb' and 'nn'
  368. mLocaleFixes.put("vi-hue", new Locale("vi", "VN", "hue"));
  369. mLocaleFixes.put("vi-sgn", new Locale("vi", "VN", "saigon"));
  370. mLocaleFixes.put("zh-yue", new Locale("zh", "HK"));
  371. }
  372. }