implement set_voice for AVFoundation backend

- TODO: test the implementation
- fixed: set_voice mutability of self parameter
This commit is contained in:
François Caddet 2020-09-03 18:40:32 +02:00
parent 5b0d1b6621
commit 6ed94686f3
4 changed files with 15 additions and 7 deletions

View File

@ -204,7 +204,7 @@ impl Backend for AppKit {
unimplemented!() unimplemented!()
} }
fn set_voice(&self, voice: String) -> Result<(),Error> { fn set_voice(&mut self, voice: String) -> Result<(),Error> {
unimplemented!() unimplemented!()
} }
} }

View File

@ -29,7 +29,7 @@ impl AvFoundation {
rate: 0.5, rate: 0.5,
volume: 1., volume: 1.,
pitch: 1., pitch: 1.,
voice: AVSpeechSynthesisVoice::new(), voice: AVSpeechSynthesisVoice::default(),
} }
} }
} }
@ -59,6 +59,7 @@ impl Backend for AvFoundation {
let _: () = msg_send![utterance, setRate: self.rate]; let _: () = msg_send![utterance, setRate: self.rate];
let _: () = msg_send![utterance, setVolume: self.volume]; let _: () = msg_send![utterance, setVolume: self.volume];
let _: () = msg_send![utterance, setPitchMultiplier: self.pitch]; let _: () = msg_send![utterance, setPitchMultiplier: self.pitch];
let _: () = msg_send![utterance, setVoice: self.voice];
let _: () = msg_send![self.synth, speakUtterance: utterance]; let _: () = msg_send![self.synth, speakUtterance: utterance];
} }
Ok(()) Ok(())
@ -149,7 +150,8 @@ impl Backend for AvFoundation {
AVSpeechSynthesisVoice::list().iter().map(|v| {v.identifier()}).collect() AVSpeechSynthesisVoice::list().iter().map(|v| {v.identifier()}).collect()
} }
fn set_voice(&self, voice: String) -> Result<(),Error> { fn set_voice(&mut self, voice: String) -> Result<(),Error> {
self.voice = AVSpeechSynthesisVoice::new(voice);
Ok(()) Ok(())
} }
} }

View File

@ -8,8 +8,14 @@ use core_foundation::string::CFString;
pub struct AVSpeechSynthesisVoice(*const Object); pub struct AVSpeechSynthesisVoice(*const Object);
impl AVSpeechSynthesisVoice { impl AVSpeechSynthesisVoice {
pub fn new() -> Self { pub fn new(identifier: String) -> Self {
Self::list()[0] let i: CFString = CFString::from(identifier.as_str());
let voice: *const Object = unsafe{msg_send![class!(AVSpeechSynthesisVoice).metaclass(), voiceWithIdentifier: i]};
AVSpeechSynthesisVoice{0: voice}
}
pub fn default() -> Self {
AVSpeechSynthesisVoice::list()[0]
} }
pub fn list() -> Vec<Self> { pub fn list() -> Vec<Self> {

View File

@ -102,7 +102,7 @@ pub trait Backend {
fn is_speaking(&self) -> Result<bool, Error>; fn is_speaking(&self) -> Result<bool, Error>;
fn voice(&self) -> Result<String, Error>; fn voice(&self) -> Result<String, Error>;
fn list_voices(&self) -> Vec<String>; fn list_voices(&self) -> Vec<String>;
fn set_voice(&self, voice: String) -> Result<(),Error>; fn set_voice(&mut self, voice: String) -> Result<(),Error>;
} }
pub struct TTS(Box<dyn Backend>); pub struct TTS(Box<dyn Backend>);
@ -399,7 +399,7 @@ impl TTS {
/** /**
* Set speaking voice. * Set speaking voice.
*/ */
pub fn set_voice(&self, voice: String) -> Result<(),Error> { pub fn set_voice(&mut self, voice: String) -> Result<(),Error> {
let Features { let Features {
voices: voices_feature, voices: voices_feature,
.. ..