1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-22 13:49: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:
Lee Baker 2022-08-06 06:08:28 -05:00
parent f404e180e4
commit a55463dac8

View File

@ -7,7 +7,7 @@ use core_foundation::array::CFArray;
use core_foundation::string::CFString; use core_foundation::string::CFString;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::{info, trace}; 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 objc::{class, declare::ClassDecl, msg_send, sel, sel_impl};
use unic_langid::LanguageIdentifier; use unic_langid::LanguageIdentifier;
@ -24,13 +24,7 @@ pub(crate) struct AvFoundation {
voice: Option<Voice>, voice: Option<Voice>,
} }
lazy_static! { fn make_speech_sythesizer_delegate() -> Result<&'static Class, Error> {
static ref NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0);
}
impl AvFoundation {
pub(crate) fn new() -> Result<Self, Error> {
info!("Initializing AVFoundation backend");
let mut decl = ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject)) let mut decl = ClassDecl::new("MyNSSpeechSynthesizerDelegate", class!(NSObject))
.ok_or(Error::OperationFailed)?; .ok_or(Error::OperationFailed)?;
decl.add_ivar::<u64>("backend_id"); decl.add_ivar::<u64>("backend_id");
@ -125,8 +119,19 @@ impl AvFoundation {
); );
} }
let delegate_class = decl.register(); Ok(decl.register())
let delegate_obj: *mut Object = unsafe { msg_send![delegate_class, new] }; }
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 mut backend_id = NEXT_BACKEND_ID.lock().unwrap();
let rv = unsafe { let rv = unsafe {
trace!("Creating synth"); trace!("Creating synth");