1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-17 08:39:38 +00:00

Add initial Android stubs.

This commit is contained in:
Nolan Darilek 2020-12-27 09:41:11 -06:00
parent 6d17447350
commit 5849e340c9
4 changed files with 131 additions and 0 deletions

View File

@ -39,3 +39,6 @@ objc = "0.2"
[target.wasm32-unknown-unknown.dependencies]
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["EventTarget", "SpeechSynthesis", "SpeechSynthesisErrorCode", "SpeechSynthesisErrorEvent", "SpeechSynthesisEvent", "SpeechSynthesisUtterance", "Window", ] }
[target.'cfg(target_os="android")'.dependencies]
jni = "0.18"

114
src/backends/android.rs Normal file
View File

@ -0,0 +1,114 @@
#[cfg(target_os = "android")]
use std::sync::Mutex;
use lazy_static::lazy_static;
use log::info;
use crate::{Backend, BackendId, Error, Features, UtteranceId, CALLBACKS};
lazy_static! {
static ref NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0);
}
#[derive(Clone, Debug)]
pub(crate) struct Android(BackendId);
impl Android {
pub(crate) fn new() -> Self {
info!("Initializing Android backend");
let mut backend_id = NEXT_BACKEND_ID.lock().unwrap();
let bid = BackendId::Android(*backend_id);
*backend_id += 1;
Self(bid)
}
}
impl Backend for Android {
fn id(&self) -> Option<BackendId> {
Some(self.0)
}
fn supported_features(&self) -> Features {
Features {
stop: false,
rate: false,
pitch: false,
volume: false,
is_speaking: false,
utterance_callbacks: false,
}
}
fn speak(&mut self, text: &str, interrupt: bool) -> Result<Option<UtteranceId>, Error> {
println!("Speaking {}, {:?}", text, interrupt);
Ok(None)
}
fn stop(&mut self) -> Result<(), Error> {
todo!()
}
fn min_rate(&self) -> f32 {
todo!()
}
fn max_rate(&self) -> f32 {
todo!()
}
fn normal_rate(&self) -> f32 {
todo!()
}
fn get_rate(&self) -> Result<f32, Error> {
todo!()
}
fn set_rate(&mut self, rate: f32) -> Result<(), Error> {
todo!()
}
fn min_pitch(&self) -> f32 {
todo!()
}
fn max_pitch(&self) -> f32 {
todo!()
}
fn normal_pitch(&self) -> f32 {
todo!()
}
fn get_pitch(&self) -> Result<f32, Error> {
todo!()
}
fn set_pitch(&mut self, pitch: f32) -> Result<(), Error> {
todo!()
}
fn min_volume(&self) -> f32 {
todo!()
}
fn max_volume(&self) -> f32 {
todo!()
}
fn normal_volume(&self) -> f32 {
todo!()
}
fn get_volume(&self) -> Result<f32, Error> {
todo!()
}
fn set_volume(&mut self, volume: f32) -> Result<(), Error> {
todo!()
}
fn is_speaking(&self) -> Result<bool, Error> {
todo!()
}
}

View File

@ -16,6 +16,9 @@ mod appkit;
#[cfg(any(target_os = "macos", target_os = "ios"))]
mod av_foundation;
#[cfg(target_os = "android")]
mod android;
#[cfg(target_os = "linux")]
pub(crate) use self::speech_dispatcher::*;
@ -30,3 +33,6 @@ pub(crate) use self::appkit::*;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) use self::av_foundation::*;
#[cfg(target_os = "android")]
pub(crate) use self::android::*;

View File

@ -43,6 +43,8 @@ pub enum Backends {
AppKit,
#[cfg(any(target_os = "macos", target_os = "ios"))]
AvFoundation,
#[cfg(target_os = "android")]
Android,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
@ -55,6 +57,8 @@ pub enum BackendId {
WinRT(u64),
#[cfg(any(target_os = "macos", target_os = "ios"))]
AvFoundation(u64),
#[cfg(target_os = "android")]
Android(u64),
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
@ -193,6 +197,8 @@ impl TTS {
Backends::AppKit => Ok(TTS(Box::new(backends::AppKit::new()))),
#[cfg(any(target_os = "macos", target_os = "ios"))]
Backends::AvFoundation => Ok(TTS(Box::new(backends::AvFoundation::new()))),
#[cfg(target_os = "android")]
Backends::Android => Ok(TTS(Box::new(backends::Android::new()))),
};
if let Ok(backend) = backend {
if let Some(id) = backend.0.id() {
@ -239,6 +245,8 @@ impl TTS {
};
#[cfg(target_os = "ios")]
let tts = TTS::new(Backends::AvFoundation);
#[cfg(target_os = "android")]
let tts = TTS::new(Backends::Android);
tts
}