tts_android.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*************************************************************************/
  2. /* tts_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. jobject TTS_Android::tts = 0;
  36. jclass TTS_Android::cls = 0;
  37. jmethodID TTS_Android::_is_speaking = 0;
  38. jmethodID TTS_Android::_is_paused = 0;
  39. jmethodID TTS_Android::_get_voices = 0;
  40. jmethodID TTS_Android::_speak = 0;
  41. jmethodID TTS_Android::_pause_speaking = 0;
  42. jmethodID TTS_Android::_resume_speaking = 0;
  43. jmethodID TTS_Android::_stop_speaking = 0;
  44. Map<int, Char16String> TTS_Android::ids;
  45. void TTS_Android::setup(jobject p_tts) {
  46. JNIEnv *env = get_jni_env();
  47. tts = env->NewGlobalRef(p_tts);
  48. jclass c = env->GetObjectClass(tts);
  49. cls = (jclass)env->NewGlobalRef(c);
  50. _is_speaking = env->GetMethodID(cls, "isSpeaking", "()Z");
  51. _is_paused = env->GetMethodID(cls, "isPaused", "()Z");
  52. _get_voices = env->GetMethodID(cls, "getVoices", "()[Ljava/lang/String;");
  53. _speak = env->GetMethodID(cls, "speak", "(Ljava/lang/String;Ljava/lang/String;IFFIZ)V");
  54. _pause_speaking = env->GetMethodID(cls, "pauseSpeaking", "()V");
  55. _resume_speaking = env->GetMethodID(cls, "resumeSpeaking", "()V");
  56. _stop_speaking = env->GetMethodID(cls, "stopSpeaking", "()V");
  57. }
  58. void TTS_Android::_java_utterance_callback(int p_event, int p_id, int p_pos) {
  59. if (ids.has(p_id)) {
  60. int pos = 0;
  61. if ((DisplayServer::TTSUtteranceEvent)p_event == DisplayServer::TTS_UTTERANCE_BOUNDARY) {
  62. // Convert position from UTF-16 to UTF-32.
  63. const Char16String &string = ids[p_id];
  64. for (int i = 0; i < MIN(p_pos, string.length()); i++) {
  65. char16_t c = string[i];
  66. if ((c & 0xfffffc00) == 0xd800) {
  67. i++;
  68. }
  69. pos++;
  70. }
  71. } else if ((DisplayServer::TTSUtteranceEvent)p_event != DisplayServer::TTS_UTTERANCE_STARTED) {
  72. ids.erase(p_id);
  73. }
  74. DisplayServer::get_singleton()->tts_post_utterance_event((DisplayServer::TTSUtteranceEvent)p_event, p_id, pos);
  75. }
  76. }
  77. bool TTS_Android::is_speaking() {
  78. if (_is_speaking) {
  79. JNIEnv *env = get_jni_env();
  80. ERR_FAIL_COND_V(env == nullptr, false);
  81. return env->CallBooleanMethod(tts, _is_speaking);
  82. } else {
  83. return false;
  84. }
  85. }
  86. bool TTS_Android::is_paused() {
  87. if (_is_paused) {
  88. JNIEnv *env = get_jni_env();
  89. ERR_FAIL_COND_V(env == nullptr, false);
  90. return env->CallBooleanMethod(tts, _is_paused);
  91. } else {
  92. return false;
  93. }
  94. }
  95. Array TTS_Android::get_voices() {
  96. Array list;
  97. if (_get_voices) {
  98. JNIEnv *env = get_jni_env();
  99. ERR_FAIL_COND_V(env == nullptr, list);
  100. jobject voices_object = env->CallObjectMethod(tts, _get_voices);
  101. jobjectArray *arr = reinterpret_cast<jobjectArray *>(&voices_object);
  102. jsize len = env->GetArrayLength(*arr);
  103. for (int i = 0; i < len; i++) {
  104. jstring jStr = (jstring)env->GetObjectArrayElement(*arr, i);
  105. String str = jstring_to_string(jStr, env);
  106. Vector<String> tokens = str.split(";", true, 2);
  107. if (tokens.size() == 2) {
  108. Dictionary voice_d;
  109. voice_d["name"] = tokens[1];
  110. voice_d["id"] = tokens[1];
  111. voice_d["language"] = tokens[0];
  112. list.push_back(voice_d);
  113. }
  114. env->DeleteLocalRef(jStr);
  115. }
  116. }
  117. return list;
  118. }
  119. 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) {
  120. if (p_interrupt) {
  121. stop();
  122. }
  123. if (p_text.is_empty()) {
  124. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, p_utterance_id);
  125. return;
  126. }
  127. ids[p_utterance_id] = p_text.utf16();
  128. if (_speak) {
  129. JNIEnv *env = get_jni_env();
  130. ERR_FAIL_COND(env == nullptr);
  131. jstring jStrT = env->NewStringUTF(p_text.utf8().get_data());
  132. jstring jStrV = env->NewStringUTF(p_voice.utf8().get_data());
  133. 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);
  134. }
  135. }
  136. void TTS_Android::pause() {
  137. if (_pause_speaking) {
  138. JNIEnv *env = get_jni_env();
  139. ERR_FAIL_COND(env == nullptr);
  140. env->CallVoidMethod(tts, _pause_speaking);
  141. }
  142. }
  143. void TTS_Android::resume() {
  144. if (_resume_speaking) {
  145. JNIEnv *env = get_jni_env();
  146. ERR_FAIL_COND(env == nullptr);
  147. env->CallVoidMethod(tts, _resume_speaking);
  148. }
  149. }
  150. void TTS_Android::stop() {
  151. for (Map<int, Char16String>::Element *E = ids.front(); E; E = E->next()) {
  152. DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, E->key());
  153. }
  154. ids.clear();
  155. if (_stop_speaking) {
  156. JNIEnv *env = get_jni_env();
  157. ERR_FAIL_COND(env == nullptr);
  158. env->CallVoidMethod(tts, _stop_speaking);
  159. }
  160. }