Skip to content

Commit

Permalink
"new_with_initialize" → "new" (VOICEVOX#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip authored Oct 31, 2023
1 parent c1bd227 commit a6fb4ae
Show file tree
Hide file tree
Showing 26 changed files with 122 additions and 155 deletions.
9 changes: 4 additions & 5 deletions crates/voicevox_core/src/engine/open_jtalk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct Resources {
unsafe impl Send for Resources {}

impl OpenJtalk {
// FIXME: この関数は廃止し、`Synthesizer`は`Option<OpenJtalk>`という形でこの構造体を持つ
pub fn new_without_dic() -> Self {
Self {
resources: Mutex::new(Resources {
Expand All @@ -45,9 +46,7 @@ impl OpenJtalk {
dict_dir: None,
}
}
pub fn new_with_initialize(
open_jtalk_dict_dir: impl AsRef<Path>,
) -> crate::result::Result<Self> {
pub fn new(open_jtalk_dict_dir: impl AsRef<Path>) -> crate::result::Result<Self> {
let mut s = Self::new_without_dic();
s.load(open_jtalk_dict_dir).map_err(|()| {
// FIXME: 「システム辞書を読もうとしたけど読めなかった」というエラーをちゃんと用意する
Expand Down Expand Up @@ -274,7 +273,7 @@ mod tests {
#[case] text: &str,
#[case] expected: std::result::Result<Vec<String>, OpenjtalkFunctionError>,
) {
let open_jtalk = OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap();
let open_jtalk = OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap();
let result = open_jtalk.extract_fullcontext(text);
assert_debug_fmt_eq!(expected, result);
}
Expand All @@ -285,7 +284,7 @@ mod tests {
#[case] text: &str,
#[case] expected: std::result::Result<Vec<String>, OpenjtalkFunctionError>,
) {
let open_jtalk = OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap();
let open_jtalk = OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap();
for _ in 0..10 {
let result = open_jtalk.extract_fullcontext(text);
assert_debug_fmt_eq!(expected, result);
Expand Down
20 changes: 6 additions & 14 deletions crates/voicevox_core/src/engine/synthesis_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,31 +652,23 @@ mod tests {
#[rstest]
#[tokio::test]
async fn is_openjtalk_dict_loaded_works() {
let core = InferenceCore::new_with_initialize(false, 0).await.unwrap();
let synthesis_engine = SynthesisEngine::new(
core,
OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR)
.unwrap()
.into(),
);
let core = InferenceCore::new(false, 0).await.unwrap();
let synthesis_engine =
SynthesisEngine::new(core, OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap().into());

assert_eq!(synthesis_engine.is_openjtalk_dict_loaded(), true);
}

#[rstest]
#[tokio::test]
async fn create_accent_phrases_works() {
let core = InferenceCore::new_with_initialize(false, 0).await.unwrap();
let core = InferenceCore::new(false, 0).await.unwrap();

let model = &VoiceModel::sample().await.unwrap();
core.load_model(model).await.unwrap();

let synthesis_engine = SynthesisEngine::new(
core,
OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR)
.unwrap()
.into(),
);
let synthesis_engine =
SynthesisEngine::new(core, OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap().into());

let accent_phrases = synthesis_engine
.create_accent_phrases("同じ、文章、です。完全に、同一です。", StyleId::new(1))
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 @@ -9,7 +9,7 @@ pub struct InferenceCore {
}

impl InferenceCore {
pub(crate) async fn new_with_initialize(use_gpu: bool, cpu_num_threads: u16) -> Result<Self> {
pub(crate) async fn new(use_gpu: bool, cpu_num_threads: u16) -> Result<Self> {
if !use_gpu || Self::can_support_gpu_feature()? {
let status = Status::new(use_gpu, cpu_num_threads);
Ok(Self { status })
Expand Down
63 changes: 30 additions & 33 deletions crates/voicevox_core/src/synthesizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ pub enum AccelerationMode {
Gpu,
}

/// [`Synthesizer::new_with_initialize`]のオプション。
/// [`Synthesizer::new`]のオプション。
///
/// [`Synthesizer::new_with_initialize`]: Synthesizer::new_with_initialize
/// [`Synthesizer::new`]: Synthesizer::new
#[derive(Default)]
pub struct InitializeOptions {
pub acceleration_mode: AccelerationMode,
Expand Down Expand Up @@ -90,8 +90,8 @@ impl Synthesizer {
///
/// use voicevox_core::{AccelerationMode, InitializeOptions, OpenJtalk, Synthesizer};
///
/// let mut syntesizer = Synthesizer::new_with_initialize(
/// Arc::new(OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap()),
/// let mut syntesizer = Synthesizer::new(
/// Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()),
/// &InitializeOptions {
/// acceleration_mode: ACCELERATION_MODE,
/// ..Default::default()
Expand All @@ -102,10 +102,7 @@ impl Synthesizer {
/// # Ok(())
/// # }
/// ```
pub async fn new_with_initialize(
open_jtalk: Arc<OpenJtalk>,
options: &InitializeOptions,
) -> Result<Self> {
pub async fn new(open_jtalk: Arc<OpenJtalk>, options: &InitializeOptions) -> Result<Self> {
#[cfg(windows)]
list_windows_video_cards();
let use_gpu = match options.acceleration_mode {
Expand All @@ -127,7 +124,7 @@ impl Synthesizer {

Ok(Self {
synthesis_engine: SynthesisEngine::new(
InferenceCore::new_with_initialize(use_gpu, options.cpu_num_threads).await?,
InferenceCore::new(use_gpu, options.cpu_num_threads).await?,
open_jtalk,
),
use_gpu,
Expand Down Expand Up @@ -256,8 +253,8 @@ impl Synthesizer {
/// # AccelerationMode, InitializeOptions, OpenJtalk, Synthesizer, VoiceModel,
/// # };
/// #
/// # let mut syntesizer = Synthesizer::new_with_initialize(
/// # Arc::new(OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap()),
/// # let mut syntesizer = Synthesizer::new(
/// # Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()),
/// # &InitializeOptions {
/// # acceleration_mode: AccelerationMode::Cpu,
/// # ..Default::default()
Expand Down Expand Up @@ -310,8 +307,8 @@ impl Synthesizer {
/// # AccelerationMode, InitializeOptions, OpenJtalk, Synthesizer, VoiceModel,
/// # };
/// #
/// # let mut syntesizer = Synthesizer::new_with_initialize(
/// # Arc::new(OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap()),
/// # let mut syntesizer = Synthesizer::new(
/// # Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()),
/// # &InitializeOptions {
/// # acceleration_mode: AccelerationMode::Cpu,
/// # ..Default::default()
Expand Down Expand Up @@ -400,8 +397,8 @@ impl Synthesizer {
/// # AccelerationMode, InitializeOptions, OpenJtalk, Synthesizer, VoiceModel,
/// # };
/// #
/// # let mut syntesizer = Synthesizer::new_with_initialize(
/// # Arc::new(OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap()),
/// # let mut syntesizer = Synthesizer::new(
/// # Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()),
/// # &InitializeOptions {
/// # acceleration_mode: AccelerationMode::Cpu,
/// # ..Default::default()
Expand Down Expand Up @@ -455,8 +452,8 @@ impl Synthesizer {
/// # AccelerationMode, InitializeOptions, OpenJtalk, Synthesizer, VoiceModel,
/// # };
/// #
/// # let mut syntesizer = Synthesizer::new_with_initialize(
/// # Arc::new(OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap()),
/// # let mut syntesizer = Synthesizer::new(
/// # Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()),
/// # &InitializeOptions {
/// # acceleration_mode: AccelerationMode::Cpu,
/// # ..Default::default()
Expand Down Expand Up @@ -582,7 +579,7 @@ mod tests {
#[case(Ok(()))]
#[tokio::test]
async fn load_model_works(#[case] expected_result_at_initialized: Result<()>) {
let syntesizer = Synthesizer::new_with_initialize(
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new_without_dic()),
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
Expand All @@ -606,7 +603,7 @@ mod tests {
#[rstest]
#[tokio::test]
async fn is_use_gpu_works() {
let syntesizer = Synthesizer::new_with_initialize(
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new_without_dic()),
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
Expand All @@ -623,7 +620,7 @@ mod tests {
#[tokio::test]
async fn is_loaded_model_by_style_id_works(#[case] style_id: u32, #[case] expected: bool) {
let style_id = StyleId::new(style_id);
let syntesizer = Synthesizer::new_with_initialize(
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new_without_dic()),
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
Expand Down Expand Up @@ -652,7 +649,7 @@ mod tests {
#[rstest]
#[tokio::test]
async fn predict_duration_works() {
let syntesizer = Synthesizer::new_with_initialize(
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new_without_dic()),
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
Expand Down Expand Up @@ -684,7 +681,7 @@ mod tests {
#[rstest]
#[tokio::test]
async fn predict_intonation_works() {
let syntesizer = Synthesizer::new_with_initialize(
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new_without_dic()),
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
Expand Down Expand Up @@ -726,7 +723,7 @@ mod tests {
#[rstest]
#[tokio::test]
async fn decode_works() {
let syntesizer = Synthesizer::new_with_initialize(
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new_without_dic()),
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
Expand Down Expand Up @@ -819,8 +816,8 @@ mod tests {
#[case] expected_text_consonant_vowel_data: &TextConsonantVowelData,
#[case] expected_kana_text: &str,
) {
let syntesizer = Synthesizer::new_with_initialize(
Arc::new(OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap()),
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()),
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
..Default::default()
Expand Down Expand Up @@ -888,8 +885,8 @@ mod tests {
#[case] input: Input,
#[case] expected_text_consonant_vowel_data: &TextConsonantVowelData,
) {
let syntesizer = Synthesizer::new_with_initialize(
Arc::new(OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap()),
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()),
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
..Default::default()
Expand Down Expand Up @@ -954,8 +951,8 @@ mod tests {
#[rstest]
#[tokio::test]
async fn mora_length_works() {
let syntesizer = Synthesizer::new_with_initialize(
Arc::new(OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap()),
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()),
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
..Default::default()
Expand Down Expand Up @@ -991,8 +988,8 @@ mod tests {
#[rstest]
#[tokio::test]
async fn mora_pitch_works() {
let syntesizer = Synthesizer::new_with_initialize(
Arc::new(OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap()),
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()),
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
..Default::default()
Expand Down Expand Up @@ -1024,8 +1021,8 @@ mod tests {
#[rstest]
#[tokio::test]
async fn mora_data_works() {
let syntesizer = Synthesizer::new_with_initialize(
Arc::new(OpenJtalk::new_with_initialize(OPEN_JTALK_DIC_DIR).unwrap()),
let syntesizer = Synthesizer::new(
Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()),
&InitializeOptions {
acceleration_mode: AccelerationMode::Cpu,
..Default::default()
Expand Down
Loading

0 comments on commit a6fb4ae

Please sign in to comment.