Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Engine::load_from_bytes() #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,25 @@ impl Engine {

Ok(Self::new(voiceset, condition))
}

/// Load htsvoice file content and create a new [`Engine`].
#[cfg(feature = "htsvoice")]
pub fn load_from_bytes(voices: &[&[u8]]) -> Result<Self, EngineError> {
use crate::model::load_htsvoice_from_bytes;

let voices = voices
.iter()
.map(|bytes| Ok(Arc::new(load_htsvoice_from_bytes(bytes)?)))
.collect::<Result<Vec<_>, ModelError>>()?;

let voiceset = VoiceSet::new(voices)?;

let mut condition = Condition::default();
condition.load_model(&voiceset)?;

Ok(Self::new(voiceset, condition))
}

/// Create a new [`Engine`] with provided voices and condition.
pub fn new(voices: VoiceSet, condition: Condition) -> Self {
Engine { voices, condition }
Expand Down
6 changes: 6 additions & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ pub fn load_htsvoice_file<P: AsRef<std::path::Path>>(path: &P) -> Result<Voice,
Ok(parser::parse_htsvoice(&f)?)
}

/// Load `.htsvoice` file content as [`Voice`].
#[cfg(feature = "htsvoice")]
pub fn load_htsvoice_from_bytes(bytes: &[u8]) -> Result<Voice, ModelError> {
Ok(parser::parse_htsvoice(bytes)?)
}

Comment on lines +166 to +171
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この辺,データでロードできるならload_htsvoice_fileのstd::fs::readをEngine::loadに書くべきような気がします.

#[cfg(all(test, feature = "htsvoice"))]
pub(crate) mod tests {
use std::{borrow::Cow, sync::Arc};
Expand Down
Loading