Skip to content

Commit

Permalink
test: test_tts_engine.pyの浮動小数点の誤差を許容する (#1529)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroshiba Kazuyuki <kazuyuki_hiroshiba@dwango.co.jp>
  • Loading branch information
sabonerune and Hiroshiba authored Feb 18, 2025
1 parent 771eefa commit c9d342e
Showing 1 changed file with 55 additions and 45 deletions.
100 changes: 55 additions & 45 deletions test/unit/tts_pipeline/test_tts_engine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""TTSEngine のテスト"""

from test.utility import pydantic_to_native_type, round_floats, summarize_big_ndarray
from typing import Any
from unittest.mock import MagicMock

import numpy as np
Expand Down Expand Up @@ -347,26 +348,26 @@ def koreha_arimasuka_base_expected() -> list[AccentPhrase]:
Mora(
text="コ",
consonant="k",
consonant_length=np.float32(2.44),
consonant_length=2.44,
vowel="o",
vowel_length=np.float32(2.88),
pitch=np.float32(4.38),
vowel_length=2.88,
pitch=4.38,
),
Mora(
text="レ",
consonant="r",
consonant_length=np.float32(3.06),
consonant_length=3.06,
vowel="e",
vowel_length=np.float32(1.88),
pitch=np.float32(4.0),
vowel_length=1.88,
pitch=4.0,
),
Mora(
text="ワ",
consonant="w",
consonant_length=np.float32(3.62),
consonant_length=3.62,
vowel="a",
vowel_length=np.float32(1.44),
pitch=np.float32(4.19),
vowel_length=1.44,
pitch=4.19,
),
],
accent=3,
Expand All @@ -380,40 +381,40 @@ def koreha_arimasuka_base_expected() -> list[AccentPhrase]:
consonant=None,
consonant_length=None,
vowel="a",
vowel_length=np.float32(1.44),
pitch=np.float32(1.44),
vowel_length=1.44,
pitch=1.44,
),
Mora(
text="リ",
consonant="r",
consonant_length=np.float32(3.06),
consonant_length=3.06,
vowel="i",
vowel_length=np.float32(2.31),
pitch=np.float32(4.44),
vowel_length=2.31,
pitch=4.44,
),
Mora(
text="マ",
consonant="m",
consonant_length=np.float32(2.62),
consonant_length=2.62,
vowel="a",
vowel_length=np.float32(1.44),
pitch=np.float32(3.12),
vowel_length=1.44,
pitch=3.12,
),
Mora(
text="ス",
consonant="s",
consonant_length=np.float32(3.19),
consonant_length=3.19,
vowel="U",
vowel_length=np.float32(1.38),
pitch=np.float32(0.0),
vowel_length=1.38,
pitch=0.0,
),
Mora(
text="カ",
consonant="k",
consonant_length=np.float32(2.44),
consonant_length=2.44,
vowel="a",
vowel_length=np.float32(1.44),
pitch=np.float32(2.94),
vowel_length=1.44,
pitch=2.94,
),
],
accent=3,
Expand All @@ -428,6 +429,15 @@ def create_synthesis_test_base(text: str) -> list[AccentPhrase]:
return tts_engine.create_accent_phrases(text, StyleId(1))


def _assert_equeal_accent_phrases(
expected: list[AccentPhrase], outputs: list[AccentPhrase]
) -> None:
def _to_native_and_round(x: list[AccentPhrase]) -> Any:
return round_floats(pydantic_to_native_type(x), round_value=2)

assert _to_native_and_round(expected) == _to_native_and_round(outputs)


def test_create_accent_phrases() -> None:
"""accent_phrasesの作成時では疑問文モーラ処理を行わない
(https://github.com/VOICEVOX/voicevox_engine/issues/272#issuecomment-1022610866)
Expand All @@ -437,7 +447,7 @@ def test_create_accent_phrases() -> None:
expected = koreha_arimasuka_base_expected()
expected[-1].is_interrogative = True
actual = tts_engine.create_accent_phrases(text, StyleId(1))
assert expected == actual, f"case(text:{text})"
_assert_equeal_accent_phrases(expected, actual)


def test_upspeak_voiced_last_mora() -> None:
Expand All @@ -454,13 +464,13 @@ def test_upspeak_voiced_last_mora() -> None:
consonant_length=None,
vowel="a",
vowel_length=0.15,
pitch=np.float32(expected[-1].moras[-1].pitch) + 0.3,
pitch=expected[-1].moras[-1].pitch + 0.3,
)
]
# Outputs
outputs = _apply_interrogative_upspeak(inputs, True)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)

# voiced + "?" + flagOFF -> non-upspeak
# Inputs
Expand All @@ -471,7 +481,7 @@ def test_upspeak_voiced_last_mora() -> None:
# Outputs
outputs = _apply_interrogative_upspeak(inputs, False)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)

# voiced + "" + flagON -> non-upspeak
# Inputs
Expand All @@ -481,7 +491,7 @@ def test_upspeak_voiced_last_mora() -> None:
# Outputs
outputs = _apply_interrogative_upspeak(inputs, True)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)


def test_upspeak_voiced_N_last_mora() -> None:
Expand All @@ -494,8 +504,8 @@ def nn_base_expected() -> list[AccentPhrase]:
consonant=None,
consonant_length=None,
vowel="N",
vowel_length=np.float32(1.25),
pitch=np.float32(1.44),
vowel_length=1.25,
pitch=1.44,
)
],
accent=1,
Expand All @@ -512,7 +522,7 @@ def nn_base_expected() -> list[AccentPhrase]:
# Outputs
outputs = _apply_interrogative_upspeak(inputs, True)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)

# voiced + "?" + flagON -> upspeak
# Inputs
Expand All @@ -527,13 +537,13 @@ def nn_base_expected() -> list[AccentPhrase]:
consonant_length=None,
vowel="N",
vowel_length=0.15,
pitch=np.float32(expected[-1].moras[-1].pitch) + 0.3,
pitch=expected[-1].moras[-1].pitch + 0.3,
)
]
# Outputs
outputs = _apply_interrogative_upspeak(inputs, True)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)

# voiced + "?" + flagOFF -> non-upspeak
# Inputs
Expand All @@ -544,7 +554,7 @@ def nn_base_expected() -> list[AccentPhrase]:
# Outputs
outputs = _apply_interrogative_upspeak(inputs, False)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)


def test_upspeak_unvoiced_last_mora() -> None:
Expand All @@ -557,8 +567,8 @@ def ltu_base_expected() -> list[AccentPhrase]:
consonant=None,
consonant_length=None,
vowel="cl",
vowel_length=np.float32(1.69),
pitch=np.float32(0.0),
vowel_length=1.69,
pitch=0.0,
)
],
accent=1,
Expand All @@ -575,7 +585,7 @@ def ltu_base_expected() -> list[AccentPhrase]:
# Outputs
outputs = _apply_interrogative_upspeak(inputs, True)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)

# unvoiced + "?" + flagON -> non-upspeak
# Inputs
Expand All @@ -586,7 +596,7 @@ def ltu_base_expected() -> list[AccentPhrase]:
# Outputs
outputs = _apply_interrogative_upspeak(inputs, True)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)

# unvoiced + "?" + flagOFF -> non-upspeak
# Inputs
Expand All @@ -597,7 +607,7 @@ def ltu_base_expected() -> list[AccentPhrase]:
# Outputs
outputs = _apply_interrogative_upspeak(inputs, False)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)


def test_upspeak_voiced_u_last_mora() -> None:
Expand All @@ -608,10 +618,10 @@ def su_base_expected() -> list[AccentPhrase]:
Mora(
text="ス",
consonant="s",
consonant_length=np.float32(3.19),
consonant_length=3.19,
vowel="u",
vowel_length=np.float32(3.5),
pitch=np.float32(5.94),
vowel_length=3.5,
pitch=5.94,
)
],
accent=1,
Expand All @@ -628,7 +638,7 @@ def su_base_expected() -> list[AccentPhrase]:
# Outputs
outputs = _apply_interrogative_upspeak(inputs, True)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)

# voiced + "?" + flagON -> upspeak
# Inputs
Expand All @@ -649,7 +659,7 @@ def su_base_expected() -> list[AccentPhrase]:
# Outputs
outputs = _apply_interrogative_upspeak(inputs, True)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)

# voiced + "?" + flagOFF -> non-upspeak
# Inputs
Expand All @@ -660,4 +670,4 @@ def su_base_expected() -> list[AccentPhrase]:
# Outputs
outputs = _apply_interrogative_upspeak(inputs, False)
# Test
assert expected == outputs
_assert_equeal_accent_phrases(expected, outputs)

0 comments on commit c9d342e

Please sign in to comment.