Support setting voice with Speech Dispatcher, and clarify features to indicate where getting current voice isn't supported.

This commit is contained in:
Nolan Darilek 2022-03-30 18:38:25 -05:00
parent 142f2e6b3a
commit 51cd84a6cd
2 changed files with 25 additions and 12 deletions

View File

@ -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)
}
}

View File

@ -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<bool, Error>;
fn voices(&self) -> Result<Vec<Voice>, Error>;
fn voice(&self) -> Result<String, Error>;
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<Vec<Voice>, Error> {
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<String, Error> {
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<S: Into<String>>(&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)
}