From b733ea1b5b8c5e9942571b7ebe936db347548786 Mon Sep 17 00:00:00 2001 From: Gray Suitcase <41382894+PickledChair@users.noreply.github.com> Date: Thu, 2 Feb 2023 08:05:39 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20`audio=5Fquery`=20=E9=96=A2=E6=95=B0?= =?UTF-8?q?=E3=81=A7=20`kana`=20=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=82=92=20`true`=20=E3=81=AB=E8=A8=AD=E5=AE=9A?= =?UTF-8?q?=E3=81=97=E3=81=9F=E3=81=A8=E3=81=8D=E3=81=AB=E9=81=A9=E5=88=87?= =?UTF-8?q?=E3=81=AA=20`accent=5Fphrases`=20=E3=82=92=E5=BE=97=E3=82=89?= =?UTF-8?q?=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B?= =?UTF-8?q?=20(#407)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close https://github.com/VOICEVOX/voicevox_core/issues/405 --- crates/voicevox_core/src/publish.rs | 124 +++++++++++++++++++++------- 1 file changed, 92 insertions(+), 32 deletions(-) diff --git a/crates/voicevox_core/src/publish.rs b/crates/voicevox_core/src/publish.rs index 2cdebadb8..36d43169c 100644 --- a/crates/voicevox_core/src/publish.rs +++ b/crates/voicevox_core/src/publish.rs @@ -167,7 +167,8 @@ impl VoicevoxCore { return Err(Error::NotLoadedOpenjtalkDict); } let accent_phrases = if options.kana { - parse_kana(text)? + self.synthesis_engine + .replace_mora_data(&parse_kana(text)?, speaker_id)? } else { self.synthesis_engine .create_accent_phrases(text, speaker_id)? @@ -895,9 +896,58 @@ mod tests { assert_eq!(result.unwrap().len(), F0_LENGTH * 256); } + type TextConsonantVowelData = + [(&'static [(&'static str, &'static str, &'static str)], usize)]; + + // [([(テキスト, 母音, 子音), ...], アクセントの位置), ...] の形式 + const TEXT_CONSONANT_VOWEL_DATA1: &TextConsonantVowelData = &[ + (&[("コ", "k", "o"), ("レ", "r", "e"), ("ワ", "w", "a")], 3), + ( + &[ + ("テ", "t", "e"), + ("ス", "s", "U"), + ("ト", "t", "o"), + ("デ", "d", "e"), + ("ス", "s", "U"), + ], + 1, + ), + ]; + + const TEXT_CONSONANT_VOWEL_DATA2: &TextConsonantVowelData = &[ + (&[("コ", "k", "o"), ("レ", "r", "e"), ("ワ", "w", "a")], 1), + ( + &[ + ("テ", "t", "e"), + ("ス", "s", "U"), + ("ト", "t", "o"), + ("デ", "d", "e"), + ("ス", "s", "U"), + ], + 3, + ), + ]; + #[rstest] + #[case( + "これはテストです", + false, + TEXT_CONSONANT_VOWEL_DATA1, + "コレワ'/テ'_ストデ_ス" + )] + #[case( + "コ'レワ/テ_スト'デ_ス", + true, + TEXT_CONSONANT_VOWEL_DATA2, + "コ'レワ/テ_スト'デ_ス" + )] #[async_std::test] - async fn audio_query_works() { + async fn audio_query_works( + #[case] input_text: &str, + #[case] input_kana_option: bool, + #[case] expected_text_consonant_vowel_data: &TextConsonantVowelData, + #[case] expected_kana_text: &str, + ) { let open_jtalk_dic_dir = download_open_jtalk_dict_if_no_exists().await; let core = VoicevoxCore::new_with_mutex(); @@ -914,41 +964,51 @@ mod tests { let query = core .lock() .unwrap() - .audio_query("これはテストです", 0, Default::default()) + .audio_query( + input_text, + 0, + AudioQueryOptions { + kana: input_kana_option, + }, + ) .unwrap(); - assert_eq!(query.accent_phrases().len(), 2); + assert_eq!( + query.accent_phrases().len(), + expected_text_consonant_vowel_data.len() + ); - assert_eq!(query.accent_phrases()[0].moras().len(), 3); - for (i, (text, consonant, vowel)) in [("コ", "k", "o"), ("レ", "r", "e"), ("ワ", "w", "a")] - .iter() - .enumerate() - { - let mora = query.accent_phrases()[0].moras().get(i).unwrap(); - assert_eq!(mora.text(), text); - assert_eq!(mora.consonant(), &Some(consonant.to_string())); - assert_eq!(mora.vowel(), vowel); - } - assert_eq!(query.accent_phrases()[0].accent(), &3); - - assert_eq!(query.accent_phrases()[1].moras().len(), 5); - for (i, (text, consonant, vowel)) in [ - ("テ", "t", "e"), - ("ス", "s", "U"), - ("ト", "t", "o"), - ("デ", "d", "e"), - ("ス", "s", "U"), - ] - .iter() - .enumerate() + for (accent_phrase, (text_consonant_vowel_slice, accent_pos)) in + std::iter::zip(query.accent_phrases(), expected_text_consonant_vowel_data) { - let mora = query.accent_phrases()[1].moras().get(i).unwrap(); - assert_eq!(mora.text(), text); - assert_eq!(mora.consonant(), &Some(consonant.to_string())); - assert_eq!(mora.vowel(), vowel); + assert_eq!( + accent_phrase.moras().len(), + text_consonant_vowel_slice.len() + ); + assert_eq!(accent_phrase.accent(), accent_pos); + + for (mora, (text, consonant, vowel)) in + std::iter::zip(accent_phrase.moras(), *text_consonant_vowel_slice) + { + assert_eq!(mora.text(), text); + // NOTE: 子音の長さが必ず非ゼロになるテストケースを想定している + assert_ne!( + mora.consonant_length(), + &Some(0.), + "expected mora.consonant_length is not Some(0.0), but got Some(0.0)." + ); + assert_eq!(mora.consonant(), &Some(consonant.to_string())); + assert_eq!(mora.vowel(), vowel); + // NOTE: 母音の長さが必ず非ゼロになるテストケースを想定している + assert_ne!( + mora.vowel_length(), + &0., + "expected mora.vowel_length is not 0.0, but got 0.0." + ); + } } - assert_eq!(query.accent_phrases()[1].accent(), &1); - assert_eq!(query.kana(), "コレワ'/テ'_ストデ_ス"); + + assert_eq!(query.kana(), expected_kana_text); } #[rstest]