-
Notifications
You must be signed in to change notification settings - Fork 182
Support Android TTS #7497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shivansps
wants to merge
8
commits into
scp-fs2open:master
Choose a base branch
from
Shivansps:tts-android
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+304
−1
Open
Support Android TTS #7497
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
258ff50
add tts support
Shivansps 342bd15
correct type
Shivansps f7054fe
more robust method binding and hide volume option
Shivansps da27c21
missing ifndef
Shivansps 9f8cdd0
thread safe jnienv
Shivansps 499acef
check jni env before use it
Shivansps 957e53a
remove opt in volume option and cleanup
Shivansps 6bd0748
cleanup
Shivansps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,298 @@ | ||
| #ifdef FS2_SPEECH | ||
| #ifdef __ANDROID__ | ||
| #include "globalincs/pstypes.h" | ||
| #include "utils/unicode.h" | ||
| #include "speech.h" | ||
| #include <jni.h> | ||
| #include "SDL.h" | ||
| #include "SDL_system.h" | ||
|
|
||
| bool Speech_init = false; | ||
| static jclass j_game_class = nullptr; | ||
| static jmethodID tts_speak = nullptr; | ||
| static jmethodID tts_stop = nullptr; | ||
| static jmethodID tts_pause = nullptr; | ||
| static jmethodID tts_resume = nullptr; | ||
| static jmethodID tts_isSpeaking = nullptr; | ||
| static jmethodID tts_shutdown = nullptr; | ||
| static jmethodID tts_setRate = nullptr; | ||
| static jmethodID tts_setVoice = nullptr; | ||
| static jmethodID tts_getVoices = nullptr; | ||
|
|
||
| // Helper to get a static method from a java class and clear the exception | ||
| // if the method is not found. This is needed to avoid crashing on the next JNI request. | ||
| static jmethodID get_static_method(JNIEnv* e, jclass cls, const char* name, const char* sig) | ||
| { | ||
| jmethodID m = e->GetStaticMethodID(cls, name, sig); | ||
| if (e->ExceptionCheck()) { | ||
| e->ExceptionClear(); | ||
| m = nullptr; | ||
| mprintf(("Speech : Method: %s. Not found on GameActivity! Signature: %s. \n", name, sig)); | ||
| } | ||
| return m; | ||
| } | ||
|
|
||
| // Ask SDL for the JNI Environment and hook to | ||
| // the external TTSManager on the android side of things. | ||
| // Then assign all method IDs for later use. | ||
| bool speech_init() | ||
| { | ||
| Speech_init = false; | ||
| mprintf(("Speech : Try to init TTSManager on GameActivity...\n")); | ||
|
|
||
| // Get the JNI Environment pointer and current Activity instance via SDL | ||
| JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); | ||
| jobject activity = (jobject)SDL_AndroidGetActivity(); | ||
|
|
||
| if (env == nullptr) { | ||
| mprintf(("Speech : Unable to get JNI environment!\n")); | ||
| return false; | ||
| } | ||
|
|
||
| if (activity == nullptr) { | ||
| mprintf(("Speech : Unable to get SDL Android activity!\n")); | ||
| return false; | ||
| } | ||
|
|
||
| // GetObjectClass returns a local ref — promote to global so it survives | ||
| // across JNI calls made from different scopes/threads. | ||
| jclass local_class = env->GetObjectClass(activity); | ||
| env->DeleteLocalRef(activity); | ||
|
|
||
| if (local_class == nullptr) { | ||
| mprintf(("Speech : Unable to find the GameActivity class!\n")); | ||
| return false; | ||
| } | ||
|
|
||
| j_game_class = (jclass)env->NewGlobalRef(local_class); | ||
| env->DeleteLocalRef(local_class); | ||
|
|
||
| // Map all static methods from TTSManager | ||
| tts_speak = get_static_method (env, j_game_class, "tts_speak", "(Ljava/lang/String;)Z"); | ||
| tts_stop = get_static_method (env, j_game_class, "tts_stop", "()Z"); | ||
| tts_pause = get_static_method (env, j_game_class, "tts_pause", "()Z"); | ||
| tts_resume = get_static_method (env, j_game_class, "tts_resume", "()Z"); | ||
| tts_isSpeaking = get_static_method (env, j_game_class, "tts_isSpeaking", "()Z"); | ||
| tts_shutdown = get_static_method (env, j_game_class, "tts_shutdown", "()V"); | ||
| tts_setRate = get_static_method (env, j_game_class, "tts_setRate", "(F)V"); | ||
| tts_setVoice = get_static_method (env, j_game_class, "tts_setLanguageTag", "(Ljava/lang/String;)V"); | ||
| tts_getVoices = get_static_method (env, j_game_class, "tts_getAvailableLanguageTags", "()[Ljava/lang/String;"); | ||
|
|
||
| if (!tts_speak || !tts_stop || !tts_pause || !tts_resume || !tts_isSpeaking || !tts_shutdown || !tts_setRate || !tts_setVoice || !tts_getVoices) { | ||
| mprintf(("Speech : Unable to map at least one core TTS method to GameActivity!\n")); | ||
| env->DeleteGlobalRef(j_game_class); | ||
| j_game_class = nullptr; | ||
| return false; | ||
| } | ||
|
|
||
| mprintf(("Speech : Init Completed!\n")); | ||
| Speech_init = true; | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| bool speech_play(const SCP_string& text) | ||
| { | ||
| if (!Speech_init) | ||
| return false; | ||
|
|
||
| if (text.empty()) { | ||
| nprintf(("Speech", "Not playing speech because passed text is empty.\n")); | ||
| return false; | ||
| } | ||
|
|
||
| JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); | ||
| if (env == nullptr) { | ||
| mprintf(("Speech : Unable to get JNI environment!\n")); | ||
| return false; | ||
| } | ||
|
|
||
| jstring j_txt = env->NewStringUTF(text.c_str()); | ||
| nprintf(("Speech : Playing TTS string: %s!\n", text.c_str())); | ||
| jboolean ok = env->CallStaticBooleanMethod(j_game_class, tts_speak, j_txt); | ||
| env->DeleteLocalRef(j_txt); | ||
|
|
||
| if (ok != JNI_TRUE) { | ||
| mprintf(("Speech : Error playing TTS string!\n")); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool speech_stop() | ||
| { | ||
| if (!Speech_init) | ||
| return false; | ||
|
|
||
| JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); | ||
| if (env == nullptr) { | ||
| mprintf(("Speech : Unable to get JNI environment!\n")); | ||
| return false; | ||
| } | ||
|
|
||
| return env->CallStaticBooleanMethod(j_game_class, tts_stop) == JNI_TRUE; | ||
| } | ||
|
|
||
| bool speech_pause() | ||
| { | ||
| if (!Speech_init) | ||
| return false; | ||
|
|
||
| JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); | ||
| if (env == nullptr) { | ||
| mprintf(("Speech : Unable to get JNI environment!\n")); | ||
| return false; | ||
| } | ||
|
|
||
| return env->CallStaticBooleanMethod(j_game_class, tts_pause) == JNI_TRUE; | ||
| } | ||
|
|
||
| bool speech_resume() | ||
| { | ||
| if (!Speech_init) | ||
| return false; | ||
|
|
||
| JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); | ||
| if (env == nullptr) { | ||
| mprintf(("Speech : Unable to get JNI environment!\n")); | ||
| return false; | ||
| } | ||
|
|
||
| return env->CallStaticBooleanMethod(j_game_class, tts_resume) == JNI_TRUE; | ||
| } | ||
|
|
||
| bool speech_is_speaking() | ||
| { | ||
| if (!Speech_init) | ||
| return false; | ||
|
|
||
| JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); | ||
| if (env == nullptr) { | ||
| mprintf(("Speech : Unable to get JNI environment!\n")); | ||
| return false; | ||
| } | ||
|
|
||
| return env->CallStaticBooleanMethod(j_game_class, tts_isSpeaking) == JNI_TRUE; | ||
| } | ||
|
|
||
| void speech_deinit() | ||
| { | ||
| if (!Speech_init) | ||
| return; | ||
|
|
||
| JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); | ||
| if (env != nullptr) { | ||
| env->CallStaticVoidMethod(j_game_class, tts_shutdown); | ||
| env->DeleteGlobalRef(j_game_class); | ||
| } | ||
| j_game_class = nullptr; | ||
| tts_speak = nullptr; | ||
| tts_stop = nullptr; | ||
| tts_pause = nullptr; | ||
| tts_resume = nullptr; | ||
| tts_isSpeaking = nullptr; | ||
| tts_shutdown = nullptr; | ||
| tts_setRate = nullptr; | ||
| tts_setVoice = nullptr; | ||
| tts_getVoices = nullptr; | ||
| Speech_init = false; | ||
| } | ||
|
|
||
|
|
||
| // Android TTS does not expose a direct volume API. | ||
| // Volume is controlled by the STREAM_MUSIC channel at the OS level. | ||
| bool speech_set_volume(unsigned short /*volume*/) | ||
| { | ||
| if (!Speech_init) | ||
| return false; | ||
| return true; | ||
| } | ||
|
|
||
| bool speech_set_rate(float rate_percent) | ||
| { | ||
| if (!Speech_init) | ||
| return false; | ||
|
|
||
| if(rate_percent > 150.0) | ||
| rate_percent = 150.0; | ||
| else if(rate_percent < 50.0) | ||
| rate_percent = 50.0; | ||
|
|
||
| JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); | ||
| if (env == nullptr) { | ||
| mprintf(("Speech : Unable to get JNI environment!\n")); | ||
| return false; | ||
| } | ||
|
|
||
| float android_rate = rate_percent / 100.0f; // 0.5 .. 1.0 .. 1.5 | ||
| env->CallStaticVoidMethod(j_game_class, tts_setRate, (jfloat)android_rate); | ||
| return true; | ||
| } | ||
|
|
||
| bool speech_set_voice(int voice) | ||
| { | ||
| if (!Speech_init) | ||
| return false; | ||
|
|
||
| JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); | ||
| if (env == nullptr) { | ||
| mprintf(("Speech : Unable to get JNI environment!\n")); | ||
| return false; | ||
| } | ||
|
|
||
| jobjectArray tags = (jobjectArray)env->CallStaticObjectMethod(j_game_class, tts_getVoices); | ||
| if (tags == nullptr) | ||
| return false; | ||
|
|
||
| jsize count = env->GetArrayLength(tags); | ||
| if (voice < 0 || voice >= (int)count) { | ||
| env->DeleteLocalRef(tags); | ||
| return false; | ||
| } | ||
|
|
||
| jstring tag = (jstring)env->GetObjectArrayElement(tags, (jsize)voice); | ||
| env->CallStaticVoidMethod(j_game_class, tts_setVoice, tag); | ||
| env->DeleteLocalRef(tag); | ||
| env->DeleteLocalRef(tags); | ||
| return true; | ||
| } | ||
|
|
||
| SCP_vector<std::pair<int, SCP_string>> speech_enumerate_voices() | ||
| { | ||
| SCP_vector<std::pair<int, SCP_string>> voices; | ||
|
|
||
| if (!Speech_init) | ||
| return voices; | ||
|
|
||
| JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv(); | ||
| if (env == nullptr) { | ||
| mprintf(("Speech : Unable to get JNI environment!\n")); | ||
| return voices; | ||
| } | ||
|
|
||
| jobjectArray tags = (jobjectArray)env->CallStaticObjectMethod(j_game_class, tts_getVoices); | ||
| if (tags == nullptr) | ||
| return voices; | ||
|
|
||
| jsize count = env->GetArrayLength(tags); | ||
| voices.reserve((size_t)count); | ||
|
|
||
| for (jsize i = 0; i < count; ++i) { | ||
| jstring tag = (jstring)env->GetObjectArrayElement(tags, i); | ||
| if (tag == nullptr) | ||
| continue; | ||
|
|
||
| const char* raw = env->GetStringUTFChars(tag, nullptr); | ||
| if (raw) { | ||
| voices.emplace_back((int)i, SCP_string(raw)); | ||
| env->ReleaseStringUTFChars(tag, raw); | ||
| } | ||
| env->DeleteLocalRef(tag); | ||
| } | ||
|
|
||
| env->DeleteLocalRef(tags); | ||
| return voices; | ||
| } | ||
|
|
||
| #endif // __ANDROID__ | ||
| #endif // FS2_SPEECH |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.