tts_android.cpp 8.0 KB

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