godot-tts/src/lib.rs

330 lines
11 KiB
Rust
Raw Permalink Normal View History

2020-11-18 01:43:57 +00:00
use std::sync::mpsc::{channel, Receiver};
use gdnative::prelude::*;
use tts::{Features, Tts, UtteranceId};
2020-11-18 01:43:57 +00:00
#[derive(NativeClass)]
struct Utterance(pub(crate) Option<UtteranceId>);
#[methods]
impl Utterance {
fn new(_owner: &Reference) -> Self {
Self(None)
}
}
#[allow(clippy::enum_variant_names)]
enum Msg {
UtteranceBegin(UtteranceId),
UtteranceEnd(UtteranceId),
UtteranceStop(UtteranceId),
}
#[allow(clippy::upper_case_acronyms)]
2020-11-18 01:43:57 +00:00
#[derive(NativeClass)]
#[inherit(Node)]
#[register_with(Self::register)]
struct TTS(Tts, Receiver<Msg>);
#[methods]
impl TTS {
fn new(owner: &Node) -> Self {
owner.set_pause_mode(2);
2020-11-24 18:20:02 +00:00
let tts = Tts::default().expect("Failed to initialize TTS");
2020-11-18 01:43:57 +00:00
let (tx, rx) = channel();
let Features {
utterance_callbacks,
..
} = tts.supported_features();
if utterance_callbacks {
let tx_end = tx.clone();
let tx_stop = tx.clone();
tts.on_utterance_begin(Some(Box::new(move |utterance| {
2020-11-24 18:20:02 +00:00
tx.send(Msg::UtteranceBegin(utterance))
.expect("Failed to send UtteranceBegin");
2020-11-18 01:43:57 +00:00
})))
.expect("Failed to set utterance_begin callback");
tts.on_utterance_end(Some(Box::new(move |utterance| {
2020-11-24 18:20:02 +00:00
tx_end
.send(Msg::UtteranceEnd(utterance))
.expect("Failed to send UtteranceEnd");
2020-11-18 01:43:57 +00:00
})))
.expect("Failed to set utterance_end callback");
tts.on_utterance_stop(Some(Box::new(move |utterance| {
2020-11-24 18:20:02 +00:00
tx_stop
.send(Msg::UtteranceStop(utterance))
.expect("Failed to send UtteranceStop");
2020-11-18 01:43:57 +00:00
})))
.expect("Failed to set utterance_stop callback");
}
Self(tts, rx)
}
fn register(builder: &ClassBuilder<Self>) {
2022-03-12 20:42:20 +00:00
builder
.property("volume")
2022-03-12 20:42:20 +00:00
.with_getter(|this: &TTS, _| match this.0.get_volume() {
Ok(volume) => volume,
_ => 0.,
})
.with_setter(|this: &mut TTS, _, v: f32| {
let Features {
volume: volume_supported,
..
} = this.0.supported_features();
if volume_supported {
let mut v = v;
if v < this.0.min_volume() {
v = this.0.min_volume();
} else if v > this.0.max_volume() {
v = this.0.max_volume();
}
this.0.set_volume(v).expect("Failed to set volume");
}
})
.done();
builder
.property("min_volume")
2022-03-12 20:42:20 +00:00
.with_getter(|this: &TTS, _| {
let Features {
volume: volume_supported,
..
} = this.0.supported_features();
if volume_supported {
this.0.min_volume()
} else {
0.
}
})
.done();
builder
.property("max_volume")
2022-03-12 20:42:20 +00:00
.with_getter(|this: &TTS, _| {
let Features {
volume: volume_supported,
..
} = this.0.supported_features();
if volume_supported {
this.0.max_volume()
} else {
0.
}
})
.done();
builder
.property("normal_volume")
2022-03-12 20:42:20 +00:00
.with_getter(|this: &TTS, _| {
let Features {
volume: volume_supported,
..
} = this.0.supported_features();
if volume_supported {
this.0.normal_volume()
} else {
0.
}
})
.done();
2020-11-18 01:43:57 +00:00
builder
.property("rate")
2020-11-18 01:43:57 +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();
}
2020-11-24 18:20:02 +00:00
this.0.set_rate(v).expect("Failed to set rate");
2020-11-18 01:43:57 +00:00
}
})
.done();
builder
.property("min_rate")
2020-11-18 01:43:57 +00:00
.with_getter(|this: &TTS, _| {
let Features {
rate: rate_supported,
..
} = this.0.supported_features();
if rate_supported {
this.0.min_rate()
} else {
0.
}
})
.done();
builder
.property("max_rate")
2020-11-18 01:43:57 +00:00
.with_getter(|this: &TTS, _| {
let Features {
rate: rate_supported,
..
} = this.0.supported_features();
if rate_supported {
this.0.max_rate()
} else {
0.
}
})
.done();
builder
.property("normal_rate")
2020-11-18 01:43:57 +00:00
.with_getter(|this: &TTS, _| {
let Features {
rate: rate_supported,
..
} = this.0.supported_features();
if rate_supported {
this.0.normal_rate()
} else {
0.
}
})
.done();
builder
.property("can_detect_screen_reader")
.with_getter(|_: &TTS, _| cfg!(windows))
2020-11-18 01:43:57 +00:00
.done();
builder
.property("has_screen_reader")
.with_getter(|_, _| Tts::screen_reader_available())
2020-11-18 01:43:57 +00:00
.done();
builder
.property("can_detect_is_speaking")
2020-11-18 01:43:57 +00:00
.with_getter(|this: &TTS, _| {
let Features {
is_speaking: is_speaking_supported,
..
} = this.0.supported_features();
is_speaking_supported
})
.done();
builder
.property("is_speaking")
2020-11-18 01:43:57 +00:00
.with_getter(|this: &TTS, _| {
let Features {
is_speaking: is_speaking_supported,
..
} = this.0.supported_features();
if is_speaking_supported {
2020-11-24 18:20:02 +00:00
this.0
.is_speaking()
.expect("Failed to determine if speaking")
2020-11-18 01:43:57 +00:00
} else {
false
}
})
.done();
builder
.signal("utterance_begin")
.with_param_custom(SignalParam {
name: "utterance".into(),
2020-11-18 01:43:57 +00:00
default: Variant::default(),
export_info: ExportInfo::new(VariantType::Object),
usage: PropertyUsage::DEFAULT,
})
.done();
builder
.signal("utterance_end")
.with_param_custom(SignalParam {
name: "utterance".into(),
2020-11-18 01:43:57 +00:00
default: Variant::default(),
export_info: ExportInfo::new(VariantType::Object),
usage: PropertyUsage::DEFAULT,
})
.done();
builder
.signal("utterance_stop")
.with_param_custom(SignalParam {
name: "utterance".into(),
2020-11-18 01:43:57 +00:00
default: Variant::default(),
export_info: ExportInfo::new(VariantType::Object),
usage: PropertyUsage::DEFAULT,
})
.done();
2020-11-18 01:43:57 +00:00
}
#[method]
fn speak(&mut self, message: String, interrupt: bool) -> Variant {
2020-11-18 01:43:57 +00:00
if let Ok(id) = self.0.speak(message, interrupt) {
let utterance: Instance<Utterance, Unique> = Instance::new();
if id.is_some() {
utterance
.map_mut(|u, _| u.0 = id)
.expect("Failed to set utterance ID");
}
utterance.owned_to_variant()
} else {
Variant::default()
}
}
#[method]
fn stop(&mut self) {
2020-11-24 18:20:02 +00:00
self.0.stop().expect("Failed to stop");
2020-11-18 01:43:57 +00:00
}
#[method]
fn is_rate_supported(&mut self) -> bool {
2020-11-18 01:43:57 +00:00
let Features {
rate: rate_supported,
..
} = self.0.supported_features();
rate_supported
}
#[method]
fn are_utterance_callbacks_supported(&mut self) -> bool {
2020-11-18 01:43:57 +00:00
let Features {
utterance_callbacks: supported,
..
} = self.0.supported_features();
supported
}
#[method]
fn _process(&mut self, #[base] base: &Node, _delta: f32) {
2020-11-18 01:43:57 +00:00
if let Ok(msg) = self.1.try_recv() {
match msg {
Msg::UtteranceBegin(utterance_id) => {
let utterance: Instance<Utterance, Unique> = Instance::new();
utterance
.map_mut(|u, _| u.0 = Some(utterance_id))
.expect("Failed to set utterance ID");
base.emit_signal("utterance_begin", &[utterance.owned_to_variant()]);
2020-11-18 01:43:57 +00:00
}
Msg::UtteranceEnd(utterance_id) => {
let utterance: Instance<Utterance, Unique> = Instance::new();
utterance
.map_mut(|u, _| u.0 = Some(utterance_id))
.expect("Failed to set utterance ID");
base.emit_signal("utterance_end", &[utterance.owned_to_variant()]);
2020-11-18 01:43:57 +00:00
}
Msg::UtteranceStop(utterance_id) => {
let utterance: Instance<Utterance, Unique> = Instance::new();
utterance
.map_mut(|u, _| u.0 = Some(utterance_id))
.expect("Failed to set utterance ID");
base.emit_signal("utterance_stop", &[utterance.owned_to_variant()]);
2020-11-18 01:43:57 +00:00
}
}
}
}
}
fn init(handle: InitHandle) {
env_logger::init();
handle.add_tool_class::<Utterance>();
handle.add_class::<TTS>();
}
godot_init!(init);