mirror of
https://github.com/ndarilek/tts-rs.git
synced 2024-11-22 08:09:38 +00:00
Don't define the AVFoundation delegate more than once.
This prevents a crash when multiple instances of the TTS engine are instantiated.
This commit is contained in:
parent
f404e180e4
commit
a55463dac8
|
@ -7,7 +7,7 @@ use core_foundation::array::CFArray;
|
|||
use core_foundation::string::CFString;
|
||||
use lazy_static::lazy_static;
|
||||
use log::{info, trace};
|
||||
use objc::runtime::{Object, Sel};
|
||||
use objc::runtime::{Class, Object, Sel};
|
||||
use objc::{class, declare::ClassDecl, msg_send, sel, sel_impl};
|
||||
use unic_langid::LanguageIdentifier;
|
||||
|
||||
|
@ -24,13 +24,7 @@ pub(crate) struct AvFoundation {
|
|||
voice: Option<Voice>,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0);
|
||||
}
|
||||
|
||||
impl AvFoundation {
|
||||
pub(crate) fn new() -> Result<Self, Error> {
|
||||
info!("Initializing AVFoundation backend");
|
||||
fn make_speech_sythesizer_delegate() -> Result<&'static Class, Error> {
|
||||
let mut decl = ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject))
|
||||
.ok_or(Error::OperationFailed)?;
|
||||
decl.add_ivar::<u64>("backend_id");
|
||||
|
@ -125,8 +119,19 @@ impl AvFoundation {
|
|||
);
|
||||
}
|
||||
|
||||
let delegate_class = decl.register();
|
||||
let delegate_obj: *mut Object = unsafe { msg_send![delegate_class, new] };
|
||||
Ok(decl.register())
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0);
|
||||
static ref DELEGATE_CLASS: &'static Class = make_speech_sythesizer_delegate().unwrap();
|
||||
}
|
||||
|
||||
impl AvFoundation {
|
||||
pub(crate) fn new() -> Result<Self, Error> {
|
||||
info!("Initializing AVFoundation backend");
|
||||
|
||||
let delegate_obj: *mut Object = unsafe { msg_send![*DELEGATE_CLASS, new] };
|
||||
let mut backend_id = NEXT_BACKEND_ID.lock().unwrap();
|
||||
let rv = unsafe {
|
||||
trace!("Creating synth");
|
||||
|
|
Loading…
Reference in New Issue
Block a user