Skip to content

Commit

Permalink
fix: audio_query 関数で kana オプションを true に設定したときに適切な `accent_phras…
Browse files Browse the repository at this point in the history
…es` を得られるようにする (VOICEVOX#407)

close VOICEVOX#405
  • Loading branch information
PickledChair authored and Hiroshiba committed Feb 4, 2023
1 parent eebf0c9 commit b733ea1
Showing 1 changed file with 92 additions and 32 deletions.
124 changes: 92 additions & 32 deletions crates/voicevox_core/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down Expand Up @@ -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();
Expand All @@ -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]
Expand Down

0 comments on commit b733ea1

Please sign in to comment.