mirror of https://github.com/ndarilek/tts-rs.git
Create list of supported features and check them before a method is called.
This commit is contained in:
parent
9724c5e087
commit
ac4ebea05a
|
@ -4,7 +4,7 @@ use std::u8;
|
||||||
use log::{info, trace};
|
use log::{info, trace};
|
||||||
use speech_dispatcher::*;
|
use speech_dispatcher::*;
|
||||||
|
|
||||||
use crate::{Backend, Error};
|
use crate::{Backend, Error, Features};
|
||||||
|
|
||||||
pub struct SpeechDispatcher(Connection);
|
pub struct SpeechDispatcher(Connection);
|
||||||
|
|
||||||
|
@ -29,6 +29,15 @@ fn i32_to_u8(v: i32) -> u8 {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Backend for SpeechDispatcher {
|
impl Backend for SpeechDispatcher {
|
||||||
|
fn supported_features(&self) -> Features {
|
||||||
|
Features {
|
||||||
|
stop: true,
|
||||||
|
rate: true,
|
||||||
|
pitch: true,
|
||||||
|
volume: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> {
|
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> {
|
||||||
trace!("speak({}, {})", text, interrupt);
|
trace!("speak({}, {})", text, interrupt);
|
||||||
if interrupt {
|
if interrupt {
|
||||||
|
|
|
@ -24,6 +24,15 @@ impl Web {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Backend for Web {
|
impl Backend for Web {
|
||||||
|
fn supported_features(&self) -> Features {
|
||||||
|
Features {
|
||||||
|
stop: true,
|
||||||
|
rate: true,
|
||||||
|
pitch: true,
|
||||||
|
volume: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> {
|
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> {
|
||||||
trace!("speak({}, {})", text, interrupt);
|
trace!("speak({}, {})", text, interrupt);
|
||||||
let utterance = SpeechSynthesisUtterance::new_with_text(text).unwrap();
|
let utterance = SpeechSynthesisUtterance::new_with_text(text).unwrap();
|
||||||
|
|
74
src/lib.rs
74
src/lib.rs
|
@ -37,7 +37,15 @@ impl convert::From<Error> for io::Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Backend {
|
pub struct Features {
|
||||||
|
stop: bool,
|
||||||
|
rate: bool,
|
||||||
|
pitch: bool,
|
||||||
|
volume: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Backend {
|
||||||
|
fn supported_features(&self) -> Features;
|
||||||
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error>;
|
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error>;
|
||||||
fn stop(&self) -> Result<(), Error>;
|
fn stop(&self) -> Result<(), Error>;
|
||||||
fn get_rate(&self) -> Result<u8, Error>;
|
fn get_rate(&self) -> Result<u8, Error>;
|
||||||
|
@ -78,6 +86,13 @@ impl TTS {
|
||||||
tts
|
tts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the features supported by this TTS engine
|
||||||
|
*/
|
||||||
|
pub fn supported_features(&self) -> Features {
|
||||||
|
self.0.supported_features()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Speaks the specified text, optionally interrupting current speech.
|
* Speaks the specified text, optionally interrupting current speech.
|
||||||
*/
|
*/
|
||||||
|
@ -90,52 +105,87 @@ impl TTS {
|
||||||
* Stops current speech.
|
* Stops current speech.
|
||||||
*/
|
*/
|
||||||
pub fn stop(&self) -> Result<&Self, Error> {
|
pub fn stop(&self) -> Result<&Self, Error> {
|
||||||
self.0.stop()?;
|
let Features { stop, .. } = self.supported_features();
|
||||||
Ok(self)
|
if stop {
|
||||||
|
self.0.stop()?;
|
||||||
|
Ok(self)
|
||||||
|
} else {
|
||||||
|
Err(Error("Feature not supported".to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the current speech rate.
|
* Gets the current speech rate.
|
||||||
*/
|
*/
|
||||||
pub fn get_rate(&self) -> Result<u8, Error> {
|
pub fn get_rate(&self) -> Result<u8, Error> {
|
||||||
self.0.get_rate()
|
let Features { rate, .. } = self.supported_features();
|
||||||
|
if rate {
|
||||||
|
self.0.get_rate()
|
||||||
|
} else {
|
||||||
|
Err(Error("Feature not supported".to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the desired speech rate.
|
* Sets the desired speech rate.
|
||||||
*/
|
*/
|
||||||
pub fn set_rate(&mut self, rate: u8) -> Result<&Self, Error> {
|
pub fn set_rate(&mut self, rate: u8) -> Result<&Self, Error> {
|
||||||
self.0.set_rate(rate)?;
|
let Features { rate: rate_feature, .. } = self.supported_features();
|
||||||
Ok(self)
|
if rate_feature {
|
||||||
|
self.0.set_rate(rate)?;
|
||||||
|
Ok(self)
|
||||||
|
} else {
|
||||||
|
Err(Error("Unsupported feature".to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the current speech pitch.
|
* Gets the current speech pitch.
|
||||||
*/
|
*/
|
||||||
pub fn get_pitch(&self) -> Result<u8, Error> {
|
pub fn get_pitch(&self) -> Result<u8, Error> {
|
||||||
self.0.get_pitch()
|
let Features { pitch, .. } = self.supported_features();
|
||||||
|
if pitch {
|
||||||
|
self.0.get_pitch()
|
||||||
|
} else {
|
||||||
|
Err(Error("Feature not supported".to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the desired speech pitch.
|
* Sets the desired speech pitch.
|
||||||
*/
|
*/
|
||||||
pub fn set_pitch(&mut self, pitch: u8) -> Result<&Self, Error> {
|
pub fn set_pitch(&mut self, pitch: u8) -> Result<&Self, Error> {
|
||||||
self.0.set_pitch(pitch)?;
|
let Features { pitch: pitch_feature, .. } = self.supported_features();
|
||||||
Ok(self)
|
if pitch_feature {
|
||||||
|
self.0.set_pitch(pitch)?;
|
||||||
|
Ok(self)
|
||||||
|
} else {
|
||||||
|
Err(Error("Unsupported feature".to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the current speech volume.
|
* Gets the current speech volume.
|
||||||
*/
|
*/
|
||||||
pub fn get_volume(&self) -> Result<u8, Error> {
|
pub fn get_volume(&self) -> Result<u8, Error> {
|
||||||
self.0.get_volume()
|
let Features { volume, .. } = self.supported_features();
|
||||||
|
if volume {
|
||||||
|
self.0.get_volume()
|
||||||
|
} else {
|
||||||
|
Err(Error("Unsupported feature".to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the desired speech volume.
|
* Sets the desired speech volume.
|
||||||
*/
|
*/
|
||||||
pub fn set_volume(&mut self, volume: u8) -> Result<&Self, Error> {
|
pub fn set_volume(&mut self, volume: u8) -> Result<&Self, Error> {
|
||||||
self.0.set_volume(volume)?;
|
let Features { volume: volume_feature, .. } = self.supported_features();
|
||||||
Ok(self)
|
if volume_feature {
|
||||||
|
self.0.set_volume(volume)?;
|
||||||
|
Ok(self)
|
||||||
|
} else {
|
||||||
|
Err(Error("Unsupported feature".to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue