From 6b023c3071a4853b8fa49341215877f6b4c68b82 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 22 Sep 2020 14:08:19 -0500 Subject: [PATCH] Add AV Foundation support for returning utterance IDs. --- src/backends/appkit.rs | 6 +++--- src/backends/av_foundation.rs | 11 +++++++---- src/lib.rs | 2 ++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/backends/appkit.rs b/src/backends/appkit.rs index 8584188..4b9a089 100644 --- a/src/backends/appkit.rs +++ b/src/backends/appkit.rs @@ -7,7 +7,7 @@ use objc::declare::ClassDecl; use objc::runtime::*; use objc::*; -use crate::{Backend, Error, Features}; +use crate::{Backend, Error, Features, UtteranceId}; pub struct AppKit(*mut Object, *mut Object); @@ -101,7 +101,7 @@ impl Backend for AppKit { } } - fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error> { + fn speak(&mut self, text: &str, interrupt: bool) -> Result, Error> { trace!("speak({}, {})", text, interrupt); if interrupt { self.stop()?; @@ -110,7 +110,7 @@ impl Backend for AppKit { let str = NSString::alloc(nil).init_str(text); let _: () = msg_send![self.1, enqueueAndSpeak: str]; } - Ok(()) + Ok(None) } fn stop(&mut self) -> Result<(), Error> { diff --git a/src/backends/av_foundation.rs b/src/backends/av_foundation.rs index a04eaea..fdd9cd7 100644 --- a/src/backends/av_foundation.rs +++ b/src/backends/av_foundation.rs @@ -1,12 +1,14 @@ #[cfg(any(target_os = "macos", target_os = "ios"))] #[link(name = "AVFoundation", kind = "framework")] +use std::sync::Mutex; + use cocoa_foundation::base::{id, nil}; use cocoa_foundation::foundation::NSString; use log::{info, trace}; use objc::runtime::*; use objc::*; -use crate::{Backend, Error, Features}; +use crate::{Backend, Error, Features, UtteranceId}; pub struct AvFoundation { synth: *mut Object, @@ -41,21 +43,22 @@ impl Backend for AvFoundation { } } - fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error> { + fn speak(&mut self, text: &str, interrupt: bool) -> Result, Error> { trace!("speak({}, {})", text, interrupt); if interrupt { self.stop()?; } + let utterance: id; unsafe { let str = NSString::alloc(nil).init_str(text); - let utterance: id = msg_send![class!(AVSpeechUtterance), alloc]; + utterance = msg_send![class!(AVSpeechUtterance), alloc]; let _: () = msg_send![utterance, initWithString: str]; let _: () = msg_send![utterance, setRate: self.rate]; let _: () = msg_send![utterance, setVolume: self.volume]; let _: () = msg_send![utterance, setPitchMultiplier: self.pitch]; let _: () = msg_send![self.synth, speakUtterance: utterance]; } - Ok(()) + Ok(Some(UtteranceId::AvFoundation(utterance))) } fn stop(&mut self) -> Result<(), Error> { diff --git a/src/lib.rs b/src/lib.rs index 2d494cf..c8166a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,6 +48,8 @@ pub enum UtteranceId { Web(u64), #[cfg(windows)] WinRT(u64), + #[cfg(any(target_os = "macos", target_os = "ios"))] + AvFoundation(id), } pub struct Features {