Bridge TTS callbacks to signals.

This commit is contained in:
Nolan Darilek 2020-09-29 11:41:00 -05:00
parent 0c9efa5c7b
commit a71bf417b6
2 changed files with 29 additions and 19 deletions

21
TTS.gd
View File

@ -1,12 +1,14 @@
tool
extends Node
signal utterance_begin
signal utterance_end
var TTS
var tts
signal done
func _init():
if OS.get_name() == "Server" or OS.has_feature("JavaScript"):
@ -17,6 +19,10 @@ func _init():
TTS = preload("godot-tts.gdns")
if TTS and (TTS.can_instance() or Engine.editor_hint):
tts = TTS.new()
self.add_child(tts)
if self.are_utterance_callbacks_supported:
tts.connect("utterance_begin", self, "_on_utterance_begin")
tts.connect("utterance_end", self, "_on_utterance_end")
else:
print_debug("TTS not available!")
@ -229,15 +235,12 @@ func singular_or_plural(count, singular, plural):
return plural
var _was_speaking = false
func _on_utterance_begin():
emit_signal("utterance_begin")
func _process(delta):
if self.is_speaking:
_was_speaking = true
elif _was_speaking:
emit_signal("done")
_was_speaking = false
func _on_utterance_end():
emit_signal("utterance_end")
func _exit_tree():

View File

@ -15,9 +15,15 @@ struct TTS(Tts, Receiver<Msg>);
#[methods]
impl TTS {
fn new(_owner: &Node) -> Self {
fn new(owner: &Node) -> Self {
owner.set_pause_mode(2);
let tts = Tts::default().unwrap();
let (tx, rx) = channel();
let Features {
utterance_callbacks,
..
} = tts.supported_features();
if utterance_callbacks {
let tx_end = tx.clone();
tts.on_utterance_begin(Some(Box::new(move |utterance| {
tx.send(Msg::UtteranceBegin(utterance)).unwrap();
@ -27,6 +33,7 @@ impl TTS {
tx_end.send(Msg::UtteranceEnd(utterance)).unwrap();
})))
.expect("Failed to set utterance_end callback");
}
Self(tts, rx)
}