1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-14 06:19:37 +00:00
tts-rs/src/backends/web.rs

176 lines
4.5 KiB
Rust
Raw Normal View History

2018-12-30 17:13:48 +00:00
#[cfg(target_arch = "wasm32")]
use std::sync::Mutex;
use lazy_static::lazy_static;
2018-12-30 17:13:48 +00:00
use log::{info, trace};
2020-09-24 19:26:30 +00:00
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{SpeechSynthesisEvent, SpeechSynthesisUtterance};
2018-12-30 17:13:48 +00:00
2020-09-24 19:26:30 +00:00
use crate::{Backend, BackendId, Error, Features, UtteranceId, CALLBACKS};
2018-12-30 17:13:48 +00:00
pub struct Web {
2020-09-24 19:26:30 +00:00
id: BackendId,
rate: f32,
pitch: f32,
volume: f32,
2018-12-30 17:13:48 +00:00
}
lazy_static! {
2020-09-24 19:26:30 +00:00
static ref NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0);
}
2018-12-30 17:13:48 +00:00
impl Web {
2019-12-23 13:37:48 +00:00
pub fn new() -> Result<Self, Error> {
2018-12-30 17:13:48 +00:00
info!("Initializing Web backend");
2020-09-24 19:26:30 +00:00
let mut backend_id = NEXT_BACKEND_ID.lock().unwrap();
let rv = Web {
id: BackendId::Web(*backend_id),
rate: 1.,
pitch: 1.,
volume: 1.,
2020-09-24 19:26:30 +00:00
};
*backend_id += 1;
Ok(rv)
2018-12-30 17:13:48 +00:00
}
}
impl Backend for Web {
2020-09-24 19:26:30 +00:00
fn id(&self) -> Option<BackendId> {
Some(self.id)
}
fn supported_features(&self) -> Features {
Features {
stop: true,
rate: true,
pitch: true,
volume: true,
is_speaking: true,
2020-09-24 19:26:30 +00:00
utterance_callbacks: true,
}
}
fn speak(&mut self, text: &str, interrupt: bool) -> Result<Option<UtteranceId>, Error> {
2018-12-30 17:13:48 +00:00
trace!("speak({}, {})", text, interrupt);
let utterance = SpeechSynthesisUtterance::new_with_text(text).unwrap();
utterance.set_rate(self.rate);
utterance.set_pitch(self.pitch);
utterance.set_volume(self.volume);
2020-09-24 19:26:30 +00:00
let id = self.id().unwrap();
let utterance_id = UtteranceId::Web(utterance.clone());
let callback = Closure::wrap(Box::new(move |evt: SpeechSynthesisEvent| {
let callbacks = CALLBACKS.lock().unwrap();
let callback = callbacks.get(&id).unwrap();
if let Some(f) = callback.utterance_begin {
let utterance_id = UtteranceId::Web(evt.utterance());
f(utterance_id);
}
}) as Box<dyn Fn(_)>);
utterance.set_onstart(Some(callback.as_ref().unchecked_ref()));
let callback = Closure::wrap(Box::new(move |evt: SpeechSynthesisEvent| {
let callbacks = CALLBACKS.lock().unwrap();
let callback = callbacks.get(&id).unwrap();
if let Some(f) = callback.utterance_end {
let utterance_id = UtteranceId::Web(evt.utterance());
f(utterance_id);
}
}) as Box<dyn Fn(_)>);
utterance.set_onend(Some(callback.as_ref().unchecked_ref()));
2018-12-30 17:13:48 +00:00
if interrupt {
self.stop()?;
}
if let Some(window) = web_sys::window() {
let speech_synthesis = window.speech_synthesis().unwrap();
speech_synthesis.speak(&utterance);
2020-09-24 19:26:30 +00:00
Ok(Some(utterance_id))
} else {
Err(Error::NoneError)
2018-12-30 17:13:48 +00:00
}
}
fn stop(&mut self) -> Result<(), Error> {
2018-12-30 17:13:48 +00:00
trace!("stop()");
if let Some(window) = web_sys::window() {
let speech_synthesis = window.speech_synthesis().unwrap();
speech_synthesis.cancel();
}
Ok(())
}
fn min_rate(&self) -> f32 {
0.1
}
fn max_rate(&self) -> f32 {
10.
}
fn normal_rate(&self) -> f32 {
1.
}
fn get_rate(&self) -> Result<f32, Error> {
2018-12-30 17:13:48 +00:00
Ok(self.rate)
}
fn set_rate(&mut self, rate: f32) -> Result<(), Error> {
2018-12-30 17:13:48 +00:00
self.rate = rate;
Ok(())
}
fn min_pitch(&self) -> f32 {
0.
}
fn max_pitch(&self) -> f32 {
2.
}
fn normal_pitch(&self) -> f32 {
1.
}
fn get_pitch(&self) -> Result<f32, Error> {
2018-12-30 17:13:48 +00:00
Ok(self.pitch)
}
fn set_pitch(&mut self, pitch: f32) -> Result<(), Error> {
2018-12-30 17:13:48 +00:00
self.pitch = pitch;
Ok(())
}
fn min_volume(&self) -> f32 {
0.
}
fn max_volume(&self) -> f32 {
1.
}
fn normal_volume(&self) -> f32 {
1.
}
fn get_volume(&self) -> Result<f32, Error> {
2018-12-30 17:13:48 +00:00
Ok(self.volume)
}
fn set_volume(&mut self, volume: f32) -> Result<(), Error> {
2018-12-30 17:13:48 +00:00
self.volume = volume;
Ok(())
}
fn is_speaking(&self) -> Result<bool, Error> {
trace!("is_speaking()");
if let Some(window) = web_sys::window() {
match window.speech_synthesis() {
Ok(speech_synthesis) => Ok(speech_synthesis.speaking()),
Err(e) => Err(Error::JavaScriptError(e)),
}
} else {
Err(Error::NoneError)
}
}
2018-12-30 17:13:48 +00:00
}