mirror of https://github.com/ndarilek/tts-rs.git
Under WinRT, recreate player completely when interruption is requested.
This commit is contained in:
parent
1d7c668a4a
commit
16a6f6378a
|
@ -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);
|
trace!("speak({}, {})", text, interrupt);
|
||||||
if interrupt {
|
if interrupt {
|
||||||
self.stop()?;
|
self.stop()?;
|
||||||
|
@ -41,7 +41,7 @@ impl Backend for SpeechDispatcher {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stop(&self) -> Result<(), Error> {
|
fn stop(&mut self) -> Result<(), Error> {
|
||||||
trace!("stop()");
|
trace!("stop()");
|
||||||
self.0.cancel();
|
self.0.cancel();
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -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);
|
trace!("speak({}, {})", text, interrupt);
|
||||||
self.0.speak(text, interrupt);
|
self.0.speak(text, interrupt);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stop(&self) -> Result<(), Error> {
|
fn stop(&mut self) -> Result<(), Error> {
|
||||||
trace!("stop()");
|
trace!("stop()");
|
||||||
self.0.silence();
|
self.0.silence();
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -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);
|
trace!("speak({}, {})", text, interrupt);
|
||||||
let utterance = SpeechSynthesisUtterance::new_with_text(text).unwrap();
|
let utterance = SpeechSynthesisUtterance::new_with_text(text).unwrap();
|
||||||
utterance.set_rate(self.rate);
|
utterance.set_rate(self.rate);
|
||||||
|
@ -48,7 +48,7 @@ impl Backend for Web {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stop(&self) -> Result<(), Error> {
|
fn stop(&mut self) -> Result<(), Error> {
|
||||||
trace!("stop()");
|
trace!("stop()");
|
||||||
if let Some(window) = web_sys::window() {
|
if let Some(window) = web_sys::window() {
|
||||||
let speech_synthesis = window.speech_synthesis().unwrap();
|
let speech_synthesis = window.speech_synthesis().unwrap();
|
||||||
|
|
|
@ -24,9 +24,9 @@ pub struct WinRT {
|
||||||
impl WinRT {
|
impl WinRT {
|
||||||
pub fn new() -> std::result::Result<Self, Error> {
|
pub fn new() -> std::result::Result<Self, Error> {
|
||||||
info!("Initializing WinRT backend");
|
info!("Initializing WinRT backend");
|
||||||
|
let playback_list = MediaPlaybackList::new()?;
|
||||||
let player = MediaPlayer::new()?;
|
let player = MediaPlayer::new()?;
|
||||||
player.set_auto_play(true)?;
|
player.set_auto_play(true)?;
|
||||||
let playback_list = MediaPlaybackList::new()?;
|
|
||||||
player.set_source(&playback_list)?;
|
player.set_source(&playback_list)?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
synth: SpeechSynthesizer::new()?,
|
synth: SpeechSynthesizer::new()?,
|
||||||
|
@ -34,6 +34,14 @@ impl WinRT {
|
||||||
playback_list: playback_list,
|
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 {
|
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);
|
trace!("speak({}, {})", text, interrupt);
|
||||||
if interrupt {
|
if interrupt {
|
||||||
self.stop()?;
|
self.stop()?;
|
||||||
|
@ -61,7 +69,7 @@ impl Backend for WinRT {
|
||||||
let index = self.playback_list.current_item_index()?;
|
let index = self.playback_list.current_item_index()?;
|
||||||
let total = self.playback_list.items()?.size()?;
|
let total = self.playback_list.items()?.size()?;
|
||||||
if total != 0 && index == total - 1 {
|
if total != 0 && index == total - 1 {
|
||||||
self.playback_list.items()?.clear()?;
|
self.reinit_player()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.playback_list.items()?.append(item)?;
|
self.playback_list.items()?.append(item)?;
|
||||||
|
@ -71,10 +79,9 @@ impl Backend for WinRT {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stop(&self) -> std::result::Result<(), Error> {
|
fn stop(&mut self) -> std::result::Result<(), Error> {
|
||||||
trace!("stop()");
|
trace!("stop()");
|
||||||
self.player.pause()?;
|
self.reinit_player()?;
|
||||||
self.playback_list.items()?.clear()?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,8 +51,8 @@ pub enum Error {
|
||||||
|
|
||||||
pub trait Backend {
|
pub trait Backend {
|
||||||
fn supported_features(&self) -> Features;
|
fn supported_features(&self) -> Features;
|
||||||
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error>;
|
fn speak(&mut self, text: &str, interrupt: bool) -> Result<(), Error>;
|
||||||
fn stop(&self) -> Result<(), Error>;
|
fn stop(&mut self) -> Result<(), Error>;
|
||||||
fn min_rate(&self) -> f32;
|
fn min_rate(&self) -> f32;
|
||||||
fn max_rate(&self) -> f32;
|
fn max_rate(&self) -> f32;
|
||||||
fn normal_rate(&self) -> f32;
|
fn normal_rate(&self) -> f32;
|
||||||
|
@ -131,7 +131,7 @@ impl TTS {
|
||||||
/**
|
/**
|
||||||
* Speaks the specified text, optionally interrupting current speech.
|
* 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)?;
|
self.0.speak(text.into().as_str(), interrupt)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,7 @@ impl TTS {
|
||||||
/**
|
/**
|
||||||
* Stops current speech.
|
* Stops current speech.
|
||||||
*/
|
*/
|
||||||
pub fn stop(&self) -> Result<&Self, Error> {
|
pub fn stop(&mut self) -> Result<&Self, Error> {
|
||||||
let Features { stop, .. } = self.supported_features();
|
let Features { stop, .. } = self.supported_features();
|
||||||
if stop {
|
if stop {
|
||||||
self.0.stop()?;
|
self.0.stop()?;
|
||||||
|
|
Loading…
Reference in New Issue