2018-12-28 15:39:50 +00:00
|
|
|
/*!
|
|
|
|
* a Text-To-Speech (TTS) library providing high-level interfaces to a variety of backends.
|
2018-12-30 17:20:03 +00:00
|
|
|
* Currently supported backends are:
|
2020-08-13 16:15:23 +00:00
|
|
|
* * Windows
|
2020-10-09 00:08:18 +00:00
|
|
|
* * Screen readers/SAPI via Tolk (requires `use_tolk` Cargo feature)
|
2020-08-13 16:15:23 +00:00
|
|
|
* * WinRT
|
|
|
|
* * Linux via [Speech Dispatcher](https://freebsoft.org/speechd)
|
|
|
|
* * MacOS
|
|
|
|
* * AppKit on MacOS 10.13 and below
|
2020-08-18 20:22:12 +00:00
|
|
|
* * AVFoundation on MacOS 10.14 and above, and iOS
|
2018-12-30 17:20:03 +00:00
|
|
|
* * WebAssembly
|
2020-08-13 16:15:23 +00:00
|
|
|
*/
|
2018-12-28 15:39:50 +00:00
|
|
|
|
2020-08-18 20:16:30 +00:00
|
|
|
use std::boxed::Box;
|
2020-09-23 15:12:51 +00:00
|
|
|
use std::collections::HashMap;
|
2020-08-13 16:11:38 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
2020-08-18 20:16:30 +00:00
|
|
|
use std::ffi::CStr;
|
2020-09-23 15:12:51 +00:00
|
|
|
use std::sync::Mutex;
|
2020-08-13 16:08:00 +00:00
|
|
|
|
2020-09-22 19:29:45 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
2020-08-13 16:08:00 +00:00
|
|
|
use cocoa_foundation::base::id;
|
2020-11-03 03:27:13 +00:00
|
|
|
use dyn_clonable::*;
|
2020-09-23 15:12:51 +00:00
|
|
|
use lazy_static::lazy_static;
|
2020-08-13 16:08:00 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
use libc::c_char;
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
use objc::{class, msg_send, sel, sel_impl};
|
2020-05-18 20:01:28 +00:00
|
|
|
use thiserror::Error;
|
2020-09-22 19:51:59 +00:00
|
|
|
|
2018-12-14 19:35:49 +00:00
|
|
|
mod backends;
|
|
|
|
|
2020-11-03 03:27:13 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2018-12-14 19:35:49 +00:00
|
|
|
pub enum Backends {
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
SpeechDispatcher,
|
2018-12-30 17:13:48 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
Web,
|
2020-10-09 00:07:07 +00:00
|
|
|
#[cfg(all(windows, feature = "use_tolk"))]
|
2019-03-25 19:15:08 +00:00
|
|
|
Tolk,
|
2020-05-18 20:01:28 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
WinRT,
|
2020-08-11 17:11:19 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
2020-08-13 11:46:16 +00:00
|
|
|
AppKit,
|
2020-08-18 20:16:30 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
2020-08-13 16:08:00 +00:00
|
|
|
AvFoundation,
|
2018-12-14 19:35:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 15:31:21 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
2020-09-23 15:12:51 +00:00
|
|
|
pub enum BackendId {
|
2020-09-22 17:40:03 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
2020-09-23 15:12:51 +00:00
|
|
|
SpeechDispatcher(u64),
|
2020-09-22 17:40:03 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
Web(u64),
|
|
|
|
#[cfg(windows)]
|
|
|
|
WinRT(u64),
|
2020-09-22 19:08:19 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
2020-09-23 16:28:56 +00:00
|
|
|
AvFoundation(u64),
|
2020-09-22 17:40:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 12:16:10 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
2020-09-22 17:40:03 +00:00
|
|
|
pub enum UtteranceId {
|
|
|
|
#[cfg(target_os = "linux")]
|
2020-09-23 15:12:51 +00:00
|
|
|
SpeechDispatcher(u64),
|
2020-09-22 17:40:03 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2020-10-08 12:16:10 +00:00
|
|
|
Web(u64),
|
2020-09-22 17:40:03 +00:00
|
|
|
#[cfg(windows)]
|
2020-10-08 12:16:10 +00:00
|
|
|
WinRT(u64),
|
2020-09-23 16:28:56 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
|
|
|
AvFoundation(id),
|
2020-09-22 17:40:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 21:30:45 +00:00
|
|
|
pub struct Features {
|
2019-03-25 16:34:30 +00:00
|
|
|
pub stop: bool,
|
|
|
|
pub rate: bool,
|
|
|
|
pub pitch: bool,
|
|
|
|
pub volume: bool,
|
2020-06-02 19:53:14 +00:00
|
|
|
pub is_speaking: bool,
|
2020-09-23 15:12:51 +00:00
|
|
|
pub utterance_callbacks: bool,
|
2019-03-24 21:30:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 21:44:00 +00:00
|
|
|
impl Default for Features {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
stop: false,
|
|
|
|
rate: false,
|
|
|
|
pitch: false,
|
|
|
|
volume: false,
|
|
|
|
is_speaking: false,
|
2020-09-23 15:12:51 +00:00
|
|
|
utterance_callbacks: false,
|
2020-08-24 21:44:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 20:01:28 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum Error {
|
|
|
|
#[error("IO error: {0}")]
|
|
|
|
IO(#[from] std::io::Error),
|
2020-06-02 19:53:14 +00:00
|
|
|
#[error("Value not received")]
|
|
|
|
NoneError,
|
2020-06-02 19:57:21 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
2020-06-02 19:53:14 +00:00
|
|
|
#[error("JavaScript error: [0])]")]
|
|
|
|
JavaScriptError(wasm_bindgen::JsValue),
|
2020-05-18 20:01:28 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
#[error("WinRT error")]
|
|
|
|
WinRT(winrt::Error),
|
|
|
|
#[error("Unsupported feature")]
|
|
|
|
UnsupportedFeature,
|
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
|
|
|
#[error("Out of range")]
|
|
|
|
OutOfRange,
|
2020-05-18 20:01:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 03:27:13 +00:00
|
|
|
#[clonable]
|
2020-11-03 03:44:47 +00:00
|
|
|
trait Backend: Clone + std::fmt::Debug {
|
2020-09-23 15:12:51 +00:00
|
|
|
fn id(&self) -> Option<BackendId>;
|
2019-03-24 21:30:45 +00:00
|
|
|
fn supported_features(&self) -> Features;
|
2020-09-22 17:40:03 +00:00
|
|
|
fn speak(&mut self, text: &str, interrupt: bool) -> Result<Option<UtteranceId>, Error>;
|
2020-07-06 17:52:18 +00:00
|
|
|
fn stop(&mut self) -> Result<(), Error>;
|
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;
|
|
|
|
fn max_rate(&self) -> f32;
|
|
|
|
fn normal_rate(&self) -> f32;
|
|
|
|
fn get_rate(&self) -> Result<f32, Error>;
|
|
|
|
fn set_rate(&mut self, rate: f32) -> Result<(), Error>;
|
|
|
|
fn min_pitch(&self) -> f32;
|
|
|
|
fn max_pitch(&self) -> f32;
|
|
|
|
fn normal_pitch(&self) -> f32;
|
|
|
|
fn get_pitch(&self) -> Result<f32, Error>;
|
|
|
|
fn set_pitch(&mut self, pitch: f32) -> Result<(), Error>;
|
|
|
|
fn min_volume(&self) -> f32;
|
|
|
|
fn max_volume(&self) -> f32;
|
|
|
|
fn normal_volume(&self) -> f32;
|
|
|
|
fn get_volume(&self) -> Result<f32, Error>;
|
|
|
|
fn set_volume(&mut self, volume: f32) -> Result<(), Error>;
|
2020-06-02 19:53:14 +00:00
|
|
|
fn is_speaking(&self) -> Result<bool, Error>;
|
2018-12-14 19:35:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 17:03:55 +00:00
|
|
|
#[derive(Default)]
|
2020-09-23 15:12:51 +00:00
|
|
|
struct Callbacks {
|
2020-11-03 17:03:55 +00:00
|
|
|
utterance_begin: Option<Box<dyn FnMut(UtteranceId)>>,
|
|
|
|
utterance_end: Option<Box<dyn FnMut(UtteranceId)>>,
|
|
|
|
utterance_stop: Option<Box<dyn FnMut(UtteranceId)>>,
|
2020-09-23 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 16:08:19 +00:00
|
|
|
unsafe impl Send for Callbacks {}
|
|
|
|
|
|
|
|
unsafe impl Sync for Callbacks {}
|
|
|
|
|
2020-09-23 15:12:51 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref CALLBACKS: Mutex<HashMap<BackendId, Callbacks>> = {
|
|
|
|
let m: HashMap<BackendId, Callbacks> = HashMap::new();
|
|
|
|
Mutex::new(m)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-03 03:27:13 +00:00
|
|
|
#[derive(Clone)]
|
2019-09-30 15:36:20 +00:00
|
|
|
pub struct TTS(Box<dyn Backend>);
|
2018-12-14 19:35:49 +00:00
|
|
|
|
2020-10-14 08:54:53 +00:00
|
|
|
unsafe impl Send for TTS {}
|
2019-01-03 17:20:04 +00:00
|
|
|
|
2020-10-14 08:54:53 +00:00
|
|
|
unsafe impl Sync for TTS {}
|
2019-01-03 17:20:04 +00:00
|
|
|
|
2018-12-14 19:35:49 +00:00
|
|
|
impl TTS {
|
2018-12-28 15:39:50 +00:00
|
|
|
/**
|
|
|
|
* Create a new `TTS` instance with the specified backend.
|
2019-01-03 16:16:54 +00:00
|
|
|
*/
|
2018-12-30 17:13:48 +00:00
|
|
|
pub fn new(backend: Backends) -> Result<TTS, Error> {
|
2020-09-23 15:12:51 +00:00
|
|
|
let backend = match backend {
|
2018-12-14 19:35:49 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
2018-12-30 17:13:48 +00:00
|
|
|
Backends::SpeechDispatcher => Ok(TTS(Box::new(backends::SpeechDispatcher::new()))),
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
Backends::Web => {
|
|
|
|
let tts = backends::Web::new()?;
|
|
|
|
Ok(TTS(Box::new(tts)))
|
2019-01-03 16:16:54 +00:00
|
|
|
}
|
2020-10-09 00:07:07 +00:00
|
|
|
#[cfg(all(windows, feature = "use_tolk"))]
|
2019-03-25 19:15:08 +00:00
|
|
|
Backends::Tolk => {
|
|
|
|
let tts = backends::Tolk::new();
|
2020-06-11 18:00:24 +00:00
|
|
|
if let Some(tts) = tts {
|
|
|
|
Ok(TTS(Box::new(tts)))
|
|
|
|
} else {
|
|
|
|
Err(Error::NoneError)
|
|
|
|
}
|
2019-12-23 13:37:48 +00:00
|
|
|
}
|
2020-05-18 20:01:28 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
Backends::WinRT => {
|
|
|
|
let tts = backends::winrt::WinRT::new()?;
|
|
|
|
Ok(TTS(Box::new(tts)))
|
|
|
|
}
|
2020-08-11 17:11:19 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
2020-08-13 11:46:16 +00:00
|
|
|
Backends::AppKit => Ok(TTS(Box::new(backends::AppKit::new()))),
|
2020-08-18 20:16:30 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
2020-08-13 16:08:00 +00:00
|
|
|
Backends::AvFoundation => Ok(TTS(Box::new(backends::AvFoundation::new()))),
|
2020-09-23 15:12:51 +00:00
|
|
|
};
|
2020-09-26 17:43:16 +00:00
|
|
|
if let Ok(backend) = backend {
|
2020-09-23 15:12:51 +00:00
|
|
|
if let Some(id) = backend.0.id() {
|
2020-09-24 22:56:46 +00:00
|
|
|
let mut callbacks = CALLBACKS.lock().unwrap();
|
2020-11-03 17:03:55 +00:00
|
|
|
callbacks.insert(id, Callbacks::default());
|
2020-09-23 15:12:51 +00:00
|
|
|
}
|
|
|
|
Ok(backend)
|
|
|
|
} else {
|
|
|
|
backend
|
2018-12-14 19:35:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 17:13:48 +00:00
|
|
|
pub fn default() -> Result<TTS, Error> {
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
let tts = TTS::new(Backends::SpeechDispatcher);
|
2020-10-09 00:07:07 +00:00
|
|
|
#[cfg(all(windows, feature = "use_tolk"))]
|
2020-09-26 17:43:16 +00:00
|
|
|
let tts = if let Ok(tts) = TTS::new(Backends::Tolk) {
|
2020-06-11 18:00:24 +00:00
|
|
|
Ok(tts)
|
|
|
|
} else {
|
|
|
|
TTS::new(Backends::WinRT)
|
2020-05-18 20:01:28 +00:00
|
|
|
};
|
2020-10-09 00:07:07 +00:00
|
|
|
#[cfg(all(windows, not(feature = "use_tolk")))]
|
|
|
|
let tts = TTS::new(Backends::WinRT);
|
2018-12-30 17:13:48 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
let tts = TTS::new(Backends::Web);
|
2020-08-11 17:11:19 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
2020-08-13 16:08:00 +00:00
|
|
|
let tts = unsafe {
|
|
|
|
// Needed because the Rust NSProcessInfo structs report bogus values, and I don't want to pull in a full bindgen stack.
|
|
|
|
let pi: id = msg_send![class!(NSProcessInfo), new];
|
|
|
|
let version: id = msg_send![pi, operatingSystemVersionString];
|
|
|
|
let str: *const c_char = msg_send![version, UTF8String];
|
|
|
|
let str = CStr::from_ptr(str);
|
|
|
|
let str = str.to_string_lossy();
|
|
|
|
let version: Vec<&str> = str.split(" ").collect();
|
|
|
|
let version = version[1];
|
|
|
|
let version_parts: Vec<&str> = version.split(".").collect();
|
2020-11-25 16:13:17 +00:00
|
|
|
let major_version: i8 = version_parts[0].parse().unwrap();
|
2020-08-13 16:08:00 +00:00
|
|
|
let minor_version: i8 = version_parts[1].parse().unwrap();
|
2020-11-25 16:13:17 +00:00
|
|
|
if major_version >= 11 || minor_version >= 14 {
|
2020-08-13 16:08:00 +00:00
|
|
|
TTS::new(Backends::AvFoundation)
|
|
|
|
} else {
|
|
|
|
TTS::new(Backends::AppKit)
|
|
|
|
}
|
|
|
|
};
|
2020-08-18 20:16:30 +00:00
|
|
|
#[cfg(target_os = "ios")]
|
|
|
|
let tts = TTS::new(Backends::AvFoundation);
|
2018-12-30 17:13:48 +00:00
|
|
|
tts
|
|
|
|
}
|
|
|
|
|
2019-03-24 21:30:45 +00:00
|
|
|
/**
|
|
|
|
* Returns the features supported by this TTS engine
|
|
|
|
*/
|
|
|
|
pub fn supported_features(&self) -> Features {
|
|
|
|
self.0.supported_features()
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:39:50 +00:00
|
|
|
/**
|
|
|
|
* Speaks the specified text, optionally interrupting current speech.
|
2019-01-03 16:16:54 +00:00
|
|
|
*/
|
2020-09-22 17:40:03 +00:00
|
|
|
pub fn speak<S: Into<String>>(
|
|
|
|
&mut self,
|
|
|
|
text: S,
|
|
|
|
interrupt: bool,
|
|
|
|
) -> Result<Option<UtteranceId>, Error> {
|
|
|
|
self.0.speak(text.into().as_str(), interrupt)
|
2018-12-14 19:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 15:39:50 +00:00
|
|
|
/**
|
|
|
|
* Stops current speech.
|
2019-01-03 16:16:54 +00:00
|
|
|
*/
|
2020-07-06 17:52:18 +00:00
|
|
|
pub fn stop(&mut self) -> Result<&Self, Error> {
|
2019-03-24 21:30:45 +00:00
|
|
|
let Features { stop, .. } = self.supported_features();
|
|
|
|
if stop {
|
|
|
|
self.0.stop()?;
|
|
|
|
Ok(self)
|
|
|
|
} else {
|
2020-05-18 20:01:28 +00:00
|
|
|
Err(Error::UnsupportedFeature)
|
2019-03-24 21:30:45 +00:00
|
|
|
}
|
2018-12-28 14:49:02 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Returns the minimum rate for this speech synthesizer.
|
|
|
|
*/
|
|
|
|
pub fn min_rate(&self) -> f32 {
|
|
|
|
self.0.min_rate()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the maximum rate for this speech synthesizer.
|
|
|
|
*/
|
|
|
|
pub fn max_rate(&self) -> f32 {
|
|
|
|
self.0.max_rate()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the normal rate for this speech synthesizer.
|
|
|
|
*/
|
|
|
|
pub fn normal_rate(&self) -> f32 {
|
|
|
|
self.0.normal_rate()
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:39:50 +00:00
|
|
|
/**
|
|
|
|
* Gets the current speech rate.
|
2019-01-03 16:16:54 +00:00
|
|
|
*/
|
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
|
|
|
pub fn get_rate(&self) -> Result<f32, Error> {
|
2019-03-24 21:30:45 +00:00
|
|
|
let Features { rate, .. } = self.supported_features();
|
|
|
|
if rate {
|
|
|
|
self.0.get_rate()
|
|
|
|
} else {
|
2020-05-18 20:01:28 +00:00
|
|
|
Err(Error::UnsupportedFeature)
|
2019-03-24 21:30:45 +00:00
|
|
|
}
|
2018-12-14 19:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 15:39:50 +00:00
|
|
|
/**
|
|
|
|
* Sets the desired speech rate.
|
2019-01-03 16:16:54 +00:00
|
|
|
*/
|
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
|
|
|
pub fn set_rate(&mut self, rate: f32) -> Result<&Self, Error> {
|
2019-03-25 16:34:30 +00:00
|
|
|
let Features {
|
|
|
|
rate: rate_feature, ..
|
|
|
|
} = self.supported_features();
|
2019-03-24 21:30:45 +00:00
|
|
|
if rate_feature {
|
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
|
|
|
if rate < self.0.min_rate() || rate > self.0.max_rate() {
|
|
|
|
Err(Error::OutOfRange)
|
|
|
|
} else {
|
|
|
|
self.0.set_rate(rate)?;
|
|
|
|
Ok(self)
|
|
|
|
}
|
2019-03-24 21:30:45 +00:00
|
|
|
} else {
|
2020-05-18 20:01:28 +00:00
|
|
|
Err(Error::UnsupportedFeature)
|
2019-03-24 21:30:45 +00:00
|
|
|
}
|
2018-12-14 19:35:49 +00:00
|
|
|
}
|
2018-12-15 15:56:13 +00:00
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Returns the minimum pitch for this speech synthesizer.
|
|
|
|
*/
|
|
|
|
pub fn min_pitch(&self) -> f32 {
|
|
|
|
self.0.min_pitch()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the maximum pitch for this speech synthesizer.
|
|
|
|
*/
|
|
|
|
pub fn max_pitch(&self) -> f32 {
|
|
|
|
self.0.max_pitch()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the normal pitch for this speech synthesizer.
|
|
|
|
*/
|
|
|
|
pub fn normal_pitch(&self) -> f32 {
|
|
|
|
self.0.normal_pitch()
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:39:50 +00:00
|
|
|
/**
|
|
|
|
* Gets the current speech pitch.
|
2019-01-03 16:16:54 +00:00
|
|
|
*/
|
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
|
|
|
pub fn get_pitch(&self) -> Result<f32, Error> {
|
2019-03-24 21:30:45 +00:00
|
|
|
let Features { pitch, .. } = self.supported_features();
|
|
|
|
if pitch {
|
|
|
|
self.0.get_pitch()
|
|
|
|
} else {
|
2020-05-18 20:01:28 +00:00
|
|
|
Err(Error::UnsupportedFeature)
|
2019-03-24 21:30:45 +00:00
|
|
|
}
|
2018-12-15 15:56:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 15:39:50 +00:00
|
|
|
/**
|
|
|
|
* Sets the desired speech pitch.
|
2019-01-03 16:16:54 +00:00
|
|
|
*/
|
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
|
|
|
pub fn set_pitch(&mut self, pitch: f32) -> Result<&Self, Error> {
|
2019-03-25 16:34:30 +00:00
|
|
|
let Features {
|
|
|
|
pitch: pitch_feature,
|
|
|
|
..
|
|
|
|
} = self.supported_features();
|
2019-03-24 21:30:45 +00:00
|
|
|
if pitch_feature {
|
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
|
|
|
if pitch < self.0.min_pitch() || pitch > self.0.max_pitch() {
|
|
|
|
Err(Error::OutOfRange)
|
|
|
|
} else {
|
|
|
|
self.0.set_pitch(pitch)?;
|
|
|
|
Ok(self)
|
|
|
|
}
|
2019-03-24 21:30:45 +00:00
|
|
|
} else {
|
2020-05-18 20:01:28 +00:00
|
|
|
Err(Error::UnsupportedFeature)
|
2019-03-24 21:30:45 +00:00
|
|
|
}
|
2018-12-15 15:56:13 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Returns the minimum volume for this speech synthesizer.
|
|
|
|
*/
|
|
|
|
pub fn min_volume(&self) -> f32 {
|
|
|
|
self.0.min_volume()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the maximum volume for this speech synthesizer.
|
|
|
|
*/
|
|
|
|
pub fn max_volume(&self) -> f32 {
|
|
|
|
self.0.max_volume()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the normal volume for this speech synthesizer.
|
|
|
|
*/
|
|
|
|
pub fn normal_volume(&self) -> f32 {
|
|
|
|
self.0.normal_volume()
|
|
|
|
}
|
|
|
|
|
2018-12-28 15:39:50 +00:00
|
|
|
/**
|
|
|
|
* Gets the current speech volume.
|
2019-01-03 16:16:54 +00:00
|
|
|
*/
|
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
|
|
|
pub fn get_volume(&self) -> Result<f32, Error> {
|
2019-03-24 21:30:45 +00:00
|
|
|
let Features { volume, .. } = self.supported_features();
|
|
|
|
if volume {
|
|
|
|
self.0.get_volume()
|
|
|
|
} else {
|
2020-05-18 20:01:28 +00:00
|
|
|
Err(Error::UnsupportedFeature)
|
2019-03-24 21:30:45 +00:00
|
|
|
}
|
2018-12-15 15:56:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 15:39:50 +00:00
|
|
|
/**
|
|
|
|
* Sets the desired speech volume.
|
2019-01-03 16:16:54 +00:00
|
|
|
*/
|
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
|
|
|
pub fn set_volume(&mut self, volume: f32) -> Result<&Self, Error> {
|
2019-03-25 16:34:30 +00:00
|
|
|
let Features {
|
|
|
|
volume: volume_feature,
|
|
|
|
..
|
|
|
|
} = self.supported_features();
|
2019-03-24 21:30:45 +00:00
|
|
|
if volume_feature {
|
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
|
|
|
if volume < self.0.min_volume() || volume > self.0.max_volume() {
|
|
|
|
Err(Error::OutOfRange)
|
|
|
|
} else {
|
|
|
|
self.0.set_volume(volume)?;
|
|
|
|
Ok(self)
|
|
|
|
}
|
2019-03-24 21:30:45 +00:00
|
|
|
} else {
|
2020-05-18 20:01:28 +00:00
|
|
|
Err(Error::UnsupportedFeature)
|
2019-03-24 21:30:45 +00:00
|
|
|
}
|
2018-12-14 19:35:49 +00:00
|
|
|
}
|
2020-06-02 19:53:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this speech synthesizer is speaking.
|
|
|
|
*/
|
|
|
|
pub fn is_speaking(&self) -> Result<bool, Error> {
|
|
|
|
let Features { is_speaking, .. } = self.supported_features();
|
|
|
|
if is_speaking {
|
|
|
|
self.0.is_speaking()
|
|
|
|
} else {
|
|
|
|
Err(Error::UnsupportedFeature)
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 15:12:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when this speech synthesizer begins speaking an utterance.
|
|
|
|
*/
|
2020-09-25 16:08:19 +00:00
|
|
|
pub fn on_utterance_begin(
|
|
|
|
&self,
|
2020-11-03 17:03:55 +00:00
|
|
|
callback: Option<Box<dyn FnMut(UtteranceId)>>,
|
2020-09-25 16:08:19 +00:00
|
|
|
) -> Result<(), Error> {
|
2020-09-23 15:12:51 +00:00
|
|
|
let Features {
|
|
|
|
utterance_callbacks,
|
|
|
|
..
|
|
|
|
} = self.supported_features();
|
|
|
|
if utterance_callbacks {
|
|
|
|
let mut callbacks = CALLBACKS.lock().unwrap();
|
|
|
|
let id = self.0.id().unwrap();
|
|
|
|
let mut callbacks = callbacks.get_mut(&id).unwrap();
|
|
|
|
callbacks.utterance_begin = callback;
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::UnsupportedFeature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when this speech synthesizer finishes speaking an utterance.
|
|
|
|
*/
|
2020-09-25 16:08:19 +00:00
|
|
|
pub fn on_utterance_end(
|
|
|
|
&self,
|
2020-11-03 17:03:55 +00:00
|
|
|
callback: Option<Box<dyn FnMut(UtteranceId)>>,
|
2020-09-25 16:08:19 +00:00
|
|
|
) -> Result<(), Error> {
|
2020-09-23 15:12:51 +00:00
|
|
|
let Features {
|
|
|
|
utterance_callbacks,
|
|
|
|
..
|
|
|
|
} = self.supported_features();
|
|
|
|
if utterance_callbacks {
|
|
|
|
let mut callbacks = CALLBACKS.lock().unwrap();
|
|
|
|
let id = self.0.id().unwrap();
|
|
|
|
let mut callbacks = callbacks.get_mut(&id).unwrap();
|
|
|
|
callbacks.utterance_end = callback;
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::UnsupportedFeature)
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 08:51:08 +00:00
|
|
|
|
2020-10-08 12:56:45 +00:00
|
|
|
/**
|
|
|
|
* Called when this speech synthesizer is stopped and still has utterances in its queue.
|
|
|
|
*/
|
|
|
|
pub fn on_utterance_stop(
|
|
|
|
&self,
|
2020-11-03 17:03:55 +00:00
|
|
|
callback: Option<Box<dyn FnMut(UtteranceId)>>,
|
2020-10-08 12:56:45 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
let Features {
|
|
|
|
utterance_callbacks,
|
|
|
|
..
|
|
|
|
} = self.supported_features();
|
|
|
|
if utterance_callbacks {
|
|
|
|
let mut callbacks = CALLBACKS.lock().unwrap();
|
|
|
|
let id = self.0.id().unwrap();
|
|
|
|
let mut callbacks = callbacks.get_mut(&id).unwrap();
|
|
|
|
callbacks.utterance_stop = callback;
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::UnsupportedFeature)
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 15:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TTS {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(id) = self.0.id() {
|
|
|
|
let mut callbacks = CALLBACKS.lock().unwrap();
|
|
|
|
callbacks.remove(&id);
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 19:35:49 +00:00
|
|
|
}
|