From 51cd84a6cd830e20de47a9d7b2560182aee55e0d Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 30 Mar 2022 18:38:25 -0500 Subject: [PATCH] Support setting voice with Speech Dispatcher, and clarify features to indicate where getting current voice isn't supported. --- src/backends/speech_dispatcher.rs | 13 ++++++++++--- src/lib.rs | 24 +++++++++++++++--------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/backends/speech_dispatcher.rs b/src/backends/speech_dispatcher.rs index f3581d5..23e1e32 100644 --- a/src/backends/speech_dispatcher.rs +++ b/src/backends/speech_dispatcher.rs @@ -82,7 +82,8 @@ impl Backend for SpeechDispatcher { pitch: true, volume: true, is_speaking: true, - voices: false, + voice: true, + get_voice: false, utterance_callbacks: true, } } @@ -201,8 +202,14 @@ impl Backend for SpeechDispatcher { unimplemented!() } - fn set_voice(&mut self, voice: &str) -> Result<(), Error> { - unimplemented!() + fn set_voice(&mut self, voice: &Voice) -> Result<(), Error> { + for v in self.0.list_synthesis_voices()? { + if v.name == voice.name { + self.0.set_synthesis_voice(&v)?; + return Ok(()); + } + } + Err(Error::OperationFailed) } } diff --git a/src/lib.rs b/src/lib.rs index 256a608..ee86440 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -166,7 +166,8 @@ pub struct Features { pub rate: bool, pub stop: bool, pub utterance_callbacks: bool, - pub voices: bool, + pub voice: bool, + pub get_voice: bool, pub volume: bool, } @@ -232,7 +233,7 @@ pub trait Backend: Clone { fn is_speaking(&self) -> Result; fn voices(&self) -> Result, Error>; fn voice(&self) -> Result; - fn set_voice(&mut self, voice: &str) -> Result<(), Error>; + fn set_voice(&mut self, voice: &Voice) -> Result<(), Error>; } #[derive(Default)] @@ -565,15 +566,20 @@ impl Tts { * Returns list of available voices. */ pub fn voices(&self) -> Result, Error> { - self.0.read().unwrap().voices() + let Features { voice, .. } = self.supported_features(); + if voice { + self.0.read().unwrap().voices() + } else { + Err(Error::UnsupportedFeature) + } } /** * Return the current speaking voice. */ pub fn voice(&self) -> Result { - let Features { voices, .. } = self.supported_features(); - if voices { + let Features { get_voice, .. } = self.supported_features(); + if get_voice { self.0.read().unwrap().voice() } else { Err(Error::UnsupportedFeature) @@ -583,13 +589,13 @@ impl Tts { /** * Set speaking voice. */ - pub fn set_voice>(&mut self, voice: S) -> Result<(), Error> { + pub fn set_voice(&mut self, voice: &Voice) -> Result<(), Error> { let Features { - voices: voices_feature, + voice: voice_feature, .. } = self.supported_features(); - if voices_feature { - self.0.write().unwrap().set_voice(voice.into().as_str()) + if voice_feature { + self.0.write().unwrap().set_voice(voice) } else { Err(Error::UnsupportedFeature) }