From bd57075d53fcce210a6411ebb8d4d0b0e937a56c Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 23 Sep 2020 11:28:56 -0500 Subject: [PATCH] Implement unused framework for AVFoundation callbacks. --- src/backends/appkit.rs | 10 +++++++--- src/backends/av_foundation.rs | 27 ++++++++++++++++++++++----- src/backends/mod.rs | 4 ++-- src/lib.rs | 4 +++- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/backends/appkit.rs b/src/backends/appkit.rs index 4b9a089..647cc3f 100644 --- a/src/backends/appkit.rs +++ b/src/backends/appkit.rs @@ -7,12 +7,12 @@ use objc::declare::ClassDecl; use objc::runtime::*; use objc::*; -use crate::{Backend, Error, Features, UtteranceId}; +use crate::{Backend, BackendId, Error, Features, UtteranceId}; -pub struct AppKit(*mut Object, *mut Object); +pub(crate) struct AppKit(*mut Object, *mut Object); impl AppKit { - pub fn new() -> Self { + pub(crate) fn new() -> Self { info!("Initializing AppKit backend"); unsafe { let obj: *mut Object = msg_send![class!(NSSpeechSynthesizer), new]; @@ -91,6 +91,10 @@ impl AppKit { } impl Backend for AppKit { + fn id(&self) -> Option { + None + } + fn supported_features(&self) -> Features { Features { stop: true, diff --git a/src/backends/av_foundation.rs b/src/backends/av_foundation.rs index be1eb6d..bdd5620 100644 --- a/src/backends/av_foundation.rs +++ b/src/backends/av_foundation.rs @@ -1,36 +1,52 @@ #[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 lazy_static::lazy_static; use log::{info, trace}; use objc::runtime::*; use objc::*; -use crate::{Backend, Error, Features, UtteranceId}; +use crate::{Backend, BackendId, Error, Features, UtteranceId}; -pub struct AvFoundation { +pub(crate) struct AvFoundation { + id: BackendId, synth: *mut Object, rate: f32, volume: f32, pitch: f32, } +lazy_static! { + static ref NEXT_BACKEND_ID: Mutex = Mutex::new(0); +} + impl AvFoundation { - pub fn new() -> Self { + pub(crate) fn new() -> Self { info!("Initializing AVFoundation backend"); - unsafe { + let mut backend_id = NEXT_BACKEND_ID.lock().unwrap(); + let rv = unsafe { let synth: *mut Object = msg_send![class!(AVSpeechSynthesizer), new]; AvFoundation { + id: BackendId::AvFoundation(*backend_id), synth: synth, rate: 0.5, volume: 1., pitch: 1., } - } + }; + *backend_id += 1; + rv } } impl Backend for AvFoundation { + fn id(&self) -> Option { + Some(self.id) + } + fn supported_features(&self) -> Features { Features { stop: true, @@ -38,6 +54,7 @@ impl Backend for AvFoundation { pitch: true, volume: true, is_speaking: true, + utterance_callbacks: true, } } diff --git a/src/backends/mod.rs b/src/backends/mod.rs index a9f094f..3e7106b 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -26,7 +26,7 @@ pub use self::tolk::*; pub use self::web::*; #[cfg(target_os = "macos")] -pub use self::appkit::*; +pub(crate) use self::appkit::*; #[cfg(any(target_os = "macos", target_os = "ios"))] -pub use self::av_foundation::*; +pub(crate) use self::av_foundation::*; diff --git a/src/lib.rs b/src/lib.rs index b9d4aba..ced1f17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,7 +55,7 @@ pub enum BackendId { #[cfg(windows)] WinRT(u64), #[cfg(any(target_os = "macos", target_os = "ios"))] - AvFoundation(id), + AvFoundation(u64), } #[derive(Clone, Debug, PartialEq)] @@ -66,6 +66,8 @@ pub enum UtteranceId { Web(u64), #[cfg(windows)] WinRT(MediaPlaybackItem), + #[cfg(any(target_os = "macos", target_os = "ios"))] + AvFoundation(id), } pub struct Features {