godot-tts/src/lib.rs

200 lines
5.8 KiB
Rust
Raw Normal View History

use std::sync::mpsc::{channel, Receiver};
use gdnative::prelude::*;
use tts::{Features, UtteranceId, TTS as Tts};
enum Msg {
UtteranceBegin(UtteranceId),
UtteranceEnd(UtteranceId),
}
2018-06-13 12:14:03 +00:00
2020-03-19 18:16:56 +00:00
#[derive(NativeClass)]
#[inherit(Node)]
#[register_with(Self::register)]
struct TTS(Tts, Receiver<Msg>);
2020-03-19 18:16:56 +00:00
#[methods]
impl TTS {
fn new(_owner: &Node) -> Self {
2020-03-19 18:16:56 +00:00
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");
Self(tts, rx)
2019-09-27 18:03:19 +00:00
}
fn register(builder: &ClassBuilder<Self>) {
2020-03-19 18:16:56 +00:00
builder
.add_property("rate")
2020-03-19 18:16:56 +00:00
.with_getter(|this: &TTS, _| match this.0.get_rate() {
Ok(rate) => rate,
_ => 0.,
})
.with_setter(|this: &mut TTS, _, v: f32| {
let Features {
rate: rate_supported,
..
} = this.0.supported_features();
if rate_supported {
let mut v = v;
if v < this.0.min_rate() {
v = this.0.min_rate();
} else if v > this.0.max_rate() {
v = this.0.max_rate();
}
this.0.set_rate(v).unwrap();
}
})
.done();
builder
.add_property("min_rate")
.with_getter(|this: &TTS, _| {
let Features {
rate: rate_supported,
..
} = this.0.supported_features();
if rate_supported {
this.0.min_rate()
} else {
0.
}
2020-03-19 18:16:56 +00:00
})
.done();
builder
.add_property("max_rate")
.with_getter(|this: &TTS, _| {
let Features {
rate: rate_supported,
..
} = this.0.supported_features();
if rate_supported {
this.0.max_rate()
} else {
0.
}
})
.done();
builder
.add_property("normal_rate")
.with_getter(|this: &TTS, _| {
let Features {
2020-03-19 18:16:56 +00:00
rate: rate_supported,
..
} = this.0.supported_features();
if rate_supported {
this.0.normal_rate()
} else {
0.
}
2020-03-19 18:16:56 +00:00
})
.done();
builder
.add_property("can_detect_screen_reader")
.with_getter(|_: &TTS, _| if cfg!(windows) { true } else { false })
.done();
#[allow(unreachable_code)]
builder
.add_property("has_screen_reader")
.with_getter(|_: &TTS, _| {
#[cfg(windows)]
{
let tolk = tolk::Tolk::new();
return tolk.detect_screen_reader().is_some();
}
2020-06-11 18:11:09 +00:00
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();
builder.add_signal(Signal {
name: "utterance_begin",
args: &[],
});
builder.add_signal(Signal {
name: "utterance_end",
args: &[],
});
}
2018-06-13 12:14:03 +00:00
#[export]
fn speak(&mut self, _owner: &Node, message: GodotString, interrupt: bool) {
let message = message.to_string();
2019-09-10 15:09:04 +00:00
self.0.speak(message, interrupt).unwrap();
}
2018-06-13 12:14:03 +00:00
#[export]
fn stop(&mut self, _owner: &Node) {
2019-09-10 15:09:04 +00:00
self.0.stop().unwrap();
2018-06-13 12:14:03 +00:00
}
#[export]
fn is_rate_supported(&mut self, _owner: &Node) -> bool {
let Features {
2020-03-19 18:16:56 +00:00
rate: rate_supported,
..
} = self.0.supported_features();
rate_supported
}
#[export]
fn are_utterance_callbacks_supported(&mut self, _owner: &Node) -> bool {
let Features {
utterance_callbacks: supported,
..
} = self.0.supported_features();
supported
}
#[export]
fn _process(&mut self, owner: &Node, _delta: f32) {
if let Some(msg) = self.1.try_recv().ok() {
match msg {
Msg::UtteranceBegin(_utterance) => {
owner.emit_signal("utterance_begin", &[]);
}
Msg::UtteranceEnd(_utterance) => {
owner.emit_signal("utterance_end", &[]);
}
}
}
}
2018-06-13 12:14:03 +00:00
}
fn init(handle: InitHandle) {
2020-06-11 18:12:49 +00:00
env_logger::init();
handle.add_class::<TTS>();
2018-06-13 12:14:03 +00:00
}
godot_gdnative_init!();
godot_nativescript_init!(init);
godot_gdnative_terminate!();