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

[project-vvm-async-api] get_supported_devices_jsonをfallibleに #502

Merged
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
6 changes: 3 additions & 3 deletions crates/voicevox_core/src/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct SupportedDevices {

impl SupportedDevices {
/// サポートされているデバイス情報を取得する
pub fn get_supported_devices() -> Result<Self> {
pub fn create() -> Result<Self> {
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
let mut cuda_support = false;
let mut dml_support = false;
for provider in onnxruntime::session::get_available_providers()
Expand Down Expand Up @@ -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:?}");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/voicevox_core/src/inference_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl InferenceCore {
}

fn can_support_gpu_feature() -> Result<bool> {
let supported_devices = SupportedDevices::get_supported_devices()?;
let supported_devices = SupportedDevices::create()?;

cfg_if! {
if #[cfg(feature = "directml")]{
Expand Down
2 changes: 1 addition & 1 deletion crates/voicevox_core/src/voice_synthesizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]{
Expand Down
9 changes: 7 additions & 2 deletions crates/voicevox_core_c_api/include/voicevox_core.h

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

6 changes: 5 additions & 1 deletion crates/voicevox_core_c_api/src/compatible_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CString> = Lazy::new(|| {
CString::new(SupportedDevices::create().unwrap().to_json().to_string()).unwrap()
});
}

#[no_mangle]
Expand Down
25 changes: 14 additions & 11 deletions crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CString> =
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 のオプション
Expand Down
4 changes: 2 additions & 2 deletions crates/voicevox_core_c_api/tests/e2e/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down
2 changes: 1 addition & 1 deletion crates/voicevox_core_python_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down