1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-25 12:39:37 +00:00

FFI: Implement tts_new(backend)

This required giving the Backends enum and Features struct a C
representation.
This commit is contained in:
mcb2003 2020-12-12 16:32:53 +00:00 committed by michael Connor buchan
parent 5e7ab42f59
commit 8e86afb444
2 changed files with 17 additions and 1 deletions

View File

@ -5,7 +5,7 @@ use std::{
ptr, ptr,
}; };
use crate::TTS; use crate::{Backends, TTS};
thread_local! { thread_local! {
/// Stores the last reported error, so it can be retrieved at will from C /// Stores the last reported error, so it can be retrieved at will from C
@ -38,6 +38,20 @@ pub extern "C" fn tts_clear_error() {
}); });
} }
/// Creates a new TTS object with the specified backend and returns a pointer to it.
/// If an error occured, a null pointer is returned,
/// Call `tts_get_error()` for more information about the specific error.
#[no_mangle]
pub extern "C" fn tts_new(backend: Backends) -> *mut TTS {
match TTS::new(backend) {
Ok(tts) => Box::into_raw(Box::new(tts)),
Err(e) => {
set_last_error(e.to_string()).unwrap();
ptr::null_mut()
}
}
}
/// Creates a new TTS object with the default backend and returns a pointer to it. /// Creates a new TTS object with the default backend and returns a pointer to it.
/// If an error occured, a null pointer is returned, /// If an error occured, a null pointer is returned,
/// Call `tts_get_error()` for more information about the specific error. /// Call `tts_get_error()` for more information about the specific error.

View File

@ -32,6 +32,7 @@ mod backends;
#[cfg(feature = "ffi")] #[cfg(feature = "ffi")]
pub mod ffi; pub mod ffi;
#[repr(C)]
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub enum Backends { pub enum Backends {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
@ -82,6 +83,7 @@ unsafe impl Send for UtteranceId {}
unsafe impl Sync for UtteranceId {} unsafe impl Sync for UtteranceId {}
#[repr(C)]
pub struct Features { pub struct Features {
pub stop: bool, pub stop: bool,
pub rate: bool, pub rate: bool,