Skip to content

Commit

Permalink
engine互換性を保つためのコード実装を復活させた
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerty2501 committed Sep 14, 2022
1 parent d865b5a commit 824373e
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 1 deletion.
160 changes: 160 additions & 0 deletions crates/voicevox_core_c_api/src/compatible_engine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
use super::*;
use libc::c_int;

pub use voicevox_core::result_code::VoicevoxResultCode;

static ERROR_MESSAGE: Lazy<Mutex<String>> = Lazy::new(|| Mutex::new(String::new()));

fn set_message(message: &str) {
ERROR_MESSAGE
.lock()
.unwrap()
.replace_range(.., &format!("{}\0", message));
}

#[no_mangle]
pub extern "C" fn initialize(use_gpu: bool, cpu_num_threads: c_int, load_all_models: bool) -> bool {
let result = lock_internal().initialize(voicevox_core::InitializeOptions {
acceleration_mode: if use_gpu {
voicevox_core::AccelerationMode::Gpu
} else {
voicevox_core::AccelerationMode::Cpu
},
cpu_num_threads: cpu_num_threads as u16,
load_all_models,
..Default::default()
});
if let Some(err) = result.err() {
set_message(&format!("{}", err));
false
} else {
true
}
}

#[no_mangle]
pub extern "C" fn load_model(speaker_id: i64) -> bool {
let result = lock_internal().load_model(speaker_id as u32);
if let Some(err) = result.err() {
set_message(&format!("{}", err));
false
} else {
true
}
}

#[no_mangle]
pub extern "C" fn is_model_loaded(speaker_id: i64) -> bool {
lock_internal().is_model_loaded(speaker_id as u32)
}

#[no_mangle]
pub extern "C" fn finalize() {
lock_internal().finalize()
}

#[no_mangle]
pub extern "C" fn metas() -> *const c_char {
voicevox_get_metas_json()
}

#[no_mangle]
pub extern "C" fn last_error_message() -> *const c_char {
ERROR_MESSAGE.lock().unwrap().as_ptr() as *const c_char
}

#[no_mangle]
pub extern "C" fn supported_devices() -> *const c_char {
voicevox_get_supported_devices_json()
}

#[no_mangle]
pub extern "C" fn yukarin_s_forward(
length: i64,
phoneme_list: *mut i64,
speaker_id: *mut i64,
output: *mut f32,
) -> bool {
let result = lock_internal().predict_duration(
unsafe { std::slice::from_raw_parts_mut(phoneme_list, length as usize) },
unsafe { *speaker_id as u32 },
);
match result {
Ok(output_vec) => {
let output_slice = unsafe { std::slice::from_raw_parts_mut(output, length as usize) };
output_slice.clone_from_slice(&output_vec);
true
}
Err(err) => {
set_message(&format!("{}", err));
false
}
}
}

#[no_mangle]
pub extern "C" fn yukarin_sa_forward(
length: i64,
vowel_phoneme_list: *mut i64,
consonant_phoneme_list: *mut i64,
start_accent_list: *mut i64,
end_accent_list: *mut i64,
start_accent_phrase_list: *mut i64,
end_accent_phrase_list: *mut i64,
speaker_id: *mut i64,
output: *mut f32,
) -> bool {
let result = lock_internal().predict_intonation(
length as usize,
unsafe { std::slice::from_raw_parts(vowel_phoneme_list, length as usize) },
unsafe { std::slice::from_raw_parts(consonant_phoneme_list, length as usize) },
unsafe { std::slice::from_raw_parts(start_accent_list, length as usize) },
unsafe { std::slice::from_raw_parts(end_accent_list, length as usize) },
unsafe { std::slice::from_raw_parts(start_accent_phrase_list, length as usize) },
unsafe { std::slice::from_raw_parts(end_accent_phrase_list, length as usize) },
unsafe { *speaker_id as u32 },
);
match result {
Ok(output_vec) => {
let output_slice = unsafe { std::slice::from_raw_parts_mut(output, length as usize) };
output_slice.clone_from_slice(&output_vec);
true
}
Err(err) => {
set_message(&format!("{}", err));
false
}
}
}

#[no_mangle]
pub extern "C" fn decode_forward(
length: i64,
phoneme_size: i64,
f0: *mut f32,
phoneme: *mut f32,
speaker_id: *mut i64,
output: *mut f32,
) -> bool {
let length = length as usize;
let phoneme_size = phoneme_size as usize;
let result = lock_internal().decode(
length,
phoneme_size,
unsafe { std::slice::from_raw_parts(f0, length) },
unsafe { std::slice::from_raw_parts(phoneme, phoneme_size * length) },
unsafe { *speaker_id as u32 },
);
match result {
Ok(output_vec) => {
let output_slice =
unsafe { std::slice::from_raw_parts_mut(output, (length as usize) * 256) };
output_slice.clone_from_slice(&output_vec);
true
}
Err(err) => {
set_message(&format!("{}", err));
false
}
}
}
4 changes: 3 additions & 1 deletion crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// cbindgen:ignore
mod compatible_engine;
mod helpers;
use helpers::*;
use libc::c_void;
Expand All @@ -18,7 +20,7 @@ type Internal = VoicevoxCore;

static INTERNAL: Lazy<Mutex<Internal>> = Lazy::new(Internal::new_with_mutex);

fn lock_internal() -> MutexGuard<'static, Internal> {
pub(crate) fn lock_internal() -> MutexGuard<'static, Internal> {
INTERNAL.lock().unwrap()
}

Expand Down

0 comments on commit 824373e

Please sign in to comment.