1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Fix Android TTS on-demand init.

This commit is contained in:
Pāvels Nadtočajevs
2025-07-31 16:47:19 +03:00
parent 2a9ff39264
commit 4915d61362
3 changed files with 138 additions and 21 deletions

View File

@@ -38,9 +38,14 @@ bool TTS_Android::initialized = false;
jobject TTS_Android::tts = nullptr;
jclass TTS_Android::cls = nullptr;
Thread TTS_Android::init_thread;
SafeFlag TTS_Android::quit_request;
SafeFlag TTS_Android::init_done;
jmethodID TTS_Android::_init = nullptr;
jmethodID TTS_Android::_is_speaking = nullptr;
jmethodID TTS_Android::_is_paused = nullptr;
jmethodID TTS_Android::_get_state = nullptr;
jmethodID TTS_Android::_get_voices = nullptr;
jmethodID TTS_Android::_speak = nullptr;
jmethodID TTS_Android::_pause_speaking = nullptr;
@@ -49,12 +54,49 @@ jmethodID TTS_Android::_stop_speaking = nullptr;
HashMap<int, Char16String> TTS_Android::ids;
void TTS_Android::initialize_tts() {
void TTS_Android::_thread_function(void *self) {
JNIEnv *env = get_jni_env();
ERR_FAIL_NULL(env);
if (_init) {
env->CallVoidMethod(tts, _init);
env->CallVoidMethod(tts, _init);
uint64_t sleep = 200;
while (env->CallIntMethod(tts, _get_state) == INIT_STATE_UNKNOWN && !quit_request.is_set()) {
OS::get_singleton()->delay_usec(1000 * sleep);
}
init_done.set();
}
void TTS_Android::initialize_tts(bool p_wait) {
if (!_init || !_get_state || !tts) {
return;
}
JNIEnv *env = get_jni_env();
ERR_FAIL_NULL(env);
if (!init_thread.is_started() && !init_done.is_set()) {
init_thread.start(TTS_Android::_thread_function, nullptr);
}
if (env->CallIntMethod(tts, _get_state) == INIT_STATE_SUCCESS) {
initialized = true;
return;
}
// If it's not initialized at launch wait for 1 second for TTS init.
if (p_wait) {
uint64_t sleep = 200;
uint64_t wait = 1000000;
uint64_t time = OS::get_singleton()->get_ticks_usec();
while (OS::get_singleton()->get_ticks_usec() - time < wait) {
OS::get_singleton()->delay_usec(1000 * sleep);
if (init_done.is_set()) {
break;
}
}
}
if (env->CallIntMethod(tts, _get_state) == INIT_STATE_SUCCESS) {
initialized = true;
}
}
@@ -64,6 +106,8 @@ void TTS_Android::setup(jobject p_tts) {
ERR_FAIL_NULL(env);
tts = env->NewGlobalRef(p_tts);
quit_request.clear();
init_done.clear();
jclass c = env->GetObjectClass(tts);
cls = (jclass)env->NewGlobalRef(c);
@@ -71,6 +115,7 @@ void TTS_Android::setup(jobject p_tts) {
_init = env->GetMethodID(cls, "init", "()V");
_is_speaking = env->GetMethodID(cls, "isSpeaking", "()Z");
_is_paused = env->GetMethodID(cls, "isPaused", "()Z");
_get_state = env->GetMethodID(cls, "getState", "()I");
_get_voices = env->GetMethodID(cls, "getVoices", "()[Ljava/lang/String;");
_speak = env->GetMethodID(cls, "speak", "(Ljava/lang/String;Ljava/lang/String;IFFIZ)V");
_pause_speaking = env->GetMethodID(cls, "pauseSpeaking", "()V");
@@ -79,7 +124,7 @@ void TTS_Android::setup(jobject p_tts) {
bool tts_enabled = GLOBAL_GET("audio/general/text_to_speech");
if (tts_enabled) {
initialize_tts();
initialize_tts(false);
}
}
@@ -87,6 +132,11 @@ void TTS_Android::terminate() {
JNIEnv *env = get_jni_env();
ERR_FAIL_NULL(env);
if (init_thread.is_started()) {
quit_request.set();
init_thread.wait_to_finish();
}
if (cls) {
env->DeleteGlobalRef(cls);
}
@@ -99,7 +149,7 @@ void TTS_Android::_java_utterance_callback(int p_event, int p_id, int p_pos) {
if (unlikely(!initialized)) {
initialize_tts();
}
ERR_FAIL_NULL(tts);
ERR_FAIL_COND_MSG(!initialized || tts == nullptr, "Text to Speech not initialized.");
if (ids.has(p_id)) {
int pos = 0;
if ((DisplayServer::TTSUtteranceEvent)p_event == DisplayServer::TTS_UTTERANCE_BOUNDARY) {
@@ -123,7 +173,7 @@ bool TTS_Android::is_speaking() {
if (unlikely(!initialized)) {
initialize_tts();
}
ERR_FAIL_NULL_V(tts, false);
ERR_FAIL_COND_V_MSG(!initialized || tts == nullptr, false, "Text to Speech not initialized.");
if (_is_speaking) {
JNIEnv *env = get_jni_env();
@@ -138,7 +188,7 @@ bool TTS_Android::is_paused() {
if (unlikely(!initialized)) {
initialize_tts();
}
ERR_FAIL_NULL_V(tts, false);
ERR_FAIL_COND_V_MSG(!initialized || tts == nullptr, false, "Text to Speech not initialized.");
if (_is_paused) {
JNIEnv *env = get_jni_env();
@@ -153,7 +203,7 @@ Array TTS_Android::get_voices() {
if (unlikely(!initialized)) {
initialize_tts();
}
ERR_FAIL_NULL_V(tts, Array());
ERR_FAIL_COND_V_MSG(!initialized || tts == nullptr, Array(), "Text to Speech not initialized.");
Array list;
if (_get_voices) {
JNIEnv *env = get_jni_env();
@@ -184,7 +234,7 @@ void TTS_Android::speak(const String &p_text, const String &p_voice, int p_volum
if (unlikely(!initialized)) {
initialize_tts();
}
ERR_FAIL_NULL(tts);
ERR_FAIL_COND_MSG(!initialized || tts == nullptr, "Text to Speech not initialized.");
if (p_interrupt) {
stop();
}
@@ -212,7 +262,7 @@ void TTS_Android::pause() {
if (unlikely(!initialized)) {
initialize_tts();
}
ERR_FAIL_NULL(tts);
ERR_FAIL_COND_MSG(!initialized || tts == nullptr, "Text to Speech not initialized.");
if (_pause_speaking) {
JNIEnv *env = get_jni_env();
@@ -225,7 +275,7 @@ void TTS_Android::resume() {
if (unlikely(!initialized)) {
initialize_tts();
}
ERR_FAIL_NULL(tts);
ERR_FAIL_COND_MSG(!initialized || tts == nullptr, "Text to Speech not initialized.");
if (_resume_speaking) {
JNIEnv *env = get_jni_env();
@@ -238,7 +288,7 @@ void TTS_Android::stop() {
if (unlikely(!initialized)) {
initialize_tts();
}
ERR_FAIL_NULL(tts);
ERR_FAIL_COND_MSG(!initialized || tts == nullptr, "Text to Speech not initialized.");
for (const KeyValue<int, Char16String> &E : ids) {
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, E.key);
}