2020-08-11 17:11:19 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
2020-08-12 20:14:17 +00:00
|
|
|
use cocoa_foundation::base::{id, nil};
|
2020-08-11 17:11:19 +00:00
|
|
|
use cocoa_foundation::foundation::NSString;
|
|
|
|
use log::{info, trace};
|
|
|
|
use objc::declare::ClassDecl;
|
|
|
|
use objc::runtime::*;
|
|
|
|
use objc::*;
|
|
|
|
|
2022-03-31 16:52:30 +00:00
|
|
|
use crate::{Backend, BackendId, Error, Features, UtteranceId, Voice};
|
2020-08-11 17:11:19 +00:00
|
|
|
|
2020-11-03 03:44:47 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2020-09-23 16:28:56 +00:00
|
|
|
pub(crate) struct AppKit(*mut Object, *mut Object);
|
2020-08-11 17:11:19 +00:00
|
|
|
|
2020-08-13 11:46:16 +00:00
|
|
|
impl AppKit {
|
2022-06-14 18:09:50 +00:00
|
|
|
pub(crate) fn new() -> Result<Self, Error> {
|
2020-08-13 16:08:00 +00:00
|
|
|
info!("Initializing AppKit backend");
|
2020-08-11 17:11:19 +00:00
|
|
|
unsafe {
|
2020-08-12 20:14:17 +00:00
|
|
|
let obj: *mut Object = msg_send![class!(NSSpeechSynthesizer), new];
|
2022-06-14 18:09:50 +00:00
|
|
|
let mut decl = ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject))
|
|
|
|
.ok_or(Error::OperationFailed)?;
|
2020-08-12 20:14:17 +00:00
|
|
|
decl.add_ivar::<id>("synth");
|
|
|
|
decl.add_ivar::<id>("strings");
|
2020-08-12 20:41:57 +00:00
|
|
|
|
2020-08-12 20:14:17 +00:00
|
|
|
extern "C" fn enqueue_and_speak(this: &Object, _: Sel, string: id) {
|
|
|
|
unsafe {
|
|
|
|
let strings: id = *this.get_ivar("strings");
|
|
|
|
let _: () = msg_send![strings, addObject: string];
|
|
|
|
let count: u32 = msg_send![strings, count];
|
|
|
|
if count == 1 {
|
|
|
|
let str: id = msg_send!(strings, firstObject);
|
|
|
|
let synth: id = *this.get_ivar("synth");
|
|
|
|
let _: BOOL = msg_send![synth, startSpeakingString: str];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-12 20:41:57 +00:00
|
|
|
decl.add_method(
|
|
|
|
sel!(enqueueAndSpeak:),
|
|
|
|
enqueue_and_speak as extern "C" fn(&Object, Sel, id) -> (),
|
|
|
|
);
|
|
|
|
|
2020-08-12 20:14:17 +00:00
|
|
|
extern "C" fn speech_synthesizer_did_finish_speaking(
|
|
|
|
this: &Object,
|
|
|
|
_: Sel,
|
|
|
|
synth: *const Object,
|
|
|
|
_: BOOL,
|
|
|
|
) {
|
|
|
|
unsafe {
|
|
|
|
let strings: id = *this.get_ivar("strings");
|
|
|
|
let count: u32 = msg_send![strings, count];
|
|
|
|
if count > 0 {
|
|
|
|
let str: id = msg_send!(strings, firstObject);
|
2022-10-16 21:18:19 +00:00
|
|
|
let _: () = msg_send![str, release];
|
|
|
|
let _: () = msg_send!(strings, removeObjectAtIndex:0);
|
|
|
|
if count > 1 {
|
|
|
|
let str: id = msg_send!(strings, firstObject);
|
|
|
|
let _: BOOL = msg_send![synth, startSpeakingString: str];
|
|
|
|
}
|
2020-08-12 20:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-12 20:41:57 +00:00
|
|
|
decl.add_method(
|
|
|
|
sel!(speechSynthesizer:didFinishSpeaking:),
|
|
|
|
speech_synthesizer_did_finish_speaking
|
|
|
|
as extern "C" fn(&Object, Sel, *const Object, BOOL) -> (),
|
|
|
|
);
|
|
|
|
|
2020-08-12 20:14:17 +00:00
|
|
|
extern "C" fn clear_queue(this: &Object, _: Sel) {
|
|
|
|
unsafe {
|
|
|
|
let strings: id = *this.get_ivar("strings");
|
|
|
|
let mut count: u32 = msg_send![strings, count];
|
|
|
|
while count > 0 {
|
|
|
|
let str: id = msg_send!(strings, firstObject);
|
|
|
|
let _: () = msg_send![str, release];
|
|
|
|
let _: () = msg_send!(strings, removeObjectAtIndex:0);
|
|
|
|
count = msg_send![strings, count];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
decl.add_method(
|
|
|
|
sel!(clearQueue),
|
|
|
|
clear_queue as extern "C" fn(&Object, Sel) -> (),
|
|
|
|
);
|
2020-08-12 20:41:57 +00:00
|
|
|
|
2020-08-12 20:14:17 +00:00
|
|
|
let delegate_class = decl.register();
|
|
|
|
let delegate_obj: *mut Object = msg_send![delegate_class, new];
|
2022-06-14 18:09:50 +00:00
|
|
|
delegate_obj
|
|
|
|
.as_mut()
|
|
|
|
.ok_or(Error::OperationFailed)?
|
|
|
|
.set_ivar("synth", obj);
|
2020-08-12 20:14:17 +00:00
|
|
|
let strings: id = msg_send![class!(NSMutableArray), new];
|
2022-06-14 18:09:50 +00:00
|
|
|
delegate_obj
|
|
|
|
.as_mut()
|
|
|
|
.ok_or(Error::OperationFailed)?
|
|
|
|
.set_ivar("strings", strings);
|
2020-08-12 20:14:17 +00:00
|
|
|
let _: Object = msg_send![obj, setDelegate: delegate_obj];
|
2022-06-14 18:09:50 +00:00
|
|
|
Ok(AppKit(obj, delegate_obj))
|
2020-08-12 20:14:17 +00:00
|
|
|
}
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:46:16 +00:00
|
|
|
impl Backend for AppKit {
|
2020-09-23 16:28:56 +00:00
|
|
|
fn id(&self) -> Option<BackendId> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:11:19 +00:00
|
|
|
fn supported_features(&self) -> Features {
|
|
|
|
Features {
|
2020-08-12 20:41:57 +00:00
|
|
|
stop: true,
|
|
|
|
rate: true,
|
|
|
|
volume: true,
|
|
|
|
is_speaking: true,
|
2020-08-24 21:46:57 +00:00
|
|
|
..Default::default()
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 19:08:19 +00:00
|
|
|
fn speak(&mut self, text: &str, interrupt: bool) -> Result<Option<UtteranceId>, Error> {
|
2020-08-12 14:54:25 +00:00
|
|
|
trace!("speak({}, {})", text, interrupt);
|
2020-08-12 20:14:17 +00:00
|
|
|
if interrupt {
|
|
|
|
self.stop()?;
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
let str = NSString::alloc(nil).init_str(text);
|
|
|
|
let _: () = msg_send![self.1, enqueueAndSpeak: str];
|
|
|
|
}
|
2020-09-22 19:08:19 +00:00
|
|
|
Ok(None)
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(&mut self) -> Result<(), Error> {
|
|
|
|
trace!("stop()");
|
2020-08-12 20:14:17 +00:00
|
|
|
unsafe {
|
|
|
|
let _: () = msg_send![self.1, clearQueue];
|
|
|
|
let _: () = msg_send![self.0, stopSpeaking];
|
|
|
|
}
|
|
|
|
Ok(())
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn min_rate(&self) -> f32 {
|
2020-08-12 20:41:57 +00:00
|
|
|
10.
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn max_rate(&self) -> f32 {
|
2020-08-12 20:41:57 +00:00
|
|
|
500.
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn normal_rate(&self) -> f32 {
|
2020-08-12 20:41:57 +00:00
|
|
|
175.
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_rate(&self) -> Result<f32, Error> {
|
2020-08-12 20:41:57 +00:00
|
|
|
let rate: f32 = unsafe { msg_send![self.0, rate] };
|
|
|
|
Ok(rate)
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_rate(&mut self, rate: f32) -> Result<(), Error> {
|
2020-08-12 20:41:57 +00:00
|
|
|
trace!("set_rate({})", rate);
|
|
|
|
unsafe {
|
|
|
|
let _: () = msg_send![self.0, setRate: rate];
|
|
|
|
}
|
|
|
|
Ok(())
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn min_pitch(&self) -> f32 {
|
2020-08-12 20:41:57 +00:00
|
|
|
unimplemented!()
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn max_pitch(&self) -> f32 {
|
2020-08-12 20:41:57 +00:00
|
|
|
unimplemented!()
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn normal_pitch(&self) -> f32 {
|
2020-08-12 20:41:57 +00:00
|
|
|
unimplemented!()
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_pitch(&self) -> Result<f32, Error> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:58:16 +00:00
|
|
|
fn set_pitch(&mut self, _pitch: f32) -> Result<(), Error> {
|
2020-08-11 17:11:19 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn min_volume(&self) -> f32 {
|
2020-08-12 20:41:57 +00:00
|
|
|
0.
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn max_volume(&self) -> f32 {
|
2020-08-12 20:41:57 +00:00
|
|
|
1.
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn normal_volume(&self) -> f32 {
|
2020-08-12 20:41:57 +00:00
|
|
|
1.
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_volume(&self) -> Result<f32, Error> {
|
2020-08-12 20:41:57 +00:00
|
|
|
let volume: f32 = unsafe { msg_send![self.0, volume] };
|
|
|
|
Ok(volume)
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_volume(&mut self, volume: f32) -> Result<(), Error> {
|
2020-08-12 20:41:57 +00:00
|
|
|
unsafe {
|
|
|
|
let _: () = msg_send![self.0, setVolume: volume];
|
|
|
|
}
|
|
|
|
Ok(())
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_speaking(&self) -> Result<bool, Error> {
|
2020-08-12 20:14:17 +00:00
|
|
|
let is_speaking: i8 = unsafe { msg_send![self.0, isSpeaking] };
|
2021-03-15 18:02:32 +00:00
|
|
|
Ok(is_speaking != NO as i8)
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
2020-09-03 14:50:11 +00:00
|
|
|
|
2022-03-31 16:52:30 +00:00
|
|
|
fn voice(&self) -> Result<Option<Voice>, Error> {
|
2020-09-03 14:50:11 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2022-03-31 16:52:30 +00:00
|
|
|
fn voices(&self) -> Result<Vec<Voice>, Error> {
|
2020-09-03 14:50:11 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2022-03-31 18:16:35 +00:00
|
|
|
fn set_voice(&mut self, _voice: &Voice) -> Result<(), Error> {
|
2020-09-03 14:50:11 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
2020-08-11 17:11:19 +00:00
|
|
|
}
|
2020-08-12 14:52:16 +00:00
|
|
|
|
2020-08-13 11:46:16 +00:00
|
|
|
impl Drop for AppKit {
|
2020-08-12 14:52:16 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
2020-08-12 20:14:17 +00:00
|
|
|
let _: Object = msg_send![self.0, release];
|
|
|
|
let _: Object = msg_send![self.1, release];
|
2020-08-12 14:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|