From 6ed94686f3acccc095a1157b230693212a8e7e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Caddet?= Date: Thu, 3 Sep 2020 18:40:32 +0200 Subject: [PATCH] implement set_voice for AVFoundation backend - TODO: test the implementation - fixed: set_voice mutability of self parameter --- src/backends/appkit.rs | 2 +- src/backends/av_foundation.rs | 6 ++++-- src/backends/av_foundation/voices.rs | 10 ++++++++-- src/lib.rs | 4 ++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/backends/appkit.rs b/src/backends/appkit.rs index ec32729..164cf53 100644 --- a/src/backends/appkit.rs +++ b/src/backends/appkit.rs @@ -204,7 +204,7 @@ impl Backend for AppKit { unimplemented!() } - fn set_voice(&self, voice: String) -> Result<(),Error> { + fn set_voice(&mut self, voice: String) -> Result<(),Error> { unimplemented!() } } diff --git a/src/backends/av_foundation.rs b/src/backends/av_foundation.rs index 70e3f33..c51988a 100644 --- a/src/backends/av_foundation.rs +++ b/src/backends/av_foundation.rs @@ -29,7 +29,7 @@ impl AvFoundation { rate: 0.5, volume: 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, setVolume: self.volume]; let _: () = msg_send![utterance, setPitchMultiplier: self.pitch]; + let _: () = msg_send![utterance, setVoice: self.voice]; let _: () = msg_send![self.synth, speakUtterance: utterance]; } Ok(()) @@ -149,7 +150,8 @@ impl Backend for AvFoundation { 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(()) } } diff --git a/src/backends/av_foundation/voices.rs b/src/backends/av_foundation/voices.rs index 98c6036..a23c14a 100644 --- a/src/backends/av_foundation/voices.rs +++ b/src/backends/av_foundation/voices.rs @@ -8,8 +8,14 @@ use core_foundation::string::CFString; pub struct AVSpeechSynthesisVoice(*const Object); impl AVSpeechSynthesisVoice { - pub fn new() -> Self { - Self::list()[0] + pub fn new(identifier: String) -> Self { + 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 { diff --git a/src/lib.rs b/src/lib.rs index af6db3f..61a47ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,7 +102,7 @@ pub trait Backend { fn is_speaking(&self) -> Result; fn voice(&self) -> Result; fn list_voices(&self) -> Vec; - fn set_voice(&self, voice: String) -> Result<(),Error>; + fn set_voice(&mut self, voice: String) -> Result<(),Error>; } pub struct TTS(Box); @@ -399,7 +399,7 @@ impl TTS { /** * Set speaking voice. */ - pub fn set_voice(&self, voice: String) -> Result<(),Error> { + pub fn set_voice(&mut self, voice: String) -> Result<(),Error> { let Features { voices: voices_feature, ..