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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright (C) 2012-2013 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. static {
  45. System.loadLibrary("ttsespeak");
  46. nativeClassInit();
  47. }
  48. private final Context mContext;
  49. private final SynthReadyCallback mCallback;
  50. private final String mDatapath;
  51. private boolean mInitialized = false;
  52. public SpeechSynthesis(Context context, SynthReadyCallback callback) {
  53. // First, ensure the data directory exists, otherwise init will crash.
  54. final File dataPath = CheckVoiceData.getDataPath(context);
  55. if (!dataPath.exists()) {
  56. Log.e(TAG, "Missing voice data");
  57. dataPath.mkdirs();
  58. }
  59. mContext = context;
  60. mCallback = callback;
  61. mDatapath = dataPath.getParentFile().getPath();
  62. attemptInit();
  63. }
  64. @Override
  65. protected void finalize() {
  66. nativeDestroy();
  67. }
  68. public static String getVersion() {
  69. return nativeGetVersion();
  70. }
  71. public int getSampleRate() {
  72. return nativeGetSampleRate();
  73. }
  74. public int getChannelCount() {
  75. return nativeGetChannelCount();
  76. }
  77. public int getAudioFormat() {
  78. return nativeGetAudioFormat();
  79. }
  80. public int getBufferSizeInBytes() {
  81. final int bufferSizeInMillis = nativeGetBufferSizeInMillis();
  82. final int sampleRate = nativeGetSampleRate();
  83. return (bufferSizeInMillis * sampleRate) / 1000;
  84. }
  85. public List<Voice> getAvailableVoices() {
  86. final List<Voice> voices = new LinkedList<Voice>();
  87. final String[] results = nativeGetAvailableVoices();
  88. for (int i = 0; i < results.length; i += 4) {
  89. final String name = results[i];
  90. final String identifier = results[i + 1];
  91. final int gender = Integer.parseInt(results[i + 2]);
  92. final int age = Integer.parseInt(results[i + 3]);
  93. final Locale locale;
  94. if (name.equals("fa-pin")) {
  95. // Android locales do not support scripts, so fa-Latn is not possible for Farsi Pinglish:
  96. locale = null;
  97. } else if (name.equals("om")) {
  98. // This is an experimental voice that is not currently well tested to be used.
  99. locale = null;
  100. } else if (name.equals("en-sc")) {
  101. // 'SC' is not a country code.
  102. locale = new Locale("en", "GB", "scotland");
  103. } else if (name.equals("en-wi")) {
  104. // 'WI' is not a country code. Also, territory code 029 (Caribbean)
  105. // is not supported by Android.
  106. locale = new Locale("en", "JM");
  107. } else if (name.equals("es-la")) {
  108. // 'LA' is the country code for Laos, not Latin America. Also, territory code 419
  109. // (Latin America) is not supported by Android.
  110. locale = new Locale("es", "MX");
  111. } else if (name.equals("hy-west")) {
  112. // 'west' is not a country code.
  113. locale = new Locale("hy", "", "arevmda");
  114. } else if (name.equals("vi-hue")) {
  115. // 'hue' is for the Hue Province accent/dialect (Central Vietnamese).
  116. locale = new Locale("vi", "VN", "hue");
  117. } else if (name.equals("vi-sgn")) {
  118. // 'sgn' is for the Saigon accent/dialect (South Vietnamese).
  119. locale = new Locale("vi", "VN", "saigon");
  120. } else if (name.equals("zh-yue")) {
  121. // Android/Java does not support macrolanguages.
  122. locale = new Locale("zh", "HK");
  123. } else {
  124. String[] parts = name.split("-");
  125. switch (parts.length) {
  126. case 1: // language
  127. locale = new Locale(parts[0]);
  128. break;
  129. case 2: // language-country
  130. if (parts[1].equals("uk")) {
  131. // 'uk' is the language code for Ukranian, not Great Britain.
  132. parts[1] = "GB";
  133. }
  134. locale = new Locale(parts[0], parts[1]);
  135. break;
  136. case 3: // language-country-variant
  137. if (parts[1].equals("uk")) {
  138. // 'uk' is the language code for Ukranian, not Great Britain.
  139. parts[1] = "GB";
  140. }
  141. locale = new Locale(parts[0], parts[1], parts[2]);
  142. break;
  143. default:
  144. locale = null;
  145. }
  146. }
  147. try {
  148. if (locale != null && !locale.getISO3Language().equals("")) {
  149. final Voice voice = new Voice(name, identifier, gender, age, locale);
  150. voices.add(voice);
  151. }
  152. } catch (MissingResourceException e) {
  153. // Android 4.3 throws this exception if the 3-letter language
  154. // (e.g. nci) or country (e.g. 021) code is missing for a locale.
  155. // Earlier versions of Android return an empty string.
  156. }
  157. }
  158. return voices;
  159. }
  160. public void setVoice(Voice voice, VoiceVariant variant) {
  161. // NOTE: espeak_SetVoiceByProperties does not support specifying the
  162. // voice variant (e.g. klatt), but espeak_SetVoiceByName does.
  163. if (variant.variant == null) {
  164. nativeSetVoiceByProperties(voice.name, variant.gender, variant.age);
  165. } else {
  166. nativeSetVoiceByName(voice.identifier + "+" + variant.variant);
  167. }
  168. }
  169. /** Don't announce any punctuation characters. */
  170. public static int PUNCT_NONE = 0;
  171. /** Announce every punctuation character. */
  172. public static int PUNCT_ALL = 1;
  173. /** Announce some of the punctuation characters. */
  174. public static int PUNCT_SOME = 2;
  175. public enum UnitType {
  176. Percentage,
  177. WordsPerMinute,
  178. /** One of the PUNCT_* constants. */
  179. Punctuation,
  180. }
  181. public class Parameter {
  182. private final int id;
  183. private final int min;
  184. private final int max;
  185. private final UnitType unitType;
  186. private Parameter(int id, int min, int max, UnitType unitType) {
  187. this.id = id;
  188. this.min = min;
  189. this.max = max;
  190. this.unitType = unitType;
  191. }
  192. public int getMinValue() {
  193. return min;
  194. }
  195. public int getMaxValue() {
  196. return max;
  197. }
  198. public int getDefaultValue() {
  199. return nativeGetParameter(id, 0);
  200. }
  201. public int getValue() {
  202. return nativeGetParameter(id, 1);
  203. }
  204. public void setValue(int value, int scale) {
  205. setValue((value * scale) / 100);
  206. }
  207. public void setValue(int value) {
  208. nativeSetParameter(id, value);
  209. }
  210. public UnitType getUnitType() {
  211. return unitType;
  212. }
  213. }
  214. /** Speech rate. */
  215. public final Parameter Rate = new Parameter(1, 80, 450, UnitType.WordsPerMinute);
  216. /** Audio volume. */
  217. public final Parameter Volume = new Parameter(2, 0, 200, UnitType.Percentage);
  218. /** Base pitch. */
  219. public final Parameter Pitch = new Parameter(3, 0, 100, UnitType.Percentage);
  220. /** Pitch range (monotone = 0). */
  221. public final Parameter PitchRange = new Parameter(4, 0, 100, UnitType.Percentage);
  222. /** Which punctuation characters to announce. */
  223. public final Parameter Punctuation = new Parameter(5, 0, 2, UnitType.Punctuation);
  224. public void synthesize(String text, boolean isSsml) {
  225. nativeSynthesize(text, isSsml);
  226. }
  227. public void stop() {
  228. nativeStop();
  229. }
  230. private void nativeSynthCallback(byte[] audioData) {
  231. if (mCallback == null)
  232. return;
  233. if (audioData == null) {
  234. mCallback.onSynthDataComplete();
  235. } else {
  236. mCallback.onSynthDataReady(audioData);
  237. }
  238. }
  239. private void attemptInit() {
  240. if (mInitialized) {
  241. return;
  242. }
  243. if (!CheckVoiceData.hasBaseResources(mContext)) {
  244. Log.e(TAG, "Missing base resources");
  245. return;
  246. }
  247. if (!nativeCreate(mDatapath)) {
  248. Log.e(TAG, "Failed to initialize speech synthesis library");
  249. return;
  250. }
  251. Log.i(TAG, "Initialized synthesis library with sample rate = " + getSampleRate());
  252. mInitialized = true;
  253. }
  254. public static String getSampleText(Context context, Locale locale) {
  255. final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
  256. final Configuration config = context.getResources().getConfiguration();
  257. final String language = getIanaLocaleCode(locale.getLanguage(), mJavaToIanaLanguageCode);
  258. final String country = getIanaLocaleCode(locale.getCountry(), mJavaToIanaCountryCode);
  259. config.locale = new Locale(language, country, locale.getVariant());
  260. Resources res = new Resources(context.getAssets(), metrics, config);
  261. return res.getString(R.string.sample_text, config.locale.getDisplayName(config.locale));
  262. }
  263. private int mNativeData;
  264. private static native final boolean nativeClassInit();
  265. private native final boolean nativeCreate(String path);
  266. private native final boolean nativeDestroy();
  267. private native final static String nativeGetVersion();
  268. private native final int nativeGetSampleRate();
  269. private native final int nativeGetChannelCount();
  270. private native final int nativeGetAudioFormat();
  271. private native final int nativeGetBufferSizeInMillis();
  272. private native final String[] nativeGetAvailableVoices();
  273. private native final boolean nativeSetVoiceByName(String name);
  274. private native final boolean nativeSetVoiceByProperties(String language, int gender, int age);
  275. private native final boolean nativeSetParameter(int parameter, int value);
  276. private native final int nativeGetParameter(int parameter, int current);
  277. private native final boolean nativeSynthesize(String text, boolean isSsml);
  278. private native final boolean nativeStop();
  279. public interface SynthReadyCallback {
  280. void onSynthDataReady(byte[] audioData);
  281. void onSynthDataComplete();
  282. }
  283. private static String getIanaLocaleCode(String code, final Map<String, String> javaToIana) {
  284. final String iana = javaToIana.get(code);
  285. if (iana != null) {
  286. return iana;
  287. }
  288. return code;
  289. }
  290. private static final Map<String, String> mJavaToIanaLanguageCode = new HashMap<String, String>();
  291. private static final Map<String, String> mJavaToIanaCountryCode = new HashMap<String, String>();
  292. static {
  293. mJavaToIanaLanguageCode.put("afr", "af");
  294. mJavaToIanaLanguageCode.put("aka", "ak");
  295. mJavaToIanaLanguageCode.put("amh", "am");
  296. mJavaToIanaLanguageCode.put("arg", "an");
  297. mJavaToIanaLanguageCode.put("aze", "az");
  298. mJavaToIanaLanguageCode.put("bul", "bg");
  299. mJavaToIanaLanguageCode.put("bos", "bs");
  300. mJavaToIanaLanguageCode.put("cat", "ca");
  301. mJavaToIanaLanguageCode.put("ces", "cs");
  302. mJavaToIanaLanguageCode.put("cym", "cy");
  303. mJavaToIanaLanguageCode.put("dan", "da");
  304. mJavaToIanaLanguageCode.put("deu", "de");
  305. mJavaToIanaLanguageCode.put("div", "dv");
  306. mJavaToIanaLanguageCode.put("ell", "el");
  307. mJavaToIanaLanguageCode.put("eng", "en");
  308. mJavaToIanaLanguageCode.put("epo", "eo");
  309. mJavaToIanaLanguageCode.put("spa", "es");
  310. mJavaToIanaLanguageCode.put("est", "et");
  311. mJavaToIanaLanguageCode.put("fas", "fa");
  312. mJavaToIanaLanguageCode.put("fin", "fi");
  313. mJavaToIanaLanguageCode.put("fra", "fr");
  314. mJavaToIanaLanguageCode.put("gle", "ga");
  315. mJavaToIanaLanguageCode.put("hin", "hi");
  316. mJavaToIanaLanguageCode.put("hrv", "hr");
  317. mJavaToIanaLanguageCode.put("hat", "ht");
  318. mJavaToIanaLanguageCode.put("hun", "hu");
  319. mJavaToIanaLanguageCode.put("hye", "hy");
  320. mJavaToIanaLanguageCode.put("ind", "in"); // NOTE: The deprecated 'in' code is used by Java/Android.
  321. mJavaToIanaLanguageCode.put("isl", "is");
  322. mJavaToIanaLanguageCode.put("ita", "it");
  323. mJavaToIanaLanguageCode.put("kat", "ka");
  324. mJavaToIanaLanguageCode.put("kaz", "kk");
  325. mJavaToIanaLanguageCode.put("kal", "kl");
  326. mJavaToIanaLanguageCode.put("kan", "kn");
  327. mJavaToIanaLanguageCode.put("kor", "ko");
  328. mJavaToIanaLanguageCode.put("kur", "ku");
  329. mJavaToIanaLanguageCode.put("lat", "la");
  330. mJavaToIanaLanguageCode.put("lit", "lt");
  331. mJavaToIanaLanguageCode.put("lav", "lv");
  332. mJavaToIanaLanguageCode.put("mkd", "mk");
  333. mJavaToIanaLanguageCode.put("mal", "ml");
  334. mJavaToIanaLanguageCode.put("mlt", "mt");
  335. mJavaToIanaLanguageCode.put("nep", "ne");
  336. mJavaToIanaLanguageCode.put("nld", "nl");
  337. mJavaToIanaLanguageCode.put("nor", "no");
  338. mJavaToIanaLanguageCode.put("pan", "pa");
  339. mJavaToIanaLanguageCode.put("pol", "pl");
  340. mJavaToIanaLanguageCode.put("por", "pt");
  341. mJavaToIanaLanguageCode.put("ron", "ro");
  342. mJavaToIanaLanguageCode.put("rus", "ru");
  343. mJavaToIanaLanguageCode.put("kin", "rw");
  344. mJavaToIanaLanguageCode.put("sin", "si");
  345. mJavaToIanaLanguageCode.put("slk", "sk");
  346. mJavaToIanaLanguageCode.put("slv", "sl");
  347. mJavaToIanaLanguageCode.put("sqi", "sq");
  348. mJavaToIanaLanguageCode.put("srp", "sr");
  349. mJavaToIanaLanguageCode.put("swe", "sv");
  350. mJavaToIanaLanguageCode.put("swa", "sw");
  351. mJavaToIanaLanguageCode.put("tam", "ta");
  352. mJavaToIanaLanguageCode.put("tel", "te");
  353. mJavaToIanaLanguageCode.put("tsn", "tn");
  354. mJavaToIanaLanguageCode.put("tur", "tr");
  355. mJavaToIanaLanguageCode.put("tat", "tt");
  356. mJavaToIanaLanguageCode.put("urd", "ur");
  357. mJavaToIanaLanguageCode.put("vie", "vi");
  358. mJavaToIanaLanguageCode.put("wol", "wo");
  359. mJavaToIanaLanguageCode.put("zho", "zh");
  360. mJavaToIanaLanguageCode.put("yue", "zh");
  361. mJavaToIanaCountryCode.put("JAM", "JM");
  362. mJavaToIanaCountryCode.put("MEX", "MX");
  363. mJavaToIanaCountryCode.put("BEL", "BE");
  364. mJavaToIanaCountryCode.put("BRA", "BR");
  365. mJavaToIanaCountryCode.put("FRA", "FR");
  366. mJavaToIanaCountryCode.put("GBR", "GB");
  367. mJavaToIanaCountryCode.put("PRT", "PT");
  368. mJavaToIanaCountryCode.put("USA", "US");
  369. mJavaToIanaCountryCode.put("VNM", "VN");
  370. }
  371. }