1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-17 13:09:37 +00:00

Add AV Foundation support for returning utterance IDs.

This commit is contained in:
Nolan Darilek 2020-09-22 14:08:19 -05:00
parent 4816ec575c
commit 6b023c3071
3 changed files with 12 additions and 7 deletions

View File

@ -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<Option<UtteranceId>, 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> {

View File

@ -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<Option<UtteranceId>, 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> {

View File

@ -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 {