Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ license.workspace = true

[dependencies]
espeak-sys = { path = "espeak-sys" }
thiserror = "2.0.18"

[dev-dependencies]
hound = "3.5.1"
6 changes: 6 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
let data_dir =
std::env::var("DEP_ESPEAK_SYS_DATA_DIR").expect("espeak-sys should export data-dir");

println!("cargo:rustc-env=ESPEAK_NG_DATA_DIR={}", data_dir);
}
2 changes: 2 additions & 0 deletions espeak-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ edition.workspace = true
authors.workspace = true
license.workspace = true

links = "espeak-sys"

[dependencies]

[build-dependencies]
Expand Down
13 changes: 7 additions & 6 deletions espeak-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ fn main() {
let espeak_src = manifest_dir.join("espeak-ng");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

// espeak-ng-data/ will contain the Espeak voices
let data_dir = out_dir.join("share/espeak-ng-data");
println!("cargo:data-dir={}", data_dir.display());

// for faster builds
unsafe {
env::set_var(
Expand All @@ -29,10 +33,6 @@ fn main() {
.define("USE_LIBSONIC", "OFF")
.define("USE_LIBPCAUDIO", "OFF")
.define("USE_SPEECHPLAYER", "OFF")
.define("COMPILE_INTONATIONS", "OFF")
.define("COMPILE_PHONEMES", "OFF")
.define("COMPILE_DICTIONARIES", "OFF")
.always_configure(false)
.very_verbose(env::var("CMAKE_VERBOSE").is_ok())
.build();

Expand Down Expand Up @@ -65,8 +65,9 @@ fn main() {
println!("cargo:rustc-link-lib=c++");
}

if cfg!(all(debug_assertions, windows)) {
println!("cargo:rustc-link-lib=dylib=msvcrtd");
if cfg!(target_os = "windows") {
// needed to find the data path
println!("cargo:rustc-link-lib=advapi32");
}

// Generate bindings
Expand Down
2 changes: 2 additions & 0 deletions espeak-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(unnecessary_transmutes)]
#![allow(clippy::ptr_offset_with_cast)]
#![allow(clippy::missing_safety_doc)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
77 changes: 77 additions & 0 deletions src/callback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use espeak_sys::espeak_EVENT;
use std::ffi::{c_int, c_short};

pub(crate) unsafe extern "C" fn synth_callback(
wav: *mut c_short,
num_samples: c_int,
events: *mut espeak_EVENT,
) -> c_int {
if wav.is_null() || num_samples <= 0 || events.is_null() {
return 0;
}

let user_data = unsafe { (*events).user_data };
if user_data.is_null() {
return 0;
}

let buffer = unsafe { &mut *(user_data as *mut Vec<i16>) };
let slice = unsafe { std::slice::from_raw_parts(wav, num_samples as usize) };
buffer.extend_from_slice(slice);

0
}

#[cfg(test)]
mod tests {
use super::*;
use std::ptr;

fn create_mock_event(user_data: *mut std::ffi::c_void) -> espeak_EVENT {
espeak_EVENT {
type_: 0,
unique_identifier: 0,
text_position: 0,
length: 0,
audio_position: 0,
sample: 0,
user_data,
id: espeak_sys::espeak_EVENT__bindgen_ty_1 { number: 0 },
}
}

#[test]
fn appends_samples_to_buffer() {
let mut buffer: Vec<i16> = Vec::new();
let mut wav_data: Vec<c_short> = vec![1, 2, 3, 4, 5, 6];
let mut event = create_mock_event(&mut buffer as *mut Vec<i16> as *mut _);

let result =
unsafe { synth_callback(wav_data.as_mut_ptr(), wav_data.len() as c_int, &mut event) };

assert_eq!(result, 0);
assert_eq!(buffer, vec![1, 2, 3, 4, 5, 6]);
}

#[test]
fn null_wav_returns_zero() {
let mut buffer: Vec<i16> = Vec::new();
let mut event = create_mock_event(&mut buffer as *mut Vec<i16> as *mut _);

let result = unsafe { synth_callback(ptr::null_mut(), 10, &mut event) };

assert_eq!(result, 0);
assert!(buffer.is_empty());
}

#[test]
fn null_user_data_returns_zero() {
let mut wav_data: Vec<c_short> = vec![1, 2, 3];
let mut event = create_mock_event(ptr::null_mut());

let result =
unsafe { synth_callback(wav_data.as_mut_ptr(), wav_data.len() as c_int, &mut event) };

assert_eq!(result, 0);
}
}
84 changes: 84 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::{ffi::NulError, str::Utf8Error};

use espeak_sys::{
espeak_ERROR, espeak_ERROR_EE_BUFFER_FULL as BUFFER_FULL,
espeak_ERROR_EE_INTERNAL_ERROR as INTERNAL_ERROR, espeak_ERROR_EE_NOT_FOUND as NOT_FOUND,
};

use super::EspeakParam;

#[derive(Clone, Debug, thiserror::Error)]
pub enum Error {
#[error("espeak operation failed: {}", espeak_error_msg(*.0))]
Espeak(espeak_ERROR),

#[error("no voices available")]
NoVoicesAvailable,

#[error("invalid value for '{0:?}': {1}")]
InvalidParamValue(EspeakParam, u32),

#[error(transparent)]
NullPointer(#[from] NulError),

#[error(transparent)]
InvalidUtf8(#[from] Utf8Error),
}

fn espeak_error_msg(code: espeak_ERROR) -> &'static str {
match code {
INTERNAL_ERROR => "internal error",
BUFFER_FULL => "buffer full",
NOT_FOUND => "not found",
_ => "unknown error",
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn espeak_variant_to_string_returns_expected() {
assert_eq!(
Error::Espeak(INTERNAL_ERROR).to_string(),
"espeak operation failed: internal error"
);
assert_eq!(
Error::Espeak(BUFFER_FULL).to_string(),
"espeak operation failed: buffer full"
);
assert_eq!(
Error::Espeak(NOT_FOUND).to_string(),
"espeak operation failed: not found"
);
assert_eq!(
Error::Espeak(10).to_string(),
"espeak operation failed: unknown error"
);
}

#[test]
fn invalid_param_value_variant_to_string_returns_expected() {
assert_eq!(
Error::InvalidParamValue(EspeakParam::Amplitude, 666).to_string(),
"invalid value for 'Amplitude': 666"
);
assert_eq!(
Error::InvalidParamValue(EspeakParam::Pitch, 666).to_string(),
"invalid value for 'Pitch': 666"
);
assert_eq!(
Error::InvalidParamValue(EspeakParam::PitchRange, 666).to_string(),
"invalid value for 'PitchRange': 666"
);
assert_eq!(
Error::InvalidParamValue(EspeakParam::Speed, 666).to_string(),
"invalid value for 'Speed': 666"
);
assert_eq!(
Error::InvalidParamValue(EspeakParam::WordGap, 666).to_string(),
"invalid value for 'WordGap': 666"
);
}
}
Loading