Skip to content

Commit

Permalink
Add: C APIの定義を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed Jul 8, 2023
1 parent b21e3c9 commit 41c5196
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 14 deletions.
1 change: 1 addition & 0 deletions crates/voicevox_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub use devices::*;
pub use manifest::*;
pub use version::*;
pub use voice_synthesizer::*;
pub use user_dict::*;

use derive_getters::*;
use derive_new::new;
Expand Down
40 changes: 28 additions & 12 deletions crates/voicevox_core/src/user_dict/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,39 @@ pub struct UserDict {

impl UserDict {
pub fn new(store_path: &str) -> Result<Self> {
let store_file = File::open(store_path).map_err(|_| Error::InvalidDictFile)?;
let words: HashMap<String, UserDictWord> =
serde_json::from_reader(store_file).map_err(|_| Error::InvalidDictFile)?;

Ok(Self {
store_path: store_path.to_string(),
words,
})
if std::path::Path::new(store_path).exists() {
let store_file = File::open(store_path).map_err(|_| Error::InvalidDictFile)?;
let words: HashMap<String, UserDictWord> =
serde_json::from_reader(store_file).map_err(|_| Error::InvalidDictFile)?;
serde_json::to_writer(&mut store_file, &words).map_err(|_| Error::InvalidDictFile)?;
Ok(Self {
store_path: store_path.to_string(),
words,
})
} else {
Ok(Self {
store_path: store_path.to_string(),
words: HashMap::new(),
})
}
}

pub fn add_word(&mut self, word: UserDictWord) -> String {
pub fn add_word(&mut self, word: UserDictWord) -> Result<String> {
let word_uuid = Uuid::new_v4().to_string();
self.words.insert(word_uuid.clone(), word);
word_uuid
self.save()?;
Ok(word_uuid)
}

pub fn remove_word(&mut self, word_uuid: &str) -> Result<Option<UserDictWord>> {
let word = self.words.remove(word_uuid);
self.save()?;
Ok(word)
}

pub fn remove_word(&mut self, word_uuid: &str) -> Option<UserDictWord> {
self.words.remove(word_uuid)
fn save(&self) -> Result<()> {
let mut file = File::create(&self.store_path).map_err(|_| Error::InvalidDictFile)?;
serde_json::to_writer(&mut file, &self.words).map_err(|_| Error::InvalidDictFile)?;
Ok(())
}
}
3 changes: 3 additions & 0 deletions crates/voicevox_core/src/user_dict/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
mod dict;
mod word;

pub use dict::*;
pub use word::*;
103 changes: 101 additions & 2 deletions crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use tokio::runtime::Runtime;
use tracing_subscriber::fmt::format::Writer;
use tracing_subscriber::EnvFilter;
use voicevox_core::{
AccentPhraseModel, AudioQueryModel, AudioQueryOptions, OpenJtalk, TtsOptions, VoiceModel,
VoiceModelId,
AccentPhraseModel, AudioQueryModel, AudioQueryOptions, OpenJtalk, TtsOptions, UserDict,
VoiceModel, VoiceModelId,
};
use voicevox_core::{StyleId, SupportedDevices, SynthesisOptions, Synthesizer};

Expand Down Expand Up @@ -692,6 +692,105 @@ pub extern "C" fn voicevox_error_result_to_message(
C_STRING_DROP_CHECKER.blacklist(message).as_ptr()
}

/// ユーザー辞書
pub struct VoicevoxUserDict {
dict: Arc<voicevox_core::UserDict>,
}

pub struct VoicevoxUserDictWord {
surface: *const c_char,
pronunciation: *const c_char,
accent_type: i32,
word_type: VoicevoxUserDictWordType,
priority: i32,
}

#[repr(i32)]
#[allow(non_camel_case_types)]
pub enum VoicevoxUserDictWordType {
VOICEVOX_PROPER_NOUN = 0,
VOICEVOX_COMMON_NOUN = 1,
VOICEVOX_VERB = 2,
VOICEVOX_ADJECTIVE = 3,
VOICEVOX_SUFFIX = 4,
}

/// ユーザー辞書をロードまたは新規作成する
/// @param [in] dict_path ユーザー辞書のパス
/// @param [out] user_dict VoicevoxUserDictのポインタ
/// @return 結果コード #VoicevoxResultCode
#[no_mangle]
pub extern "C" fn voicevox_dict_new(
dict_path: *const c_char,
user_dict: NonNull<*mut UserDict>,
) -> VoicevoxResultCode {
todo!()
}

/// ユーザー辞書に単語を追加する
/// @param [in] user_dict VoicevoxUserDictのポインタ
/// @param [in] word 追加する単語
/// @param [out] word_uuid 追加した単語のUUID
/// @return 結果コード #VoicevoxResultCode
///
/// # Safety
/// @param user_dict は有効な :VoicevoxUserDict のポインタであること
/// @param word_uuid は呼び出し側で解放する必要がある
///
#[no_mangle]
pub extern "C" fn voicevox_dict_add_word(
user_dict: &VoicevoxUserDict,
word: &VoicevoxUserDictWord,
word_uuid: NonNull<*mut u8>,
) -> VoicevoxResultCode {
todo!()
}

/// ユーザー辞書の単語を更新する
/// @param [in] user_dict VoicevoxUserDictのポインタ
/// @param [in] word_uuid 更新する単語のUUID
/// @param [in] word 新しい単語のデータ
/// @param [out] altered 単語が更新されたかどうか
/// @return 結果コード #VoicevoxResultCode
///
/// # Safety
/// @param user_dict は有効な :VoicevoxUserDict のポインタであること
#[no_mangle]
pub extern "C" fn voicevox_dict_alter_word(
user_dict: &VoicevoxUserDict,
word_uuid: *const u8,
word: NonNull<*mut VoicevoxUserDictWord>,
altered: NonNull<*mut bool>,
) -> VoicevoxResultCode {
todo!()
}

/// ユーザー辞書から単語を削除する
/// @param [in] user_dict VoicevoxUserDictのポインタ
/// @param [in] word_uuid 削除する単語のUUID
/// @param [out] deleted 単語が削除されたかどうか
/// @return 結果コード #VoicevoxResultCode
#[no_mangle]
pub extern "C" fn voicevox_dict_delete_word(
user_dict: &VoicevoxUserDict,
word_uuid: *const u8,
deleted: NonNull<*mut bool>,
) -> VoicevoxResultCode {
todo!()
}

/// ユーザー辞書の単語をJSON形式で出力する
/// @param [in] user_dict VoicevoxUserDictのポインタ
/// @param [out] json JSON形式の文字列
/// @return 結果コード #VoicevoxResultCode
#[no_mangle]
pub extern "C" fn voicevox_dict_export_json(
user_dict: &VoicevoxUserDict,
json: NonNull<*mut c_char>,
) -> VoicevoxResultCode {
todo!()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 41c5196

Please sign in to comment.