Compare commits

...

65 Commits

Author SHA1 Message Date
Nolan Darilek 1df00952e4 Update repositories and remove Gitlab CI configuration.
continuous-integration/drone/push Build is passing Details
2022-11-22 14:07:10 -06:00
Nolan Darilek db43d95cf7 Fix indentation.
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2022-11-22 13:59:51 -06:00
Nolan Darilek 89fcced31e Update CI build configuration. 2022-11-22 13:55:55 -06:00
Nolan Darilek 1c11f279ec Bump version and use correct features in docs.rs builds. 2022-11-22 13:52:19 -06:00
Nolan Darilek f854e386a6 Re-add support for speech-dispatcher 0.9. 2022-11-22 13:49:54 -06:00
Nolan Darilek 1ba40e898b Merge branch 'master' into 'master'
Fix missing size_t on latest stable

See merge request ndarilek/speech-dispatcher-rs!7
2022-10-19 14:40:39 +00:00
Tait Hoyem bcf7025f14 Fix missing size_t on latest stable 2022-10-16 15:10:24 -06:00
Nolan Darilek 2239c2539a Publish with features compatible for Rust CI image. 2022-09-07 12:13:55 -05:00
Nolan Darilek 2ea85bc1bd Bump version and drop support for 0.9. 2022-09-07 11:43:22 -05:00
Nolan Darilek 0efdccc9aa Merge branch '32bit' into 'master'
Fix msg_id and client_id being size_t in C

Closes #5

See merge request ndarilek/speech-dispatcher-rs!5
2022-09-07 14:53:43 +00:00
Nolan Darilek e770c73e67 Merge branch 'voice_type' into 'master'
Fix voice_type signedness change

Closes #4

See merge request ndarilek/speech-dispatcher-rs!6
2022-09-07 14:48:56 +00:00
Samuel Thibault 8ff6902148 Fix voice_type signedness change
The signedness changed happened in speech-dispatcher 0.11, not in
speech-dispatcher 0.10.2.

Closes #4
2022-09-07 15:54:53 +02:00
Samuel Thibault 29f990e19b Fix msg_id and client_id being size_t in C
instead of u64 which is only valid on 64bit architectures.

Fixes #5
2022-09-07 15:31:10 +02:00
Nolan Darilek 6a6bc3f805 Add Drone/cargo-release/git-cliff configuration. 2022-09-04 09:15:13 -05:00
Nolan Darilek a50a6a4bdd Should now build with default features. 2022-08-29 16:36:33 -05:00
Nolan Darilek d6f9043e60 Add support for speech-dispatcher 0.10.2 and bump version. 2022-08-29 16:29:57 -05:00
Nolan Darilek 4e1b79cb84 Don't build with default features on docs.rs. 2022-06-13 10:17:18 -05:00
Nolan Darilek 8c69d78411 And of course Cargo complains about my feature name only at publication time. Fixed. 2022-03-10 13:27:50 -06:00
Nolan Darilek 0ba2937a8a Update CI since build environment doesn't run a newer speech-dispatcher. 2022-03-10 13:13:19 -06:00
Nolan Darilek be9e4592ec And another... 2022-03-10 13:00:17 -06:00
Nolan Darilek 6aacce2d73 Feature mismatch. 2022-03-10 12:58:56 -06:00
Nolan Darilek bbae5dc983 Add 0.10 feature. 2022-03-10 12:54:53 -06:00
Nolan Darilek c6b90a7a24 Cast values for compatibility with newer speech-dispatcher. 2022-03-10 12:29:27 -06:00
Nolan Darilek ee6aba7a97 Cast to u32. 2022-03-10 12:17:10 -06:00
Nolan Darilek 36f82b78f3 Bump version. 2022-03-10 11:40:18 -06:00
Nolan Darilek 729aaf5255 Soundness fixes for cloning `Connection`. 2022-03-10 11:39:42 -06:00
Nolan Darilek 2618393758 Relicense and bump version. 2022-02-05 09:15:15 -06:00
Nolan Darilek 82090cb48d Bump versions. 2022-01-27 10:29:10 -06:00
Nolan Darilek 3709573305 Use `Voice` directly to avoid messing with stringly-typed voice names. 2022-01-27 10:26:12 -06:00
Nolan Darilek 911e98d9ec Clean up example. 2022-01-27 10:21:53 -06:00
Nolan Darilek 8434bfca64 Bump edition. 2022-01-27 10:06:41 -06:00
Nolan Darilek 0dc9205b36 Merge branch 'lv' into 'master'
Add support for listing voices

See merge request ndarilek/speech-dispatcher-rs!2
2022-01-27 16:01:45 +00:00
Malloc Voidstar 143147036c
Add support for listing voices
Rebased on top of the result changes.
2022-01-10 18:26:02 -08:00
Nolan Darilek 2879284030 Bump version. 2022-01-10 10:31:14 -06:00
Nolan Darilek 5dfc99c6d7 Merge branch 'return-results' into 'master'
Change all bool-returns to Results

See merge request ndarilek/speech-dispatcher-rs!4
2022-01-10 16:26:29 +00:00
Malloc Voidstar 83b1ac5a76
Change SpeechDispatcherError to Error, format
Also lowercase Display strings to be in line with https://rust-lang.github.io/api-guidelines/interoperability.html#examples-of-error-messages
2021-12-06 10:58:29 -08:00
Malloc Voidstar 7d3edccdda
Convert all bool-returns to Results
Additionally:
* Make open2 fallible too
* Use a Result the entire time in open and open2, instead of going from Option to Result
* Specify c_int instead of i32 since apparently the size "may differ on some esoteric systems"; I suspect it won't compile on whatever those are but might as well improve the situation
* Avoid a maybe-possible panic in get_voice_type. Probably can't happen but I'm not 100% certain, so I made it fallible
* Add a missing null check to get_language
2021-12-06 10:57:04 -08:00
Nolan Darilek 91098c0f01 Merge branch 'unused' into 'master'
Remove unused gcc dependency

See merge request ndarilek/speech-dispatcher-rs!3
2021-12-06 15:04:29 +00:00
Malloc Voidstar 675b569b32
Remove unused gcc dependency 2021-12-04 13:50:25 -08:00
Nolan Darilek e0170aa011 Bump version. 2021-12-02 09:11:26 -06:00
Nolan Darilek 3b025dc0f9 Merge branch 'fix-types' into 'master'
Fix build on ARM

See merge request ndarilek/speech-dispatcher-rs!1
2021-12-02 15:09:21 +00:00
Malloc Voidstar 67a8c19410
Fix build on ARM
Differences in signedness were preventing builds.
2021-12-02 05:34:32 -08:00
Nolan Darilek ac12fb132e Bump version. 2021-11-19 08:39:16 -06:00
Nolan Darilek 4c8521ccb8 Add error handling for initialization failure. 2021-11-19 08:38:17 -06:00
Nolan Darilek 006a4835be Eliminate a Clippy warning. 2020-09-26 12:52:10 -05:00
Nolan Darilek 5349a0fc3d Refactor to FnMut and bump version. 2020-09-25 10:36:00 -05:00
Nolan Darilek 7f952f2f34 Specify minimum bindgen version constraint. 2020-09-18 12:06:32 -05:00
Nolan Darilek f71e4b0d19 Bump bindgen and version. 2020-09-18 11:21:39 -05:00
Nolan Darilek 8b28fed8e5 cargo fmt 2020-08-19 21:00:51 -05:00
Nolan Darilek bf348af84f Make client ID easier to get at.
* Pass `client_id` into all callbacks.
* Expose via `client_id()` method.
* Bump version.
2020-08-19 20:59:48 -05:00
Nolan Darilek e022973c8e Update CI. 2020-08-19 19:55:31 -05:00
Nolan Darilek ca2e0ffe5e Upgrades and refactors.
* Bump editions to 2018.
* Bump dependencies and crate versions.
* Implement callbacks.
* Return message IDs when speaking, or `None` in case of errors.
* Add callbacks to example.
2020-08-19 17:28:22 -05:00
Nolan Darilek 0de61beafe Bump dependencies. 2020-05-12 16:11:52 -05:00
Nolan Darilek 0f06715c39 Don't fail the publish step if speech-dispatcher-sys fails. 2019-12-30 09:24:12 -06:00
Nolan Darilek e99da4a1d3 Bump version. 2019-12-30 09:09:43 -06:00
Nolan Darilek 2758f79538 Add defensive handling for speaking empty strings, which appears to crash the client. 2019-12-30 09:09:19 -06:00
Nolan Darilek 369bf23fa9 Clippy, fmt, bump versions, and update repository links. 2019-12-23 08:03:24 -06:00
Nolan Darilek cb02834873 Loosen version requirement. 2019-12-22 13:56:51 -06:00
Nolan Darilek ebb3078c8a Bump dependencies and version. 2019-12-22 13:49:58 -06:00
Nolan Darilek cc22ede3f2 Try publishing sys first. 2019-12-22 13:36:50 -06:00
Nolan Darilek 34ca29d425 Add version to dependency. 2019-12-22 13:22:23 -06:00
Nolan Darilek a0bc337852 Update CI build script for multiple manifests. 2019-12-22 12:16:36 -06:00
Nolan Darilek 3da5140735 Switch to a workspace. 2019-12-22 12:12:27 -06:00
Nolan Darilek e919820f9d Bump version. 2019-09-30 10:20:49 -05:00
Nolan Darilek cfdc01a132 Fix typo. 2019-09-30 10:20:31 -05:00
16 changed files with 1103 additions and 580 deletions

27
.drone.yml Normal file
View File

@ -0,0 +1,27 @@
kind: pipeline
type: docker
name: default
steps:
- name: test
image: rust
commands:
- rustup component add clippy rustfmt
- apt-get update -qq
- apt-get install -qqy llvm-dev libclang-dev clang libspeechd-dev
- cargo fmt --all --check
- cargo test --no-default-features --features 0_10
- cargo clippy --no-default-features --features 0_10
- name: release
image: rust
commands:
- apt-get update -qq
- apt-get install -qqy llvm-dev libclang-dev clang libspeechd-dev
- cargo publish --no-default-features --features 0_10 --manifest-path speech-dispatcher-sys/Cargo.toml || true
- cargo publish --no-default-features --features 0_10 --manifest-path speech-dispatcher/Cargo.toml
when:
ref:
- refs/tags/v*
environment:
CARGO_REGISTRY_TOKEN:
from_secret: cargo_registry_token

View File

@ -1,24 +0,0 @@
image: rust
stages:
- test
- publish
before_script:
- apt-get update
- apt-get install -y libclang-3.9-dev libspeechd-dev
- export CPATH=/usr/lib/llvm-3.9/lib/clang/3.9.1/include/
test:
stage: test
script:
- cargo test
publish:
stage: publish
script:
- cargo login $CARGO_TOKEN
- cargo package
- cargo publish
only:
- tags

View File

@ -1,10 +1,6 @@
[package]
name = "speech-dispatcher"
version = "0.3.1"
authors = ["Nolan Darilek <nolan@thewordnerd.info>"]
repository = "https://gitlab.com/ndarilek/speech-dispatcher-rs"
description = "Rusty interface to the speech-dispatcher speech synthesis library"
license = "LGPL-2.1"
[workspace]
[dependencies]
speech-dispatcher-sys = "0.3"
members = [
"speech-dispatcher-sys",
"speech-dispatcher"
]

60
cliff.toml Normal file
View File

@ -0,0 +1,60 @@
# configuration file for git-cliff (0.1.0)
[changelog]
# changelog header
header = """
# Changelog\n
All notable changes to this project will be documented in this file.\n
"""
# template for the changelog body
# https://tera.netlify.app/docs/#introduction
body = """
{% if version %}\
## Version {{ version | trim_start_matches(pat="v") }} - {{ timestamp | date(format="%Y-%m-%d") }}
{% else %}\
## Unreleased
{% endif %}\
{% for group, commits in commits | group_by(attribute="group") %}
### {{ group | upper_first }}
{% for commit in commits %}
- {% if commit.breaking %}[**breaking**] {% endif %}{{ commit.message | upper_first }}\
{% endfor %}
{% endfor %}\n
"""
# remove the leading and trailing whitespace from the template
trim = true
# changelog footer
footer = """
<!-- generated by git-cliff -->
"""
[git]
# parse the commits based on https://www.conventionalcommits.org
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features"},
{ message = "^fix", group = "Bug Fixes"},
{ message = "^doc", group = "Documentation"},
{ message = "^perf", group = "Performance"},
{ message = "^refactor", group = "Refactor"},
{ message = "^style", group = "Styling"},
{ message = "^test", group = "Testing"},
{ message = "^chore\\(release\\): prepare for", skip = true},
{ message = "^chore", group = "Miscellaneous Tasks"},
{ body = ".*security", group = "Security"},
]
# filter out the commits that are not matched by commit parsers
filter_commits = false
# glob pattern for matching git tags
tag_pattern = "v[0-9]*"
# regex for skipping tags
skip_tags = ""
# regex for ignoring tags
ignore_tags = ""
# sort the tags chronologically
date_order = false
# sort the commits inside sections by oldest/newest order
sort_commits = "oldest"

View File

@ -1,17 +0,0 @@
extern crate speech_dispatcher;
use speech_dispatcher::*;
fn main() {
let connection = speech_dispatcher::Connection::open("hello_world", "hello_world", "hello_world", Mode::Single);
connection.say(Priority::Important, format!("Hello, world at rate {}.", connection.get_voice_rate()));
connection.set_voice_rate(100);
connection.say(Priority::Important, "This is faster.");
connection.set_voice_rate(0);
connection.set_spelling(true);
connection.say(Priority::Important, "This is spelled.");
connection.set_spelling(false);
connection.set_punctuation(Punctuation::All);
connection.say(Priority::Important, "This statement, unlike others, has punctuation that is spoken!");
connection.set_punctuation(Punctuation::None);
}

View File

@ -0,0 +1,17 @@
[package]
name = "speech-dispatcher-sys"
version = "0.7.0"
authors = ["Nolan Darilek <nolan@thewordnerd.info>"]
repository = "https://dev.thewordnerd.info/nolan/speech-dispatcher-rs"
description = "speech-dispatcher system bindings"
license = "LGPL-2.1 OR MIT OR Apache-2.0"
edition = "2021"
[build-dependencies]
bindgen = ">= 0.54"
[package.metadata.release]
tag-prefix = ""
publish = false
push = false
pre-release-hook = ["git-cliff", "-o", "CHANGELOG.md", "--tag", "{{version}}"]

View File

@ -0,0 +1,24 @@
extern crate bindgen;
use std::env;
use std::path::Path;
fn main() {
println!("cargo:rustc-link-lib=speechd");
let out_dir = env::var("OUT_DIR").unwrap();
let _ = bindgen::builder()
.header("wrapper.h")
.constified_enum_module("SPDConnectionMode")
.constified_enum_module("SPDPriority")
.constified_enum_module("SPDVoiceType")
.constified_enum_module("SPDDataMode")
.constified_enum_module("SPDNotification")
.constified_enum_module("SPDPunctuation")
.constified_enum_module("SPDCapitalLetters")
.constified_enum_module("SPDSpelling")
.use_core()
.layout_tests(false)
.generate()
.unwrap()
.write_to_file(Path::new(&out_dir).join("speech_dispatcher_sys.rs"));
}

View File

@ -0,0 +1,7 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
extern crate core;
include!(concat!(env!("OUT_DIR"), "/speech_dispatcher_sys.rs"));

View File

@ -0,0 +1,2 @@
#include <speech-dispatcher/libspeechd.h>
#include <speech-dispatcher/libspeechd_version.h>

View File

@ -0,0 +1,29 @@
[package]
name = "speech-dispatcher"
version = "0.16.0"
authors = ["Nolan Darilek <nolan@thewordnerd.info>"]
repository = "https://dev.thewordnerd.info/nolan/speech-dispatcher-rs"
description = "Rusty interface to the speech-dispatcher speech synthesis library"
license = "LGPL-2.1 OR MIT OR Apache-2.0"
edition = "2021"
[features]
0_11 = ["0_10"]
0_10 = []
0_9 = []
default = ["0_11"]
[dependencies]
lazy_static = "1"
speech-dispatcher-sys = { version = "0.7", path = "../speech-dispatcher-sys" }
libc = "0.2.125"
[package.metadata.docs.rs]
no-default-features = true
features = ["0_9"]
[package.metadata.release]
tag-prefix = ""
publish = false
push = false
pre-release-hook = ["git-cliff", "-o", "CHANGELOG.md", "--tag", "{{version}}"]

View File

@ -0,0 +1,42 @@
use speech_dispatcher::*;
use std::io;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let connection = speech_dispatcher::Connection::open(
"hello_world",
"hello_world",
"hello_world",
Mode::Threaded,
)?;
connection.on_begin(Some(Box::new(|msg_id, client_id| {
println!("Beginning {} from {}", msg_id, client_id)
})));
connection.on_end(Some(Box::new(|msg_id, client_id| {
println!("Ending {} from {}", msg_id, client_id)
})));
let connection_clone = connection.clone();
drop(connection);
connection_clone.say(
Priority::Important,
format!(
"Hello, world at rate {} from client {}.",
connection_clone.get_voice_rate(),
connection_clone.client_id()
),
);
connection_clone.set_voice_rate(100)?;
connection_clone.say(Priority::Important, "This is faster.");
connection_clone.set_voice_rate(0)?;
connection_clone.set_spelling(true)?;
connection_clone.say(Priority::Important, "This is spelled.");
connection_clone.set_spelling(false)?;
connection_clone.set_punctuation(Punctuation::All)?;
connection_clone.say(
Priority::Important,
"This statement, unlike others, has punctuation that is spoken!",
);
connection_clone.set_punctuation(Punctuation::None)?;
let mut _input = String::new();
io::stdin().read_line(&mut _input).unwrap();
Ok(())
}

View File

@ -0,0 +1,40 @@
use speech_dispatcher::*;
use std::io;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let connection = speech_dispatcher::Connection::open(
"hello_world",
"hello_world",
"hello_world",
Mode::Threaded,
)?;
connection.on_begin(Some(Box::new(|msg_id, client_id| {
println!("Beginning {} from {}", msg_id, client_id)
})));
connection.on_end(Some(Box::new(|msg_id, client_id| {
println!("Ending {} from {}", msg_id, client_id)
})));
connection.say(
Priority::Important,
format!(
"Hello, world at rate {} from client {}.",
connection.get_voice_rate(),
connection.client_id()
),
);
connection.set_voice_rate(100)?;
connection.say(Priority::Important, "This is faster.");
connection.set_voice_rate(0)?;
connection.set_spelling(true)?;
connection.say(Priority::Important, "This is spelled.");
connection.set_spelling(false)?;
connection.set_punctuation(Punctuation::All)?;
connection.say(
Priority::Important,
"This statement, unlike others, has punctuation that is spoken!",
);
connection.set_punctuation(Punctuation::None)?;
let mut _input = String::new();
io::stdin().read_line(&mut _input).unwrap();
Ok(())
}

View File

@ -0,0 +1,29 @@
use speech_dispatcher::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let connection = Connection::open("list_voices", "list_voices", "list_voices", Mode::Threaded)?;
let modules = connection.list_output_modules()?;
println!("Modules available: {:?}", modules);
for module in modules {
if connection.set_output_module(&module).is_ok() {
println!("Listing voices for module {module}");
} else {
println!("Failed to set output module to {module}");
continue;
};
let voices = connection.list_synthesis_voices()?;
for voice in voices {
if let Some(variant) = voice.variant {
println!(
" Name: {} / Language: {} / Variant: {variant}",
voice.name, voice.language
);
} else {
println!(" Name: {} / Language: {}", voice.name, voice.language);
}
}
}
// Use connection.set_synthesis_voice(voice) to set the voice to use.
Ok(())
}

View File

@ -0,0 +1,819 @@
#![allow(non_upper_case_globals)]
use libc::size_t;
use std::{
collections::HashMap,
ffi::{CStr, CString},
fmt,
marker::Send,
os::raw::{c_char, c_int},
sync::{Arc, Mutex},
};
use lazy_static::lazy_static;
use speech_dispatcher_sys::*;
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum Mode {
Single = SPDConnectionMode::SPD_MODE_SINGLE,
Threaded = SPDConnectionMode::SPD_MODE_THREADED,
}
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum Priority {
Important = SPDPriority::SPD_IMPORTANT,
Message = SPDPriority::SPD_MESSAGE,
Text = SPDPriority::SPD_TEXT,
Notification = SPDPriority::SPD_NOTIFICATION,
Progress = SPDPriority::SPD_PROGRESS,
}
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum VoiceType {
Male1 = SPDVoiceType::SPD_MALE1 as u32,
Male2 = SPDVoiceType::SPD_MALE2 as u32,
Male3 = SPDVoiceType::SPD_MALE3 as u32,
Female1 = SPDVoiceType::SPD_FEMALE1 as u32,
Female2 = SPDVoiceType::SPD_FEMALE2 as u32,
Female3 = SPDVoiceType::SPD_FEMALE3 as u32,
ChildMale = SPDVoiceType::SPD_CHILD_MALE as u32,
ChildFemale = SPDVoiceType::SPD_CHILD_FEMALE as u32,
}
#[derive(Clone, Debug, Hash, PartialEq)]
pub struct Voice {
/// The name of this voice. Unique with regards to the output module it came from.
pub name: String,
/// The language of this voice. Probably a BCP 47 language tag.
pub language: String,
/// The variant of this language, if present. Loosely defined.
pub variant: Option<String>,
}
impl Voice {
/// Convert a SPDVoice to a Voice. Only fails if the fields are non-Unicode.
/// Does not check that the pointers are non-null, caller must ensure that.
unsafe fn try_from(v: &SPDVoice) -> Result<Self, std::str::Utf8Error> {
// SPDVoice fields appear to all be ASCII.
let name = CStr::from_ptr(v.name).to_str()?.to_owned();
let language = CStr::from_ptr(v.language).to_str()?.to_owned();
let variant = CStr::from_ptr(v.variant).to_str()?;
let variant = if variant == "none" {
None
} else {
Some(variant.to_owned())
};
Ok(Self {
name,
language,
variant,
})
}
}
pub type Address = SPDConnectionAddress;
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum DataMode {
Text = SPDDataMode::SPD_DATA_TEXT,
SSML = SPDDataMode::SPD_DATA_SSML,
}
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum Notification {
Begin = SPDNotification::SPD_BEGIN,
End = SPDNotification::SPD_END,
IndexMarks = SPDNotification::SPD_INDEX_MARKS,
Cancel = SPDNotification::SPD_CANCEL,
Pause = SPDNotification::SPD_PAUSE,
Resume = SPDNotification::SPD_RESUME,
All = SPDNotification::SPD_ALL,
}
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum Punctuation {
All = SPDPunctuation::SPD_PUNCT_ALL,
#[cfg(feature = "0_10")]
Most = SPDPunctuation::SPD_PUNCT_MOST,
Some = SPDPunctuation::SPD_PUNCT_SOME,
None = SPDPunctuation::SPD_PUNCT_NONE,
}
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
pub enum CapitalLetters {
None = SPDCapitalLetters::SPD_CAP_NONE,
Spell = SPDCapitalLetters::SPD_CAP_SPELL,
Icon = SPDCapitalLetters::SPD_CAP_ICON,
}
/// Converts a `0` to a success and everything else to an error.
fn c_int_to_result(r: c_int) -> Result<(), Error> {
match r {
0 => Ok(()),
_ => Err(Error::OperationFailed),
}
}
#[derive(Default)]
struct Callbacks {
begin: Option<Box<dyn FnMut(size_t, size_t)>>,
end: Option<Box<dyn FnMut(size_t, size_t)>>,
index_mark: Option<Box<dyn FnMut(size_t, size_t, String)>>,
cancel: Option<Box<dyn FnMut(size_t, size_t)>>,
pause: Option<Box<dyn FnMut(size_t, size_t)>>,
resume: Option<Box<dyn FnMut(size_t, size_t)>>,
}
unsafe impl Send for Callbacks {}
unsafe impl Sync for Callbacks {}
lazy_static! {
static ref callbacks: Mutex<HashMap<size_t, Callbacks>> = {
let m = HashMap::new();
Mutex::new(m)
};
}
unsafe extern "C" fn cb(msg_id: size_t, client_id: size_t, state: u32) {
let state = match state {
SPDNotificationType_SPD_EVENT_BEGIN => Notification::Begin,
SPDNotificationType_SPD_EVENT_END => Notification::End,
SPDNotificationType_SPD_EVENT_CANCEL => Notification::Cancel,
SPDNotificationType_SPD_EVENT_PAUSE => Notification::Pause,
SPDNotificationType_SPD_EVENT_RESUME => Notification::Resume,
_ => panic!("Unknown notification received in callback: {}", state),
};
if let Some(c) = callbacks.lock().unwrap().get_mut(&client_id) {
let f = match state {
Notification::Begin => &mut c.begin,
Notification::End => &mut c.end,
Notification::Cancel => &mut c.cancel,
Notification::Pause => &mut c.pause,
Notification::Resume => &mut c.resume,
_ => panic!("Unknown notification type"),
};
if let Some(f) = f.as_mut() {
f(msg_id, client_id);
}
}
}
unsafe extern "C" fn cb_im(msg_id: size_t, client_id: size_t, state: u32, index_mark: *mut c_char) {
let index_mark = CStr::from_ptr(index_mark);
let index_mark = index_mark.to_string_lossy().to_string();
let state = match state {
SPDNotificationType_SPD_EVENT_INDEX_MARK => Notification::IndexMarks,
_ => panic!("Unknown notification received in IM callback: {}", state),
};
if let Some(c) = callbacks.lock().unwrap().get_mut(&client_id) {
let f = match state {
Notification::IndexMarks => &mut c.index_mark,
_ => panic!("Unknown notification type"),
};
if let Some(f) = f.as_mut() {
f(msg_id, client_id, index_mark);
}
}
}
#[derive(Debug)]
pub enum Error {
/// speech-dispatcher failed to initialize. Ensure speech-dispatcher is actually working on
/// your system; for example, does the command `spd-say hello` work?
InitializationError,
/// The operation failed
OperationFailed,
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Error::*;
match self {
InitializationError => write!(f, "failed to initialize"),
OperationFailed => write!(f, "operation failed"),
}
}
}
#[derive(Clone, Debug)]
pub struct Connection(pub Arc<*mut SPDConnection>, size_t);
impl Connection {
pub fn open<S: Into<String>>(
client_name: S,
connection_name: S,
user_name: S,
mode: Mode,
) -> Result<Self, Error> {
let clientname = CString::new(client_name.into()).unwrap();
let connectionname = CString::new(connection_name.into()).unwrap();
let username = CString::new(user_name.into()).unwrap();
let connection = unsafe {
let c = spd_open(
clientname.as_ptr(),
connectionname.as_ptr(),
username.as_ptr(),
mode as u32,
);
if c.is_null() {
Err(Error::InitializationError)
} else {
Ok(Self::setup_connection(c))
}
};
let mut c = Self(Arc::new(connection?), 0);
c.setup()?;
Ok(c)
}
pub unsafe fn open2<S: Into<String>>(
client_name: S,
connection_name: S,
user_name: S,
mode: Mode,
address: *mut Address,
autospawn: bool,
) -> Result<Self, Error> {
let auto_spawn = if autospawn { 1 } else { 0 };
let error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr();
let clientname = CString::new(client_name.into()).unwrap();
let connectionname = CString::new(connection_name.into()).unwrap();
let username = CString::new(user_name.into()).unwrap();
let connection = {
let c = spd_open2(
clientname.as_ptr(),
connectionname.as_ptr(),
username.as_ptr(),
mode as u32,
address,
auto_spawn,
error_result,
);
if c.is_null() {
Err(Error::InitializationError)
} else {
Ok(Self::setup_connection(c))
}
};
let mut c = Self(Arc::new(connection?), 0);
c.setup()?;
Ok(c)
}
unsafe fn setup_connection(c: *mut SPDConnection) -> *mut SPDConnection {
(*c).callback_begin = Some(cb);
(*c).callback_end = Some(cb);
(*c).callback_cancel = Some(cb);
(*c).callback_pause = Some(cb);
(*c).callback_resume = Some(cb);
(*c).callback_im = Some(cb_im);
c
}
fn setup(&mut self) -> Result<(), Error> {
let client_id = self.send_data("HISTORY GET CLIENT_ID\r\n", true);
if let Some(client_id) = client_id {
let client_id: Vec<&str> = client_id.split("\r\n").collect();
let client_id = client_id.get(0);
if let Some(client_id) = client_id {
let client_id: Vec<&str> = client_id.split("-").collect();
if let Some(client_id) = client_id.get(1) {
if let Ok(client_id) = client_id.parse::<size_t>() {
self.1 = client_id;
}
}
}
}
callbacks.lock().unwrap().insert(self.1, Default::default());
self.set_notification_on(Notification::All)
.map_err(|_| Error::InitializationError)?;
Ok(())
}
pub fn close(&self) {
unsafe { spd_close(*self.0) };
}
pub fn say<S: Into<String>>(&self, priority: Priority, text: S) -> Option<u64> {
let text: String = text.into();
let param = CString::new(text).unwrap();
let rv = unsafe { spd_say(*self.0, priority as u32, param.as_ptr()) };
if rv != -1 {
Some(rv as u64)
} else {
None
}
}
pub fn sayf<S: Into<String>>(&self, priority: Priority, format: S) -> Option<i32> {
let format: String = format.into();
let param = CString::new(format).unwrap();
let rv = unsafe { spd_sayf(*self.0, priority as u32, param.as_ptr()) };
if rv != -1 {
Some(rv)
} else {
None
}
}
pub fn stop(&self) -> Result<(), Error> {
let v = unsafe { spd_stop(*self.0) };
c_int_to_result(v)
}
pub fn stop_all(&self) -> Result<(), Error> {
let v = unsafe { spd_stop_all(*self.0) };
c_int_to_result(v)
}
pub fn stop_uid(&self, target_uid: i32) -> Result<(), Error> {
let v = unsafe { spd_stop_uid(*self.0, target_uid) };
c_int_to_result(v)
}
pub fn cancel(&self) -> Result<(), Error> {
let v = unsafe { spd_cancel(*self.0) };
c_int_to_result(v)
}
pub fn cancel_all(&self) -> Result<(), Error> {
let v = unsafe { spd_cancel_all(*self.0) };
c_int_to_result(v)
}
pub fn cancel_uid(&self, target_uid: i32) -> Result<(), Error> {
let v = unsafe { spd_cancel_uid(*self.0, target_uid) };
c_int_to_result(v)
}
pub fn pause(&self) -> Result<(), Error> {
let v = unsafe { spd_pause(*self.0) };
c_int_to_result(v)
}
pub fn pause_all(&self) -> Result<(), Error> {
let v = unsafe { spd_pause_all(*self.0) };
c_int_to_result(v)
}
pub fn pause_uid(&self, target_uid: i32) -> Result<(), Error> {
let v = unsafe { spd_pause_uid(*self.0, target_uid) };
c_int_to_result(v)
}
pub fn resume(&self) -> Result<(), Error> {
let v = unsafe { spd_resume(*self.0) };
c_int_to_result(v)
}
pub fn resume_all(&self) -> Result<(), Error> {
let v = unsafe { spd_resume_all(*self.0) };
c_int_to_result(v)
}
pub fn resume_uid(&self, target_uid: i32) -> Result<(), Error> {
let v = unsafe { spd_resume_uid(*self.0, target_uid) };
c_int_to_result(v)
}
pub fn key<S: Into<String>>(&self, priority: Priority, key_name: S) -> Result<(), Error> {
let param = CString::new(key_name.into()).unwrap();
let v = unsafe { spd_key(*self.0, priority as u32, param.as_ptr()) };
c_int_to_result(v)
}
pub fn char<S: Into<String>>(&self, priority: Priority, char: S) -> Result<(), Error> {
let param = CString::new(char.into()).unwrap();
let v = unsafe { spd_char(*self.0, priority as u32, param.as_ptr()) };
c_int_to_result(v)
}
pub fn wchar(&self, priority: Priority, wchar: i32) -> Result<(), Error> {
let v = unsafe { spd_wchar(*self.0, priority as u32, wchar as wchar_t) };
c_int_to_result(v)
}
pub fn sound_icon<S: Into<String>>(
&self,
priority: Priority,
icon_name: S,
) -> Result<(), Error> {
let param = CString::new(icon_name.into()).unwrap();
let v = unsafe { spd_char(*self.0, priority as u32, param.as_ptr()) };
c_int_to_result(v)
}
pub fn set_voice_type(&self, voice_type: VoiceType) -> Result<(), Error> {
#[cfg(all(any(feature = "0_9", feature = "0_10"), not(feature = "0_11")))]
let v = unsafe { spd_set_voice_type(*self.0, voice_type as u32) };
#[cfg(all(not(feature = "0_9"), any(feature = "0_11", not(feature = "0_10"))))]
let v = unsafe { spd_set_voice_type(*self.0, voice_type as i32) };
c_int_to_result(v)
}
pub fn set_voice_type_all(&self, voice_type: VoiceType) -> Result<(), Error> {
#[cfg(all(any(feature = "0_9", feature = "0_10"), not(feature = "0_11")))]
let v = unsafe { spd_set_voice_type_all(*self.0, voice_type as u32) };
#[cfg(all(not(feature = "0_9"), any(feature = "0_11", not(feature = "0_10"))))]
let v = unsafe { spd_set_voice_type_all(*self.0, voice_type as i32) };
c_int_to_result(v)
}
pub fn set_voice_type_uid(&self, voice_type: VoiceType, target_uid: u32) -> Result<(), Error> {
#[cfg(all(any(feature = "0_9", feature = "0_10"), not(feature = "0_11")))]
let v = unsafe { spd_set_voice_type_uid(*self.0, voice_type as u32, target_uid) };
#[cfg(all(not(feature = "0_9"), any(feature = "0_11", not(feature = "0_10"))))]
let v = unsafe { spd_set_voice_type_uid(*self.0, voice_type as i32, target_uid) };
c_int_to_result(v)
}
pub fn get_voice_type(&self) -> Result<VoiceType, Error> {
let v = unsafe { spd_get_voice_type(*self.0) };
Ok(match v {
SPDVoiceType::SPD_MALE1 => VoiceType::Male1,
SPDVoiceType::SPD_MALE2 => VoiceType::Male2,
SPDVoiceType::SPD_MALE3 => VoiceType::Male3,
SPDVoiceType::SPD_FEMALE1 => VoiceType::Female1,
SPDVoiceType::SPD_FEMALE2 => VoiceType::Female2,
SPDVoiceType::SPD_FEMALE3 => VoiceType::Female3,
SPDVoiceType::SPD_CHILD_MALE => VoiceType::ChildMale,
SPDVoiceType::SPD_CHILD_FEMALE => VoiceType::ChildFemale,
_ => return Err(Error::OperationFailed), // can this happen?
})
}
pub fn set_synthesis_voice(&self, voice: &Voice) -> Result<(), Error> {
let param = CString::new(voice.name.clone()).unwrap();
let v = unsafe { spd_set_synthesis_voice(*self.0, param.as_ptr()) };
c_int_to_result(v)
}
pub fn set_synthesis_voice_all<S: Into<String>>(&self, voice_name: S) -> Result<(), Error> {
let param = CString::new(voice_name.into()).unwrap();
let v = unsafe { spd_set_synthesis_voice_all(*self.0, param.as_ptr()) };
c_int_to_result(v)
}
pub fn set_synthesis_voice_uid<S: Into<String>>(
&self,
voice_name: S,
target_uid: u32,
) -> Result<(), Error> {
let param = CString::new(voice_name.into()).unwrap();
let v = unsafe { spd_set_synthesis_voice_uid(*self.0, param.as_ptr(), target_uid) };
c_int_to_result(v)
}
pub fn set_data_mode(&self, mode: DataMode) -> Result<(), Error> {
let v = unsafe { spd_set_data_mode(*self.0, mode as u32) };
c_int_to_result(v)
}
pub fn set_notification_on(&self, notification: Notification) -> Result<(), Error> {
let v = unsafe { spd_set_notification_on(*self.0, notification as u32) };
c_int_to_result(v)
}
pub fn set_notification_off(&self, notification: Notification) -> Result<(), Error> {
let v = unsafe { spd_set_notification_off(*self.0, notification as u32) };
c_int_to_result(v)
}
pub fn set_notification<S: Into<String>>(
&self,
notification: Notification,
state: S,
) -> Result<(), Error> {
let param = CString::new(state.into()).unwrap();
let v = unsafe { spd_set_notification(*self.0, notification as u32, param.as_ptr()) };
c_int_to_result(v)
}
pub fn set_voice_rate(&self, rate: i32) -> Result<(), Error> {
let v = unsafe { spd_set_voice_rate(*self.0, rate) };
c_int_to_result(v)
}
pub fn set_voice_rate_all(&self, rate: i32) -> Result<(), Error> {
let v = unsafe { spd_set_voice_rate_all(*self.0, rate) };
c_int_to_result(v)
}
pub fn set_voice_rate_uid(&self, rate: i32, target_uid: u32) -> Result<(), Error> {
let v = unsafe { spd_set_voice_rate_uid(*self.0, rate, target_uid) };
c_int_to_result(v)
}
pub fn get_voice_rate(&self) -> i32 {
unsafe { spd_get_voice_rate(*self.0) }
}
pub fn set_voice_pitch(&self, pitch: i32) -> Result<(), Error> {
let v = unsafe { spd_set_voice_pitch(*self.0, pitch) };
c_int_to_result(v)
}
pub fn set_voice_pitch_all(&self, pitch: i32) -> Result<(), Error> {
let v = unsafe { spd_set_voice_pitch_all(*self.0, pitch) };
c_int_to_result(v)
}
pub fn set_voice_pitch_uid(&self, pitch: i32, target_uid: u32) -> Result<(), Error> {
let v = unsafe { spd_set_voice_pitch_uid(*self.0, pitch, target_uid) };
c_int_to_result(v)
}
pub fn get_voice_pitch(&self) -> i32 {
unsafe { spd_get_voice_pitch(*self.0) }
}
pub fn set_volume(&self, volume: i32) -> Result<(), Error> {
let v = unsafe { spd_set_volume(*self.0, volume) };
c_int_to_result(v)
}
pub fn set_volume_all(&self, volume: i32) -> Result<(), Error> {
let v = unsafe { spd_set_volume_all(*self.0, volume) };
c_int_to_result(v)
}
pub fn set_volume_uid(&self, volume: i32, target_uid: u32) -> Result<(), Error> {
let v = unsafe { spd_set_volume_uid(*self.0, volume, target_uid) };
c_int_to_result(v)
}
pub fn get_volume(&self) -> i32 {
unsafe { spd_get_volume(*self.0) }
}
pub fn set_punctuation(&self, punctuation: Punctuation) -> Result<(), Error> {
let v = unsafe { spd_set_punctuation(*self.0, punctuation as u32) };
c_int_to_result(v)
}
pub fn set_punctuation_all(&self, punctuation: Punctuation) -> Result<(), Error> {
let v = unsafe { spd_set_punctuation_all(*self.0, punctuation as u32) };
c_int_to_result(v)
}
pub fn set_punctuation_uid(
&self,
punctuation: Punctuation,
target_uid: u32,
) -> Result<(), Error> {
let v = unsafe { spd_set_punctuation_uid(*self.0, punctuation as u32, target_uid) };
c_int_to_result(v)
}
pub fn set_capital_letters(&self, capital_letters: CapitalLetters) -> Result<(), Error> {
let v = unsafe { spd_set_capital_letters(*self.0, capital_letters as u32) };
c_int_to_result(v)
}
pub fn set_capital_letters_all(&self, capital_letters: CapitalLetters) -> Result<(), Error> {
let v = unsafe { spd_set_capital_letters_all(*self.0, capital_letters as u32) };
c_int_to_result(v)
}
pub fn set_capital_letters_uid(
&self,
capital_letters: CapitalLetters,
target_uid: u32,
) -> Result<(), Error> {
let v = unsafe { spd_set_capital_letters_uid(*self.0, capital_letters as u32, target_uid) };
c_int_to_result(v)
}
pub fn set_spelling(&self, spelling: bool) -> Result<(), Error> {
let s = if spelling {
SPDSpelling::SPD_SPELL_ON
} else {
SPDSpelling::SPD_SPELL_OFF
};
let v = unsafe { spd_set_spelling(*self.0, s) };
c_int_to_result(v)
}
pub fn set_spelling_all(&self, spelling: bool) -> Result<(), Error> {
let s = if spelling {
SPDSpelling::SPD_SPELL_ON
} else {
SPDSpelling::SPD_SPELL_OFF
};
let v = unsafe { spd_set_spelling_all(*self.0, s) };
c_int_to_result(v)
}
pub fn set_spelling_uid(&self, spelling: bool, target_uid: u32) -> Result<(), Error> {
let s = if spelling {
SPDSpelling::SPD_SPELL_ON
} else {
SPDSpelling::SPD_SPELL_OFF
};
let v = unsafe { spd_set_spelling_uid(*self.0, s, target_uid) };
c_int_to_result(v)
}
pub fn set_language<S: Into<String>>(&self, language: S) -> Result<(), Error> {
let param = CString::new(language.into()).unwrap();
let v = unsafe { spd_set_language(*self.0, param.as_ptr()) };
c_int_to_result(v)
}
pub fn set_language_all<S: Into<String>>(&self, language: S) -> Result<(), Error> {
let param = CString::new(language.into()).unwrap();
let v = unsafe { spd_set_language_all(*self.0, param.as_ptr()) };
c_int_to_result(v)
}
pub fn set_language_uid<S: Into<String>>(
&self,
language: S,
target_uid: u32,
) -> Result<(), Error> {
let param = CString::new(language.into()).unwrap();
let v = unsafe { spd_set_language_uid(*self.0, param.as_ptr(), target_uid) };
c_int_to_result(v)
}
pub fn get_language(&self) -> Result<&str, Error> {
let language = unsafe { spd_get_language(*self.0) };
if language.is_null() {
Err(Error::OperationFailed)
} else {
let language = unsafe { CStr::from_ptr(language) };
language.to_str().map_err(|_| Error::OperationFailed)
}
}
pub fn set_output_module<S: Into<String>>(&self, output_module: S) -> Result<(), Error> {
let param = CString::new(output_module.into()).unwrap();
let v = unsafe { spd_set_output_module(*self.0, param.as_ptr()) };
c_int_to_result(v)
}
pub fn set_output_module_all<S: Into<String>>(&self, output_module: S) -> Result<(), Error> {
let param = CString::new(output_module.into()).unwrap();
let v = unsafe { spd_set_output_module_all(*self.0, param.as_ptr()) };
c_int_to_result(v)
}
pub fn set_output_module_uid<S: Into<String>>(
&self,
output_module: S,
target_uid: u32,
) -> Result<(), Error> {
let param = CString::new(output_module.into()).unwrap();
let v = unsafe { spd_set_output_module_uid(*self.0, param.as_ptr(), target_uid) };
c_int_to_result(v)
}
pub fn send_data<S: Into<String>>(&self, data: S, wait_for_reply: bool) -> Option<String> {
let wfr: i32 = if wait_for_reply {
SPD_WAIT_REPLY as i32
} else {
SPD_NO_REPLY as i32
};
let data = CString::new(data.into()).unwrap();
let rv = unsafe { spd_send_data(*self.0, data.as_ptr(), wfr) };
if rv.is_null() {
None
} else {
let rv = unsafe { CStr::from_ptr(rv) };
Some(rv.to_string_lossy().to_string())
}
}
pub fn on_begin(&self, f: Option<Box<dyn FnMut(size_t, size_t)>>) {
if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb {
cb.begin = f;
}
}
}
pub fn on_end(&self, f: Option<Box<dyn FnMut(size_t, size_t)>>) {
if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb {
cb.end = f;
}
}
}
pub fn on_cancel(&self, f: Option<Box<dyn FnMut(size_t, size_t)>>) {
if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb {
cb.cancel = f;
}
}
}
pub fn on_pause(&self, f: Option<Box<dyn FnMut(size_t, size_t)>>) {
if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb {
cb.pause = f;
}
}
}
pub fn on_resume(&self, f: Option<Box<dyn FnMut(size_t, size_t)>>) {
if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb {
cb.resume = f;
}
}
}
pub fn on_index_mark(&self, f: Option<Box<dyn FnMut(size_t, size_t, String)>>) {
if let Ok(mut cbs) = callbacks.lock() {
let cb = cbs.get_mut(&self.1);
if let Some(cb) = cb {
cb.index_mark = f;
}
}
}
pub fn list_synthesis_voices(&self) -> Result<Vec<Voice>, Error> {
let start = unsafe { spd_list_synthesis_voices(*self.0) };
let slice = unsafe { null_term_array_ptr_to_slice(start) }.ok_or(Error::OperationFailed)?;
let voices = unsafe {
slice
.iter()
.map(|v| v.read())
.flat_map(|v| {
if v.name.is_null() || v.language.is_null() || v.variant.is_null() {
None
} else {
Voice::try_from(&v).ok()
}
})
.collect()
};
unsafe {
free_spd_voices(start);
}
Ok(voices)
}
pub fn list_output_modules(&self) -> Result<Vec<String>, Error> {
let start = unsafe { spd_list_modules(*self.0) };
let slice = unsafe { null_term_array_ptr_to_slice(start) }.ok_or(Error::OperationFailed)?;
let modules = unsafe {
slice
.iter()
.flat_map(|v| CStr::from_ptr(*v).to_str().ok().map(String::from))
.collect()
};
unsafe {
free_spd_modules(start);
}
Ok(modules)
}
pub fn client_id(&self) -> size_t {
self.1
}
}
/// Interpret a null-terminated array of pointers as a slice of pointers.
/// None of the pointers will be null if this returns Some.
unsafe fn null_term_array_ptr_to_slice<'a, T>(start: *mut *mut T) -> Option<&'a [*mut T]> {
if start.is_null() {
return None;
}
let mut current = start;
let mut len = 0;
while !current.read().is_null() {
len += 1;
current = current.add(1);
}
Some(std::slice::from_raw_parts(start, len))
}
unsafe impl Send for Connection {}
impl Drop for Connection {
fn drop(&mut self) {
if Arc::strong_count(&self.0) <= 1 {
self.close();
callbacks.lock().unwrap().remove(&self.1);
}
}
}

View File

@ -0,0 +1,2 @@
#include <speech-dispatcher/libspeechd.h>
#include <speech-dispatcher/libspeechd_version.h>

View File

@ -1,530 +0,0 @@
#![allow(non_upper_case_globals)]
extern crate speech_dispatcher_sys;
use std::ffi::{CStr, CString};
use std::marker::Send;
use speech_dispatcher_sys::*;
pub enum Mode {
Single = SPDConnectionMode::SPD_MODE_SINGLE as isize,
Threaded = SPDConnectionMode::SPD_MODE_THREADED as isize,
}
pub enum Priority {
Important = SPDPriority::SPD_IMPORTANT as isize,
Message = SPDPriority::SPD_MESSAGE as isize,
Text = SPDPriority::SPD_TEXT as isize,
Notification = SPDPriority::SPD_NOTIFICATION as isize,
Progress = SPDPriority::SPD_PROGRESS as isize,
}
pub enum VoiceType {
Male1 = SPDVoiceType::SPD_MALE1 as isize,
Male2 = SPDVoiceType::SPD_MALE2 as isize,
Male3 = SPDVoiceType::SPD_MALE3 as isize,
Female1 = SPDVoiceType::SPD_FEMALE1 as isize,
Female2 = SPDVoiceType::SPD_FEMALE2 as isize,
Female3 = SPDVoiceType::SPD_FEMALE3 as isize,
ChildMale = SPDVoiceType::SPD_CHILD_MALE as isize,
ChildFemale = SPDVoiceType::SPD_CHILD_FEMALE as isize,
}
pub struct Connection {
connection: *mut SPDConnection,
}
pub type Address = SPDConnectionAddress;
pub enum DataMode {
Text = SPDDataMode::SPD_DATA_TEXT as isize,
SSML = SPDDataMode::SPD_DATA_SSML as isize,
}
pub enum Notification {
Begin = SPDNotification::SPD_BEGIN as isize,
End = SPDNotification::SPD_END as isize,
IndexMarks = SPDNotification::SPD_INDEX_MARKS as isize,
Cancel = SPDNotification::SPD_CANCEL as isize,
Pause = SPDNotification::SPD_PAUSE as isize,
Resume = SPDNotification::SPD_RESUME as isize,
All = SPDNotification::SPD_ALL as isize,
}
pub enum Punctuation {
All = SPDPunctuation::SPD_PUNCT_ALL as isize,
None = SPDPunctuation::SPD_PUNCT_NONE as isize,
Some = SPDPunctuation::SPD_PUNCT_SOME as isize,
}
pub enum CapitalLetters {
None = SPDCapitalLetters::SPD_CAP_NONE as isize,
Spell = SPDCapitalLetters::SPD_CAP_SPELL as isize,
Icon = SPDCapitalLetters::SPD_CAP_ICON as isize,
}
fn i32_to_bool(v: i32) -> bool {
if v == 1 {
true
} else {
false
}
}
impl Connection {
pub fn open<S: Into<String>>(
client_name: S,
connection_name: S,
user_name: S,
mode: Mode,
) -> Connection {
let connection = unsafe {
spd_open(
CString::new(client_name.into()).unwrap().as_ptr(),
CString::new(connection_name.into()).unwrap().as_ptr(),
CString::new(user_name.into()).unwrap().as_ptr(),
mode as u32,
)
};
Connection {
connection: connection,
}
}
pub fn open2<S: Into<String>>(
client_name: S,
connection_name: S,
user_name: S,
mode: Mode,
address: *mut Address,
autospawn: bool,
) -> Connection {
let auto_spawn = if autospawn { 1 } else { 0 };
let error_result = vec![CString::new("").unwrap().into_raw()].as_mut_ptr();
let connection = unsafe {
spd_open2(
CString::new(client_name.into()).unwrap().as_ptr(),
CString::new(connection_name.into()).unwrap().as_ptr(),
CString::new(user_name.into()).unwrap().as_ptr(),
mode as u32,
address,
auto_spawn,
error_result,
)
};
Connection { connection }
}
pub fn close(&self) {
unsafe { spd_close(self.connection) };
}
pub fn say<S: Into<String>>(&self, priority: Priority, text: S) -> bool {
let v = unsafe {
spd_say(
self.connection,
priority as u32,
CString::new(text.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn sayf<S: Into<String>>(&self, priority: Priority, format: S) -> bool {
let v = unsafe {
spd_sayf(
self.connection,
priority as u32,
CString::new(format.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn stop(&self) -> bool {
let v = unsafe { spd_stop(self.connection) };
i32_to_bool(v)
}
pub fn stop_all(&self) -> bool {
let v = unsafe { spd_stop_all(self.connection) };
i32_to_bool(v)
}
pub fn stop_uid(&self, target_uid: i32) -> bool {
let v = unsafe { spd_stop_uid(self.connection, target_uid) };
i32_to_bool(v)
}
pub fn cancel(&self) -> bool {
let v = unsafe { spd_cancel(self.connection) };
i32_to_bool(v)
}
pub fn cancel_all(&self) -> bool {
let v = unsafe { spd_cancel_all(self.connection) };
i32_to_bool(v)
}
pub fn cancel_uid(&self, target_uid: i32) -> bool {
let v = unsafe { spd_cancel_uid(self.connection, target_uid) };
i32_to_bool(v)
}
pub fn pause(&self) -> bool {
let v = unsafe { spd_pause(self.connection) };
i32_to_bool(v)
}
pub fn ause_all(&self) -> bool {
let v = unsafe { spd_pause_all(self.connection) };
i32_to_bool(v)
}
pub fn pause_uid(&self, target_uid: i32) -> bool {
let v = unsafe { spd_pause_uid(self.connection, target_uid) };
i32_to_bool(v)
}
pub fn resume(&self) -> bool {
let v = unsafe { spd_resume(self.connection) };
i32_to_bool(v)
}
pub fn resume_all(&self) -> bool {
let v = unsafe { spd_resume_all(self.connection) };
i32_to_bool(v)
}
pub fn resume_uid(&self, target_uid: i32) -> bool {
let v = unsafe { spd_resume_uid(self.connection, target_uid) };
i32_to_bool(v)
}
pub fn key<S: Into<String>>(&self, priority: Priority, key_name: S) -> bool {
let v = unsafe {
spd_key(
self.connection,
priority as u32,
CString::new(key_name.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn char<S: Into<String>>(&self, priority: Priority, char: S) -> bool {
let v = unsafe {
spd_char(
self.connection,
priority as u32,
CString::new(char.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn wchar(&self, priority: Priority, wchar: i32) -> bool {
let v = unsafe { spd_wchar(self.connection, priority as u32, wchar) };
i32_to_bool(v)
}
pub fn sound_icon<S: Into<String>>(&self, priority: Priority, icon_name: S) -> bool {
let v = unsafe {
spd_char(
self.connection,
priority as u32,
CString::new(icon_name.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn set_voice_type(&self, voice_type: VoiceType) -> bool {
let v = unsafe { spd_set_voice_type(self.connection, voice_type as u32) };
i32_to_bool(v)
}
pub fn set_voice_type_all(&self, voice_type: VoiceType) -> bool {
let v = unsafe { spd_set_voice_type_all(self.connection, voice_type as u32) };
i32_to_bool(v)
}
pub fn set_voice_type_uid(&self, voice_type: VoiceType, target_uid: u32) -> bool {
let v = unsafe { spd_set_voice_type_uid(self.connection, voice_type as u32, target_uid) };
i32_to_bool(v)
}
pub fn get_voice_type(&self) -> VoiceType {
let v = unsafe { spd_get_voice_type(self.connection) };
match v {
SPDVoiceType::SPD_MALE1 => VoiceType::Male1,
SPDVoiceType::SPD_MALE2 => VoiceType::Male2,
SPDVoiceType::SPD_MALE3 => VoiceType::Male3,
SPDVoiceType::SPD_FEMALE1 => VoiceType::Female1,
SPDVoiceType::SPD_FEMALE2 => VoiceType::Female2,
SPDVoiceType::SPD_FEMALE3 => VoiceType::Female3,
SPDVoiceType::SPD_CHILD_MALE => VoiceType::ChildMale,
SPDVoiceType::SPD_CHILD_FEMALE => VoiceType::ChildFemale,
_ => panic!("Invalid voice type"),
}
}
pub fn set_synthesis_voice<S: Into<String>>(&self, voice_name: S) -> bool {
let v = unsafe {
spd_set_synthesis_voice(
self.connection,
CString::new(voice_name.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn set_synthesis_voice_all<S: Into<String>>(&self, voice_name: S) -> bool {
let v = unsafe {
spd_set_synthesis_voice_all(
self.connection,
CString::new(voice_name.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn set_synthesis_voice_uid<S: Into<String>>(&self, voice_name: S, target_uid: u32) -> bool {
let v = unsafe {
spd_set_synthesis_voice_uid(
self.connection,
CString::new(voice_name.into()).unwrap().as_ptr(),
target_uid,
)
};
i32_to_bool(v)
}
pub fn set_data_mode(&self, mode: DataMode) -> bool {
let v = unsafe { spd_set_data_mode(self.connection, mode as u32) };
i32_to_bool(v)
}
pub fn set_notification_on(&self, notification: Notification) -> bool {
let v = unsafe { spd_set_notification_on(self.connection, notification as u32) };
i32_to_bool(v)
}
pub fn set_notification_off(&self, notification: Notification) -> bool {
let v = unsafe { spd_set_notification_off(self.connection, notification as u32) };
i32_to_bool(v)
}
pub fn set_notification<S: Into<String>>(&self, notification: Notification, state: S) -> bool {
let v = unsafe {
spd_set_notification(
self.connection,
notification as u32,
CString::new(state.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn set_voice_rate(&self, rate: i32) -> bool {
let v = unsafe { spd_set_voice_rate(self.connection, rate) };
i32_to_bool(v)
}
pub fn set_voice_rate_all(&self, rate: i32) -> bool {
let v = unsafe { spd_set_voice_rate_all(self.connection, rate) };
i32_to_bool(v)
}
pub fn set_voice_rate_uid(&self, rate: i32, target_uid: u32) -> bool {
let v = unsafe { spd_set_voice_rate_uid(self.connection, rate, target_uid) };
i32_to_bool(v)
}
pub fn get_voice_rate(&self) -> i32 {
unsafe { spd_get_voice_rate(self.connection) }
}
pub fn set_voice_pitch(&self, pitch: i32) -> bool {
let v = unsafe { spd_set_voice_pitch(self.connection, pitch) };
i32_to_bool(v)
}
pub fn set_voice_pitch_all(&self, pitch: i32) -> bool {
let v = unsafe { spd_set_voice_pitch_all(self.connection, pitch) };
i32_to_bool(v)
}
pub fn set_voice_pitch_uid(&self, pitch: i32, target_uid: u32) -> bool {
let v = unsafe { spd_set_voice_pitch_uid(self.connection, pitch, target_uid) };
i32_to_bool(v)
}
pub fn get_voice_pitch(&self) -> i32 {
unsafe { spd_get_voice_pitch(self.connection) }
}
pub fn set_volume(&self, volume: i32) -> bool {
let v = unsafe { spd_set_volume(self.connection, volume) };
i32_to_bool(v)
}
pub fn set_volume_all(&self, volume: i32) -> bool {
let v = unsafe { spd_set_volume_all(self.connection, volume) };
i32_to_bool(v)
}
pub fn set_volume_uid(&self, volume: i32, target_uid: u32) -> bool {
let v = unsafe { spd_set_volume_uid(self.connection, volume, target_uid) };
i32_to_bool(v)
}
pub fn get_volume(&self) -> i32 {
unsafe { spd_get_volume(self.connection) }
}
pub fn set_punctuation(&self, punctuation: Punctuation) -> bool {
let v = unsafe { spd_set_punctuation(self.connection, punctuation as u32) };
i32_to_bool(v)
}
pub fn set_punctuation_all(&self, punctuation: Punctuation) -> bool {
let v = unsafe { spd_set_punctuation_all(self.connection, punctuation as u32) };
i32_to_bool(v)
}
pub fn set_punctuation_uid(&self, punctuation: Punctuation, target_uid: u32) -> bool {
let v = unsafe { spd_set_punctuation_uid(self.connection, punctuation as u32, target_uid) };
i32_to_bool(v)
}
pub fn set_capital_letters(&self, capital_letters: CapitalLetters) -> bool {
let v = unsafe { spd_set_capital_letters(self.connection, capital_letters as u32) };
i32_to_bool(v)
}
pub fn set_capital_letters_all(&self, capital_letters: CapitalLetters) -> bool {
let v = unsafe { spd_set_capital_letters_all(self.connection, capital_letters as u32) };
i32_to_bool(v)
}
pub fn set_capital_letters_uid(
&self,
capital_letters: CapitalLetters,
target_uid: u32,
) -> bool {
let v = unsafe {
spd_set_capital_letters_uid(self.connection, capital_letters as u32, target_uid)
};
i32_to_bool(v)
}
pub fn set_spelling(&self, spelling: bool) -> bool {
let s = if spelling {
SPDSpelling::SPD_SPELL_ON
} else {
SPDSpelling::SPD_SPELL_OFF
};
let v = unsafe { spd_set_spelling(self.connection, s) };
i32_to_bool(v)
}
pub fn set_spelling_all(&self, spelling: bool) -> bool {
let s = if spelling {
SPDSpelling::SPD_SPELL_ON
} else {
SPDSpelling::SPD_SPELL_OFF
};
let v = unsafe { spd_set_spelling_all(self.connection, s) };
i32_to_bool(v)
}
pub fn set_spelling_uid(&self, spelling: bool, target_uid: u32) -> bool {
let s = if spelling {
SPDSpelling::SPD_SPELL_ON
} else {
SPDSpelling::SPD_SPELL_OFF
};
let v = unsafe { spd_set_spelling_uid(self.connection, s, target_uid) };
i32_to_bool(v)
}
pub fn set_language<S: Into<String>>(&self, language: S) -> bool {
let v = unsafe {
spd_set_language(
self.connection,
CString::new(language.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn set_language_all<S: Into<String>>(&self, language: S) -> bool {
let v = unsafe {
spd_set_language_all(
self.connection,
CString::new(language.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn set_language_uid<S: Into<String>>(&self, language: S, target_uid: u32) -> bool {
let v = unsafe {
spd_set_language_uid(
self.connection,
CString::new(language.into()).unwrap().as_ptr(),
target_uid,
)
};
i32_to_bool(v)
}
pub fn get_language(&self) -> &str {
let v = unsafe { CStr::from_ptr(spd_get_language(self.connection)) };
v.to_str().unwrap()
}
pub fn set_output_module<S: Into<String>>(&self, output_module: S) -> bool {
let v = unsafe {
spd_set_output_module(
self.connection,
CString::new(output_module.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn set_output_module_all<S: Into<String>>(&self, output_module: S) -> bool {
let v = unsafe {
spd_set_output_module_all(
self.connection,
CString::new(output_module.into()).unwrap().as_ptr(),
)
};
i32_to_bool(v)
}
pub fn set_output_module_uid<S: Into<String>>(
&self,
output_module: S,
target_uid: u32,
) -> bool {
let v = unsafe {
spd_set_output_module_uid(
self.connection,
CString::new(output_module.into()).unwrap().as_ptr(),
target_uid,
)
};
i32_to_bool(v)
}
}
unsafe impl Send for Connection {}
impl Drop for Connection {
fn drop(&mut self) {
self.close();
}
}