mirror of
https://github.com/ndarilek/tts-rs.git
synced 2024-11-22 19:49:36 +00:00
Pass TTS instance as first argument to utterance callbacks.
This commit is contained in:
parent
cf0ad2221e
commit
29c0a8463e
|
@ -17,13 +17,13 @@ fn main() -> Result<(), Error> {
|
|||
..
|
||||
} = tts.supported_features();
|
||||
if utterance_callbacks {
|
||||
tts.on_utterance_begin(Some(Box::new(|utterance| {
|
||||
tts.on_utterance_begin(Some(Box::new(|_tts, utterance| {
|
||||
println!("Started speaking {:?}", utterance)
|
||||
})))?;
|
||||
tts.on_utterance_end(Some(Box::new(|utterance| {
|
||||
tts.on_utterance_end(Some(Box::new(|_tts, utterance| {
|
||||
println!("Finished speaking {:?}", utterance)
|
||||
})))?;
|
||||
tts.on_utterance_stop(Some(Box::new(|utterance| {
|
||||
tts.on_utterance_stop(Some(Box::new(|_tts, utterance| {
|
||||
println!("Stopped speaking {:?}", utterance)
|
||||
})))?;
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ impl AvFoundation {
|
|||
let callbacks = callbacks.get_mut(&backend_id).unwrap();
|
||||
if let Some(callback) = callbacks.utterance_begin.as_mut() {
|
||||
let utterance_id = UtteranceId::AvFoundation(utterance);
|
||||
callback(utterance_id);
|
||||
callback(callbacks.tts.clone(), utterance_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ impl AvFoundation {
|
|||
let callbacks = callbacks.get_mut(&backend_id).unwrap();
|
||||
if let Some(callback) = callbacks.utterance_end.as_mut() {
|
||||
let utterance_id = UtteranceId::AvFoundation(utterance);
|
||||
callback(utterance_id);
|
||||
callback(callbacks.tts.clone(), utterance_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ impl AvFoundation {
|
|||
let callbacks = callbacks.get_mut(&backend_id).unwrap();
|
||||
if let Some(callback) = callbacks.utterance_stop.as_mut() {
|
||||
let utterance_id = UtteranceId::AvFoundation(utterance);
|
||||
callback(utterance_id);
|
||||
callback(callbacks.tts.clone(), utterance_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ impl SpeechDispatcher {
|
|||
let cb = callbacks.get_mut(&backend_id).unwrap();
|
||||
let utterance_id = UtteranceId::SpeechDispatcher(msg_id);
|
||||
if let Some(f) = cb.utterance_begin.as_mut() {
|
||||
f(utterance_id);
|
||||
f(cb.tts.clone(), utterance_id);
|
||||
}
|
||||
})));
|
||||
sd.0.on_end(Some(Box::new(|msg_id, client_id| {
|
||||
|
@ -44,7 +44,7 @@ impl SpeechDispatcher {
|
|||
let cb = callbacks.get_mut(&backend_id).unwrap();
|
||||
let utterance_id = UtteranceId::SpeechDispatcher(msg_id);
|
||||
if let Some(f) = cb.utterance_end.as_mut() {
|
||||
f(utterance_id);
|
||||
f(cb.tts.clone(), utterance_id);
|
||||
}
|
||||
})));
|
||||
sd.0.on_cancel(Some(Box::new(|msg_id, client_id| {
|
||||
|
@ -55,7 +55,7 @@ impl SpeechDispatcher {
|
|||
let cb = callbacks.get_mut(&backend_id).unwrap();
|
||||
let utterance_id = UtteranceId::SpeechDispatcher(msg_id);
|
||||
if let Some(f) = cb.utterance_stop.as_mut() {
|
||||
f(utterance_id);
|
||||
f(cb.tts.clone(), utterance_id);
|
||||
}
|
||||
})));
|
||||
sd.0.on_pause(Some(Box::new(|_msg_id, client_id| {
|
||||
|
|
|
@ -75,7 +75,7 @@ impl Backend for Web {
|
|||
let mut callbacks = CALLBACKS.lock().unwrap();
|
||||
let callback = callbacks.get_mut(&id).unwrap();
|
||||
if let Some(f) = callback.utterance_begin.as_mut() {
|
||||
f(utterance_id);
|
||||
f(callback.tts.clone(), utterance_id);
|
||||
}
|
||||
}) as Box<dyn Fn(_)>);
|
||||
utterance.set_onstart(Some(callback.as_ref().unchecked_ref()));
|
||||
|
@ -83,7 +83,7 @@ impl Backend for Web {
|
|||
let mut callbacks = CALLBACKS.lock().unwrap();
|
||||
let callback = callbacks.get_mut(&id).unwrap();
|
||||
if let Some(f) = callback.utterance_end.as_mut() {
|
||||
f(utterance_id);
|
||||
f(callback.tts.clone(), utterance_id);
|
||||
}
|
||||
let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
||||
mappings.retain(|v| v.1 != utterance_id);
|
||||
|
@ -94,7 +94,7 @@ impl Backend for Web {
|
|||
let mut callbacks = CALLBACKS.lock().unwrap();
|
||||
let callback = callbacks.get_mut(&id).unwrap();
|
||||
if let Some(f) = callback.utterance_stop.as_mut() {
|
||||
f(utterance_id);
|
||||
f(callback.tts.clone(), utterance_id);
|
||||
}
|
||||
}
|
||||
let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
||||
|
|
|
@ -79,7 +79,7 @@ impl WinRT {
|
|||
let callbacks = callbacks.get_mut(&self.id).unwrap();
|
||||
if let Some(callback) = callbacks.utterance_stop.as_mut() {
|
||||
for mapping in &*mappings {
|
||||
callback(mapping.2);
|
||||
callback(callbacks.tts.clone(), mapping.2);
|
||||
}
|
||||
}
|
||||
mappings.retain(|v| v.0 != self.id);
|
||||
|
@ -101,7 +101,7 @@ impl WinRT {
|
|||
if let Some(callback) = callbacks.utterance_end.as_mut() {
|
||||
let last_spoken_utterance = LAST_SPOKEN_UTTERANCE.lock().unwrap();
|
||||
if let Some(utterance_id) = last_spoken_utterance.get(&id) {
|
||||
callback(utterance_id.clone());
|
||||
callback(callbacks.tts.clone(), utterance_id.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -124,7 +124,7 @@ impl WinRT {
|
|||
if let Some(callback) = callbacks.utterance_end.as_mut() {
|
||||
for mapping in &*mappings {
|
||||
if mapping.1 == old_item {
|
||||
callback(mapping.2);
|
||||
callback(callbacks.tts.clone(), mapping.2);
|
||||
}
|
||||
}
|
||||
mappings.retain(|v| v.1 != old_item);
|
||||
|
@ -142,7 +142,7 @@ impl WinRT {
|
|||
if let Some(callback) = callbacks.utterance_begin.as_mut() {
|
||||
for mapping in &*mappings {
|
||||
if mapping.1 == new_item {
|
||||
callback(mapping.2);
|
||||
callback(callbacks.tts.clone(), mapping.2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
22
src/lib.rs
22
src/lib.rs
|
@ -133,11 +133,11 @@ trait Backend: Clone + std::fmt::Debug {
|
|||
fn is_speaking(&self) -> Result<bool, Error>;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Callbacks {
|
||||
utterance_begin: Option<Box<dyn FnMut(UtteranceId)>>,
|
||||
utterance_end: Option<Box<dyn FnMut(UtteranceId)>>,
|
||||
utterance_stop: Option<Box<dyn FnMut(UtteranceId)>>,
|
||||
tts: TTS,
|
||||
utterance_begin: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
|
||||
utterance_end: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
|
||||
utterance_stop: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
|
||||
}
|
||||
|
||||
unsafe impl Send for Callbacks {}
|
||||
|
@ -193,7 +193,13 @@ impl TTS {
|
|||
if let Ok(backend) = backend {
|
||||
if let Some(id) = backend.0.id() {
|
||||
let mut callbacks = CALLBACKS.lock().unwrap();
|
||||
callbacks.insert(id, Callbacks::default());
|
||||
let cb = Callbacks {
|
||||
tts: backend.clone(),
|
||||
utterance_begin: None,
|
||||
utterance_end: None,
|
||||
utterance_stop: None,
|
||||
};
|
||||
callbacks.insert(id, cb);
|
||||
}
|
||||
Ok(backend)
|
||||
} else {
|
||||
|
@ -443,7 +449,7 @@ impl TTS {
|
|||
*/
|
||||
pub fn on_utterance_begin(
|
||||
&self,
|
||||
callback: Option<Box<dyn FnMut(UtteranceId)>>,
|
||||
callback: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
|
||||
) -> Result<(), Error> {
|
||||
let Features {
|
||||
utterance_callbacks,
|
||||
|
@ -465,7 +471,7 @@ impl TTS {
|
|||
*/
|
||||
pub fn on_utterance_end(
|
||||
&self,
|
||||
callback: Option<Box<dyn FnMut(UtteranceId)>>,
|
||||
callback: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
|
||||
) -> Result<(), Error> {
|
||||
let Features {
|
||||
utterance_callbacks,
|
||||
|
@ -487,7 +493,7 @@ impl TTS {
|
|||
*/
|
||||
pub fn on_utterance_stop(
|
||||
&self,
|
||||
callback: Option<Box<dyn FnMut(UtteranceId)>>,
|
||||
callback: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
|
||||
) -> Result<(), Error> {
|
||||
let Features {
|
||||
utterance_callbacks,
|
||||
|
|
Loading…
Reference in New Issue
Block a user