Skip to content

Commit

Permalink
[project-vvm-async-api] get_supported_devices_jsonをfallibleに (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip authored Jun 6, 2023
1 parent 486d3f4 commit 38549d3
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 23 deletions.
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> {
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

0 comments on commit 38549d3

Please sign in to comment.