1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-17 12:19:37 +00:00

Implement support for detecting when TTS is speaking.

This commit is contained in:
Nolan Darilek 2020-06-02 14:53:14 -05:00
parent 026bcd0a0d
commit c24c1d3230
5 changed files with 46 additions and 0 deletions

View File

@ -21,6 +21,7 @@ impl Backend for SpeechDispatcher {
rate: true,
pitch: true,
volume: true,
is_speaking: false,
}
}
@ -108,4 +109,8 @@ impl Backend for SpeechDispatcher {
self.0.set_volume(volume as i32);
Ok(())
}
fn is_speaking(&self) -> Result<bool, Error> {
unimplemented!()
}
}

View File

@ -22,6 +22,7 @@ impl Backend for Tolk {
rate: false,
pitch: false,
volume: false,
is_speaking: false,
}
}
@ -96,4 +97,8 @@ impl Backend for Tolk {
fn set_volume(&mut self, _volume: f32) -> Result<(), Error> {
unimplemented!();
}
fn is_speaking(&self) -> Result<bool, Error> {
unimplemented!()
}
}

View File

@ -28,6 +28,7 @@ impl Backend for Web {
rate: true,
pitch: true,
volume: true,
is_speaking: true,
}
}
@ -118,4 +119,16 @@ impl Backend for Web {
self.volume = volume;
Ok(())
}
fn is_speaking(&self) -> Result<bool, Error> {
trace!("is_speaking()");
if let Some(window) = web_sys::window() {
match window.speech_synthesis() {
Ok(speech_synthesis) => Ok(speech_synthesis.speaking()),
Err(e) => Err(Error::JavaScriptError(e)),
}
} else {
Err(Error::NoneError)
}
}
}

View File

@ -51,6 +51,7 @@ impl Backend for WinRT {
rate: true,
pitch: true,
volume: true,
is_speaking: false,
}
}
@ -136,4 +137,8 @@ impl Backend for WinRT {
self.synth.options()?.set_audio_volume(volume.into())?;
Ok(())
}
fn is_speaking(&self) -> Result<bool, Error> {
unimplemented!()
}
}

View File

@ -28,12 +28,17 @@ pub struct Features {
pub rate: bool,
pub pitch: bool,
pub volume: bool,
pub is_speaking: bool,
}
#[derive(Debug, Error)]
pub enum Error {
#[error("IO error: {0}")]
IO(#[from] std::io::Error),
#[error("Value not received")]
NoneError,
#[error("JavaScript error: [0])]")]
JavaScriptError(wasm_bindgen::JsValue),
#[cfg(windows)]
#[error("WinRT error")]
WinRT(winrt::Error),
@ -62,6 +67,7 @@ pub trait Backend {
fn normal_volume(&self) -> f32;
fn get_volume(&self) -> Result<f32, Error>;
fn set_volume(&mut self, volume: f32) -> Result<(), Error>;
fn is_speaking(&self) -> Result<bool, Error>;
}
pub struct TTS(Box<dyn Backend>);
@ -298,4 +304,16 @@ impl TTS {
Err(Error::UnsupportedFeature)
}
}
/**
* Returns whether this speech synthesizer is speaking.
*/
pub fn is_speaking(&self) -> Result<bool, Error> {
let Features { is_speaking, .. } = self.supported_features();
if is_speaking {
self.0.is_speaking()
} else {
Err(Error::UnsupportedFeature)
}
}
}