1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-10-09 22:49:38 +00:00

Under WinRT, recreate player completely when interruption is requested.

This commit is contained in:
Nolan Darilek 2020-07-06 12:52:18 -05:00
parent 1d7c668a4a
commit 16a6f6378a
5 changed files with 23 additions and 16 deletions

View File

@ -25,7 +25,7 @@ impl Backend for SpeechDispatcher {
}
}
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> {
fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error> {
trace!("speak({}, {})", text, interrupt);
if interrupt {
self.stop()?;
@ -41,7 +41,7 @@ impl Backend for SpeechDispatcher {
Ok(())
}
fn stop(&self) -> Result<(), Error> {
fn stop(&mut self) -> Result<(), Error> {
trace!("stop()");
self.0.cancel();
Ok(())

View File

@ -29,13 +29,13 @@ impl Backend for Tolk {
}
}
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> {
fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error> {
trace!("speak({}, {})", text, interrupt);
self.0.speak(text, interrupt);
Ok(())
}
fn stop(&self) -> Result<(), Error> {
fn stop(&mut self) -> Result<(), Error> {
trace!("stop()");
self.0.silence();
Ok(())

View File

@ -32,7 +32,7 @@ impl Backend for Web {
}
}
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> {
fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error> {
trace!("speak({}, {})", text, interrupt);
let utterance = SpeechSynthesisUtterance::new_with_text(text).unwrap();
utterance.set_rate(self.rate);
@ -48,7 +48,7 @@ impl Backend for Web {
Ok(())
}
fn stop(&self) -> Result<(), Error> {
fn stop(&mut self) -> Result<(), Error> {
trace!("stop()");
if let Some(window) = web_sys::window() {
let speech_synthesis = window.speech_synthesis().unwrap();

View File

@ -24,9 +24,9 @@ pub struct WinRT {
impl WinRT {
pub fn new() -> std::result::Result<Self, Error> {
info!("Initializing WinRT backend");
let playback_list = MediaPlaybackList::new()?;
let player = MediaPlayer::new()?;
player.set_auto_play(true)?;
let playback_list = MediaPlaybackList::new()?;
player.set_source(&playback_list)?;
Ok(Self {
synth: SpeechSynthesizer::new()?,
@ -34,6 +34,14 @@ impl WinRT {
playback_list: playback_list,
})
}
fn reinit_player(&mut self) -> std::result::Result<(), Error> {
self.playback_list = MediaPlaybackList::new()?;
self.player = MediaPlayer::new()?;
self.player.set_auto_play(true)?;
self.player.set_source(&self.playback_list)?;
Ok(())
}
}
impl Backend for WinRT {
@ -47,7 +55,7 @@ impl Backend for WinRT {
}
}
fn speak(&self, text: &str, interrupt: bool) -> std::result::Result<(), Error> {
fn speak(&mut self, text: &str, interrupt: bool) -> std::result::Result<(), Error> {
trace!("speak({}, {})", text, interrupt);
if interrupt {
self.stop()?;
@ -61,7 +69,7 @@ impl Backend for WinRT {
let index = self.playback_list.current_item_index()?;
let total = self.playback_list.items()?.size()?;
if total != 0 && index == total - 1 {
self.playback_list.items()?.clear()?;
self.reinit_player()?;
}
}
self.playback_list.items()?.append(item)?;
@ -71,10 +79,9 @@ impl Backend for WinRT {
Ok(())
}
fn stop(&self) -> std::result::Result<(), Error> {
fn stop(&mut self) -> std::result::Result<(), Error> {
trace!("stop()");
self.player.pause()?;
self.playback_list.items()?.clear()?;
self.reinit_player()?;
Ok(())
}

View File

@ -51,8 +51,8 @@ pub enum Error {
pub trait Backend {
fn supported_features(&self) -> Features;
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error>;
fn stop(&self) -> Result<(), Error>;
fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error>;
fn stop(&mut self) -> Result<(), Error>;
fn min_rate(&self) -> f32;
fn max_rate(&self) -> f32;
fn normal_rate(&self) -> f32;
@ -131,7 +131,7 @@ impl TTS {
/**
* Speaks the specified text, optionally interrupting current speech.
*/
pub fn speak<S: Into<String>>(&self, text: S, interrupt: bool) -> Result<&Self, Error> {
pub fn speak<S: Into<String>>(&mut self, text: S, interrupt: bool) -> Result<&Self, Error> {
self.0.speak(text.into().as_str(), interrupt)?;
Ok(self)
}
@ -139,7 +139,7 @@ impl TTS {
/**
* Stops current speech.
*/
pub fn stop(&self) -> Result<&Self, Error> {
pub fn stop(&mut self) -> Result<&Self, Error> {
let Features { stop, .. } = self.supported_features();
if stop {
self.0.stop()?;