From 6788277a4dadcaddc3b9f72502a5f90fafae82cc Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 23 Sep 2020 10:31:21 -0500 Subject: [PATCH] Implement framework for utterance callbacks in Windows backends, though they aren't currently called. --- src/backends/tolk.rs | 6 +++++- src/backends/winrt.rs | 23 ++++++++++++++++++++--- src/lib.rs | 4 ++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/backends/tolk.rs b/src/backends/tolk.rs index 28c3f42..0be20d1 100644 --- a/src/backends/tolk.rs +++ b/src/backends/tolk.rs @@ -2,7 +2,7 @@ use log::{info, trace}; use tolk::Tolk as TolkPtr; -use crate::{Backend, Error, Features, UtteranceId}; +use crate::{Backend, BackendId, Error, Features, UtteranceId}; pub struct Tolk(TolkPtr); @@ -19,6 +19,10 @@ impl Tolk { } impl Backend for Tolk { + fn id(&self) -> Option { + None + } + fn supported_features(&self) -> Features { Features { stop: true, diff --git a/src/backends/winrt.rs b/src/backends/winrt.rs index f558c91..c72040c 100644 --- a/src/backends/winrt.rs +++ b/src/backends/winrt.rs @@ -1,4 +1,7 @@ #[cfg(windows)] +use std::sync::Mutex; + +use lazy_static::lazy_static; use log::{info, trace}; use tts_winrt_bindings::windows::media::core::MediaSource; @@ -7,7 +10,7 @@ use tts_winrt_bindings::windows::media::playback::{ }; use tts_winrt_bindings::windows::media::speech_synthesis::SpeechSynthesizer; -use crate::{Backend, Error, Features, UtteranceId}; +use crate::{Backend, BackendId, Error, Features, UtteranceId}; impl From for Error { fn from(e: winrt::Error) -> Self { @@ -16,11 +19,16 @@ impl From for Error { } pub struct WinRT { + id: BackendId, synth: SpeechSynthesizer, player: MediaPlayer, playback_list: MediaPlaybackList, } +lazy_static! { + static ref NEXT_BACKEND_ID: Mutex = Mutex::new(0); +} + impl WinRT { pub fn new() -> std::result::Result { info!("Initializing WinRT backend"); @@ -28,11 +36,15 @@ impl WinRT { let player = MediaPlayer::new()?; player.set_auto_play(true)?; player.set_source(&playback_list)?; - Ok(Self { + let mut backend_id = NEXT_BACKEND_ID.lock().unwrap(); + let rv = Ok(Self { + id: BackendId::WinRT(*backend_id), synth: SpeechSynthesizer::new()?, player: player, playback_list: playback_list, - }) + }); + *backend_id += 1; + rv } fn reinit_player(&mut self) -> std::result::Result<(), Error> { @@ -45,6 +57,10 @@ impl WinRT { } impl Backend for WinRT { + fn id(&self) -> Option { + Some(self.id) + } + fn supported_features(&self) -> Features { Features { stop: true, @@ -52,6 +68,7 @@ impl Backend for WinRT { pitch: true, volume: true, is_speaking: true, + ..Default::default() } } diff --git a/src/lib.rs b/src/lib.rs index c98a625..e4c6d8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,14 +46,14 @@ pub enum Backends { AvFoundation, } -#[derive(Clone, Debug, Eq, Hash, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub enum BackendId { #[cfg(target_os = "linux")] SpeechDispatcher(u64), #[cfg(target_arch = "wasm32")] Web(u64), #[cfg(windows)] - WinRT(MediaPlaybackItem), + WinRT(u64), } #[derive(Clone, Debug, PartialEq)]