mirror of
https://github.com/lightsoutgames/godot-tts
synced 2024-11-22 09:55:56 +00:00
Bridge TTS callbacks to signals.
This commit is contained in:
parent
0c9efa5c7b
commit
a71bf417b6
21
TTS.gd
21
TTS.gd
|
@ -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():
|
||||
|
|
27
src/lib.rs
27
src/lib.rs
|
@ -15,18 +15,25 @@ 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 tx_end = tx.clone();
|
||||
tts.on_utterance_begin(Some(Box::new(move |utterance| {
|
||||
tx.send(Msg::UtteranceBegin(utterance)).unwrap();
|
||||
})))
|
||||
.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");
|
||||
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();
|
||||
})))
|
||||
.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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user