diff --git a/Cargo.toml b/Cargo.toml index 3a4bfc6..258aa5f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,8 +22,8 @@ env_logger = "0.8" [target.'cfg(windows)'.dependencies] tolk = { version = "0.3", optional = true } -winrt = "0.7" -tts_winrt_bindings = { version = "0.2", path="winrt_bindings" } +windows = "0.2" +tts_winrt_bindings = { version = "0.3", path="winrt_bindings" } [target.'cfg(target_os = "linux")'.dependencies] speech-dispatcher = "0.7" diff --git a/src/backends/winrt.rs b/src/backends/winrt.rs index 890ef63..d9b7704 100644 --- a/src/backends/winrt.rs +++ b/src/backends/winrt.rs @@ -13,8 +13,8 @@ use tts_winrt_bindings::windows::{foundation::TypedEventHandler, media::core::Me use crate::{Backend, BackendId, Error, Features, UtteranceId, CALLBACKS}; -impl From for Error { - fn from(e: winrt::Error) -> Self { +impl From for Error { + fn from(e: windows::Error) -> Self { Error::WinRT(e) } } @@ -77,60 +77,48 @@ impl WinRT { drop(backend_to_speech_synthesizer); let bid_clone = bid; player.media_ended(TypedEventHandler::new( - move |sender: &MediaPlayer, _args| { - let backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap(); - let id = backend_to_media_player.iter().find(|v| v.1 == sender); - if let Some((id, _)) = id { - let mut utterances = UTTERANCES.lock().unwrap(); - if let Some(utterances) = utterances.get_mut(id) { - if let Some(utterance) = utterances.pop_front() { - let mut callbacks = CALLBACKS.lock().unwrap(); - let callbacks = callbacks.get_mut(id).unwrap(); - if let Some(callback) = callbacks.utterance_end.as_mut() { - callback(utterance.id); - } - if let Some(utterance) = utterances.front() { - let backend_to_speech_synthesizer = - BACKEND_TO_SPEECH_SYNTHESIZER.lock().unwrap(); - let id = backend_to_speech_synthesizer - .iter() - .find(|v| *v.0 == bid_clone); - if let Some((_, tts)) = id { - tts.options()?.set_speaking_rate(utterance.rate.into())?; - tts.options()?.set_audio_pitch(utterance.pitch.into())?; - tts.options()?.set_audio_volume(utterance.volume.into())?; - let stream = tts - .synthesize_text_to_stream_async(utterance.text.as_str())? - .get()?; - let content_type = stream.content_type()?; - let source = - MediaSource::create_from_stream(stream, content_type)?; - sender.set_source(source)?; - sender.play()?; - if let Some(callback) = callbacks.utterance_begin.as_mut() { - callback(utterance.id); + move |sender: &Option, _args| { + if let Some(sender) = sender { + let backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap(); + let id = backend_to_media_player.iter().find(|v| v.1 == sender); + if let Some((id, _)) = id { + let mut utterances = UTTERANCES.lock().unwrap(); + if let Some(utterances) = utterances.get_mut(id) { + if let Some(utterance) = utterances.pop_front() { + let mut callbacks = CALLBACKS.lock().unwrap(); + let callbacks = callbacks.get_mut(id).unwrap(); + if let Some(callback) = callbacks.utterance_end.as_mut() { + callback(utterance.id); + } + if let Some(utterance) = utterances.front() { + let backend_to_speech_synthesizer = + BACKEND_TO_SPEECH_SYNTHESIZER.lock().unwrap(); + let id = backend_to_speech_synthesizer + .iter() + .find(|v| *v.0 == bid_clone); + if let Some((_, tts)) = id { + tts.options()?.set_speaking_rate(utterance.rate.into())?; + tts.options()?.set_audio_pitch(utterance.pitch.into())?; + tts.options()?.set_audio_volume(utterance.volume.into())?; + let stream = tts + .synthesize_text_to_stream_async( + utterance.text.as_str(), + )? + .get()?; + let content_type = stream.content_type()?; + let source = + MediaSource::create_from_stream(stream, content_type)?; + sender.set_source(source)?; + sender.play()?; + if let Some(callback) = callbacks.utterance_begin.as_mut() { + callback(utterance.id); + } } } } } } } - /*let source = sender.source()?; - let source: MediaPlaybackList = source.try_into()?; - source.items()?.clear()?; - let backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap(); - let id = backend_to_media_player.iter().find(|v| v.1 == sender); - if let Some(id) = id { - let id = id.0; - let mut callbacks = CALLBACKS.lock().unwrap(); - let callbacks = callbacks.get_mut(&id).unwrap(); - 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); - } - } - }*/ Ok(()) }, ))?; diff --git a/src/lib.rs b/src/lib.rs index fa538df..d2b80d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -115,7 +115,7 @@ pub enum Error { JavaScriptError(wasm_bindgen::JsValue), #[cfg(windows)] #[error("WinRT error")] - WinRT(winrt::Error), + WinRT(windows::Error), #[error("Unsupported feature")] UnsupportedFeature, #[error("Out of range")] diff --git a/winrt_bindings/Cargo.toml b/winrt_bindings/Cargo.toml index c0afe2c..eaf4bb2 100644 --- a/winrt_bindings/Cargo.toml +++ b/winrt_bindings/Cargo.toml @@ -1,13 +1,13 @@ [package] name = "tts_winrt_bindings" -version = "0.2.0" +version = "0.3.0" authors = ["Nolan Darilek "] description = "Internal crate used by `tts`" license = "MIT" edition = "2018" [dependencies] -winrt = "0.7" +windows = "0.2" [build-dependencies] -winrt = "0.7" +windows = "0.2" diff --git a/winrt_bindings/build.rs b/winrt_bindings/build.rs index 2cd927d..654291a 100644 --- a/winrt_bindings/build.rs +++ b/winrt_bindings/build.rs @@ -1,12 +1,7 @@ -winrt::build!( - dependencies - os - types +fn main() { + windows::build!( windows::media::core::MediaSource windows::media::playback::{MediaPlaybackState, MediaPlayer} windows::media::speech_synthesis::SpeechSynthesizer -); - -fn main() { - build(); + ); } diff --git a/winrt_bindings/src/lib.rs b/winrt_bindings/src/lib.rs index 056780e..42af6ba 100644 --- a/winrt_bindings/src/lib.rs +++ b/winrt_bindings/src/lib.rs @@ -1 +1 @@ -include!(concat!(env!("OUT_DIR"), "/winrt.rs")); +::windows::include_bindings!();