commit 7f32fee73e659ad2d1c2fca3c1f53ab877e42da2 Author: Nolan Darilek Date: Thu Dec 3 15:39:35 2020 -0600 Initial commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b354aec --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target/ \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..620ba33 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "bevy_tts" +version = "0.1.0" +authors = ["Nolan Darilek "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[features] + +use_tolk = ["tts/use_tolk"] + +[dependencies] + +bevy = { git = "https://github.com/bevyengine/bevy" } +tts = { version = "0.12", features = ["use_tolk"] } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..2b0641f --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,55 @@ +use std::sync::mpsc::{channel, Receiver}; + +use bevy::prelude::*; +pub use tts::{Features, UtteranceId, TTS}; + +#[derive(Clone, Copy, Debug)] +pub enum TtsEvent { + UtteranceBegin(UtteranceId), + UtteranceEnd(UtteranceId), + UtteranceStop(UtteranceId), +} + +struct TtsChannel(Receiver); + +fn poll_callbacks_system(_: &mut World, resources: &mut Resources) { + let channel = resources.get_thread_local::().unwrap(); + if let Ok(msg) = channel.0.try_recv() { + let mut events = resources.get_mut::>().unwrap(); + events.send(msg); + } +} + +pub struct TtsPlugin; + +impl Plugin for TtsPlugin { + fn build(&self, app: &mut AppBuilder) { + let tts = TTS::default().unwrap(); + let (tx, rx) = channel(); + let tx_begin = tx.clone(); + let tx_end = tx.clone(); + let tx_stop = tx; + let Features { + utterance_callbacks, + .. + } = tts.supported_features(); + if utterance_callbacks { + tts.on_utterance_begin(Some(Box::new(move |utterance| { + tx_begin.send(TtsEvent::UtteranceBegin(utterance)).unwrap(); + }))) + .unwrap(); + tts.on_utterance_end(Some(Box::new(move |utterance| { + tx_end.send(TtsEvent::UtteranceEnd(utterance)).unwrap(); + }))) + .unwrap(); + tts.on_utterance_stop(Some(Box::new(move |utterance| { + tx_stop.send(TtsEvent::UtteranceStop(utterance)).unwrap(); + }))) + .unwrap(); + } + app.add_event::() + .add_thread_local_resource(TtsChannel(rx)) + .add_resource(tts) + .add_system(poll_callbacks_system); + } +}