From 951e31b284a5b50a4a2c5313000adb6d35c9ae84 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 19 Aug 2020 21:28:30 -0500 Subject: [PATCH] Implement `is_speaking` For Speech-dispatcher. --- Cargo.toml | 5 ++-- src/backends/speech_dispatcher.rs | 49 +++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index aee2d9a..17afd2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tts" -version = "0.6.0" +version = "0.6.1" authors = ["Nolan Darilek "] repository = "https://github.com/ndarilek/tts-rs" description = "High-level Text-To-Speech (TTS) interface" @@ -12,6 +12,7 @@ edition = "2018" crate-type = ["lib", "staticlib"] [dependencies] +lazy_static = "1" log = "0.4" thiserror = "1" @@ -24,7 +25,7 @@ winrt = "0.7" tts_winrt_bindings = { version = "0.1", path="winrt_bindings" } [target.'cfg(target_os = "linux")'.dependencies] -speech-dispatcher = "0.4" +speech-dispatcher = "0.6" [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies] cocoa-foundation = "0.1" diff --git a/src/backends/speech_dispatcher.rs b/src/backends/speech_dispatcher.rs index 0239016..b12f51a 100644 --- a/src/backends/speech_dispatcher.rs +++ b/src/backends/speech_dispatcher.rs @@ -1,4 +1,8 @@ #[cfg(target_os = "linux")] +use std::collections::HashMap; +use std::sync::Mutex; + +use lazy_static::*; use log::{info, trace}; use speech_dispatcher::*; @@ -6,11 +10,41 @@ use crate::{Backend, Error, Features}; pub struct SpeechDispatcher(Connection); +lazy_static! { + static ref SPEAKING: Mutex> = { + let m: HashMap = HashMap::new(); + Mutex::new(m) + }; +} + impl SpeechDispatcher { pub fn new() -> Self { info!("Initializing SpeechDispatcher backend"); let connection = speech_dispatcher::Connection::open("tts", "tts", "tts", Mode::Single); - SpeechDispatcher(connection) + let sd = SpeechDispatcher(connection); + let mut speaking = SPEAKING.lock().unwrap(); + speaking.insert(sd.0.client_id(), false); + sd.0.on_begin(Some(|_msg_id, client_id| { + let mut speaking = SPEAKING.lock().unwrap(); + speaking.insert(client_id, true); + })); + sd.0.on_end(Some(|_msg_id, client_id| { + let mut speaking = SPEAKING.lock().unwrap(); + speaking.insert(client_id, false); + })); + sd.0.on_cancel(Some(|_msg_id, client_id| { + let mut speaking = SPEAKING.lock().unwrap(); + speaking.insert(client_id, false); + })); + sd.0.on_pause(Some(|_msg_id, client_id| { + let mut speaking = SPEAKING.lock().unwrap(); + speaking.insert(client_id, false); + })); + sd.0.on_resume(Some(|_msg_id, client_id| { + let mut speaking = SPEAKING.lock().unwrap(); + speaking.insert(client_id, true); + })); + sd } } @@ -21,7 +55,7 @@ impl Backend for SpeechDispatcher { rate: true, pitch: true, volume: true, - is_speaking: false, + is_speaking: true, } } @@ -111,6 +145,15 @@ impl Backend for SpeechDispatcher { } fn is_speaking(&self) -> Result { - unimplemented!() + let speaking = SPEAKING.lock().unwrap(); + let is_speaking = speaking.get(&self.0.client_id()).unwrap(); + Ok(*is_speaking) + } +} + +impl Drop for SpeechDispatcher { + fn drop(&mut self) { + let mut speaking = SPEAKING.lock().unwrap(); + speaking.remove(&self.0.client_id()); } }