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.

eSpeakService.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 contains the JNI bindings to eSpeak used by SpeechSynthesis.java.
  19. *
  20. * Android Version: 4.0 (Ice Cream Sandwich)
  21. * API Version: 14
  22. */
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <jni.h>
  27. #include <speak_lib.h>
  28. #include <Log.h>
  29. /** @name Java to Wide String Helpers
  30. * @brief These are helpers for converting a jstring to wchar_t*.
  31. *
  32. * This assumes that wchar_t is a 32-bit (UTF-32) value.
  33. */
  34. //@{
  35. const char *utf8_read(const char *in, wchar_t &c)
  36. {
  37. if (uint8_t(*in) < 0x80)
  38. c = *in++;
  39. else switch (uint8_t(*in) & 0xF0)
  40. {
  41. default:
  42. c = uint8_t(*in++) & 0x1F;
  43. c = (c << 6) + (uint8_t(*in++) & 0x3F);
  44. break;
  45. case 0xE0:
  46. c = uint8_t(*in++) & 0x0F;
  47. c = (c << 6) + (uint8_t(*in++) & 0x3F);
  48. c = (c << 6) + (uint8_t(*in++) & 0x3F);
  49. break;
  50. case 0xF0:
  51. c = uint8_t(*in++) & 0x07;
  52. c = (c << 6) + (uint8_t(*in++) & 0x3F);
  53. c = (c << 6) + (uint8_t(*in++) & 0x3F);
  54. c = (c << 6) + (uint8_t(*in++) & 0x3F);
  55. break;
  56. }
  57. return in;
  58. }
  59. class unicode_string
  60. {
  61. static_assert(sizeof(wchar_t) == 4, "wchar_t is not UTF-32");
  62. public:
  63. unicode_string(JNIEnv *env, jstring str);
  64. ~unicode_string();
  65. const wchar_t *c_str() const { return mString; }
  66. private:
  67. wchar_t *mString;
  68. };
  69. unicode_string::unicode_string(JNIEnv *env, jstring str)
  70. : mString(NULL)
  71. {
  72. if (str == NULL) return;
  73. const char *utf8 = env->GetStringUTFChars(str, NULL);
  74. mString = (wchar_t *)malloc((strlen(utf8) + 1) * sizeof(wchar_t));
  75. const char *utf8_current = utf8;
  76. wchar_t *utf32_current = mString;
  77. while (*utf8_current)
  78. {
  79. utf8_current = utf8_read(utf8_current, *utf32_current);
  80. ++utf32_current;
  81. }
  82. *utf32_current = 0;
  83. env->ReleaseStringUTFChars(str, utf8);
  84. }
  85. unicode_string::~unicode_string()
  86. {
  87. if (mString) free(mString);
  88. }
  89. //@}
  90. #define LOG_TAG "eSpeakService"
  91. #define DEBUG true
  92. enum synthesis_result {
  93. SYNTH_CONTINUE = 0,
  94. SYNTH_ABORT = 1
  95. };
  96. static JavaVM *jvm = NULL;
  97. jmethodID METHOD_nativeSynthCallback;
  98. static JNIEnv *getJniEnv() {
  99. JNIEnv *env = NULL;
  100. jvm->AttachCurrentThread(&env, NULL);
  101. return env;
  102. }
  103. /* Callback from espeak. Should call back to the TTS API */
  104. static int SynthCallback(short *audioData, int numSamples,
  105. espeak_EVENT *events) {
  106. JNIEnv *env = getJniEnv();
  107. jobject object = (jobject)events->user_data;
  108. if (numSamples < 1) {
  109. env->CallVoidMethod(object, METHOD_nativeSynthCallback, NULL);
  110. return SYNTH_ABORT;
  111. } else {
  112. jbyteArray arrayAudioData = env->NewByteArray(numSamples * 2);
  113. env->SetByteArrayRegion(arrayAudioData, 0, (numSamples * 2), (jbyte *) audioData);
  114. env->CallVoidMethod(object, METHOD_nativeSynthCallback, arrayAudioData);
  115. return SYNTH_CONTINUE;
  116. }
  117. }
  118. #ifdef __cplusplus
  119. extern "C" {
  120. #endif /* __cplusplus */
  121. JNIEXPORT jint
  122. JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
  123. jvm = vm;
  124. JNIEnv *env;
  125. if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
  126. LOGE("Failed to get the environment using GetEnv()");
  127. return -1;
  128. }
  129. return JNI_VERSION_1_6;
  130. }
  131. JNIEXPORT jboolean
  132. JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeClassInit(
  133. JNIEnv* env, jclass clazz) {
  134. if (DEBUG) LOGV("%s", __FUNCTION__);
  135. METHOD_nativeSynthCallback = env->GetMethodID(clazz, "nativeSynthCallback", "([B)V");
  136. return JNI_TRUE;
  137. }
  138. JNIEXPORT jint
  139. JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeCreate(
  140. JNIEnv *env, jobject object, jstring path, jint bufferSizeInMillis) {
  141. if (DEBUG) LOGV("%s [env=%p, object=%p]", __FUNCTION__, env, object);
  142. const char *c_path = path ? env->GetStringUTFChars(path, NULL) : NULL;
  143. if (DEBUG) LOGV("Initializing with path %s", c_path);
  144. int sampleRate = espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, bufferSizeInMillis, c_path, 0);
  145. if (c_path) env->ReleaseStringUTFChars(path, c_path);
  146. return sampleRate;
  147. }
  148. JNIEXPORT jobject
  149. JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeGetVersion(
  150. JNIEnv *env, jclass clazz) {
  151. if (DEBUG) LOGV("%s", __FUNCTION__);
  152. return env->NewStringUTF(espeak_Info(NULL));
  153. }
  154. JNIEXPORT jobjectArray
  155. JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeGetAvailableVoices(
  156. JNIEnv *env, jobject object) {
  157. if (DEBUG) LOGV("%s", __FUNCTION__);
  158. const espeak_VOICE **voices = espeak_ListVoices(NULL);
  159. int count;
  160. // First, count the number of voices returned.
  161. for (count = 0; voices[count] != NULL; count++);
  162. // Next, create a Java String array.
  163. jobjectArray voicesArray = (jobjectArray) env->NewObjectArray(
  164. count * 4, env->FindClass("java/lang/String"), NULL);
  165. const espeak_VOICE *v;
  166. char gender_buf[12];
  167. char age_buf[12];
  168. // Finally, populate the array.
  169. for (int i = 0, voicesIndex = 0; (v = voices[i]) != NULL; i++) {
  170. const char *lang_name = v->languages + 1;
  171. const char *identifier = v->identifier;
  172. sprintf(gender_buf, "%d", v->gender);
  173. sprintf(age_buf, "%d", v->age);
  174. env->SetObjectArrayElement(
  175. voicesArray, voicesIndex++, env->NewStringUTF(lang_name));
  176. env->SetObjectArrayElement(
  177. voicesArray, voicesIndex++, env->NewStringUTF(identifier));
  178. env->SetObjectArrayElement(
  179. voicesArray, voicesIndex++, env->NewStringUTF(gender_buf));
  180. env->SetObjectArrayElement(
  181. voicesArray, voicesIndex++, env->NewStringUTF(age_buf));
  182. }
  183. return voicesArray;
  184. }
  185. JNIEXPORT jboolean
  186. JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSetVoiceByName(
  187. JNIEnv *env, jobject object, jstring name) {
  188. const char *c_name = name ? env->GetStringUTFChars(name, NULL) : NULL;
  189. if (DEBUG) LOGV("%s(name=%s)", __FUNCTION__, c_name);
  190. const espeak_ERROR result = espeak_SetVoiceByName(c_name);
  191. if (c_name) env->ReleaseStringUTFChars(name, c_name);
  192. switch (result) {
  193. case EE_OK: return JNI_TRUE;
  194. case EE_INTERNAL_ERROR: LOGE("espeak_SetVoiceByName: internal error."); break;
  195. case EE_BUFFER_FULL: LOGE("espeak_SetVoiceByName: buffer full."); break;
  196. case EE_NOT_FOUND: LOGE("espeak_SetVoiceByName: not found."); break;
  197. }
  198. return JNI_FALSE;
  199. }
  200. JNIEXPORT jboolean
  201. JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSetVoiceByProperties(
  202. JNIEnv *env, jobject object, jstring language, jint gender, jint age) {
  203. const char *c_language = language ? env->GetStringUTFChars(language, NULL) : NULL;
  204. if (DEBUG) LOGV("%s(language=%s, gender=%d, age=%d)", __FUNCTION__, c_language, gender, age);
  205. espeak_VOICE voice_select;
  206. memset(&voice_select, 0, sizeof(espeak_VOICE));
  207. voice_select.languages = c_language;
  208. voice_select.gender = (int) gender;
  209. voice_select.age = (int) age;
  210. const espeak_ERROR result = espeak_SetVoiceByProperties(&voice_select);
  211. if (c_language) env->ReleaseStringUTFChars(language, c_language);
  212. switch (result) {
  213. case EE_OK: return JNI_TRUE;
  214. case EE_INTERNAL_ERROR: LOGE("espeak_SetVoiceByProperties: internal error."); break;
  215. case EE_BUFFER_FULL: LOGE("espeak_SetVoiceByProperties: buffer full."); break;
  216. case EE_NOT_FOUND: LOGE("espeak_SetVoiceByProperties: not found."); break;
  217. }
  218. return JNI_FALSE;
  219. }
  220. JNIEXPORT jboolean
  221. JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSetParameter(
  222. JNIEnv *env, jobject object, jint parameter, jint value) {
  223. if (DEBUG) LOGV("%s(parameter=%d, value=%d)", __FUNCTION__, parameter, value);
  224. const espeak_ERROR result = espeak_SetParameter((espeak_PARAMETER)parameter, (int)value, 0);
  225. switch (result) {
  226. case EE_OK: return JNI_TRUE;
  227. case EE_INTERNAL_ERROR: LOGE("espeak_SetParameter: internal error."); break;
  228. case EE_BUFFER_FULL: LOGE("espeak_SetParameter: buffer full."); break;
  229. case EE_NOT_FOUND: LOGE("espeak_SetParameter: not found."); break;
  230. }
  231. return JNI_FALSE;
  232. }
  233. JNIEXPORT jint
  234. JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeGetParameter(
  235. JNIEnv *env, jobject object, jint parameter, jint current) {
  236. if (DEBUG) LOGV("%s(parameter=%d, pitch=%d)", __FUNCTION__, parameter, current);
  237. return espeak_GetParameter((espeak_PARAMETER)parameter, (int)current);
  238. }
  239. JNIEXPORT jboolean
  240. JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSetPunctuationCharacters(
  241. JNIEnv *env, jobject object, jstring characters) {
  242. if (DEBUG) LOGV("%s)", __FUNCTION__);
  243. unicode_string list(env, characters);
  244. const espeak_ERROR result = espeak_SetPunctuationList(list.c_str());
  245. switch (result) {
  246. case EE_OK: return JNI_TRUE;
  247. case EE_INTERNAL_ERROR: LOGE("espeak_SetPunctuationList: internal error."); break;
  248. case EE_BUFFER_FULL: LOGE("espeak_SetPunctuationList: buffer full."); break;
  249. case EE_NOT_FOUND: LOGE("espeak_SetPunctuationList: not found."); break;
  250. }
  251. return JNI_FALSE;
  252. }
  253. JNIEXPORT jboolean
  254. JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeSynthesize(
  255. JNIEnv *env, jobject object, jstring text, jboolean isSsml) {
  256. if (DEBUG) LOGV("%s", __FUNCTION__);
  257. const char *c_text = text ? env->GetStringUTFChars(text, NULL) : NULL;
  258. unsigned int unique_identifier;
  259. espeak_SetSynthCallback(SynthCallback);
  260. const espeak_ERROR result = espeak_Synth(c_text, strlen(c_text), 0, // position
  261. POS_CHARACTER, 0, // end position (0 means no end position)
  262. isSsml ? espeakCHARS_UTF8 | espeakSSML // UTF-8 encoded SSML
  263. : espeakCHARS_UTF8, // UTF-8 encoded text
  264. &unique_identifier, object);
  265. espeak_Synchronize();
  266. if (c_text) env->ReleaseStringUTFChars(text, c_text);
  267. switch (result) {
  268. case EE_OK: return JNI_TRUE;
  269. case EE_INTERNAL_ERROR: LOGE("espeak_Synth: internal error."); break;
  270. case EE_BUFFER_FULL: LOGE("espeak_Synth: buffer full."); break;
  271. case EE_NOT_FOUND: LOGE("espeak_Synth: not found."); break;
  272. }
  273. return JNI_TRUE;
  274. }
  275. JNIEXPORT jboolean
  276. JNICALL Java_com_reecedunn_espeak_SpeechSynthesis_nativeStop(
  277. JNIEnv *env, jobject object) {
  278. if (DEBUG) LOGV("%s", __FUNCTION__);
  279. espeak_Cancel();
  280. return JNI_TRUE;
  281. }
  282. #ifdef __cplusplus
  283. }
  284. #endif /* __cplusplus */