2018-12-30 17:13:48 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
use log::{info, trace};
|
|
|
|
use web_sys::SpeechSynthesisUtterance;
|
|
|
|
|
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
|
|
|
use crate::{Backend, Error, Features};
|
2018-12-30 17:13:48 +00:00
|
|
|
|
|
|
|
pub struct Web {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
Ok(Web {
|
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.,
|
2018-12-30 17:13:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Backend for Web {
|
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,
|
2019-03-24 21:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 17:13:48 +00:00
|
|
|
fn speak(&self, text: &str, interrupt: bool) -> Result<(), Error> {
|
|
|
|
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);
|
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);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(&self) -> Result<(), Error> {
|
|
|
|
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
|
|
|
}
|