From 38549d311144c3b594bad0e3bf85ca465ec233eb Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Tue, 6 Jun 2023 13:01:25 +0900 Subject: [PATCH] =?UTF-8?q?[project-vvm-async-api]=20`get=5Fsupported=5Fde?= =?UTF-8?q?vices=5Fjson`=E3=82=92fallible=E3=81=AB=20(#502)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/voicevox_core/src/devices.rs | 6 ++--- crates/voicevox_core/src/inference_core.rs | 2 +- crates/voicevox_core/src/voice_synthesizer.rs | 2 +- .../include/voicevox_core.h | 9 +++++-- .../src/compatible_engine.rs | 6 ++++- crates/voicevox_core_c_api/src/lib.rs | 25 +++++++++++-------- .../voicevox_core_c_api/tests/e2e/symbols.rs | 4 +-- .../tests/e2e/testcases/compatible_engine.rs | 2 +- crates/voicevox_core_python_api/src/lib.rs | 2 +- 9 files changed, 35 insertions(+), 23 deletions(-) diff --git a/crates/voicevox_core/src/devices.rs b/crates/voicevox_core/src/devices.rs index ceef4b9d3..d3141664c 100644 --- a/crates/voicevox_core/src/devices.rs +++ b/crates/voicevox_core/src/devices.rs @@ -11,7 +11,7 @@ pub struct SupportedDevices { impl SupportedDevices { /// サポートされているデバイス情報を取得する - pub fn get_supported_devices() -> Result { + pub fn create() -> Result { let mut cuda_support = false; let mut dml_support = false; for provider in onnxruntime::session::get_available_providers() @@ -41,8 +41,8 @@ impl SupportedDevices { mod tests { use super::*; #[rstest] - fn supported_devices_get_supported_devices_works() { - let result = SupportedDevices::get_supported_devices(); + fn supported_devices_create_works() { + let result = SupportedDevices::create(); // 環境によって結果が変わるので、関数呼び出しが成功するかどうかの確認のみ行う assert!(result.is_ok(), "{result:?}"); } diff --git a/crates/voicevox_core/src/inference_core.rs b/crates/voicevox_core/src/inference_core.rs index e22269486..578e57453 100644 --- a/crates/voicevox_core/src/inference_core.rs +++ b/crates/voicevox_core/src/inference_core.rs @@ -32,7 +32,7 @@ impl InferenceCore { } fn can_support_gpu_feature() -> Result { - let supported_devices = SupportedDevices::get_supported_devices()?; + let supported_devices = SupportedDevices::create()?; cfg_if! { if #[cfg(feature = "directml")]{ diff --git a/crates/voicevox_core/src/voice_synthesizer.rs b/crates/voicevox_core/src/voice_synthesizer.rs index 3557aa03d..174d3782e 100644 --- a/crates/voicevox_core/src/voice_synthesizer.rs +++ b/crates/voicevox_core/src/voice_synthesizer.rs @@ -89,7 +89,7 @@ impl Synthesizer { list_windows_video_cards(); let use_gpu = match options.acceleration_mode { AccelerationMode::Auto => { - let supported_devices = SupportedDevices::get_supported_devices()?; + let supported_devices = SupportedDevices::create()?; cfg_if! { if #[cfg(feature="directml")]{ diff --git a/crates/voicevox_core_c_api/include/voicevox_core.h b/crates/voicevox_core_c_api/include/voicevox_core.h index 55c40aa2a..eebbc2d41 100644 --- a/crates/voicevox_core_c_api/include/voicevox_core.h +++ b/crates/voicevox_core_c_api/include/voicevox_core.h @@ -428,12 +428,17 @@ const char *voicevox_synthesizer_get_metas_json(const struct VoicevoxSynthesizer /** * サポートデバイス情報をjsonで取得する - * @return サポートデバイス情報のjson文字列 + * @param [out] output_supported_devices_json サポートデバイス情報のjson文字列 + * @return 結果コード #VoicevoxResultCode + * + * # Safety + * @param output_supported_devices_json 自動でheapメモリが割り当てられるので ::voicevox_json_free で解放する必要がある */ #ifdef _WIN32 __declspec(dllimport) #endif - const char *voicevox_get_supported_devices_json(void); + +VoicevoxResultCode voicevox_create_supported_devices_json(char **output_supported_devices_json); /** * デフォルトの AudioQuery のオプションを生成する diff --git a/crates/voicevox_core_c_api/src/compatible_engine.rs b/crates/voicevox_core_c_api/src/compatible_engine.rs index d944c0259..e84040abc 100644 --- a/crates/voicevox_core_c_api/src/compatible_engine.rs +++ b/crates/voicevox_core_c_api/src/compatible_engine.rs @@ -137,7 +137,11 @@ pub extern "C" fn last_error_message() -> *const c_char { #[no_mangle] pub extern "C" fn supported_devices() -> *const c_char { - voicevox_get_supported_devices_json() + return SUPPORTED_DEVICES.as_ptr(); + + static SUPPORTED_DEVICES: Lazy = Lazy::new(|| { + CString::new(SupportedDevices::create().unwrap().to_json().to_string()).unwrap() + }); } #[no_mangle] diff --git a/crates/voicevox_core_c_api/src/lib.rs b/crates/voicevox_core_c_api/src/lib.rs index 1391302e0..f592e72e4 100644 --- a/crates/voicevox_core_c_api/src/lib.rs +++ b/crates/voicevox_core_c_api/src/lib.rs @@ -346,19 +346,22 @@ pub unsafe extern "C" fn voicevox_synthesizer_get_metas_json( synthesizer.metas().as_ptr() } -static VOICEVOX_SUPPORTED_DEVICES_JSON: once_cell::sync::Lazy = - once_cell::sync::Lazy::new(|| { - CString::new( - serde_json::to_string(&SupportedDevices::get_supported_devices().unwrap()).unwrap(), - ) - .unwrap() - }); - /// サポートデバイス情報をjsonで取得する -/// @return サポートデバイス情報のjson文字列 +/// @param [out] output_supported_devices_json サポートデバイス情報のjson文字列 +/// @return 結果コード #VoicevoxResultCode +/// +/// # Safety +/// @param output_supported_devices_json 自動でheapメモリが割り当てられるので ::voicevox_json_free で解放する必要がある #[no_mangle] -pub extern "C" fn voicevox_get_supported_devices_json() -> *const c_char { - VOICEVOX_SUPPORTED_DEVICES_JSON.as_ptr() +pub unsafe extern "C" fn voicevox_create_supported_devices_json( + output_supported_devices_json: *mut *mut c_char, +) -> VoicevoxResultCode { + into_result_code_with_error((|| { + let supported_devices = + CString::new(SupportedDevices::create()?.to_json().to_string()).unwrap(); + output_supported_devices_json.write(supported_devices.into_raw()); + Ok(()) + })()) } /// Audio query のオプション diff --git a/crates/voicevox_core_c_api/tests/e2e/symbols.rs b/crates/voicevox_core_c_api/tests/e2e/symbols.rs index c26cb5e34..7c5e0b1b7 100644 --- a/crates/voicevox_core_c_api/tests/e2e/symbols.rs +++ b/crates/voicevox_core_c_api/tests/e2e/symbols.rs @@ -53,7 +53,7 @@ pub(crate) struct Symbols<'lib> { >, pub(crate) voicevox_synthesizer_get_metas_json: Symbol<'lib, unsafe extern "C" fn(*const VoicevoxSynthesizer) -> *const c_char>, - pub(crate) voicevox_get_supported_devices_json: + pub(crate) voicevox_create_supported_devices_json: Symbol<'lib, unsafe extern "C" fn() -> *const c_char>, pub(crate) voicevox_make_default_audio_query_options: Symbol<'lib, unsafe extern "C" fn() -> VoicevoxAudioQueryOptions>, @@ -153,7 +153,7 @@ impl<'lib> Symbols<'lib> { voicevox_synthesizer_is_gpu_mode, voicevox_is_loaded_voice_model, voicevox_synthesizer_get_metas_json, - voicevox_get_supported_devices_json, + voicevox_create_supported_devices_json, voicevox_make_default_audio_query_options, voicevox_synthesizer_audio_query, voicevox_make_default_synthesis_options, diff --git a/crates/voicevox_core_c_api/tests/e2e/testcases/compatible_engine.rs b/crates/voicevox_core_c_api/tests/e2e/testcases/compatible_engine.rs index df15173da..3682036ee 100644 --- a/crates/voicevox_core_c_api/tests/e2e/testcases/compatible_engine.rs +++ b/crates/voicevox_core_c_api/tests/e2e/testcases/compatible_engine.rs @@ -121,7 +121,7 @@ impl assert_cdylib::TestCase for TestCase { std::assert_eq!(SNAPSHOTS.metas, metas_json); std::assert_eq!( - SupportedDevices::get_supported_devices().unwrap().to_json(), + SupportedDevices::create().unwrap().to_json(), supported_devices, ); diff --git a/crates/voicevox_core_python_api/src/lib.rs b/crates/voicevox_core_python_api/src/lib.rs index 0f8469b43..e40294609 100644 --- a/crates/voicevox_core_python_api/src/lib.rs +++ b/crates/voicevox_core_python_api/src/lib.rs @@ -51,7 +51,7 @@ fn supported_devices(py: Python) -> PyResult<&PyAny> { .import("voicevox_core")? .getattr("SupportedDevices")? .downcast()?; - let s = voicevox_core::SupportedDevices::get_supported_devices().into_py_result()?; + let s = voicevox_core::SupportedDevices::create().into_py_result()?; to_pydantic_dataclass(s, class) }