Skip to content

Commit

Permalink
型を分離して定義するようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerty2501 committed Jan 23, 2023
1 parent 385dd01 commit d7ddce2
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 121 deletions.
49 changes: 49 additions & 0 deletions crates/voicevox_core/src/devices.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use serde::{Deserialize, Serialize};

use super::*;

#[derive(Getters, Debug, Serialize, Deserialize)]
pub struct SupportedDevices {
cpu: bool,
cuda: bool,
dml: bool,
}

impl SupportedDevices {
/// サポートされているデバイス情報を取得する
pub fn get_supported_devices() -> Result<Self> {
let mut cuda_support = false;
let mut dml_support = false;
for provider in onnxruntime::session::get_available_providers()
.map_err(|e| Error::GetSupportedDevices(e.into()))?
.iter()
{
match provider.as_str() {
"CUDAExecutionProvider" => cuda_support = true,
"DmlExecutionProvider" => dml_support = true,
_ => {}
}
}

Ok(SupportedDevices {
cpu: true,
cuda: cuda_support,
dml: dml_support,
})
}

pub fn to_json(&self) -> serde_json::Value {
serde_json::to_value(self).expect("should not fail")
}
}

#[cfg(test)]
mod tests {
use super::*;
#[rstest]
fn supported_devices_get_supported_devices_works() {
let result = SupportedDevices::get_supported_devices();
// 環境によって結果が変わるので、関数呼び出しが成功するかどうかの確認のみ行う
assert!(result.is_ok(), "{:?}", result);
}
}
10 changes: 10 additions & 0 deletions crates/voicevox_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#![deny(unsafe_code)]

mod devices;
/// cbindgen:ignore
mod engine;
mod error;
mod manifest;
mod metas;
mod numerics;
mod publish;
mod result;
pub mod result_code;
mod status;
mod voice_model;
mod voice_synthesizer;

pub use self::publish::*;

Expand All @@ -19,7 +24,12 @@ use self::test_util::*;

pub use self::engine::AudioQueryModel;
pub use self::error::*;
pub use self::metas::*;
pub use self::result::*;
pub use self::voice_model::*;
pub use devices::*;
pub use manifest::*;
pub use voice_synthesizer::*;

use derive_getters::*;
use derive_new::new;
Expand Down
15 changes: 15 additions & 0 deletions crates/voicevox_core/src/manifest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use derive_getters::Getters;
use serde::Deserialize;
use std::path::PathBuf;

pub type RawManifestVersion = String;
#[derive(Deserialize)]
pub struct ManifestVersion(RawManifestVersion);

#[derive(Deserialize, Getters)]
pub struct Manifest {
manifest_version: ManifestVersion,
decode_file_path: PathBuf,
predict_duration_file_path: PathBuf,
predict_intonation_file_path: PathBuf,
}
38 changes: 38 additions & 0 deletions crates/voicevox_core/src/metas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use derive_getters::Getters;
use serde::Deserialize;

/// スタイルIdの実体
pub type RawStyleId = usize;
/// スタイルId
#[derive(Deserialize)]
pub struct StyleId(RawStyleId);

/// スピーカーIdの実体
pub type RawSpeakerId = usize;

/// スピーカーId
#[derive(Deserialize)]
pub struct SpeakerId(RawSpeakerId);

/// 音声合成モデルのメタ情報
#[derive(Deserialize, Getters)]
pub struct VoiceModelMeta {
speakers: Vec<SpeakerMeta>,
}

/// スピーカーのメタ情報
#[derive(Deserialize, Getters)]
pub struct SpeakerMeta {
id: SpeakerId,
name: String,
styles: Vec<StyleMeta>,
version: String,
uuid: String,
}

/// スタイルのメタ情報
#[derive(Deserialize, Getters)]
pub struct StyleMeta {
id: StyleId,
name: String,
}
81 changes: 1 addition & 80 deletions crates/voicevox_core/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,91 +7,12 @@ use onnxruntime::{
ndarray,
session::{AnyArray, NdArray},
};
use std::ffi::{CStr, CString};
use std::path::PathBuf;
use std::sync::Mutex;
use std::{
ffi::{CStr, CString},
path::Path,
};

const PHONEME_LENGTH_MINIMAL: f32 = 0.01;

/// 音声合成モデルIdの実体
pub type RawVoiceModelId = usize;

/// 音声合成モデルId (型を強く分けるためにこうしている)
pub struct VoiceModelId(RawVoiceModelId);

/// 音声合成モデル
pub struct VoiceModel {}

/// 音声合成モデルのメタ情報
pub struct VoiceModelMeta {
id: VoiceModelId,
speakers: Vec<SpeakerMeta>,
}

/// スピーカーのメタ情報
pub struct SpeakerMeta {
id: SpeakerId,
name: String,
styles: Vec<StyleMeta>,
version: String,
uuid: String,
}

pub struct StyleMeta {
id: StyleId,
name: String,
}

impl VoiceModel {
/// 与えられたパスからモデルを取得する
pub fn from_path(path: impl AsRef<Path>) -> Result<Self> {}
/// ランダムに発行されたこのモデルのIdを返す
pub fn id(&self) -> &VoiceModelId {}
/// このモデルのメタ情報を返す
pub fn metas(&self) -> &VoiceModelMeta {}
}

/// スタイルIdの実体
pub type RawStyleId = usize;
/// スタイルId
pub struct StyleId(RawStyleId);

/// スピーカーIdの実体
pub type RawSpeakerId = usize;

/// スピーカーId
pub struct SpeakerId(RawSpeakerId);

/// 音声シンセサイザ
pub struct VoiceSynthesizer {}
impl VoiceSynthesizer {
/// コンストラクタ兼初期化
pub fn new_with_initialize(options: InitializeOptions) -> Self {}

/// 音声合成モデルを読み込む
pub fn load_model(&mut self, model: VoiceModel) -> Result<()> {}

/// 指定したモデルIdの音声合成モデルを開放する
pub fn unload_model(&mut self, model_id: VoiceModelId) -> Result<()> {}

/// 指定したモデルIdの音声合成モデルが読み込まれているか判定する
pub fn is_loaded_model(&self, model_id: VoiceModelId) -> bool {}

/// 今読み込んでいる音声合成モデルのメタ情報を返す
pub fn metas(&self) -> &VoiceModelMeta {}

/// 音声合成を行う
pub fn synthesis(&self, style_id: &StyleId) -> Result<Vec<u8>> {}
}

pub struct Device {}

/// サポートされているデバイス情報を取得する
pub fn get_supported_devices() -> Result<SupportedDevices> {}

pub struct VoicevoxCore {
synthesis_engine: SynthesisEngine,
use_gpu: bool,
Expand Down
41 changes: 0 additions & 41 deletions crates/voicevox_core/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,40 +165,6 @@ static ENVIRONMENT: Lazy<Environment> = Lazy::new(|| {
.unwrap()
});

#[derive(Getters, Debug, Serialize, Deserialize)]
pub struct SupportedDevices {
cpu: bool,
cuda: bool,
dml: bool,
}

impl SupportedDevices {
pub fn get_supported_devices() -> Result<Self> {
let mut cuda_support = false;
let mut dml_support = false;
for provider in onnxruntime::session::get_available_providers()
.map_err(|e| Error::GetSupportedDevices(e.into()))?
.iter()
{
match provider.as_str() {
"CUDAExecutionProvider" => cuda_support = true,
"DmlExecutionProvider" => dml_support = true,
_ => {}
}
}

Ok(SupportedDevices {
cpu: true,
cuda: cuda_support,
dml: dml_support,
})
}

pub fn to_json(&self) -> serde_json::Value {
serde_json::to_value(self).expect("should not fail")
}
}

#[allow(unsafe_code)]
unsafe impl Send for Status {}

Expand Down Expand Up @@ -396,13 +362,6 @@ mod tests {
assert_eq!(expected, status.supported_styles);
}

#[rstest]
fn supported_devices_get_supported_devices_works() {
let result = SupportedDevices::get_supported_devices();
// 環境によって結果が変わるので、関数呼び出しが成功するかどうかの確認のみ行う
assert!(result.is_ok(), "{:?}", result);
}

#[rstest]
fn status_load_model_works() {
let mut status = Status::new(false, 0);
Expand Down
20 changes: 20 additions & 0 deletions crates/voicevox_core/src/voice_model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use super::*;
use std::path::Path;

/// 音声合成モデルIdの実体
pub type RawVoiceModelId = usize;

/// 音声合成モデルId (型を強く分けるためにこうしている)
pub struct VoiceModelId(RawVoiceModelId);

/// 音声合成モデル
pub struct VoiceModel {}

impl VoiceModel {
/// 与えられたパスからモデルを取得する
pub fn from_path(path: impl AsRef<Path>) -> Result<Self> {}
/// ランダムに発行されたこのモデルのIdを返す
pub fn id(&self) -> &VoiceModelId {}
/// このモデルのメタ情報を返す
pub fn metas(&self) -> &VoiceModelMeta {}
}
23 changes: 23 additions & 0 deletions crates/voicevox_core/src/voice_synthesizer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::*;

/// 音声シンセサイザ
pub struct VoiceSynthesizer {}
impl VoiceSynthesizer {
/// コンストラクタ兼初期化
pub fn new_with_initialize(options: InitializeOptions) -> Self {}

/// 音声合成モデルを読み込む
pub fn load_model(&mut self, model: VoiceModel) -> Result<()> {}

/// 指定したモデルIdの音声合成モデルを開放する
pub fn unload_model(&mut self, model_id: VoiceModelId) -> Result<()> {}

/// 指定したモデルIdの音声合成モデルが読み込まれているか判定する
pub fn is_loaded_model(&self, model_id: VoiceModelId) -> bool {}

/// 今読み込んでいる音声合成モデルのメタ情報を返す
pub fn metas(&self) -> &VoiceModelMeta {}

/// 音声合成を行う
pub fn synthesis(&self, style_id: &StyleId) -> Result<Vec<u8>> {}
}

0 comments on commit d7ddce2

Please sign in to comment.