tts_android.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**************************************************************************/
  2. /* tts_android.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "tts_android.h"
  31. #include "java_godot_wrapper.h"
  32. #include "os_android.h"
  33. #include "thread_jandroid.h"
  34. bool TTS_Android::initialized = false;
  35. jobject TTS_Android::tts = nullptr;
  36. jclass TTS_Android::cls = nullptr;
  37. Thread TTS_Android::init_thread;
  38. SafeFlag TTS_Android::quit_request;
  39. SafeFlag TTS_Android::init_done;
  40. jmethodID TTS_Android::_init = nullptr;
  41. jmethodID TTS_Android::_is_speaking = nullptr;
  42. jmethodID TTS_Android::_is_paused = nullptr;
  43. jmethodID TTS_Android::_get_state = nullptr;
  44. jmethodID TTS_Android::_get_voices = nullptr;
  45. jmethodID TTS_Android::_speak = nullptr;
  46. jmethodID TTS_Android::_pause_speaking = nullptr;
  47. jmethodID TTS_Android::_resume_speaking = nullptr;
  48. jmethodID TTS_Android::_stop_speaking = nullptr;
  49. HashMap<int64_t, Char16String> TTS_Android::ids;
  50. void TTS_Android::_thread_function(void *self) {
  51. JNIEnv *env = get_jni_env();
  52. ERR_FAIL_NULL(env);
  53. env->CallVoidMethod(tts, _init);
  54. uint64_t sleep = 200;
  55. while (env->CallIntMethod(tts, _get_state) == INIT_STATE_UNKNOWN && !quit_request.is_set()) {
  56. OS::get_singleton()->delay_usec(1000 * sleep);
  57. }
  58. init_done.set();
  59. }
  60. void TTS_Android::initialize_tts(bool p_wait) {
  61. if (!_init || !_get_state || !tts) {
  62. return;
  63. }
  64. JNIEnv *env = get_jni_env();
  65. ERR_FAIL_NULL(env);
  66. if (!init_thread.is_started() && !init_done.is_set()) {
  67. init_thread.start(TTS_Android::_thread_function, nullptr);
  68. }
  69. if (env->CallIntMethod(tts, _get_state) == INIT_STATE_SUCCESS) {
  70. initialized = true;
  71. return;
  72. }
  73. // If it's not initialized at launch wait for 1 second for TTS init.
  74. if (p_wait) {
  75. uint64_t sleep = 200;
  76. uint64_t wait = 1000000;
  77. uint64_t time = OS::get_singleton()->get_ticks_usec();
  78. while (OS::get_singleton()->get_ticks_usec() - time < wait) {
  79. OS::get_singleton()->delay_usec(1000 * sleep);
  80. if (init_done.is_set()) {
  81. break;
  82. }
  83. }
  84. }
  85. if (env->CallIntMethod(tts, _get_state) == INIT_STATE_SUCCESS) {
  86. initialized = true;
  87. }
  88. }
  89. void TTS_Android::setup(jobject p_tts) {
  90. JNIEnv *env = get_jni_env();
  91. ERR_FAIL_NULL(env);
  92. tts = env->NewGlobalRef(p_tts);
  93. quit_request.clear();
  94. init_done.clear();
  95. jclass c = env->GetObjectClass(tts);
  96. cls = (jclass)env->NewGlobalRef(c);
  97. _init = env->GetMethodID(cls, "init", "()V");
  98. _is_speaking = env->GetMethodID(cls, "isSpeaking", "()Z");
  99. _is_paused = env->GetMethodID(cls, "isPaused", "()Z");
  100. _get_state = env->GetMethodID(cls, "getState", "()I");
  101. _get_voices = env->GetMethodID(cls, "getVoices", "()[Ljava/lang/String;");
  102. _speak = env->GetMethodID(cls, "speak", "(Ljava/lang/String;Ljava/lang/String;IFFJZ)V");
  103. _pause_speaking = env->GetMethodID(cls, "pauseSpeaking", "()V");
  104. _resume_speaking = env->GetMethodID(cls, "resumeSpeaking", "()V");
  105. _stop_speaking = env->GetMethodID(cls, "stopSpeaking", "()V");
  106. bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech");
  107. if (tts_enabled) {
  108. initialize_tts(false);
  109. }
  110. }
  111. void TTS_Android::terminate() {
  112. JNIEnv *env = get_jni_env();
  113. ERR_FAIL_NULL(env);
  114. if (init_thread.is_started()) {
  115. quit_request.set();
  116. init_thread.wait_to_finish();
  117. }
  118. if (cls) {
  119. env->DeleteGlobalRef(cls);
  120. }
  121. if (tts) {
  122. env->DeleteGlobalRef(tts);
  123. }
  124. }
  125. void TTS_Android::_java_utterance_callback(int p_event, int64_t p_id, int p_pos) {
  126. if (unlikely(!initialized)) {
  127. initialize_tts();
  128. }
  129. ERR_FAIL_COND_MSG(!initialized || tts == nullptr, "Text to Speech not initialized.");
  130. if (ids.has(p_id)) {
  131. int pos = 0;
  132. if ((DisplayServer::TTSUtteranceEvent)p_event == DisplayServer::TTS_UTTERANCE_BOUNDARY) {
  133. // Convert position from UTF-16 to UTF-32.
  134. const Char16String &string = ids[p_id];
  135. for (int i = 0; i < MIN(p_pos, string.length()); i++) {
  136. char16_t c = string[i];
  137. if ((c & 0xfffffc00) == 0xd800) {
  138. i++;
  139. }
  140. pos++;
  141. }
  142. } else if ((DisplayServer::TTSUtteranceEvent)p_event != DisplayServer::TTS_UTTERANCE_STARTED) {
  143. ids.erase(p_id);
  144. }
  145. DisplayServer::get_singleton()->tts_post_utterance_event((DisplayServer::TTSUtteranceEvent)p_event, p_id, pos);
  146. }
  147. }
  148. bool TTS_Android::is_speaking() {
  149. if (unlikely(!initialized)) {
  150. initialize_tts();
  151. }
  152. ERR_FAIL_COND_V_MSG(!initialized || tts == nullptr, false, "Text to Speech not initialized.");
  153. if (_is_speaking) {
  154. JNIEnv *env = get_jni_env();
  155. ERR_FAIL_NULL_V(env, false);
  156. return env->CallBooleanMethod(tts, _is_speaking);
  157. } else {
  158. return false;
  159. }
  160. }
  161. bool TTS_Android::is_paused() {
  162. if (unlikely(!initialized)) {
  163. initialize_tts();
  164. }
  165. ERR_FAIL_COND_V_MSG(!initialized || tts == nullptr, false, "Text to Speech not initialized.");
  166. if (_is_paused) {
  167. JNIEnv *env = get_jni_env();
  168. ERR_FAIL_NULL_V(env, false);
  169. return env->CallBooleanMethod(tts, _is_paused);
  170. } else {
  171. return false;
  172. }
  173. }
  174. Array TTS_Android::get_voices() {
  175. if (unlikely(!initialized)) {
  176. initialize_tts();
  177. }
  178. ERR_FAIL_COND_V_MSG(!initialized || tts == nullptr, Array(), "Text to Speech not initialized.");
  179. Array list;
  180. if (_get_voices) {
  181. JNIEnv *env = get_jni_env();
  182. ERR_FAIL_NULL_V(env, list);
  183. jobject voices_object = env->CallObjectMethod(tts, _get_voices);
  184. jobjectArray *arr = reinterpret_cast<jobjectArray *>(&voices_object);
  185. jsize len = env->GetArrayLength(*arr);
  186. for (int i = 0; i < len; i++) {
  187. jstring jStr = (jstring)env->GetObjectArrayElement(*arr, i);
  188. String str = jstring_to_string(jStr, env);
  189. Vector<String> tokens = str.split(";", true, 2);
  190. if (tokens.size() == 2) {
  191. Dictionary voice_d;
  192. voice_d["name"] = tokens[1];
  193. voice_d["id"] = tokens[1];
  194. voice_d["language"] = tokens[0];
  195. list.push_back(voice_d);
  196. }
  197. env->DeleteLocalRef(jStr);
  198. }
  199. }
  200. return list;
  201. }
  202. void TTS_Android::speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int64_t p_utterance_id, bool p_interrupt) {
  203. if (unlikely(!initialized)) {
  204. initialize_tts();
  205. }
  206. ERR_FAIL_COND_MSG(!initialized || tts == nullptr, "Text to Speech not initialized.");
  207. if (p_interrupt) {
  208. stop();
  209. }
  210. if (p_text.is_empty()) {
  211. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, p_utterance_id);
  212. return;
  213. }
  214. ids[p_utterance_id] = p_text.utf16();
  215. if (_speak) {
  216. JNIEnv *env = get_jni_env();
  217. ERR_FAIL_NULL(env);
  218. jstring jStrT = env->NewStringUTF(p_text.utf8().get_data());
  219. jstring jStrV = env->NewStringUTF(p_voice.utf8().get_data());
  220. env->CallVoidMethod(tts, _speak, jStrT, jStrV, CLAMP(p_volume, 0, 100), CLAMP(p_pitch, 0.f, 2.f), CLAMP(p_rate, 0.1f, 10.f), p_utterance_id, p_interrupt);
  221. env->DeleteLocalRef(jStrT);
  222. env->DeleteLocalRef(jStrV);
  223. }
  224. }
  225. void TTS_Android::pause() {
  226. if (unlikely(!initialized)) {
  227. initialize_tts();
  228. }
  229. ERR_FAIL_COND_MSG(!initialized || tts == nullptr, "Text to Speech not initialized.");
  230. if (_pause_speaking) {
  231. JNIEnv *env = get_jni_env();
  232. ERR_FAIL_NULL(env);
  233. env->CallVoidMethod(tts, _pause_speaking);
  234. }
  235. }
  236. void TTS_Android::resume() {
  237. if (unlikely(!initialized)) {
  238. initialize_tts();
  239. }
  240. ERR_FAIL_COND_MSG(!initialized || tts == nullptr, "Text to Speech not initialized.");
  241. if (_resume_speaking) {
  242. JNIEnv *env = get_jni_env();
  243. ERR_FAIL_NULL(env);
  244. env->CallVoidMethod(tts, _resume_speaking);
  245. }
  246. }
  247. void TTS_Android::stop() {
  248. if (unlikely(!initialized)) {
  249. initialize_tts();
  250. }
  251. ERR_FAIL_COND_MSG(!initialized || tts == nullptr, "Text to Speech not initialized.");
  252. for (const KeyValue<int64_t, Char16String> &E : ids) {
  253. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, E.key);
  254. }
  255. ids.clear();
  256. if (_stop_speaking) {
  257. JNIEnv *env = get_jni_env();
  258. ERR_FAIL_NULL(env);
  259. env->CallVoidMethod(tts, _stop_speaking);
  260. }
  261. }