tts_android.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "string_android.h"
  34. #include "thread_jandroid.h"
  35. bool TTS_Android::initialized = false;
  36. jobject TTS_Android::tts = 0;
  37. jclass TTS_Android::cls = 0;
  38. jmethodID TTS_Android::_init = 0;
  39. jmethodID TTS_Android::_is_speaking = 0;
  40. jmethodID TTS_Android::_is_paused = 0;
  41. jmethodID TTS_Android::_get_voices = 0;
  42. jmethodID TTS_Android::_speak = 0;
  43. jmethodID TTS_Android::_pause_speaking = 0;
  44. jmethodID TTS_Android::_resume_speaking = 0;
  45. jmethodID TTS_Android::_stop_speaking = 0;
  46. HashMap<int, Vector<char16_t>> TTS_Android::ids;
  47. Vector<char16_t> TTS_Android::str_to_utf16(const String &p_string) {
  48. int l = p_string.length();
  49. if (!l) {
  50. return Vector<char16_t>();
  51. }
  52. const CharType *d = &p_string[0];
  53. int fl = 0;
  54. for (int i = 0; i < l; i++) {
  55. uint32_t c = d[i];
  56. if (c <= 0xffff) { // 16 bits.
  57. fl += 1;
  58. } else if (c <= 0x10ffff) { // 32 bits.
  59. fl += 2;
  60. } else {
  61. print_error("Unicode parsing error: Invalid unicode codepoint " + String::num_int64(c, 16) + ".");
  62. return Vector<char16_t>();
  63. }
  64. if (c >= 0xd800 && c <= 0xdfff) {
  65. print_error("Unicode parsing error: Invalid unicode codepoint " + String::num_int64(c, 16) + ".");
  66. return Vector<char16_t>();
  67. }
  68. }
  69. Vector<char16_t> utf16s;
  70. if (fl == 0) {
  71. return utf16s;
  72. }
  73. utf16s.resize(fl + 1);
  74. uint16_t *cdst = (uint16_t *)utf16s.ptrw();
  75. #define APPEND_CHAR(m_c) *(cdst++) = m_c
  76. for (int i = 0; i < l; i++) {
  77. uint32_t c = d[i];
  78. if (c <= 0xffff) { // 16 bits.
  79. APPEND_CHAR(c);
  80. } else { // 32 bits.
  81. APPEND_CHAR(uint32_t((c >> 10) + 0xd7c0)); // lead surrogate.
  82. APPEND_CHAR(uint32_t((c & 0x3ff) | 0xdc00)); // trail surrogate.
  83. }
  84. }
  85. #undef APPEND_CHAR
  86. *cdst = 0; //trailing zero
  87. return utf16s;
  88. }
  89. void TTS_Android::setup(jobject p_tts) {
  90. bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech");
  91. if (tts_enabled) {
  92. JNIEnv *env = get_jni_env();
  93. tts = env->NewGlobalRef(p_tts);
  94. jclass c = env->GetObjectClass(tts);
  95. cls = (jclass)env->NewGlobalRef(c);
  96. _init = env->GetMethodID(cls, "init", "()V");
  97. _is_speaking = env->GetMethodID(cls, "isSpeaking", "()Z");
  98. _is_paused = env->GetMethodID(cls, "isPaused", "()Z");
  99. _get_voices = env->GetMethodID(cls, "getVoices", "()[Ljava/lang/String;");
  100. _speak = env->GetMethodID(cls, "speak", "(Ljava/lang/String;Ljava/lang/String;IFFIZ)V");
  101. _pause_speaking = env->GetMethodID(cls, "pauseSpeaking", "()V");
  102. _resume_speaking = env->GetMethodID(cls, "resumeSpeaking", "()V");
  103. _stop_speaking = env->GetMethodID(cls, "stopSpeaking", "()V");
  104. if (_init) {
  105. env->CallVoidMethod(tts, _init);
  106. initialized = true;
  107. }
  108. }
  109. }
  110. void TTS_Android::_java_utterance_callback(int p_event, int p_id, int p_pos) {
  111. ERR_FAIL_COND_MSG(!initialized, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  112. if (ids.has(p_id)) {
  113. int pos = 0;
  114. if ((OS::TTSUtteranceEvent)p_event == OS::TTS_UTTERANCE_BOUNDARY) {
  115. // Convert position from UTF-16 to UTF-32.
  116. const Vector<char16_t> &string = ids[p_id];
  117. for (int i = 0; i < MIN(p_pos, string.size() - 1); i++) {
  118. char16_t c = string[i];
  119. if ((c & 0xfffffc00) == 0xd800) {
  120. i++;
  121. }
  122. pos++;
  123. }
  124. } else if ((OS::TTSUtteranceEvent)p_event != OS::TTS_UTTERANCE_STARTED) {
  125. ids.erase(p_id);
  126. }
  127. OS::get_singleton()->tts_post_utterance_event((OS::TTSUtteranceEvent)p_event, p_id, pos);
  128. }
  129. }
  130. bool TTS_Android::is_speaking() {
  131. ERR_FAIL_COND_V_MSG(!initialized, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  132. if (_is_speaking) {
  133. JNIEnv *env = get_jni_env();
  134. ERR_FAIL_COND_V(env == nullptr, false);
  135. return env->CallBooleanMethod(tts, _is_speaking);
  136. } else {
  137. return false;
  138. }
  139. }
  140. bool TTS_Android::is_paused() {
  141. ERR_FAIL_COND_V_MSG(!initialized, false, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  142. if (_is_paused) {
  143. JNIEnv *env = get_jni_env();
  144. ERR_FAIL_COND_V(env == nullptr, false);
  145. return env->CallBooleanMethod(tts, _is_paused);
  146. } else {
  147. return false;
  148. }
  149. }
  150. Array TTS_Android::get_voices() {
  151. ERR_FAIL_COND_V_MSG(!initialized, Array(), "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  152. Array list;
  153. if (_get_voices) {
  154. JNIEnv *env = get_jni_env();
  155. ERR_FAIL_COND_V(env == nullptr, list);
  156. jobject voices_object = env->CallObjectMethod(tts, _get_voices);
  157. jobjectArray *arr = reinterpret_cast<jobjectArray *>(&voices_object);
  158. jsize len = env->GetArrayLength(*arr);
  159. for (int i = 0; i < len; i++) {
  160. jstring jStr = (jstring)env->GetObjectArrayElement(*arr, i);
  161. String str = jstring_to_string(jStr, env);
  162. Vector<String> tokens = str.split(";", true, 2);
  163. if (tokens.size() == 2) {
  164. Dictionary voice_d;
  165. voice_d["name"] = tokens[1];
  166. voice_d["id"] = tokens[1];
  167. voice_d["language"] = tokens[0];
  168. list.push_back(voice_d);
  169. }
  170. env->DeleteLocalRef(jStr);
  171. }
  172. }
  173. return list;
  174. }
  175. 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) {
  176. ERR_FAIL_COND_MSG(!initialized, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  177. if (p_interrupt) {
  178. stop();
  179. }
  180. if (p_text.empty()) {
  181. OS::get_singleton()->tts_post_utterance_event(OS::TTS_UTTERANCE_CANCELED, p_utterance_id);
  182. return;
  183. }
  184. ids[p_utterance_id] = str_to_utf16(p_text);
  185. if (_speak) {
  186. JNIEnv *env = get_jni_env();
  187. ERR_FAIL_COND(env == nullptr);
  188. jstring jStrT = env->NewStringUTF(p_text.utf8().get_data());
  189. jstring jStrV = env->NewStringUTF(p_voice.utf8().get_data());
  190. 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);
  191. }
  192. }
  193. void TTS_Android::pause() {
  194. ERR_FAIL_COND_MSG(!initialized, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  195. if (_pause_speaking) {
  196. JNIEnv *env = get_jni_env();
  197. ERR_FAIL_COND(env == nullptr);
  198. env->CallVoidMethod(tts, _pause_speaking);
  199. }
  200. }
  201. void TTS_Android::resume() {
  202. ERR_FAIL_COND_MSG(!initialized, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  203. if (_resume_speaking) {
  204. JNIEnv *env = get_jni_env();
  205. ERR_FAIL_COND(env == nullptr);
  206. env->CallVoidMethod(tts, _resume_speaking);
  207. }
  208. }
  209. void TTS_Android::stop() {
  210. ERR_FAIL_COND_MSG(!initialized, "Enable the \"audio/general/text_to_speech\" project setting to use text-to-speech.");
  211. const int *k = NULL;
  212. while ((k = ids.next(k))) {
  213. OS::get_singleton()->tts_post_utterance_event(OS::TTS_UTTERANCE_CANCELED, *k);
  214. }
  215. ids.clear();
  216. if (_stop_speaking) {
  217. JNIEnv *env = get_jni_env();
  218. ERR_FAIL_COND(env == nullptr);
  219. env->CallVoidMethod(tts, _stop_speaking);
  220. }
  221. }