Skip to content

Commit

Permalink
[project-vvm-async-api] ZSTにポインタキャストして提供するのをやめる (#512)
Browse files Browse the repository at this point in the history
ZSTにポインタキャストして提供するのをやめる
  • Loading branch information
qryxip authored Jun 9, 2023
1 parent f4b502a commit 2e58dcd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 64 deletions.
10 changes: 3 additions & 7 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.

26 changes: 5 additions & 21 deletions crates/voicevox_core_c_api/src/c_impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use derive_getters::Getters;
use std::{
ffi::{CStr, CString},
path::Path,
Expand All @@ -7,27 +6,19 @@ use std::{

use voicevox_core::{InitializeOptions, OpenJtalk, Result, Synthesizer, VoiceModel, VoiceModelId};

pub(crate) struct COpenJtalkRc {
open_jtalk: Arc<OpenJtalk>,
}
use crate::{OpenJtalkRc, VoicevoxSynthesizer, VoicevoxVoiceModel};

impl COpenJtalkRc {
impl OpenJtalkRc {
pub(crate) fn new_with_initialize(open_jtalk_dic_dir: impl AsRef<Path>) -> Result<Self> {
Ok(Self {
open_jtalk: Arc::new(OpenJtalk::new_with_initialize(open_jtalk_dic_dir)?),
})
}
}

#[derive(Getters)]
pub(crate) struct CSynthesizer {
synthesizer: Synthesizer,
metas_cstring: CString,
}

impl CSynthesizer {
impl VoicevoxSynthesizer {
pub(crate) async fn new_with_initialize(
open_jtalk: &COpenJtalkRc,
open_jtalk: &OpenJtalkRc,
options: &InitializeOptions,
) -> Result<Self> {
Ok(Self {
Expand Down Expand Up @@ -56,14 +47,7 @@ impl CSynthesizer {
}
}

#[derive(Getters)]
pub(crate) struct CVoiceModel {
model: VoiceModel,
id: CString,
metas: CString,
}

impl CVoiceModel {
impl VoicevoxVoiceModel {
pub(crate) async fn from_path(path: impl AsRef<Path>) -> Result<Self> {
let model = VoiceModel::from_path(path).await?;
let id = CString::new(model.id().raw_voice_model_id().as_str()).unwrap();
Expand Down
84 changes: 48 additions & 36 deletions crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ mod c_impls;
mod compatible_engine;
mod helpers;
use self::helpers::*;
use c_impls::*;
use chrono::SecondsFormat;
use const_default::ConstDefault;
use derive_getters::Getters;
use is_terminal::IsTerminal;
use once_cell::sync::Lazy;
use std::env;
use std::ffi::{CStr, CString};
use std::fmt;
use std::io::{self, Write};
use std::os::raw::c_char;
use std::sync::{Mutex, MutexGuard};
use std::sync::{Arc, Mutex, MutexGuard};
use tokio::runtime::Runtime;
use tracing_subscriber::fmt::format::Writer;
use tracing_subscriber::EnvFilter;
use voicevox_core::StyleId;
use voicevox_core::{
AccentPhraseModel, AudioQueryModel, AudioQueryOptions, TtsOptions, VoiceModelId,
AccentPhraseModel, AudioQueryModel, AudioQueryOptions, OpenJtalk, TtsOptions, VoiceModel,
VoiceModelId,
};
use voicevox_core::{SupportedDevices, SynthesisOptions};
use voicevox_core::{StyleId, SupportedDevices, SynthesisOptions, Synthesizer};

#[cfg(test)]
use rstest::*;
Expand Down Expand Up @@ -76,7 +76,9 @@ static BUFFER_MANAGER: Mutex<BufferManager> = Mutex::new(BufferManager::new());
*/

/// 参照カウントで管理されたOpenJtalk
pub struct OpenJtalkRc;
pub struct OpenJtalkRc {
open_jtalk: Arc<OpenJtalk>,
}

/// 参照カウントで管理されたOpenJtalkを生成する
///
Expand All @@ -89,8 +91,8 @@ pub unsafe extern "C" fn voicevox_open_jtalk_rc_new(
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let open_jtalk_dic_dir = ensure_utf8(CStr::from_ptr(open_jtalk_dic_dir))?;
let open_jtalk = COpenJtalkRc::new_with_initialize(open_jtalk_dic_dir)?;
out_open_jtalk.write(Box::into_raw(Box::new(open_jtalk)) as *mut OpenJtalkRc);
let open_jtalk = OpenJtalkRc::new_with_initialize(open_jtalk_dic_dir)?;
out_open_jtalk.write(Box::into_raw(Box::new(open_jtalk)));
Ok(())
})())
}
Expand All @@ -102,7 +104,7 @@ pub unsafe extern "C" fn voicevox_open_jtalk_rc_new(
/// @open_jtalk 有効な :OpenJtalkRc のポインタであること
#[no_mangle]
pub unsafe extern "C" fn voicevox_open_jtalk_rc_delete(open_jtalk: *mut OpenJtalkRc) {
let _ = Box::from_raw(open_jtalk as *mut COpenJtalkRc);
let _ = Box::from_raw(open_jtalk);
}

pub use voicevox_core::result_code::VoicevoxResultCode;
Expand Down Expand Up @@ -149,8 +151,12 @@ pub static voicevox_version: &c_char = {
};

/// 音声モデル
#[repr(C)]
pub struct VoicevoxVoiceModel;
#[derive(Getters)]
pub struct VoicevoxVoiceModel {
model: VoiceModel,
id: CString,
metas: CString,
}

/// 音声モデルID
pub type VoicevoxVoiceModelId = *const c_char;
Expand All @@ -172,8 +178,10 @@ pub unsafe extern "C" fn voicevox_voice_model_new_from_path(
out_model: *mut *mut VoicevoxVoiceModel,
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let model = RUNTIME.block_on(CVoiceModel::from_path(ensure_utf8(CStr::from_ptr(path))?))?;
out_model.write(Box::into_raw(Box::new(model)) as *mut VoicevoxVoiceModel);
let model = RUNTIME.block_on(VoicevoxVoiceModel::from_path(ensure_utf8(
CStr::from_ptr(path),
)?))?;
out_model.write(Box::into_raw(Box::new(model)));
Ok(())
})())
}
Expand All @@ -188,7 +196,7 @@ pub unsafe extern "C" fn voicevox_voice_model_new_from_path(
pub unsafe extern "C" fn voicevox_voice_model_id(
model: *const VoicevoxVoiceModel,
) -> VoicevoxVoiceModelId {
let model = &*(model as *const CVoiceModel);
let model = &*model;
model.id().as_ptr()
}

Expand All @@ -202,7 +210,7 @@ pub unsafe extern "C" fn voicevox_voice_model_id(
pub unsafe extern "C" fn voicevox_voice_model_get_metas_json(
model: *const VoicevoxVoiceModel,
) -> *const c_char {
let model = &*(model as *const CVoiceModel);
let model = &*model;
model.metas().as_ptr()
}

Expand All @@ -213,11 +221,14 @@ pub unsafe extern "C" fn voicevox_voice_model_get_metas_json(
/// @param model 有効な #VoicevoxVoiceModel へのポインタであること
#[no_mangle]
pub unsafe extern "C" fn voicevox_voice_model_delete(model: *mut VoicevoxVoiceModel) {
let _ = Box::from_raw(model as *mut CVoiceModel);
let _ = Box::from_raw(model);
}

#[repr(C)]
pub struct VoicevoxSynthesizer;
#[derive(Getters)]
pub struct VoicevoxSynthesizer {
synthesizer: Synthesizer,
metas_cstring: CString,
}

/// 音声シンセサイザを生成して初期化する
/// @param [in] open_jtalk 参照カウントで管理されたOpenJtalk
Expand All @@ -235,11 +246,12 @@ pub unsafe extern "C" fn voicevox_synthesizer_new_with_initialize(
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let options = options.into();
let open_jtalk = &*(open_jtalk as *const COpenJtalkRc);
let open_jtalk = &*open_jtalk;

let synthesizer =
RUNTIME.block_on(CSynthesizer::new_with_initialize(open_jtalk, &options))?;
out_synthesizer.write(Box::into_raw(Box::new(synthesizer)) as *mut VoicevoxSynthesizer);
let synthesizer = RUNTIME.block_on(VoicevoxSynthesizer::new_with_initialize(
open_jtalk, &options,
))?;
out_synthesizer.write(Box::into_raw(Box::new(synthesizer)));
Ok(())
})())
}
Expand All @@ -251,7 +263,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_new_with_initialize(
/// @param synthesizer 有効な #VoicevoxSynthesizer へのポインタであること
#[no_mangle]
pub unsafe extern "C" fn voicevox_synthesizer_delete(synthesizer: *mut VoicevoxSynthesizer) {
let _ = Box::from_raw(synthesizer as *mut CSynthesizer);
let _ = Box::from_raw(synthesizer);
}

/// モデルを読み込む
Expand All @@ -267,8 +279,8 @@ pub unsafe extern "C" fn voicevox_synthesizer_load_voice_model(
synthesizer: *mut VoicevoxSynthesizer,
model: *const VoicevoxVoiceModel,
) -> VoicevoxResultCode {
let synthesizer = &mut *(synthesizer as *mut CSynthesizer);
let model = &*(model as *const CVoiceModel);
let synthesizer = &mut *synthesizer;
let model = &*model;
into_result_code_with_error(
RUNTIME
.block_on(synthesizer.load_voice_model(model.model()))
Expand All @@ -289,7 +301,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_unload_voice_model(
synthesizer: *mut VoicevoxSynthesizer,
model_id: VoicevoxVoiceModelId,
) -> VoicevoxResultCode {
let synthesizer = &mut *(synthesizer as *mut CSynthesizer);
let synthesizer = &mut *synthesizer;
into_result_code_with_error((|| {
let raw_model_id = ensure_utf8(unsafe { CStr::from_ptr(model_id) })?;
synthesizer
Expand All @@ -308,7 +320,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_unload_voice_model(
pub unsafe extern "C" fn voicevox_synthesizer_is_gpu_mode(
synthesizer: *const VoicevoxSynthesizer,
) -> bool {
let synthesizer = &*(synthesizer as *const CSynthesizer);
let synthesizer = &*synthesizer;
synthesizer.synthesizer().is_gpu_mode()
}

Expand All @@ -325,7 +337,7 @@ pub unsafe extern "C" fn voicevox_is_loaded_voice_model(
synthesizer: *const VoicevoxSynthesizer,
model_id: VoicevoxVoiceModelId,
) -> bool {
let synthesizer = &*(synthesizer as *const CSynthesizer);
let synthesizer = &*synthesizer;
let raw_model_id = ensure_utf8(unsafe { CStr::from_ptr(model_id) }).unwrap();
synthesizer
.synthesizer()
Expand All @@ -342,7 +354,7 @@ pub unsafe extern "C" fn voicevox_is_loaded_voice_model(
pub unsafe extern "C" fn voicevox_synthesizer_get_metas_json(
synthesizer: *const VoicevoxSynthesizer,
) -> *const c_char {
let synthesizer = &*(synthesizer as *const CSynthesizer);
let synthesizer = &*synthesizer;
synthesizer.metas().as_ptr()
}

Expand Down Expand Up @@ -395,7 +407,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_audio_query(
output_audio_query_json: *mut *mut c_char,
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let synthesizer = &*(synthesizer as *const CSynthesizer);
let synthesizer = &*synthesizer;
let text = CStr::from_ptr(text);
let japanese_or_kana = ensure_utf8(text)?;
let audio_query = RUNTIME.block_on(synthesizer.synthesizer().audio_query(
Expand Down Expand Up @@ -440,7 +452,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_create_accent_phrases(
output_accent_phrases_json: *mut *mut c_char,
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let synthesizer = &*(synthesizer as *const CSynthesizer);
let synthesizer = &*synthesizer;
let text = ensure_utf8(CStr::from_ptr(text))?;
let accent_phrases = RUNTIME.block_on(synthesizer.synthesizer().create_accent_phrases(
text,
Expand Down Expand Up @@ -471,7 +483,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_replace_mora_data(
output_accent_phrases_json: *mut *mut c_char,
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let synthesizer = &*(synthesizer as *const CSynthesizer);
let synthesizer = &*synthesizer;
let accent_phrases: Vec<AccentPhraseModel> =
serde_json::from_str(ensure_utf8(CStr::from_ptr(accent_phrases_json))?)
.map_err(CApiError::InvalidAccentPhrase)?;
Expand Down Expand Up @@ -504,7 +516,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_replace_phoneme_length(
output_accent_phrases_json: *mut *mut c_char,
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let synthesizer = &*(synthesizer as *const CSynthesizer);
let synthesizer = &*synthesizer;
let accent_phrases: Vec<AccentPhraseModel> =
serde_json::from_str(ensure_utf8(CStr::from_ptr(accent_phrases_json))?)
.map_err(CApiError::InvalidAccentPhrase)?;
Expand Down Expand Up @@ -537,7 +549,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_replace_mora_pitch(
output_accent_phrases_json: *mut *mut c_char,
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let synthesizer = &*(synthesizer as *const CSynthesizer);
let synthesizer = &*synthesizer;
let accent_phrases: Vec<AccentPhraseModel> =
serde_json::from_str(ensure_utf8(CStr::from_ptr(accent_phrases_json))?)
.map_err(CApiError::InvalidAccentPhrase)?;
Expand Down Expand Up @@ -586,7 +598,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_synthesis(
output_wav: *mut *mut u8,
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let synthesizer = &*(synthesizer as *const CSynthesizer);
let synthesizer = &*synthesizer;
let audio_query_json = CStr::from_ptr(audio_query_json)
.to_str()
.map_err(|_| CApiError::InvalidUtf8Input)?;
Expand Down Expand Up @@ -639,7 +651,7 @@ pub unsafe extern "C" fn voicevox_synthesizer_tts(
output_wav: *mut *mut u8,
) -> VoicevoxResultCode {
into_result_code_with_error((|| {
let synthesizer = &*(synthesizer as *const CSynthesizer);
let synthesizer = &*synthesizer;
let text = ensure_utf8(CStr::from_ptr(text))?;
let output = RUNTIME.block_on(synthesizer.synthesizer().tts(
text,
Expand Down

0 comments on commit 2e58dcd

Please sign in to comment.