From 0d7ba71769f1671cf9293bf8c7f15ef4644e7da9 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 2 Jun 2020 16:23:45 -0500 Subject: [PATCH] Various improvements. * Add ability to detect when speaking. Doesn't yet work under Linux or WinRT. * Add signal to indicate when speaking is done. * Refine screen reader detection under Android. --- TTS.gd | 41 +++++++++++++++++++ .../src/games/lightsout/godot/tts/TTS.java | 21 ++++++---- src/lib.rs | 26 +++++++++++- 3 files changed, 78 insertions(+), 10 deletions(-) diff --git a/TTS.gd b/TTS.gd index c339cbb..8b85dfb 100644 --- a/TTS.gd +++ b/TTS.gd @@ -5,6 +5,8 @@ var TTS var tts +signal done + func _ready(): if OS.get_name() == "Server" or OS.has_feature("JavaScript"): @@ -150,6 +152,32 @@ func get_is_rate_supported(): var is_rate_supported setget , get_is_rate_supported +func _get_can_detect_is_speaking(): + if Engine.has_singleton("AndroidTTS"): + return true + elif OS.has_feature('JavaScript'): + return true + elif tts != null: + return tts.can_detect_is_speaking + return false + + +var can_detect_is_speaking setget , _get_can_detect_is_speaking + + +func _get_is_speaking(): + if Engine.has_singleton("AndroidTTS"): + return tts.is_speaking() + elif OS.has_feature('JavaScript'): + return JavaScript.eval("window.speechSynthesis.speaking") + elif tts != null: + return tts.is_speaking + return false + + +var is_speaking setget , _get_is_speaking + + func _get_can_detect_screen_reader(): if Engine.has_singleton("AndroidTTS"): return true @@ -183,6 +211,19 @@ func singular_or_plural(count, singular, plural): return plural +var _was_speaking = false + + +func _process(delta): + if self.is_speaking: + print("xxx Speaking") + _was_speaking = true + elif _was_speaking: + print("xxx Done") + emit_signal("done") + _was_speaking = false + + func _exit_tree(): if not tts or not TTS: return diff --git a/android/src/games/lightsout/godot/tts/TTS.java b/android/src/games/lightsout/godot/tts/TTS.java index 6b1cc42..b35fa6f 100644 --- a/android/src/games/lightsout/godot/tts/TTS.java +++ b/android/src/games/lightsout/godot/tts/TTS.java @@ -2,18 +2,14 @@ package games.lightsout.godot.tts; import java.util.List; +import org.godotengine.godot.Godot; + import android.accessibilityservice.AccessibilityServiceInfo; import android.app.Activity; -import android.content.Intent; import android.content.Context; import android.speech.tts.TextToSpeech; -import android.util.Log; import android.view.accessibility.AccessibilityManager; -import com.godot.game.R; -import javax.microedition.khronos.opengles.GL10; -import org.godotengine.godot.Godot; - public class TTS extends Godot.SingletonBase implements TextToSpeech.OnInitListener { protected Activity appActivity; @@ -48,14 +44,20 @@ public class TTS extends Godot.SingletonBase implements TextToSpeech.OnInitListe tts.setSpeechRate(rate); } + public boolean is_speaking() { + return tts.isSpeaking(); + } + public boolean has_screen_reader() { AccessibilityManager accessibilityManager = (AccessibilityManager) appContext .getSystemService(Context.ACCESSIBILITY_SERVICE); if (accessibilityManager != null) { - List screenReaders =accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_SPOKEN); + List screenReaders = accessibilityManager + .getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_SPOKEN); return screenReaders.size() != 0; - } else + } else { return false; + } } public void getInstanceId(int pInstanceId) { @@ -74,7 +76,8 @@ public class TTS extends Godot.SingletonBase implements TextToSpeech.OnInitListe this.appContext = appActivity.getApplicationContext(); this.tts = new TextToSpeech(this.appContext, this); // Register class name and functions to bind. - registerClass("AndroidTTS", new String[] { "speak", "stop", "get_rate", "set_rate", "getInstanceId" }); + registerClass("AndroidTTS", new String[] { "speak", "stop", "get_rate", "set_rate", "has_screen_reader", + "is_speaking", "getInstanceId" }); this.activity.runOnUiThread(new Runnable() { public void run() { } diff --git a/src/lib.rs b/src/lib.rs index 850be5d..8a64578 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,11 +93,35 @@ impl TTS { #[cfg(windows)] { let tolk = tolk::Tolk::new(); - return tolk.detect_screen_reader().is_some() + return tolk.detect_screen_reader().is_some(); } return false; }) .done(); + builder + .add_property("can_detect_is_speaking") + .with_getter(|this: &TTS, _| { + let Features { + is_speaking: is_speaking_supported, + .. + } = this.0.supported_features(); + return is_speaking_supported; + }) + .done(); + builder + .add_property("is_speaking") + .with_getter(|this: &TTS, _| { + let Features { + is_speaking: is_speaking_supported, + .. + } = this.0.supported_features(); + if is_speaking_supported { + return this.0.is_speaking().unwrap(); + } else { + return false; + } + }) + .done(); } #[export]