-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6f553a
commit cc7aed8
Showing
2 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |