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

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