1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-23 00:19:38 +00:00

Pass TTS instance as first argument to utterance callbacks.

This commit is contained in:
Nolan Darilek 2020-11-02 22:40:30 -06:00
parent cf0ad2221e
commit 29c0a8463e
6 changed files with 30 additions and 24 deletions

View File

@ -17,13 +17,13 @@ fn main() -> Result<(), Error> {
.. ..
} = tts.supported_features(); } = tts.supported_features();
if utterance_callbacks { if utterance_callbacks {
tts.on_utterance_begin(Some(Box::new(|utterance| { tts.on_utterance_begin(Some(Box::new(|_tts, utterance| {
println!("Started speaking {:?}", 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) 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) println!("Stopped speaking {:?}", utterance)
})))?; })))?;
} }

View File

@ -44,7 +44,7 @@ impl AvFoundation {
let callbacks = callbacks.get_mut(&backend_id).unwrap(); let callbacks = callbacks.get_mut(&backend_id).unwrap();
if let Some(callback) = callbacks.utterance_begin.as_mut() { if let Some(callback) = callbacks.utterance_begin.as_mut() {
let utterance_id = UtteranceId::AvFoundation(utterance); 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(); let callbacks = callbacks.get_mut(&backend_id).unwrap();
if let Some(callback) = callbacks.utterance_end.as_mut() { if let Some(callback) = callbacks.utterance_end.as_mut() {
let utterance_id = UtteranceId::AvFoundation(utterance); 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(); let callbacks = callbacks.get_mut(&backend_id).unwrap();
if let Some(callback) = callbacks.utterance_stop.as_mut() { if let Some(callback) = callbacks.utterance_stop.as_mut() {
let utterance_id = UtteranceId::AvFoundation(utterance); let utterance_id = UtteranceId::AvFoundation(utterance);
callback(utterance_id); callback(callbacks.tts.clone(), utterance_id);
} }
} }
} }

View File

@ -33,7 +33,7 @@ impl SpeechDispatcher {
let cb = callbacks.get_mut(&backend_id).unwrap(); let cb = callbacks.get_mut(&backend_id).unwrap();
let utterance_id = UtteranceId::SpeechDispatcher(msg_id); let utterance_id = UtteranceId::SpeechDispatcher(msg_id);
if let Some(f) = cb.utterance_begin.as_mut() { 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| { 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 cb = callbacks.get_mut(&backend_id).unwrap();
let utterance_id = UtteranceId::SpeechDispatcher(msg_id); let utterance_id = UtteranceId::SpeechDispatcher(msg_id);
if let Some(f) = cb.utterance_end.as_mut() { 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| { 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 cb = callbacks.get_mut(&backend_id).unwrap();
let utterance_id = UtteranceId::SpeechDispatcher(msg_id); let utterance_id = UtteranceId::SpeechDispatcher(msg_id);
if let Some(f) = cb.utterance_stop.as_mut() { 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| { sd.0.on_pause(Some(Box::new(|_msg_id, client_id| {

View File

@ -75,7 +75,7 @@ impl Backend for Web {
let mut callbacks = CALLBACKS.lock().unwrap(); let mut callbacks = CALLBACKS.lock().unwrap();
let callback = callbacks.get_mut(&id).unwrap(); let callback = callbacks.get_mut(&id).unwrap();
if let Some(f) = callback.utterance_begin.as_mut() { if let Some(f) = callback.utterance_begin.as_mut() {
f(utterance_id); f(callback.tts.clone(), utterance_id);
} }
}) as Box<dyn Fn(_)>); }) as Box<dyn Fn(_)>);
utterance.set_onstart(Some(callback.as_ref().unchecked_ref())); utterance.set_onstart(Some(callback.as_ref().unchecked_ref()));
@ -83,7 +83,7 @@ impl Backend for Web {
let mut callbacks = CALLBACKS.lock().unwrap(); let mut callbacks = CALLBACKS.lock().unwrap();
let callback = callbacks.get_mut(&id).unwrap(); let callback = callbacks.get_mut(&id).unwrap();
if let Some(f) = callback.utterance_end.as_mut() { 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(); let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();
mappings.retain(|v| v.1 != utterance_id); mappings.retain(|v| v.1 != utterance_id);
@ -94,7 +94,7 @@ impl Backend for Web {
let mut callbacks = CALLBACKS.lock().unwrap(); let mut callbacks = CALLBACKS.lock().unwrap();
let callback = callbacks.get_mut(&id).unwrap(); let callback = callbacks.get_mut(&id).unwrap();
if let Some(f) = callback.utterance_stop.as_mut() { 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(); let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();

View File

@ -79,7 +79,7 @@ impl WinRT {
let callbacks = callbacks.get_mut(&self.id).unwrap(); let callbacks = callbacks.get_mut(&self.id).unwrap();
if let Some(callback) = callbacks.utterance_stop.as_mut() { if let Some(callback) = callbacks.utterance_stop.as_mut() {
for mapping in &*mappings { for mapping in &*mappings {
callback(mapping.2); callback(callbacks.tts.clone(), mapping.2);
} }
} }
mappings.retain(|v| v.0 != self.id); mappings.retain(|v| v.0 != self.id);
@ -101,7 +101,7 @@ impl WinRT {
if let Some(callback) = callbacks.utterance_end.as_mut() { if let Some(callback) = callbacks.utterance_end.as_mut() {
let last_spoken_utterance = LAST_SPOKEN_UTTERANCE.lock().unwrap(); let last_spoken_utterance = LAST_SPOKEN_UTTERANCE.lock().unwrap();
if let Some(utterance_id) = last_spoken_utterance.get(&id) { 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() { if let Some(callback) = callbacks.utterance_end.as_mut() {
for mapping in &*mappings { for mapping in &*mappings {
if mapping.1 == old_item { if mapping.1 == old_item {
callback(mapping.2); callback(callbacks.tts.clone(), mapping.2);
} }
} }
mappings.retain(|v| v.1 != old_item); mappings.retain(|v| v.1 != old_item);
@ -142,7 +142,7 @@ impl WinRT {
if let Some(callback) = callbacks.utterance_begin.as_mut() { if let Some(callback) = callbacks.utterance_begin.as_mut() {
for mapping in &*mappings { for mapping in &*mappings {
if mapping.1 == new_item { if mapping.1 == new_item {
callback(mapping.2); callback(callbacks.tts.clone(), mapping.2);
} }
} }
} }

View File

@ -133,11 +133,11 @@ trait Backend: Clone + std::fmt::Debug {
fn is_speaking(&self) -> Result<bool, Error>; fn is_speaking(&self) -> Result<bool, Error>;
} }
#[derive(Default)]
struct Callbacks { struct Callbacks {
utterance_begin: Option<Box<dyn FnMut(UtteranceId)>>, tts: TTS,
utterance_end: Option<Box<dyn FnMut(UtteranceId)>>, utterance_begin: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
utterance_stop: Option<Box<dyn FnMut(UtteranceId)>>, utterance_end: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
utterance_stop: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
} }
unsafe impl Send for Callbacks {} unsafe impl Send for Callbacks {}
@ -193,7 +193,13 @@ impl TTS {
if let Ok(backend) = backend { if let Ok(backend) = backend {
if let Some(id) = backend.0.id() { if let Some(id) = backend.0.id() {
let mut callbacks = CALLBACKS.lock().unwrap(); 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) Ok(backend)
} else { } else {
@ -443,7 +449,7 @@ impl TTS {
*/ */
pub fn on_utterance_begin( pub fn on_utterance_begin(
&self, &self,
callback: Option<Box<dyn FnMut(UtteranceId)>>, callback: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let Features { let Features {
utterance_callbacks, utterance_callbacks,
@ -465,7 +471,7 @@ impl TTS {
*/ */
pub fn on_utterance_end( pub fn on_utterance_end(
&self, &self,
callback: Option<Box<dyn FnMut(UtteranceId)>>, callback: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let Features { let Features {
utterance_callbacks, utterance_callbacks,
@ -487,7 +493,7 @@ impl TTS {
*/ */
pub fn on_utterance_stop( pub fn on_utterance_stop(
&self, &self,
callback: Option<Box<dyn FnMut(UtteranceId)>>, callback: Option<Box<dyn FnMut(TTS, UtteranceId)>>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let Features { let Features {
utterance_callbacks, utterance_callbacks,