You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
speech-dispatcher-rs/speech-dispatcher/src/lib.rs

819 lines
26 KiB

5 years ago
#![allow(non_upper_case_globals)]
use std::{
collections::HashMap,
ffi::{CStr, CString},
fmt,
marker::Send,
os::raw::{c_char, c_int},
sync::{Arc, Mutex},
};
5 years ago
use lazy_static::lazy_static;
5 years ago
use speech_dispatcher_sys::*;
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
5 years ago
pub enum Mode {
Single = SPDConnectionMode::SPD_MODE_SINGLE,
Threaded = SPDConnectionMode::SPD_MODE_THREADED,
5 years ago
}
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
5 years ago
pub enum Priority {
Important = SPDPriority::SPD_IMPORTANT,
Message = SPDPriority::SPD_MESSAGE,
Text = SPDPriority::SPD_TEXT,
Notification = SPDPriority::SPD_NOTIFICATION,
Progress = SPDPriority::SPD_PROGRESS,
5 years ago
}
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
5 years ago
pub enum VoiceType {
7 months ago
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,
5 years ago
}
#[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,
})
}
}
5 years ago
pub type Address = SPDConnectionAddress;
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
5 years ago
pub enum DataMode {
Text = SPDDataMode::SPD_DATA_TEXT,
SSML = SPDDataMode::SPD_DATA_SSML,
5 years ago
}
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
5 years ago
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,
5 years ago
}
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
5 years ago
pub enum Punctuation {
All = SPDPunctuation::SPD_PUNCT_ALL,
#[cfg(feature = "0_10")]
7 months ago
Most = SPDPunctuation::SPD_PUNCT_MOST,
Some = SPDPunctuation::SPD_PUNCT_SOME,
7 months ago
None = SPDPunctuation::SPD_PUNCT_NONE,
5 years ago
}
#[derive(Clone, Copy, Debug)]
#[repr(u32)]
5 years ago
pub enum CapitalLetters {
None = SPDCapitalLetters::SPD_CAP_NONE,
Spell = SPDCapitalLetters::SPD_CAP_SPELL,
Icon = SPDCapitalLetters::SPD_CAP_ICON,
5 years ago
}
/// 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),
}
5 years ago
}
#[derive(Default)]
struct Callbacks {
begin: Option<Box<dyn FnMut(u64, u64)>>,
end: Option<Box<dyn FnMut(u64, u64)>>,
index_mark: Option<Box<dyn FnMut(u64, u64, String)>>,
cancel: Option<Box<dyn FnMut(u64, u64)>>,
pause: Option<Box<dyn FnMut(u64, u64)>>,
resume: Option<Box<dyn FnMut(u64, u64)>>,
}
unsafe impl Send for Callbacks {}
unsafe impl Sync for Callbacks {}
lazy_static! {
static ref callbacks: Mutex<HashMap<u64, Callbacks>> = {
let m = HashMap::new();
Mutex::new(m)
};
}
unsafe extern "C" fn cb(msg_id: u64, client_id: u64, 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: u64, client_id: u64, 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>, u64);
5 years ago
impl Connection {
3 years ago
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();
5 years ago
let connection = unsafe {
let c = spd_open(
clientname.as_ptr(),
connectionname.as_ptr(),
username.as_ptr(),
3 years ago
mode as u32,
);
if c.is_null() {
Err(Error::InitializationError)
} else {
Ok(Self::setup_connection(c))
}
5 years ago
};
let mut c = Self(Arc::new(connection?), 0);
c.setup()?;
Ok(c)
5 years ago
}
pub unsafe fn open2<S: Into<String>>(
3 years ago
client_name: S,
connection_name: S,
user_name: S,
mode: Mode,
address: *mut Address,
autospawn: bool,
) -> Result<Self, Error> {
5 years ago
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::<u64>() {
self.1 = client_id;
}
}
}
}
callbacks.lock().unwrap().insert(self.1, Default::default());
self.set_notification_on(Notification::All)
.map_err(|_| Error::InitializationError)?;
Ok(())
5 years ago
}
pub fn close(&self) {
unsafe { spd_close(*self.0) };
5 years ago
}
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
}
5 years ago
}
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
}
5 years ago
}
pub fn stop(&self) -> Result<(), Error> {
let v = unsafe { spd_stop(*self.0) };
c_int_to_result(v)
5 years ago
}
pub fn stop_all(&self) -> Result<(), Error> {
let v = unsafe { spd_stop_all(*self.0) };
c_int_to_result(v)
5 years ago
}
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)
5 years ago
}
pub fn cancel(&self) -> Result<(), Error> {
let v = unsafe { spd_cancel(*self.0) };
c_int_to_result(v)
5 years ago
}
pub fn cancel_all(&self) -> Result<(), Error> {
let v = unsafe { spd_cancel_all(*self.0) };
c_int_to_result(v)
5 years ago
}
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)
5 years ago
}
pub fn pause(&self) -> Result<(), Error> {
let v = unsafe { spd_pause(*self.0) };
c_int_to_result(v)
5 years ago
}
pub fn pause_all(&self) -> Result<(), Error> {
let v = unsafe { spd_pause_all(*self.0) };
c_int_to_result(v)
5 years ago
}
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)
5 years ago
}
pub fn resume(&self) -> Result<(), Error> {
let v = unsafe { spd_resume(*self.0) };
c_int_to_result(v)
5 years ago
}
pub fn resume_all(&self) -> Result<(), Error> {
let v = unsafe { spd_resume_all(*self.0) };
c_int_to_result(v)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
pub fn set_voice_type(&self, voice_type: VoiceType) -> Result<(), Error> {
#[cfg(feature = "0_10")]
let v = unsafe { spd_set_voice_type(*self.0, voice_type as i32) };
#[cfg(not(feature = "0_10"))]
7 months ago
let v = unsafe { spd_set_voice_type(*self.0, voice_type as u32) };
c_int_to_result(v)
5 years ago
}
pub fn set_voice_type_all(&self, voice_type: VoiceType) -> Result<(), Error> {
#[cfg(feature = "0_10")]
let v = unsafe { spd_set_voice_type_all(*self.0, voice_type as i32) };
#[cfg(not(feature = "0_10"))]
7 months ago
let v = unsafe { spd_set_voice_type_all(*self.0, voice_type as u32) };
c_int_to_result(v)
5 years ago
}
pub fn set_voice_type_uid(&self, voice_type: VoiceType, target_uid: u32) -> Result<(), Error> {
#[cfg(feature = "0_10")]
let v = unsafe { spd_set_voice_type_uid(*self.0, voice_type as i32, target_uid) };
#[cfg(not(feature = "0_10"))]
7 months ago
let v = unsafe { spd_set_voice_type_uid(*self.0, voice_type as u32, target_uid) };
c_int_to_result(v)
5 years ago
}
pub fn get_voice_type(&self) -> Result<VoiceType, Error> {
let v = unsafe { spd_get_voice_type(*self.0) };
Ok(match v {
5 years ago
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?
})
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
pub fn get_voice_rate(&self) -> i32 {
unsafe { spd_get_voice_rate(*self.0) }
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
pub fn get_voice_pitch(&self) -> i32 {
unsafe { spd_get_voice_pitch(*self.0) }
5 years ago
}
pub fn set_volume(&self, volume: i32) -> Result<(), Error> {
let v = unsafe { spd_set_volume(*self.0, volume) };
c_int_to_result(v)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
pub fn get_volume(&self) -> i32 {
unsafe { spd_get_volume(*self.0) }
5 years ago
}
pub fn set_punctuation(&self, punctuation: Punctuation) -> Result<(), Error> {
let