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

View File

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