Compare commits

...

15 Commits

Author SHA1 Message Date
databasedav c4fae2cd31
Merge 2211818bc5 into 3c8ae0ae42 2024-02-19 15:27:28 +08:00
Nolan Darilek 3c8ae0ae42 Bump version. 2024-02-09 11:54:31 -06:00
Nolan Darilek 07edc20861 Switch `Arc` to `Rc` to appease Clippy. 2024-02-09 11:34:01 -06:00
Nolan Darilek 96a5209a9f Attempt to simplify CI. 2024-02-09 11:29:00 -06:00
Nolan Darilek 20b18949e2 Just build for target since we don't need an APK. 2024-02-09 11:07:11 -06:00
Nolan Darilek f29de0aede Switch to actions/checkout@v4. 2024-02-09 11:03:41 -06:00
Nolan Darilek 9e1476fd36
Merge pull request #49 from subalterngames:speech_dispatcher_voices_panic
Fixed a panic in SpeechDispatcher.voices()
2024-02-09 10:59:04 -06:00
Nolan Darilek 3032fe0fb3
Merge pull request #51 from Enyium:patch-1
Added docs.rs link to `Cargo.toml`
2024-02-09 10:54:59 -06:00
Nolan Darilek edd09c24e7 Switch from cargo-apk to xbuild. 2024-02-09 10:49:08 -06:00
Nolan Darilek b7b4e7dc85 Bump dependencies. 2024-02-09 10:37:55 -06:00
Nolan Darilek f593340051
Merge pull request #50 from MarijnS95:drop-ndk-glue
Drop `ndk-glue` dependency from the main crate
2024-02-09 10:31:54 -06:00
Enyium 12d8e1f532
Added docs.rs link to `Cargo.toml` 2023-12-05 12:28:47 +01:00
Marijn Suijten 2a81dc9b70 Drop `ndk-glue` dependency from the main crate
Commit d42d201 ("Update Android dependencies and example.") correctly
replaces `ndk-glue` with `ndk-context` as a more generic crate to hold on
to a global `JavaVM` and Android `jobject` `Context`, but didn't drop the
unused `ndk-glue` crate from the list of Android dependencies.  This
crate is only used in the example crate, and [shouldn't clobber
downstream crates].

Besides, `ndk-glue` has been deprecated for some time and should be
replaced by `android-activity` in the example in a followup PR.

[shouldn't clobber downstream crates]: https://github.com/emilk/egui/pull/3606/files#r1401313794
2023-11-22 00:12:48 +01:00
Esther Alter a0c6cbaf6a Fixed a panic in SpeechDispatcher.voices() 2023-10-25 11:59:13 -04:00
avi 2211818bc5 init 2023-04-13 01:07:05 -07:00
6 changed files with 98 additions and 28 deletions

View File

@ -12,7 +12,7 @@ jobs:
env: env:
CARGO_TOKEN: ${{ secrets.CARGO_TOKEN }} CARGO_TOKEN: ${{ secrets.CARGO_TOKEN }}
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- run: | - run: |
sudo apt-get update sudo apt-get update
sudo apt-get install -y libspeechd-dev sudo apt-get install -y libspeechd-dev

View File

@ -5,6 +5,17 @@ on:
pull_request: pull_request:
jobs: jobs:
check_formatting:
name: Check Formatting
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- run: |
rustup toolchain install stable
cargo fmt --all --check
cd examples/web
cargo fmt --all --check
check: check:
name: Check name: Check
strategy: strategy:
@ -12,41 +23,38 @@ jobs:
os: [windows-latest, ubuntu-22.04, macos-latest] os: [windows-latest, ubuntu-22.04, macos-latest]
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- run: sudo apt-get update; sudo apt-get install -y libspeechd-dev - run: sudo apt-get update; sudo apt-get install -y libspeechd-dev
if: ${{ runner.os == 'Linux' }} if: ${{ runner.os == 'Linux' }}
- run: | - run: |
rustup toolchain install stable rustup toolchain install stable
cargo fmt --check cargo clippy --all-targets
cargo clippy
check_web: check_web:
name: Check Web name: Check Web
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- run: | - run: |
rustup target add wasm32-unknown-unknown rustup target add wasm32-unknown-unknown
rustup toolchain install stable rustup toolchain install stable
cargo fmt --all --check cargo clippy --all-targets --target wasm32-unknown-unknown
cargo clippy --target wasm32-unknown-unknown
check_android: check_android:
name: Check Android name: Check Android
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- run: | - run: |
rustup target add aarch64-linux-android
rustup toolchain install stable rustup toolchain install stable
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android cargo clippy --all-targets --target aarch64-linux-android
cargo install -f cargo-apk
cargo apk build
check_web_example: check_web_example:
name: Check Web Example name: Check Web Example
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- run: | - run: |
rustup target add wasm32-unknown-unknown rustup target add wasm32-unknown-unknown
rustup toolchain install stable rustup toolchain install stable

View File

@ -1,9 +1,10 @@
[package] [package]
name = "tts" name = "tts"
version = "0.25.6" version = "0.26.0"
authors = ["Nolan Darilek <nolan@thewordnerd.info>"] authors = ["Nolan Darilek <nolan@thewordnerd.info>"]
repository = "https://github.com/ndarilek/tts-rs" repository = "https://github.com/ndarilek/tts-rs"
description = "High-level Text-To-Speech (TTS) interface" description = "High-level Text-To-Speech (TTS) interface"
documentation = "https://docs.rs/tts"
license = "MIT" license = "MIT"
exclude = ["*.cfg", "*.yml"] exclude = ["*.cfg", "*.yml"]
edition = "2021" edition = "2021"
@ -26,11 +27,11 @@ serde = { version = "1", optional = true, features = ["derive"] }
thiserror = "1" thiserror = "1"
[dev-dependencies] [dev-dependencies]
env_logger = "0.10" env_logger = "0.11"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
tolk = { version = "0.5", optional = true } tolk = { version = "0.5", optional = true }
windows = { version = "0.51", features = [ windows = { version = "0.52", features = [
"Foundation", "Foundation",
"Foundation_Collections", "Foundation_Collections",
"Media_Core", "Media_Core",
@ -64,7 +65,6 @@ web-sys = { version = "0.3", features = [
[target.'cfg(target_os="android")'.dependencies] [target.'cfg(target_os="android")'.dependencies]
jni = "0.21" jni = "0.21"
ndk-context = "0.1" ndk-context = "0.1"
ndk-glue = "0.7"
[package.metadata.docs.rs] [package.metadata.docs.rs]
no-default-features = true no-default-features = true

View File

@ -188,6 +188,7 @@ impl Backend for SpeechDispatcher {
.0 .0
.list_synthesis_voices()? .list_synthesis_voices()?
.iter() .iter()
.filter(|v| LanguageTag::parse(v.language.clone()).is_ok())
.map(|v| Voice { .map(|v| Voice {
id: v.name.clone(), id: v.name.clone(),
name: v.name.clone(), name: v.name.clone(),

View File

@ -59,6 +59,7 @@ impl Backend for Web {
voice: true, voice: true,
get_voice: true, get_voice: true,
utterance_callbacks: true, utterance_callbacks: true,
pause_resume: true,
} }
} }
@ -109,6 +110,22 @@ impl Backend for Web {
mappings.retain(|v| v.1 != utterance_id); mappings.retain(|v| v.1 != utterance_id);
}) as Box<dyn Fn(_)>); }) as Box<dyn Fn(_)>);
utterance.set_onerror(Some(callback.as_ref().unchecked_ref())); utterance.set_onerror(Some(callback.as_ref().unchecked_ref()));
let callback = Closure::wrap(Box::new(move |evt: SpeechSynthesisErrorEvent| {
let mut callbacks = CALLBACKS.lock().unwrap();
let callback = callbacks.get_mut(&id).unwrap();
if let Some(f) = callback.utterance_pause.as_mut() {
f(utterance_id);
}
}) as Box<dyn Fn(_)>);
utterance.set_onpause(Some(callback.as_ref().unchecked_ref()));
let callback = Closure::wrap(Box::new(move |evt: SpeechSynthesisErrorEvent| {
let mut callbacks = CALLBACKS.lock().unwrap();
let callback = callbacks.get_mut(&id).unwrap();
if let Some(f) = callback.utterance_resume.as_mut() {
f(utterance_id);
}
}) as Box<dyn Fn(_)>);
utterance.set_onresume(Some(callback.as_ref().unchecked_ref()));
if interrupt { if interrupt {
self.stop()?; self.stop()?;
} }
@ -130,6 +147,24 @@ impl Backend for Web {
Ok(()) Ok(())
} }
fn pause(&mut self) -> Result<(), Error> {
trace!("pause()");
if let Some(window) = web_sys::window() {
let speech_synthesis = window.speech_synthesis().unwrap();
speech_synthesis.pause();
}
Ok(())
}
fn resume(&mut self) -> Result<(), Error> {
trace!("resume()");
if let Some(window) = web_sys::window() {
let speech_synthesis = window.speech_synthesis().unwrap();
speech_synthesis.resume();
}
Ok(())
}
fn min_rate(&self) -> f32 { fn min_rate(&self) -> f32 {
0.1 0.1
} }

View File

@ -14,9 +14,10 @@ use std::collections::HashMap;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
use std::ffi::CStr; use std::ffi::CStr;
use std::fmt; use std::fmt;
use std::rc::Rc;
#[cfg(windows)] #[cfg(windows)]
use std::string::FromUtf16Error; use std::string::FromUtf16Error;
use std::sync::{Arc, Mutex}; use std::sync::Mutex;
use std::{boxed::Box, sync::RwLock}; use std::{boxed::Box, sync::RwLock};
#[cfg(any(target_os = "macos", target_os = "ios"))] #[cfg(any(target_os = "macos", target_os = "ios"))]
@ -169,6 +170,7 @@ pub struct Features {
pub voice: bool, pub voice: bool,
pub get_voice: bool, pub get_voice: bool,
pub volume: bool, pub volume: bool,
pub pause_resume: bool,
} }
impl fmt::Display for Features { impl fmt::Display for Features {
@ -244,6 +246,8 @@ struct Callbacks {
utterance_begin: Option<Box<dyn FnMut(UtteranceId)>>, utterance_begin: Option<Box<dyn FnMut(UtteranceId)>>,
utterance_end: Option<Box<dyn FnMut(UtteranceId)>>, utterance_end: Option<Box<dyn FnMut(UtteranceId)>>,
utterance_stop: Option<Box<dyn FnMut(UtteranceId)>>, utterance_stop: Option<Box<dyn FnMut(UtteranceId)>>,
utterance_pause: Option<Box<dyn FnMut(UtteranceId)>>,
utterance_resume: Option<Box<dyn FnMut(UtteranceId)>>,
} }
unsafe impl Send for Callbacks {} unsafe impl Send for Callbacks {}
@ -258,7 +262,7 @@ lazy_static! {
} }
#[derive(Clone)] #[derive(Clone)]
pub struct Tts(Arc<RwLock<Box<dyn Backend>>>); pub struct Tts(Rc<RwLock<Box<dyn Backend>>>);
unsafe impl Send for Tts {} unsafe impl Send for Tts {}
@ -271,18 +275,18 @@ impl Tts {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
Backends::SpeechDispatcher => { Backends::SpeechDispatcher => {
let tts = backends::SpeechDispatcher::new()?; let tts = backends::SpeechDispatcher::new()?;
Ok(Tts(Arc::new(RwLock::new(Box::new(tts))))) Ok(Tts(Rc::new(RwLock::new(Box::new(tts)))))
} }
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
Backends::Web => { Backends::Web => {
let tts = backends::Web::new()?; let tts = backends::Web::new()?;
Ok(Tts(Arc::new(RwLock::new(Box::new(tts))))) Ok(Tts(Rc::new(RwLock::new(Box::new(tts)))))
} }
#[cfg(all(windows, feature = "tolk"))] #[cfg(all(windows, feature = "tolk"))]
Backends::Tolk => { Backends::Tolk => {
let tts = backends::Tolk::new(); let tts = backends::Tolk::new();
if let Some(tts) = tts { if let Some(tts) = tts {
Ok(Tts(Arc::new(RwLock::new(Box::new(tts))))) Ok(Tts(Rc::new(RwLock::new(Box::new(tts)))))
} else { } else {
Err(Error::NoneError) Err(Error::NoneError)
} }
@ -290,20 +294,20 @@ impl Tts {
#[cfg(windows)] #[cfg(windows)]
Backends::WinRt => { Backends::WinRt => {
let tts = backends::WinRt::new()?; let tts = backends::WinRt::new()?;
Ok(Tts(Arc::new(RwLock::new(Box::new(tts))))) Ok(Tts(Rc::new(RwLock::new(Box::new(tts)))))
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
Backends::AppKit => Ok(Tts(Arc::new(RwLock::new(Box::new( Backends::AppKit => Ok(Tts(Rc::new(RwLock::new(
backends::AppKit::new()? Box::new(backends::AppKit::new()?),
))))), )))),
#[cfg(any(target_os = "macos", target_os = "ios"))] #[cfg(any(target_os = "macos", target_os = "ios"))]
Backends::AvFoundation => Ok(Tts(Arc::new(RwLock::new(Box::new( Backends::AvFoundation => Ok(Tts(Rc::new(RwLock::new(Box::new(
backends::AvFoundation::new()?, backends::AvFoundation::new()?,
))))), ))))),
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
Backends::Android => { Backends::Android => {
let tts = backends::Android::new()?; let tts = backends::Android::new()?;
Ok(Tts(Arc::new(RwLock::new(Box::new(tts))))) Ok(Tts(Rc::new(RwLock::new(Box::new(tts)))))
} }
}; };
if let Ok(backend) = backend { if let Ok(backend) = backend {
@ -385,6 +389,28 @@ impl Tts {
} }
} }
/// Pauses current speech.
pub fn pause(&mut self) -> Result<&Self, Error> {
let Features { pause, .. } = self.supported_features();
if pause {
self.0.write().unwrap().pause()?;
Ok(self)
} else {
Err(Error::UnsupportedFeature)
}
}
/// Resumes current speech.
pub fn resume(&mut self) -> Result<&Self, Error> {
let Features { resume, .. } = self.supported_features();
if resume {
self.0.write().unwrap().resume()?;
Ok(self)
} else {
Err(Error::UnsupportedFeature)
}
}
/// Returns the minimum rate for this speech synthesizer. /// Returns the minimum rate for this speech synthesizer.
pub fn min_rate(&self) -> f32 { pub fn min_rate(&self) -> f32 {
self.0.read().unwrap().min_rate() self.0.read().unwrap().min_rate()
@ -640,7 +666,7 @@ impl Tts {
impl Drop for Tts { impl Drop for Tts {
fn drop(&mut self) { fn drop(&mut self) {
if Arc::strong_count(&self.0) <= 1 { if Rc::strong_count(&self.0) <= 1 {
if let Some(id) = self.0.read().unwrap().id() { if let Some(id) = self.0.read().unwrap().id() {
let mut callbacks = CALLBACKS.lock().unwrap(); let mut callbacks = CALLBACKS.lock().unwrap();
callbacks.remove(&id); callbacks.remove(&id);