tts_android.cpp 7.8 KB

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