Refactor to new windows crate.

This commit is contained in:
Nolan Darilek 2021-01-22 11:28:42 -06:00
parent 296fa89f5d
commit 42879dfa1f
6 changed files with 48 additions and 65 deletions

View File

@ -22,8 +22,8 @@ env_logger = "0.8"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
tolk = { version = "0.3", optional = true } tolk = { version = "0.3", optional = true }
winrt = "0.7" windows = "0.2"
tts_winrt_bindings = { version = "0.2", path="winrt_bindings" } tts_winrt_bindings = { version = "0.3", path="winrt_bindings" }
[target.'cfg(target_os = "linux")'.dependencies] [target.'cfg(target_os = "linux")'.dependencies]
speech-dispatcher = "0.7" speech-dispatcher = "0.7"

View File

@ -13,8 +13,8 @@ use tts_winrt_bindings::windows::{foundation::TypedEventHandler, media::core::Me
use crate::{Backend, BackendId, Error, Features, UtteranceId, CALLBACKS}; use crate::{Backend, BackendId, Error, Features, UtteranceId, CALLBACKS};
impl From<winrt::Error> for Error { impl From<windows::Error> for Error {
fn from(e: winrt::Error) -> Self { fn from(e: windows::Error) -> Self {
Error::WinRT(e) Error::WinRT(e)
} }
} }
@ -77,60 +77,48 @@ impl WinRT {
drop(backend_to_speech_synthesizer); drop(backend_to_speech_synthesizer);
let bid_clone = bid; let bid_clone = bid;
player.media_ended(TypedEventHandler::new( player.media_ended(TypedEventHandler::new(
move |sender: &MediaPlayer, _args| { move |sender: &Option<MediaPlayer>, _args| {
let backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap(); if let Some(sender) = sender {
let id = backend_to_media_player.iter().find(|v| v.1 == sender); let backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap();
if let Some((id, _)) = id { let id = backend_to_media_player.iter().find(|v| v.1 == sender);
let mut utterances = UTTERANCES.lock().unwrap(); if let Some((id, _)) = id {
if let Some(utterances) = utterances.get_mut(id) { let mut utterances = UTTERANCES.lock().unwrap();
if let Some(utterance) = utterances.pop_front() { if let Some(utterances) = utterances.get_mut(id) {
let mut callbacks = CALLBACKS.lock().unwrap(); if let Some(utterance) = utterances.pop_front() {
let callbacks = callbacks.get_mut(id).unwrap(); let mut callbacks = CALLBACKS.lock().unwrap();
if let Some(callback) = callbacks.utterance_end.as_mut() { let callbacks = callbacks.get_mut(id).unwrap();
callback(utterance.id); if let Some(callback) = callbacks.utterance_end.as_mut() {
} callback(utterance.id);
if let Some(utterance) = utterances.front() { }
let backend_to_speech_synthesizer = if let Some(utterance) = utterances.front() {
BACKEND_TO_SPEECH_SYNTHESIZER.lock().unwrap(); let backend_to_speech_synthesizer =
let id = backend_to_speech_synthesizer BACKEND_TO_SPEECH_SYNTHESIZER.lock().unwrap();
.iter() let id = backend_to_speech_synthesizer
.find(|v| *v.0 == bid_clone); .iter()
if let Some((_, tts)) = id { .find(|v| *v.0 == bid_clone);
tts.options()?.set_speaking_rate(utterance.rate.into())?; if let Some((_, tts)) = id {
tts.options()?.set_audio_pitch(utterance.pitch.into())?; tts.options()?.set_speaking_rate(utterance.rate.into())?;
tts.options()?.set_audio_volume(utterance.volume.into())?; tts.options()?.set_audio_pitch(utterance.pitch.into())?;
let stream = tts tts.options()?.set_audio_volume(utterance.volume.into())?;
.synthesize_text_to_stream_async(utterance.text.as_str())? let stream = tts
.get()?; .synthesize_text_to_stream_async(
let content_type = stream.content_type()?; utterance.text.as_str(),
let source = )?
MediaSource::create_from_stream(stream, content_type)?; .get()?;
sender.set_source(source)?; let content_type = stream.content_type()?;
sender.play()?; let source =
if let Some(callback) = callbacks.utterance_begin.as_mut() { MediaSource::create_from_stream(stream, content_type)?;
callback(utterance.id); 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(()) Ok(())
}, },
))?; ))?;

View File

@ -115,7 +115,7 @@ pub enum Error {
JavaScriptError(wasm_bindgen::JsValue), JavaScriptError(wasm_bindgen::JsValue),
#[cfg(windows)] #[cfg(windows)]
#[error("WinRT error")] #[error("WinRT error")]
WinRT(winrt::Error), WinRT(windows::Error),
#[error("Unsupported feature")] #[error("Unsupported feature")]
UnsupportedFeature, UnsupportedFeature,
#[error("Out of range")] #[error("Out of range")]

View File

@ -1,13 +1,13 @@
[package] [package]
name = "tts_winrt_bindings" name = "tts_winrt_bindings"
version = "0.2.0" version = "0.3.0"
authors = ["Nolan Darilek <nolan@thewordnerd.info>"] authors = ["Nolan Darilek <nolan@thewordnerd.info>"]
description = "Internal crate used by `tts`" description = "Internal crate used by `tts`"
license = "MIT" license = "MIT"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
winrt = "0.7" windows = "0.2"
[build-dependencies] [build-dependencies]
winrt = "0.7" windows = "0.2"

View File

@ -1,12 +1,7 @@
winrt::build!( fn main() {
dependencies windows::build!(
os
types
windows::media::core::MediaSource windows::media::core::MediaSource
windows::media::playback::{MediaPlaybackState, MediaPlayer} windows::media::playback::{MediaPlaybackState, MediaPlayer}
windows::media::speech_synthesis::SpeechSynthesizer windows::media::speech_synthesis::SpeechSynthesizer
); );
fn main() {
build();
} }

View File

@ -1 +1 @@
include!(concat!(env!("OUT_DIR"), "/winrt.rs")); ::windows::include_bindings!();