tts_android.cpp 8.0 KB

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