2018-12-30 17:13:48 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2020-09-22 17:40:03 +00:00
|
|
|
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;
|
2020-10-08 12:56:45 +00:00
|
|
|
use web_sys::{
|
|
|
|
SpeechSynthesisErrorCode, SpeechSynthesisErrorEvent, 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,
|
Clean up speech synthesis properties, and implement everything for WinRT.
I'd previously attempted to normalize everything to `u8`, but this had some drawbacks:
* It failed to account for some synthesis drivers defining normal as mid-range, while most define it very low.
* It didn't track the normal value for a given synthesizer.
* There was no clean way to map a curve between the minimum, normal, and maximum rates.
Here we track the minimum, normal, and maximum values of rate, pitch, and volume. Sanity checks are done on set.
Also, as a further proof-of-concept, all properties are now implemented for the WinRT driver.
2020-05-18 23:12:59 +00:00
|
|
|
rate: f32,
|
|
|
|
pitch: f32,
|
|
|
|
volume: f32,
|
2018-12-30 17:13:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 17:40:03 +00:00
|
|
|
lazy_static! {
|
2020-09-24 19:26:30 +00:00
|
|
|
static ref NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0);
|
2020-10-08 12:16:10 +00:00
|
|
|
static ref UTTERANCE_MAPPINGS: Mutex<Vec<(BackendId, UtteranceId)>> = Mutex::new(Vec::new());
|
|
|
|
static ref NEXT_UTTERANCE_ID: Mutex<u64> = Mutex::new(0);
|
2020-09-22 17:40:03 +00:00
|
|
|
}
|
|
|
|
|
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),
|
Clean up speech synthesis properties, and implement everything for WinRT.
I'd previously attempted to normalize everything to `u8`, but this had some drawbacks:
* It failed to account for some synthesis drivers defining normal as mid-range, while most define it very low.
* It didn't track the normal value for a given synthesizer.
* There was no clean way to map a curve between the minimum, normal, and maximum rates.
Here we track the minimum, normal, and maximum values of rate, pitch, and volume. Sanity checks are done on set.
Also, as a further proof-of-concept, all properties are now implemented for the WinRT driver.
2020-05-18 23:12:59 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-03-24 21:30:45 +00:00
|
|
|
fn supported_features(&self) -> Features {
|
|
|
|
Features {
|
|
|
|
stop: true,
|
|
|
|
rate: true,
|
|
|
|
pitch: true,
|
|
|
|
volume: true,
|
2020-06-02 19:53:14 +00:00
|
|
|
is_speaking: true,
|
2020-09-24 19:26:30 +00:00
|
|
|
utterance_callbacks: true,
|
2019-03-24 21:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 17:40:03 +00:00
|
|
|
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();
|
Clean up speech synthesis properties, and implement everything for WinRT.
I'd previously attempted to normalize everything to `u8`, but this had some drawbacks:
* It failed to account for some synthesis drivers defining normal as mid-range, while most define it very low.
* It didn't track the normal value for a given synthesizer.
* There was no clean way to map a curve between the minimum, normal, and maximum rates.
Here we track the minimum, normal, and maximum values of rate, pitch, and volume. Sanity checks are done on set.
Also, as a further proof-of-concept, all properties are now implemented for the WinRT driver.
2020-05-18 23:12:59 +00:00
|
|
|
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();
|
2020-10-08 12:16:10 +00:00
|
|
|
let mut uid = NEXT_UTTERANCE_ID.lock().unwrap();
|
|
|
|
let utterance_id = UtteranceId::Web(*uid);
|
|
|
|
*uid += 1;
|
|
|
|
drop(uid);
|
|
|
|
let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
|
|
|
mappings.push((self.id, utterance_id));
|
|
|
|
drop(mappings);
|
|
|
|
let callback = Closure::wrap(Box::new(move |_evt: SpeechSynthesisEvent| {
|
2020-09-25 16:08:19 +00:00
|
|
|
let mut callbacks = CALLBACKS.lock().unwrap();
|
|
|
|
let callback = callbacks.get_mut(&id).unwrap();
|
|
|
|
if let Some(f) = callback.utterance_begin.as_mut() {
|
2020-09-24 19:26:30 +00:00
|
|
|
f(utterance_id);
|
|
|
|
}
|
|
|
|
}) as Box<dyn Fn(_)>);
|
|
|
|
utterance.set_onstart(Some(callback.as_ref().unchecked_ref()));
|
2020-10-08 12:16:10 +00:00
|
|
|
let callback = Closure::wrap(Box::new(move |_evt: SpeechSynthesisEvent| {
|
2020-09-25 16:08:19 +00:00
|
|
|
let mut callbacks = CALLBACKS.lock().unwrap();
|
|
|
|
let callback = callbacks.get_mut(&id).unwrap();
|
|
|
|
if let Some(f) = callback.utterance_end.as_mut() {
|
2020-09-24 19:26:30 +00:00
|
|
|
f(utterance_id);
|
|
|
|
}
|
2020-10-08 12:16:10 +00:00
|
|
|
let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
|
|
|
mappings.retain(|v| v.1 != utterance_id);
|
2020-09-24 19:26:30 +00:00
|
|
|
}) as Box<dyn Fn(_)>);
|
|
|
|
utterance.set_onend(Some(callback.as_ref().unchecked_ref()));
|
2020-10-08 12:56:45 +00:00
|
|
|
let callback = Closure::wrap(Box::new(move |evt: SpeechSynthesisErrorEvent| {
|
|
|
|
if evt.error() == SpeechSynthesisErrorCode::Cancel {
|
|
|
|
let mut callbacks = CALLBACKS.lock().unwrap();
|
|
|
|
let callback = callbacks.get_mut(&id).unwrap();
|
|
|
|
if let Some(f) = callback.utterance_stop.as_mut() {
|
|
|
|
f(utterance_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
|
|
|
mappings.retain(|v| v.1 != utterance_id);
|
|
|
|
}) as Box<dyn Fn(_)>);
|
|
|
|
utterance.set_onerror(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))
|
2020-09-22 17:40:03 +00:00
|
|
|
} else {
|
|
|
|
Err(Error::NoneError)
|
2018-12-30 17:13:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:52:18 +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(())
|
|
|
|
}
|
|
|
|
|
Clean up speech synthesis properties, and implement everything for WinRT.
I'd previously attempted to normalize everything to `u8`, but this had some drawbacks:
* It failed to account for some synthesis drivers defining normal as mid-range, while most define it very low.
* It didn't track the normal value for a given synthesizer.
* There was no clean way to map a curve between the minimum, normal, and maximum rates.
Here we track the minimum, normal, and maximum values of rate, pitch, and volume. Sanity checks are done on set.
Also, as a further proof-of-concept, all properties are now implemented for the WinRT driver.
2020-05-18 23:12:59 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
Clean up speech synthesis properties, and implement everything for WinRT.
I'd previously attempted to normalize everything to `u8`, but this had some drawbacks:
* It failed to account for some synthesis drivers defining normal as mid-range, while most define it very low.
* It didn't track the normal value for a given synthesizer.
* There was no clean way to map a curve between the minimum, normal, and maximum rates.
Here we track the minimum, normal, and maximum values of rate, pitch, and volume. Sanity checks are done on set.
Also, as a further proof-of-concept, all properties are now implemented for the WinRT driver.
2020-05-18 23:12:59 +00:00
|
|
|
fn set_rate(&mut self, rate: f32) -> Result<(), Error> {
|
2018-12-30 17:13:48 +00:00
|
|
|
self.rate = rate;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
Clean up speech synthesis properties, and implement everything for WinRT.
I'd previously attempted to normalize everything to `u8`, but this had some drawbacks:
* It failed to account for some synthesis drivers defining normal as mid-range, while most define it very low.
* It didn't track the normal value for a given synthesizer.
* There was no clean way to map a curve between the minimum, normal, and maximum rates.
Here we track the minimum, normal, and maximum values of rate, pitch, and volume. Sanity checks are done on set.
Also, as a further proof-of-concept, all properties are now implemented for the WinRT driver.
2020-05-18 23:12:59 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
Clean up speech synthesis properties, and implement everything for WinRT.
I'd previously attempted to normalize everything to `u8`, but this had some drawbacks:
* It failed to account for some synthesis drivers defining normal as mid-range, while most define it very low.
* It didn't track the normal value for a given synthesizer.
* There was no clean way to map a curve between the minimum, normal, and maximum rates.
Here we track the minimum, normal, and maximum values of rate, pitch, and volume. Sanity checks are done on set.
Also, as a further proof-of-concept, all properties are now implemented for the WinRT driver.
2020-05-18 23:12:59 +00:00
|
|
|
fn set_pitch(&mut self, pitch: f32) -> Result<(), Error> {
|
2018-12-30 17:13:48 +00:00
|
|
|
self.pitch = pitch;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
Clean up speech synthesis properties, and implement everything for WinRT.
I'd previously attempted to normalize everything to `u8`, but this had some drawbacks:
* It failed to account for some synthesis drivers defining normal as mid-range, while most define it very low.
* It didn't track the normal value for a given synthesizer.
* There was no clean way to map a curve between the minimum, normal, and maximum rates.
Here we track the minimum, normal, and maximum values of rate, pitch, and volume. Sanity checks are done on set.
Also, as a further proof-of-concept, all properties are now implemented for the WinRT driver.
2020-05-18 23:12:59 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
Clean up speech synthesis properties, and implement everything for WinRT.
I'd previously attempted to normalize everything to `u8`, but this had some drawbacks:
* It failed to account for some synthesis drivers defining normal as mid-range, while most define it very low.
* It didn't track the normal value for a given synthesizer.
* There was no clean way to map a curve between the minimum, normal, and maximum rates.
Here we track the minimum, normal, and maximum values of rate, pitch, and volume. Sanity checks are done on set.
Also, as a further proof-of-concept, all properties are now implemented for the WinRT driver.
2020-05-18 23:12:59 +00:00
|
|
|
fn set_volume(&mut self, volume: f32) -> Result<(), Error> {
|
2018-12-30 17:13:48 +00:00
|
|
|
self.volume = volume;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-02 19:53:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-10-08 12:16:10 +00:00
|
|
|
|
|
|
|
impl Drop for Web {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let mut mappings = UTTERANCE_MAPPINGS.lock().unwrap();
|
|
|
|
mappings.retain(|v| v.0 != self.id);
|
|
|
|
}
|
|
|
|
}
|