tts_android.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. jmethodID TTS_Android::_init = nullptr;
  38. jmethodID TTS_Android::_is_speaking = nullptr;
  39. jmethodID TTS_Android::_is_paused = nullptr;
  40. jmethodID TTS_Android::_get_voices = nullptr;
  41. jmethodID TTS_Android::_speak = nullptr;
  42. jmethodID TTS_Android::_pause_speaking = nullptr;
  43. jmethodID TTS_Android::_resume_speaking = nullptr;
  44. jmethodID TTS_Android::_stop_speaking = nullptr;
  45. HashMap<int, Char16String> TTS_Android::ids;
  46. void TTS_Android::initialize_tts() {
  47. JNIEnv *env = get_jni_env();
  48. ERR_FAIL_NULL(env);
  49. if (_init) {
  50. env->CallVoidMethod(tts, _init);
  51. initialized = true;
  52. }
  53. }
  54. void TTS_Android::setup(jobject p_tts) {
  55. JNIEnv *env = get_jni_env();
  56. ERR_FAIL_NULL(env);
  57. tts = env->NewGlobalRef(p_tts);
  58. jclass c = env->GetObjectClass(tts);
  59. cls = (jclass)env->NewGlobalRef(c);
  60. _init = env->GetMethodID(cls, "init", "()V");
  61. _is_speaking = env->GetMethodID(cls, "isSpeaking", "()Z");
  62. _is_paused = env->GetMethodID(cls, "isPaused", "()Z");
  63. _get_voices = env->GetMethodID(cls, "getVoices", "()[Ljava/lang/String;");
  64. _speak = env->GetMethodID(cls, "speak", "(Ljava/lang/String;Ljava/lang/String;IFFIZ)V");
  65. _pause_speaking = env->GetMethodID(cls, "pauseSpeaking", "()V");
  66. _resume_speaking = env->GetMethodID(cls, "resumeSpeaking", "()V");
  67. _stop_speaking = env->GetMethodID(cls, "stopSpeaking", "()V");
  68. bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech");
  69. if (tts_enabled) {
  70. initialize_tts();
  71. }
  72. }
  73. void TTS_Android::terminate() {
  74. JNIEnv *env = get_jni_env();
  75. ERR_FAIL_NULL(env);
  76. if (cls) {
  77. env->DeleteGlobalRef(cls);
  78. }
  79. if (tts) {
  80. env->DeleteGlobalRef(tts);
  81. }
  82. }
  83. void TTS_Android::_java_utterance_callback(int p_event, int p_id, int p_pos) {
  84. if (unlikely(!initialized)) {
  85. initialize_tts();
  86. }
  87. ERR_FAIL_NULL(tts);
  88. if (ids.has(p_id)) {
  89. int pos = 0;
  90. if ((DisplayServer::TTSUtteranceEvent)p_event == DisplayServer::TTS_UTTERANCE_BOUNDARY) {
  91. // Convert position from UTF-16 to UTF-32.
  92. const Char16String &string = ids[p_id];
  93. for (int i = 0; i < MIN(p_pos, string.length()); i++) {
  94. char16_t c = string[i];
  95. if ((c & 0xfffffc00) == 0xd800) {
  96. i++;
  97. }
  98. pos++;
  99. }
  100. } else if ((DisplayServer::TTSUtteranceEvent)p_event != DisplayServer::TTS_UTTERANCE_STARTED) {
  101. ids.erase(p_id);
  102. }
  103. DisplayServer::get_singleton()->tts_post_utterance_event((DisplayServer::TTSUtteranceEvent)p_event, p_id, pos);
  104. }
  105. }
  106. bool TTS_Android::is_speaking() {
  107. if (unlikely(!initialized)) {
  108. initialize_tts();
  109. }
  110. ERR_FAIL_NULL_V(tts, false);
  111. if (_is_speaking) {
  112. JNIEnv *env = get_jni_env();
  113. ERR_FAIL_NULL_V(env, false);
  114. return env->CallBooleanMethod(tts, _is_speaking);
  115. } else {
  116. return false;
  117. }
  118. }
  119. bool TTS_Android::is_paused() {
  120. if (unlikely(!initialized)) {
  121. initialize_tts();
  122. }
  123. ERR_FAIL_NULL_V(tts, false);
  124. if (_is_paused) {
  125. JNIEnv *env = get_jni_env();
  126. ERR_FAIL_NULL_V(env, false);
  127. return env->CallBooleanMethod(tts, _is_paused);
  128. } else {
  129. return false;
  130. }
  131. }
  132. Array TTS_Android::get_voices() {
  133. if (unlikely(!initialized)) {
  134. initialize_tts();
  135. }
  136. ERR_FAIL_NULL_V(tts, Array());
  137. Array list;
  138. if (_get_voices) {
  139. JNIEnv *env = get_jni_env();
  140. ERR_FAIL_NULL_V(env, list);
  141. jobject voices_object = env->CallObjectMethod(tts, _get_voices);
  142. jobjectArray *arr = reinterpret_cast<jobjectArray *>(&voices_object);
  143. jsize len = env->GetArrayLength(*arr);
  144. for (int i = 0; i < len; i++) {
  145. jstring jStr = (jstring)env->GetObjectArrayElement(*arr, i);
  146. String str = jstring_to_string(jStr, env);
  147. Vector<String> tokens = str.split(";", true, 2);
  148. if (tokens.size() == 2) {
  149. Dictionary voice_d;
  150. voice_d["name"] = tokens[1];
  151. voice_d["id"] = tokens[1];
  152. voice_d["language"] = tokens[0];
  153. list.push_back(voice_d);
  154. }
  155. env->DeleteLocalRef(jStr);
  156. }
  157. }
  158. return list;
  159. }
  160. void TTS_Android::speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int p_utterance_id, bool p_interrupt) {
  161. if (unlikely(!initialized)) {
  162. initialize_tts();
  163. }
  164. ERR_FAIL_NULL(tts);
  165. if (p_interrupt) {
  166. stop();
  167. }
  168. if (p_text.is_empty()) {
  169. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, p_utterance_id);
  170. return;
  171. }
  172. ids[p_utterance_id] = p_text.utf16();
  173. if (_speak) {
  174. JNIEnv *env = get_jni_env();
  175. ERR_FAIL_NULL(env);
  176. jstring jStrT = env->NewStringUTF(p_text.utf8().get_data());
  177. jstring jStrV = env->NewStringUTF(p_voice.utf8().get_data());
  178. 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);
  179. env->DeleteLocalRef(jStrT);
  180. env->DeleteLocalRef(jStrV);
  181. }
  182. }
  183. void TTS_Android::pause() {
  184. if (unlikely(!initialized)) {
  185. initialize_tts();
  186. }
  187. ERR_FAIL_NULL(tts);
  188. if (_pause_speaking) {
  189. JNIEnv *env = get_jni_env();
  190. ERR_FAIL_NULL(env);
  191. env->CallVoidMethod(tts, _pause_speaking);
  192. }
  193. }
  194. void TTS_Android::resume() {
  195. if (unlikely(!initialized)) {
  196. initialize_tts();
  197. }
  198. ERR_FAIL_NULL(tts);
  199. if (_resume_speaking) {
  200. JNIEnv *env = get_jni_env();
  201. ERR_FAIL_NULL(env);
  202. env->CallVoidMethod(tts, _resume_speaking);
  203. }
  204. }
  205. void TTS_Android::stop() {
  206. if (unlikely(!initialized)) {
  207. initialize_tts();
  208. }
  209. ERR_FAIL_NULL(tts);
  210. for (const KeyValue<int, Char16String> &E : ids) {
  211. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, E.key);
  212. }
  213. ids.clear();
  214. if (_stop_speaking) {
  215. JNIEnv *env = get_jni_env();
  216. ERR_FAIL_NULL(env);
  217. env->CallVoidMethod(tts, _stop_speaking);
  218. }
  219. }