tts-rs/src/lib.rs

108 lines
2.2 KiB
Rust
Raw Normal View History

2018-12-28 15:39:50 +00:00
/*!
* a Text-To-Speech (TTS) library providing high-level interfaces to a variety of backends.
* Currently supported backends are [Speech Dispatcher](https://freebsoft.org/speechd) (Linux).
*/
2018-12-14 19:35:49 +00:00
use std::boxed::Box;
mod backends;
pub enum Backends {
#[cfg(target_os = "linux")]
SpeechDispatcher,
}
trait Backend {
fn speak(&self, text: &str, interrupt: bool);
2018-12-28 14:49:02 +00:00
fn stop(&self);
2018-12-14 19:35:49 +00:00
fn get_rate(&self) -> u8;
fn set_rate(&self, rate: u8);
2018-12-15 15:56:13 +00:00
fn get_pitch(&self) -> u8;
fn set_pitch(&self, pitch: u8);
fn get_volume(&self) -> u8;
fn set_volume(&self, volume: u8);
2018-12-14 19:35:49 +00:00
}
pub struct TTS(Box<Backend>);
impl TTS {
2018-12-28 15:39:50 +00:00
/**
* Create a new `TTS` instance with the specified backend.
*/
2018-12-14 19:35:49 +00:00
pub fn new(backend: Backends) -> TTS {
match backend {
#[cfg(target_os = "linux")]
Backends::SpeechDispatcher => TTS(Box::new(backends::SpeechDispatcher::new())),
}
}
2018-12-28 15:39:50 +00:00
/**
* Speaks the specified text, optionally interrupting current speech.
*/
2018-12-14 19:35:49 +00:00
pub fn speak<S: Into<String>>(&self, text: S, interrupt: bool) -> &Self {
self.0.speak(text.into().as_str(), interrupt);
self
}
2018-12-28 15:39:50 +00:00
/**
* Stops current speech.
*/
2018-12-28 14:49:02 +00:00
pub fn stop(&self) -> &Self {
self.0.stop();
self
}
2018-12-28 15:39:50 +00:00
/**
* Gets the current speech rate.
*/
2018-12-14 19:35:49 +00:00
pub fn get_rate(&self) -> u8 {
self.0.get_rate()
}
2018-12-28 15:39:50 +00:00
/**
* Sets the desired speech rate.
*/
2018-12-14 19:35:49 +00:00
pub fn set_rate(&self, rate: u8) -> &Self {
self.0.set_rate(rate);
self
}
2018-12-15 15:56:13 +00:00
2018-12-28 15:39:50 +00:00
/**
* Gets the current speech pitch.
*/
2018-12-15 15:56:13 +00:00
pub fn get_pitch(&self) -> u8 {
self.0.get_pitch()
}
2018-12-28 15:39:50 +00:00
/**
* Sets the desired speech pitch.
*/
2018-12-15 15:56:13 +00:00
pub fn set_pitch(&self, pitch: u8) -> &Self {
self.0.set_pitch(pitch);
self
}
2018-12-28 15:39:50 +00:00
/**
* Gets the current speech volume.
*/
2018-12-15 15:56:13 +00:00
pub fn get_volume(&self) -> u8 {
self.0.get_volume()
}
2018-12-28 15:39:50 +00:00
/**
* Sets the desired speech volume.
*/
2018-12-15 15:56:13 +00:00
pub fn set_volume(&self, volume: u8) -> &Self {
self.0.set_volume(volume);
self
}
2018-12-14 19:35:49 +00:00
}
impl Default for TTS {
fn default() -> TTS {
#[cfg(target_os = "linux")]
TTS::new(Backends::SpeechDispatcher)
}
}