Skip to content

Commit

Permalink
Merge pull request #6 from tuna2134/patchelf
Browse files Browse the repository at this point in the history
Patchelf
  • Loading branch information
tuna2134 authored Nov 28, 2023
2 parents d62c18e + 76f54d5 commit 66db699
Show file tree
Hide file tree
Showing 17 changed files with 679 additions and 320 deletions.
1 change: 1 addition & 0 deletions crates/voicevox_core/src/__internal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod doctest_fixtures;
pub mod interop;

// VOICEVOX CORE内のラッパー向けの実装
// FIXME: 要議論: https://github.com/VOICEVOX/voicevox_core/issues/595
Expand Down
6 changes: 3 additions & 3 deletions crates/voicevox_core/src/__internal/doctest_fixtures.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{path::Path, sync::Arc};
use std::path::Path;

use crate::{AccelerationMode, InitializeOptions, OpenJtalk, Synthesizer, VoiceModel};

pub async fn synthesizer_with_sample_voice_model(
open_jtalk_dic_dir: impl AsRef<Path>,
) -> anyhow::Result<Synthesizer> {
) -> anyhow::Result<Synthesizer<OpenJtalk>> {
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new(open_jtalk_dic_dir).await.unwrap()),
OpenJtalk::new(open_jtalk_dic_dir).await?,
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions crates/voicevox_core/src/__internal/interop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use crate::synthesizer::PerformInference;
83 changes: 27 additions & 56 deletions crates/voicevox_core/src/engine/open_jtalk.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::io::Write;
use std::sync::Arc;
use std::{
path::{Path, PathBuf},
sync::Mutex,
};
use std::{path::Path, sync::Mutex};

use anyhow::anyhow;
use tempfile::NamedTempFile;
Expand All @@ -21,9 +18,10 @@ pub(crate) struct OpenjtalkFunctionError {
}

/// テキスト解析器としてのOpen JTalk。
#[derive(Clone)]
pub struct OpenJtalk {
resources: Arc<Mutex<Resources>>,
dict_dir: Option<PathBuf>,
dict_dir: Arc<String>, // FIXME: `camino::Utf8PathBuf`にする
}

struct Resources {
Expand All @@ -36,51 +34,45 @@ struct Resources {
unsafe impl Send for Resources {}

impl OpenJtalk {
// FIXME: この関数は廃止し、`Synthesizer`は`Option<OpenJtalk>`という形でこの構造体を持つ
pub fn new_without_dic() -> Self {
Self {
resources: Mutex::new(Resources {
pub async fn new(open_jtalk_dict_dir: impl AsRef<Path>) -> crate::result::Result<Self> {
let dict_dir = open_jtalk_dict_dir
.as_ref()
.to_str()
.unwrap_or_else(|| todo!()) // FIXME: `camino::Utf8Path`を要求するようにする
.to_owned();
let dict_dir = Arc::new(dict_dir);

crate::task::asyncify(move || {
let mut resources = Resources {
mecab: ManagedResource::initialize(),
njd: ManagedResource::initialize(),
jpcommon: ManagedResource::initialize(),
})
.into(),
dict_dir: None,
}
}

pub async fn new(open_jtalk_dict_dir: impl AsRef<Path>) -> crate::result::Result<Self> {
let open_jtalk_dict_dir = open_jtalk_dict_dir.as_ref().to_owned();
};

tokio::task::spawn_blocking(move || {
let mut s = Self::new_without_dic();
s.load(open_jtalk_dict_dir).map_err(|()| {
let result = resources.mecab.load(&*dict_dir);
if !result {
// FIXME: 「システム辞書を読もうとしたけど読めなかった」というエラーをちゃんと用意する
ErrorRepr::NotLoadedOpenjtalkDict
})?;
Ok(s)
return Err(ErrorRepr::NotLoadedOpenjtalkDict.into());
}

Ok(Self {
resources: Mutex::new(resources).into(),
dict_dir,
})
})
.await
.unwrap()
}

// 先に`load`を呼ぶ必要がある。
/// ユーザー辞書を設定する。
///
/// この関数を呼び出した後にユーザー辞書を変更した場合は、再度この関数を呼ぶ必要がある。
pub async fn use_user_dict(&self, user_dict: &UserDict) -> crate::result::Result<()> {
let dict_dir = self
.dict_dir
.as_ref()
.and_then(|dict_dir| dict_dir.to_str())
.ok_or(ErrorRepr::NotLoadedOpenjtalkDict)?
.to_owned();

let resources = self.resources.clone();
let dict_dir = self.dict_dir.clone();

let words = user_dict.to_mecab_format();

let result = tokio::task::spawn_blocking(move || -> crate::Result<_> {
let result = crate::task::asyncify(move || -> crate::Result<_> {
// ユーザー辞書用のcsvを作成
let mut temp_csv =
NamedTempFile::new().map_err(|e| ErrorRepr::UseUserDict(e.into()))?;
Expand Down Expand Up @@ -109,10 +101,9 @@ impl OpenJtalk {

let Resources { mecab, .. } = &mut *resources.lock().unwrap();

Ok(mecab.load_with_userdic(dict_dir.as_ref(), Some(Path::new(&temp_dict_path))))
Ok(mecab.load_with_userdic((*dict_dir).as_ref(), Some(Path::new(&temp_dict_path))))
})
.await
.unwrap()?;
.await?;

if !result {
return Err(ErrorRepr::UseUserDict(anyhow!("辞書のコンパイルに失敗しました")).into());
Expand Down Expand Up @@ -169,26 +160,6 @@ impl OpenJtalk {
})
}
}

fn load(&mut self, open_jtalk_dict_dir: impl AsRef<Path>) -> std::result::Result<(), ()> {
let result = self
.resources
.lock()
.unwrap()
.mecab
.load(open_jtalk_dict_dir.as_ref());
if result {
self.dict_dir = Some(open_jtalk_dict_dir.as_ref().into());
Ok(())
} else {
self.dict_dir = None;
Err(())
}
}

pub fn dict_loaded(&self) -> bool {
self.dict_dir.is_some()
}
}

#[cfg(test)]
Expand Down
13 changes: 8 additions & 5 deletions crates/voicevox_core/src/infer/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,16 @@ impl<R: InferenceRuntime, D: InferenceDomain> Status<R, D> {
self.is_loaded_model_by_style_id(style_id)
}

/// 推論を実行する。
///
/// # Performance
///
/// CPU/GPU-boundな操作であるため、非同期ランタイム上では直接実行されるべきではない。
///
/// # Panics
///
/// `self`が`model_id`を含んでいないとき、パニックする。
pub(crate) async fn run_session<I>(
pub(crate) fn run_session<I>(
&self,
model_id: &VoiceModelId,
input: I,
Expand All @@ -103,10 +109,7 @@ impl<R: InferenceRuntime, D: InferenceDomain> Status<R, D> {
I::Signature: InferenceSignature<Domain = D>,
{
let sess = self.loaded_models.lock().unwrap().get(model_id);

tokio::task::spawn_blocking(move || sess.run(input))
.await
.unwrap()
sess.run(input)
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/voicevox_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ mod metas;
mod numerics;
mod result;
mod synthesizer;
mod task;
mod user_dict;
mod version;
mod voice_model;

#[doc(hidden)]
pub mod __internal;

#[cfg(test)]
Expand All @@ -31,7 +31,9 @@ pub use self::result::*;
pub use self::voice_model::*;
pub use devices::*;
pub use manifest::*;
pub use synthesizer::*;
pub use synthesizer::{
AccelerationMode, InitializeOptions, SynthesisOptions, Synthesizer, TtsOptions,
};
pub use user_dict::*;
pub use version::*;

Expand Down
Loading

0 comments on commit 66db699

Please sign in to comment.