Skip to content

Commit

Permalink
add logic
Browse files Browse the repository at this point in the history
  • Loading branch information
phenylshima committed Oct 19, 2023
1 parent e6f553a commit cc7aed8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
57 changes: 55 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Error> {
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<Vec<i16>, Error> {
let labels = self
.jpreprocess
.extract_fullcontext(&input_text)
.map_err(|err| Error::new(Status::Unknown, err))?;
let audio: Vec<i16> = 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)
}
}

0 comments on commit cc7aed8

Please sign in to comment.