Implement unused framework for AVFoundation callbacks.

This commit is contained in:
Nolan Darilek 2020-09-23 11:28:56 -05:00
parent 36a12597de
commit bd57075d53
4 changed files with 34 additions and 11 deletions

View File

@ -7,12 +7,12 @@ use objc::declare::ClassDecl;
use objc::runtime::*; use objc::runtime::*;
use objc::*; 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 { impl AppKit {
pub fn new() -> Self { pub(crate) fn new() -> Self {
info!("Initializing AppKit backend"); info!("Initializing AppKit backend");
unsafe { unsafe {
let obj: *mut Object = msg_send![class!(NSSpeechSynthesizer), new]; let obj: *mut Object = msg_send![class!(NSSpeechSynthesizer), new];
@ -91,6 +91,10 @@ impl AppKit {
} }
impl Backend for AppKit { impl Backend for AppKit {
fn id(&self) -> Option<BackendId> {
None
}
fn supported_features(&self) -> Features { fn supported_features(&self) -> Features {
Features { Features {
stop: true, stop: true,

View File

@ -1,36 +1,52 @@
#[cfg(any(target_os = "macos", target_os = "ios"))] #[cfg(any(target_os = "macos", target_os = "ios"))]
#[link(name = "AVFoundation", kind = "framework")] #[link(name = "AVFoundation", kind = "framework")]
use std::sync::Mutex;
use cocoa_foundation::base::{id, nil}; use cocoa_foundation::base::{id, nil};
use cocoa_foundation::foundation::NSString; use cocoa_foundation::foundation::NSString;
use lazy_static::lazy_static;
use log::{info, trace}; use log::{info, trace};
use objc::runtime::*; use objc::runtime::*;
use objc::*; 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, synth: *mut Object,
rate: f32, rate: f32,
volume: f32, volume: f32,
pitch: f32, pitch: f32,
} }
lazy_static! {
static ref NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0);
}
impl AvFoundation { impl AvFoundation {
pub fn new() -> Self { pub(crate) fn new() -> Self {
info!("Initializing AVFoundation backend"); 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]; let synth: *mut Object = msg_send![class!(AVSpeechSynthesizer), new];
AvFoundation { AvFoundation {
id: BackendId::AvFoundation(*backend_id),
synth: synth, synth: synth,
rate: 0.5, rate: 0.5,
volume: 1., volume: 1.,
pitch: 1., pitch: 1.,
} }
} };
*backend_id += 1;
rv
} }
} }
impl Backend for AvFoundation { impl Backend for AvFoundation {
fn id(&self) -> Option<BackendId> {
Some(self.id)
}
fn supported_features(&self) -> Features { fn supported_features(&self) -> Features {
Features { Features {
stop: true, stop: true,
@ -38,6 +54,7 @@ impl Backend for AvFoundation {
pitch: true, pitch: true,
volume: true, volume: true,
is_speaking: true, is_speaking: true,
utterance_callbacks: true,
} }
} }

View File

@ -26,7 +26,7 @@ pub use self::tolk::*;
pub use self::web::*; pub use self::web::*;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub use self::appkit::*; pub(crate) use self::appkit::*;
#[cfg(any(target_os = "macos", target_os = "ios"))] #[cfg(any(target_os = "macos", target_os = "ios"))]
pub use self::av_foundation::*; pub(crate) use self::av_foundation::*;

View File

@ -55,7 +55,7 @@ pub enum BackendId {
#[cfg(windows)] #[cfg(windows)]
WinRT(u64), WinRT(u64),
#[cfg(any(target_os = "macos", target_os = "ios"))] #[cfg(any(target_os = "macos", target_os = "ios"))]
AvFoundation(id), AvFoundation(u64),
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -66,6 +66,8 @@ pub enum UtteranceId {
Web(u64), Web(u64),
#[cfg(windows)] #[cfg(windows)]
WinRT(MediaPlaybackItem), WinRT(MediaPlaybackItem),
#[cfg(any(target_os = "macos", target_os = "ios"))]
AvFoundation(id),
} }
pub struct Features { pub struct Features {