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

mutabilityとasyncnessを仕上げる #553

Merged
merged 21 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5d17802
[wip] Add `gpu_num_sessions` options to `load_voice_model`
qryxip Jul 24, 2023
6eab358
Merge branch 'main' into rework-mutability-and-asyncness
qryxip Jul 29, 2023
ce2f8c0
`decode-with-gpu`を剥がす
qryxip Jul 29, 2023
47a77ee
`gpu_num_sessions`オプションを剥がす
qryxip Jul 29, 2023
ee2dc76
`predict`も別々に`Mutex`に包む
qryxip Jul 29, 2023
cf92b69
`LoadedModels`のdocを書く
qryxip Jul 29, 2023
b893fed
`cargo xtask update-c-header`
qryxip Jul 29, 2023
780d880
`git restore -s main -- crates/voicevox_core_c_api/tests/`
qryxip Jul 29, 2023
1e8886d
Merge branch 'main' into rework-mutability-and-asyncness
qryxip Aug 6, 2023
73344b8
Merge branch 'main' into rework-mutability-and-asyncness
qryxip Aug 11, 2023
c460e48
`synthesizer_get_metas_json` → `synthesizer_create_metas_json`
qryxip Aug 11, 2023
f98ea9f
Merge branch 'main' into rework-mutability-and-asyncness
qryxip Aug 12, 2023
06fef88
#575 で追加されたテストを修正
qryxip Aug 12, 2023
4891a9c
warningを解消
qryxip Aug 12, 2023
e971c2b
`create_metas_json`の返り値を`*mut c_char`に
qryxip Aug 12, 2023
d4eaf55
Merge branch 'main' into rework-mutability-and-asyncness
qryxip Aug 16, 2023
24f5e0f
Rework `ensure_not_contains`
qryxip Aug 16, 2023
e66168e
`LoadedModels`からのsession取得を`get`という単一のメソッドに
qryxip Aug 16, 2023
1101624
`Error::LoadModel`に色々統合する
qryxip Aug 17, 2023
36700d8
欠番を使わない
qryxip Aug 17, 2023
60cad17
`cargo xtask update-c-header`
qryxip Aug 17, 2023
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
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/voicevox_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async_zip.workspace = true
cfg-if = "1.0.0"
derive-getters.workspace = true
derive-new = "0.5.9"
derive_more = "0.99.17"
easy-ext.workspace = true
fs-err.workspace = true
futures = "0.3.26"
Expand Down
75 changes: 44 additions & 31 deletions crates/voicevox_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,15 @@ pub enum Error {
#[error("{}", base_error_message(VOICEVOX_RESULT_GPU_SUPPORT_ERROR))]
GpuSupport,

#[error("{} ({}): {source}", base_error_message(VOICEVOX_RESULT_LOAD_MODEL_ERROR), path.display())]
LoadModel {
path: PathBuf,
#[source]
source: anyhow::Error,
},
#[error("{} ({})", base_error_message(VOICEVOX_RESULT_ALREADY_LOADED_MODEL_ERROR), path.display())]
AlreadyLoadedModel { path: PathBuf },
#[error(transparent)]
LoadModel(#[from] LoadModelError),

#[error(
"{} ({model_id:?})",
base_error_message(VOICEVOX_RESULT_UNLOADED_MODEL_ERROR)
)]
UnloadedModel { model_id: VoiceModelId },

#[error(
"{}({path}):{source}",
base_error_message(VOICEVOX_RESULT_OPEN_FILE_ERROR)
)]
OpenFile {
path: PathBuf,
#[source]
source: anyhow::Error,
},

#[error(
"{}({path}):{source}",
base_error_message(VOICEVOX_RESULT_VVM_MODEL_READ_ERROR)
)]
VvmRead {
path: PathBuf,
#[source]
source: anyhow::Error,
},

#[error("{},{0}", base_error_message(VOICEVOX_RESULT_LOAD_METAS_ERROR))]
LoadMetas(#[source] anyhow::Error),

#[error(
"{},{0}",
base_error_message(VOICEVOX_RESULT_GET_SUPPORTED_DEVICES_ERROR)
Expand Down Expand Up @@ -111,6 +82,48 @@ pub enum Error {
InvalidWord(InvalidWordError),
}

pub(crate) type LoadModelResult<T> = std::result::Result<T, LoadModelError>;

/// 音声モデル読み込みのエラー。
#[derive(Error, Debug)]
#[error(
"`{path}`の読み込みに失敗しました: {context}{}",
source.as_ref().map(|e| format!(": {e}")).unwrap_or_default())
]
pub struct LoadModelError {
pub(crate) path: PathBuf,
pub(crate) context: LoadModelErrorKind,
#[source]
pub(crate) source: Option<anyhow::Error>,
}

impl LoadModelError {
pub fn context(&self) -> &LoadModelErrorKind {
&self.context
}
}

#[derive(derive_more::Display, Debug)]
pub enum LoadModelErrorKind {
//#[display(fmt = "{}", "base_error_message(VOICEVOX_RESULT_OPEN_ZIP_FILE_ERROR)")]
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
#[display(fmt = "ZIPファイルとして開くことができませんでした")]
OpenZipFile,
//#[display(fmt = "{}", "base_error_message(VOICEVOX_RESULT_READ_ZIP_ENTRY_ERROR)")]
#[display(fmt = "`{filename}`を読み取れませんでした")]
ReadZipEntry { filename: String },
//#[display(fmt = "{}", "base_error_message(VOICEVOX_RESULT_MODEL_ALREADY_LOADED_ERROR)")]
#[display(fmt = "モデル`{id}`は既に読み込まれています")]
ModelAlreadyLoaded { id: VoiceModelId },
//#[display(fmt = "{}", "base_error_message(VOICEVOX_RESULT_STYLE_ALREADY_LOADED_ERROR)")]
#[display(fmt = "スタイル`{id}`は既に読み込まれています")]
StyleAlreadyLoaded { id: StyleId },
#[display(
fmt = "{}",
"base_error_message(VOICEVOX_RESULT_INVALID_MODEL_DATA_ERROR)"
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
)]
InvalidModelData,
}

fn base_error_message(result_code: VoicevoxResultCode) -> &'static str {
let c_message: &'static str = crate::result_code::error_result_to_message(result_code);
&c_message[..(c_message.len() - 1)]
Expand Down
32 changes: 15 additions & 17 deletions crates/voicevox_core/src/result_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ pub enum VoicevoxResultCode {
VOICEVOX_RESULT_OK = 0,
/// open_jtalk辞書ファイルが読み込まれていない
VOICEVOX_RESULT_NOT_LOADED_OPENJTALK_DICT_ERROR = 1,
/// modelの読み込みに失敗した
VOICEVOX_RESULT_LOAD_MODEL_ERROR = 2,
/// サポートされているデバイス情報取得に失敗した
VOICEVOX_RESULT_GET_SUPPORTED_DEVICES_ERROR = 3,
/// GPUモードがサポートされていない
VOICEVOX_RESULT_GPU_SUPPORT_ERROR = 4,
/// メタ情報読み込みに失敗した
VOICEVOX_RESULT_LOAD_METAS_ERROR = 5,
/// 無効なstyle_idが指定された
VOICEVOX_RESULT_INVALID_STYLE_ID_ERROR = 6,
/// 無効なmodel_idが指定された
Expand All @@ -35,12 +31,16 @@ pub enum VoicevoxResultCode {
VOICEVOX_RESULT_INVALID_AUDIO_QUERY_ERROR = 14,
/// 無効なAccentPhrase
VOICEVOX_RESULT_INVALID_ACCENT_PHRASE_ERROR = 15,
/// ファイルオープンエラー
VOICEVOX_RESULT_OPEN_FILE_ERROR = 16,
/// Modelを読み込めなかった
VOICEVOX_RESULT_VVM_MODEL_READ_ERROR = 17,
/// すでに読み込まれているModelを読み込もうとした
VOICEVOX_RESULT_ALREADY_LOADED_MODEL_ERROR = 18,
/// ZIPファイルを開くことに失敗した
VOICEVOX_RESULT_OPEN_ZIP_FILE_ERROR = 16,
/// ZIP内のファイルが読めなかった
VOICEVOX_RESULT_READ_ZIP_ENTRY_ERROR = 17,
/// すでに読み込まれている音声モデルを読み込もうとした
VOICEVOX_RESULT_MODEL_ALREADY_LOADED_ERROR = 18,
/// すでに読み込まれているスタイルを読み込もうとした
VOICEVOX_RESULT_STYLE_ALREADY_LOADED_ERROR = 2,
/// 無効なモデルデータ
VOICEVOX_RESULT_INVALID_MODEL_DATA_ERROR = 5,
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
/// Modelが読み込まれていない
VOICEVOX_RESULT_UNLOADED_MODEL_ERROR = 19,
/// ユーザー辞書を読み込めなかった
Expand All @@ -64,8 +64,6 @@ pub const fn error_result_to_message(result_code: VoicevoxResultCode) -> &'stati
VOICEVOX_RESULT_NOT_LOADED_OPENJTALK_DICT_ERROR => {
"OpenJTalkの辞書が読み込まれていません\0"
}
VOICEVOX_RESULT_LOAD_MODEL_ERROR => "modelデータ読み込みに失敗しました\0",
VOICEVOX_RESULT_LOAD_METAS_ERROR => "メタデータ読み込みに失敗しました\0",

VOICEVOX_RESULT_GPU_SUPPORT_ERROR => "GPU機能をサポートすることができません\0",
VOICEVOX_RESULT_GET_SUPPORTED_DEVICES_ERROR => {
Expand All @@ -85,11 +83,11 @@ pub const fn error_result_to_message(result_code: VoicevoxResultCode) -> &'stati
}
VOICEVOX_RESULT_INVALID_AUDIO_QUERY_ERROR => "無効なaudio_queryです\0",
VOICEVOX_RESULT_INVALID_ACCENT_PHRASE_ERROR => "無効なaccent_phraseです\0",
VOICEVOX_RESULT_OPEN_FILE_ERROR => "ファイルオープンに失敗しました\0",
VOICEVOX_RESULT_VVM_MODEL_READ_ERROR => "Modelを読み込めませんでした\0",
VOICEVOX_RESULT_ALREADY_LOADED_MODEL_ERROR => {
"すでに読み込まれているModelを読み込もうとしました\0"
}
VOICEVOX_RESULT_OPEN_ZIP_FILE_ERROR => "ZIPファイルのオープンに失敗しました\0",
VOICEVOX_RESULT_READ_ZIP_ENTRY_ERROR => "ZIP内のファイルを読むことができませんでした\0",
VOICEVOX_RESULT_MODEL_ALREADY_LOADED_ERROR => "同じIDのモデルを読むことはできません\0",
VOICEVOX_RESULT_STYLE_ALREADY_LOADED_ERROR => "同じIDのスタイルを読むことはできません\0",
VOICEVOX_RESULT_INVALID_MODEL_DATA_ERROR => "モデルデータを読むことができませんでした\0",
VOICEVOX_RESULT_UNLOADED_MODEL_ERROR => "Modelが読み込まれていません\0",
VOICEVOX_RESULT_LOAD_USER_DICT_ERROR => "ユーザー辞書を読み込めませんでした\0",
VOICEVOX_RESULT_SAVE_USER_DICT_ERROR => "ユーザー辞書を書き込めませんでした\0",
Expand Down
32 changes: 22 additions & 10 deletions crates/voicevox_core/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ impl Status {
model: &[u8],
session_options: &SessionOptions,
path: impl AsRef<Path>,
) -> Result<Session<'static>> {
) -> LoadModelResult<Session<'static>> {
self.new_session_from_bytes(|| model_file::decrypt(model), session_options)
.map_err(|source| Error::LoadModel {
path: path.as_ref().into(),
source,
.map_err(|source| LoadModelError {
path: path.as_ref().to_owned(),
context: LoadModelErrorKind::InvalidModelData,
source: Some(source),
})
}

Expand Down Expand Up @@ -337,16 +338,27 @@ impl LoadedModels {
/// # Errors
///
/// 音声モデルIDかスタイルIDが`model`と重複するとき、エラーを返す。
fn ensure_acceptable(&self, model: &VoiceModel) -> Result<()> {
fn ensure_acceptable(&self, model: &VoiceModel) -> LoadModelResult<()> {
let loaded = self.styles();
let external = model.metas().iter().flat_map(|speaker| speaker.styles());

if self.0.contains_key(model.id())
|| iproduct!(loaded, external).any(|(loaded, external)| loaded.id() == external.id())
let error = |context| LoadModelError {
path: model.path().clone(),
context,
source: None,
};

if self.0.contains_key(model.id()) {
return Err(error(LoadModelErrorKind::ModelAlreadyLoaded {
id: model.id().clone(),
}));
}
if let Some((style, _)) =
iproduct!(loaded, external).find(|(loaded, external)| loaded.id() == external.id())
{
return Err(Error::AlreadyLoadedModel {
path: model.path().clone(),
});
return Err(error(LoadModelErrorKind::StyleAlreadyLoaded {
id: *style.id(),
}));
}
Ok(())
}
Expand Down
Loading