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 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
SpeakerMeta,
SupportedDevices,
)
from ._rust import OpenJtalk, Synthesizer, VoiceModel, supported_devices # noqa: F401
from ._rust import ( # noqa: F401
OpenJtalk,
Synthesizer,
VoiceModel,
create_supported_devices,
)

__all__ = [
"AccelerationMode",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ from voicevox_core import (

__version__: str

def supported_devices() -> SupportedDevices: ...
def create_supported_devices() -> SupportedDevices: ...
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved

class VoiceModel:
@staticmethod
Expand Down
6 changes: 3 additions & 3 deletions crates/voicevox_core_python_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn rust(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
pyo3_log::init();

module.add("__version__", env!("CARGO_PKG_VERSION"))?;
module.add_wrapped(wrap_pyfunction!(supported_devices))?;
module.add_wrapped(wrap_pyfunction!(create_supported_devices))?;

module.add_class::<Synthesizer>()?;
module.add_class::<OpenJtalk>()?;
Expand All @@ -46,12 +46,12 @@ struct VoiceModel {
}

#[pyfunction]
fn supported_devices(py: Python) -> PyResult<&PyAny> {
fn create_supported_devices(py: Python) -> PyResult<&PyAny> {
let class = py
.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
4 changes: 2 additions & 2 deletions example/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ optional arguments:

```console
❯ python ./run.py ../../model/sample.vvm
[DEBUG] __main__: voicevox_core.supported_devices()=SupportedDevices(cpu=True, cuda=False, dml=False)
[DEBUG] __main__: voicevox_core.create_supported_devices()=SupportedDevices(cpu=True, cuda=False, dml=False)
[INFO] __main__: Initializing (acceleration_mode=<AccelerationMode.AUTO: 'AUTO'>, open_jtalk_dict_dir=PosixPath('open_jtalk_dic_utf_8-1.11'))
[DEBUG] __main__: synthesizer.metas=[]
[DEBUG] __main__: synthesizer.is_gpu_mode=False
[INFO] __main__: Loading `../../model/sample.vvm`
[INFO] __main__: Creating an AudioQuery from 'この音声は、ボイスボックスを使用して、出力されています。'
[INFO] __main__: Synthesizing with {"accent_phrases": [{"moras": [{"text": "コ", "consonant": "k", "consonant_length": 0.0556899, "vowel": "o", "vowel_length": 0.075180575, "pitch": 5.542309}, {"text": "ノ", "consonant": "n", "consonant_length": 0.06551014, "vowel": "o", "vowel_length": 0.09984577, "pitch": 5.6173983}], "accent": 2, "pause_mora": null, "is_interrogative": false}, {"moras": [{"text": "オ", "consonant": null, "consonant_length": null, "vowel": "o", "vowel_length": 0.116150305, "pitch": 5.7063766}, {"text": "ン", "consonant": null, "consonant_length": null, "vowel": "N", "vowel_length": 0.044380233, "pitch": 5.785717}, {"text": "セ", "consonant": "s", "consonant_length": 0.07719758, "vowel": "e", "vowel_length": 0.08653869, "pitch": 5.662092}, {"text": "エ", "consonant": null, "consonant_length": null, "vowel": "e", "vowel_length": 0.08311573, "pitch": 5.532917}, {"text": "ワ", "consonant": "w", "consonant_length": 0.06373148, "vowel": "a", "vowel_length": 0.16219379, "pitch": 5.293258}], "accent": 1, "pause_mora": {"text": "、", "consonant": null, "consonant_length": null, "vowel": "pau", "vowel_length": 0.35826492, "pitch": 0.0}, "is_interrogative": false}, {"moras": [{"text": "ボ", "consonant": "b", "consonant_length": 0.047082342, "vowel": "o", "vowel_length": 0.12611786, "pitch": 5.583892}, {"text": "イ", "consonant": null, "consonant_length": null, "vowel": "i", "vowel_length": 0.059451744, "pitch": 5.7947493}, {"text": "ス", "consonant": "s", "consonant_length": 0.089278996, "vowel": "u", "vowel_length": 0.11847979, "pitch": 5.818695}, {"text": "ボ", "consonant": "b", "consonant_length": 0.06535433, "vowel": "o", "vowel_length": 0.120458946, "pitch": 5.7965107}, {"text": "ッ", "consonant": null, "consonant_length": null, "vowel": "cl", "vowel_length": 0.06940381, "pitch": 0.0}, {"text": "ク", "consonant": "k", "consonant_length": 0.053739145, "vowel": "U", "vowel_length": 0.05395376, "pitch": 0.0}, {"text": "ス", "consonant": "s", "consonant_length": 0.10222931, "vowel": "u", "vowel_length": 0.071811065, "pitch": 5.8024883}, {"text": "オ", "consonant": null, "consonant_length": null, "vowel": "o", "vowel_length": 0.11092262, "pitch": 5.5036163}], "accent": 4, "pause_mora": null, "is_interrogative": false}, {"moras": [{"text": "シ", "consonant": "sh", "consonant_length": 0.09327768, "vowel": "i", "vowel_length": 0.09126951, "pitch": 5.369444}, {"text": "ヨ", "consonant": "y", "consonant_length": 0.06251812, "vowel": "o", "vowel_length": 0.07805054, "pitch": 5.5021667}, {"text": "オ", "consonant": null, "consonant_length": null, "vowel": "o", "vowel_length": 0.09904325, "pitch": 5.5219536}], "accent": 3, "pause_mora": null, "is_interrogative": false}, {"moras": [{"text": "シ", "consonant": "sh", "consonant_length": 0.04879771, "vowel": "I", "vowel_length": 0.06514315, "pitch": 0.0}, {"text": "テ", "consonant": "t", "consonant_length": 0.0840496, "vowel": "e", "vowel_length": 0.19438823, "pitch": 5.4875555}], "accent": 2, "pause_mora": {"text": "、", "consonant": null, "consonant_length": null, "vowel": "pau", "vowel_length": 0.35208154, "pitch": 0.0}, "is_interrogative": false}, {"moras": [{"text": "シュ", "consonant": "sh", "consonant_length": 0.05436731, "vowel": "U", "vowel_length": 0.06044446, "pitch": 0.0}, {"text": "ツ", "consonant": "ts", "consonant_length": 0.102865085, "vowel": "u", "vowel_length": 0.057028636, "pitch": 5.6402535}, {"text": "リョ", "consonant": "ry", "consonant_length": 0.058293864, "vowel": "o", "vowel_length": 0.080050275, "pitch": 5.6997967}, {"text": "ク", "consonant": "k", "consonant_length": 0.054767884, "vowel": "U", "vowel_length": 0.042932786, "pitch": 0.0}], "accent": 2, "pause_mora": null, "is_interrogative": false}, {"moras": [{"text": "サ", "consonant": "s", "consonant_length": 0.08067487, "vowel": "a", "vowel_length": 0.07377973, "pitch": 5.652378}, {"text": "レ", "consonant": "r", "consonant_length": 0.040600352, "vowel": "e", "vowel_length": 0.079322875, "pitch": 5.6290326}, {"text": "テ", "consonant": "t", "consonant_length": 0.06773268, "vowel": "e", "vowel_length": 0.08347456, "pitch": 5.6427326}], "accent": 3, "pause_mora": null, "is_interrogative": false}, {"moras": [{"text": "イ", "consonant": null, "consonant_length": null, "vowel": "i", "vowel_length": 0.07542324, "pitch": 5.641289}, {"text": "マ", "consonant": "m", "consonant_length": 0.066299975, "vowel": "a", "vowel_length": 0.107257664, "pitch": 5.6201453}, {"text": "ス", "consonant": "s", "consonant_length": 0.07186453, "vowel": "U", "vowel_length": 0.1163103, "pitch": 0.0}], "accent": 2, "pause_mora": null, "is_interrogative": false}], "speed_scale": 1.0, "pitch_scale": 0.0, "intonation_scale": 1.0, "volume_scale": 1.0, "pre_phoneme_length": 0.1, "post_phoneme_length": 0.1, "output_sampling_rate": 24000, "output_stereo": false, "kana": "コノ'/オ'ンセエワ、ボイスボ'ッ_クスオ/シヨオ'/_シテ'、_シュツ' リョ_ク/サレテ'/イマ'_ス"}
[INFO] __main__: Synthesizing with {"accent_phrases": [{"moras": [{"text": "コ", "consonant": "k", "consonant_length": 0.0556899, "vowel": "o", "vowel_length": 0.075180575, "pitch": 5.542309}, {"text": "ノ", "consonant": "n", "consonant_length": 0.06551014, "vowel": "o", "vowel_length": 0.09984577, "pitch": 5.6173983}], "accent": 2, "pause_mora": null, "is_interrogative": false}, {"moras": [{"text": "オ", "consonant": null, "consonant_length": null, "vowel": "o", "vowel_length": 0.116150305, "pitch": 5.7063766}, {"text": "ン", "consonant": null, "consonant_length": null, "vowel": "N", "vowel_length": 0.044380233, "pitch": 5.785717}, {"text": "セ", "consonant": "s", "consonant_length": 0.07719758, "vowel": "e", "vowel_length": 0.08653869, "pitch": 5.662092}, {"text": "エ", "consonant": null, "consonant_length": null, "vowel": "e", "vowel_length": 0.08311573, "pitch": 5.532917}, {"text": "ワ", "consonant": "w", "consonant_length": 0.06373148, "vowel": "a", "vowel_length": 0.16219379, "pitch": 5.293258}], "accent": 1, "pause_mora": {"text": "、", "consonant": null, "consonant_length": null, "vowel": "pau", "vowel_length": 0.35826492, "pitch": 0.0}, "is_interrogative": false}, {"moras": [{"text": "ボ", "consonant": "b", "consonant_length": 0.047082342, "vowel": "o", "vowel_length": 0.12611786, "pitch": 5.583892}, {"text": "イ", "consonant": null, "consonant_length": null, "vowel": "i", "vowel_length": 0.059451744, "pitch": 5.7947493}, {"text": "ス", "consonant": "s", "consonant_length": 0.089278996, "vowel": "u", "vowel_length": 0.11847979, "pitch": 5.818695}, {"text": "ボ", "consonant": "b", "consonant_length": 0.06535433, "vowel": "o", "vowel_length": 0.120458946, "pitch": 5.7965107}, {"text": "ッ", "consonant": null, "consonant_length": null, "vowel": "cl", "vowel_length": 0.06940381, "pitch": 0.0}, {"text": "ク", "consonant": "k", "consonant_length": 0.053739145, "vowel": "U", "vowel_length": 0.05395376, "pitch": 0.0}, {"text": "ス", "consonant": "s", "consonant_length": 0.10222931, "vowel": "u", "vowel_length": 0.071811065, "pitch": 5.8024883}, {"text": "オ", "consonant": null, "consonant_length": null, "vowel": "o", "vowel_length": 0.11092262, "pitch": 5.5036163}], "accent": 4, "pause_mora": null, "is_interrogative": false}, {"moras": [{"text": "シ", "consonant": "sh", "consonant_length": 0.09327768, "vowel": "i", "vowel_length": 0.09126951, "pitch": 5.369444}, {"text": "ヨ", "consonant": "y", "consonant_length": 0.06251812, "vowel": "o", "vowel_length": 0.07805054, "pitch": 5.5021667}, {"text": "オ", "consonant": null, "consonant_length": null, "vowel": "o", "vowel_length": 0.09904325, "pitch": 5.5219536}], "accent": 3, "pause_mora": null, "is_interrogative": false}, {"moras": [{"text": "シ", "consonant": "sh", "consonant_length": 0.04879771, "vowel": "I", "vowel_length": 0.06514315, "pitch": 0.0}, {"text": "テ", "consonant": "t", "consonant_length": 0.0840496, "vowel": "e", "vowel_length": 0.19438823, "pitch": 5.4875555}], "accent": 2, "pause_mora": {"text": "、", "consonant": null, "consonant_length": null, "vowel": "pau", "vowel_length": 0.35208154, "pitch": 0.0}, "is_interrogative": false}, {"moras": [{"text": "シュ", "consonant": "sh", "consonant_length": 0.05436731, "vowel": "U", "vowel_length": 0.06044446, "pitch": 0.0}, {"text": "ツ", "consonant": "ts", "consonant_length": 0.102865085, "vowel": "u", "vowel_length": 0.057028636, "pitch": 5.6402535}, {"text": "リョ", "consonant": "ry", "consonant_length": 0.058293864, "vowel": "o", "vowel_length": 0.080050275, "pitch": 5.6997967}, {"text": "ク", "consonant": "k", "consonant_length": 0.054767884, "vowel": "U", "vowel_length": 0.042932786, "pitch": 0.0}], "accent": 2, "pause_mora": null, "is_interrogative": false}, {"moras": [{"text": "サ", "consonant": "s", "consonant_length": 0.08067487, "vowel": "a", "vowel_length": 0.07377973, "pitch": 5.652378}, {"text": "レ", "consonant": "r", "consonant_length": 0.040600352, "vowel": "e", "vowel_length": 0.079322875, "pitch": 5.6290326}, {"text": "テ", "consonant": "t", "consonant_length": 0.06773268, "vowel": "e", "vowel_length": 0.08347456, "pitch": 5.6427326}], "accent": 3, "pause_mora": null, "is_interrogative": false}, {"moras": [{"text": "イ", "consonant": null, "consonant_length": null, "vowel": "i", "vowel_length": 0.07542324, "pitch": 5.641289}, {"text": "マ", "consonant": "m", "consonant_length": 0.066299975, "vowel": "a", "vowel_length": 0.107257664, "pitch": 5.6201453}, {"text": "ス", "consonant": "s", "consonant_length": 0.07186453, "vowel": "U", "vowel_length": 0.1163103, "pitch": 0.0}], "accent": 2, "pause_mora": null, "is_interrogative": false}], "speed_scale": 1.0, "pitch_scale": 0.0, "intonation_scale": 1.0, "volume_scale": 1.0, "pre_phoneme_length": 0.1, "post_phoneme_length": 0.1, "output_sampling_rate": 24000, "output_stereo": false, "kana": "コノ'/オ'ンセエワ、ボイスボ'ッ_クスオ/シヨオ'/_シテ'、_シュツ'リョ_ク/サレテ'/イマ'_ス"}
[INFO] __main__: Wrote `output.wav`
[DEBUG] voicevox_core_python_api: Destructing a VoicevoxCore
```
Expand Down
2 changes: 1 addition & 1 deletion example/python/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def main() -> None:
speaker_id,
) = parse_args()

logger.debug("%s", f"{voicevox_core.supported_devices()=}")
logger.debug("%s", f"{voicevox_core.create_supported_devices()=}")
qryxip marked this conversation as resolved.
Show resolved Hide resolved
qryxip marked this conversation as resolved.
Show resolved Hide resolved

logger.info("%s", f"Initializing ({acceleration_mode=}, {open_jtalk_dict_dir=})")
synthesizer = await Synthesizer.new_with_initialize(
Expand Down