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.c 11KB

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