diff --git a/Cargo.toml b/Cargo.toml index 2817e24..ddfb0c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,9 @@ crate-type = ["cdylib"] napi = { version = "2.12.2", default-features = false, features = ["napi4"] } napi-derive = "2.12.2" +jpreprocess = "0.6.0" +htsengine = { git = "https://github.com/jpreprocess/htsengine-rs.git" } + [build-dependencies] napi-build = "2.0.1" diff --git a/src/lib.rs b/src/lib.rs index b479ed7..9886953 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,62 @@ #![deny(clippy::all)] +use htsengine::HTSEngine; +use jpreprocess::{JPreprocess, JPreprocessConfig, SystemDictionaryConfig}; +use napi::{Error, Status}; + #[macro_use] extern crate napi_derive; +#[napi(object)] +pub struct AltJTalkConfig { + pub dictionary: String, + pub model: String, +} + +#[napi] +pub struct AltJTalk { + jpreprocess: JPreprocess, + htsengine: HTSEngine, +} + #[napi] -pub fn sum(a: i32, b: i32) -> i32 { - a + b +impl AltJTalk { + #[napi(factory)] + pub fn from_config(config: AltJTalkConfig) -> Result { + let mut htsengine = HTSEngine::new(); + htsengine + .load(vec![config.model]) + .map_err(|err| Error::new(Status::InvalidArg, err))?; + Ok(Self { + jpreprocess: JPreprocess::from_config(JPreprocessConfig { + dictionary: SystemDictionaryConfig::File(config.dictionary.into()), + user_dictionary: None, + }) + .map_err(|err| Error::new(Status::InvalidArg, err))?, + htsengine, + }) + } + #[napi] + pub fn synthesize(&mut self, input_text: String) -> Result, Error> { + let labels = self + .jpreprocess + .extract_fullcontext(&input_text) + .map_err(|err| Error::new(Status::Unknown, err))?; + let audio: Vec = self + .htsengine + .synthesize(labels) + .map_err(|err| Error::new(Status::Unknown, err))? + .into_iter() + .map(|d| { + if d < (i16::MIN as f64) { + i16::MIN + } else if d > (i16::MAX as f64) { + i16::MAX + } else { + d as i16 + } + }) + .collect(); + Ok(audio) + } }