Spaces:
Running on Zero
Running on Zero
File size: 9,122 Bytes
122fc80 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | """Regenerate the committed VOICEVOX synthesis fixtures.
uv run --extra voice python tests/fixtures/make_synth_fixtures.py
This is a **maintainer action**, not part of CI. The outputs are the ground truth for every
AVTR-02 assertion in plan 01-06, so every duration here is read from the WAV header and never
summed from the query - a summed query is the very thing the no-drift test exists to check.
Three cases, chosen deliberately:
===== ======================= ===== ==============================================================
Case Text Speed Why this text
===== ======================= ===== ==============================================================
short γγγ«γ‘γ― 1.0 Contains ``N`` (γ) between vowels - exercises the closed-mouth
path. Doubles as the push-to-talk fixture for VOIC-02.
long (see LONG_TEXT) 1.0 γ§γ devoices to ``d e s U`` and γγ to ``sh I t a``, so the
fixture exercises the uppercase devoiced vowels that a
lowercase-only lookup silently drops. ~36 moras, long enough
that float-accumulation drift would be plainly visible.
slow same as long 0.75 VOIC-03's mechanism; the timeline must be exactly 1/0.75x
longer, pre/post silence included.
===== ======================= ===== ==============================================================
The generator refuses to write a ``long`` fixture that does not actually contain a devoiced vowel,
a pause mora and 20+ moras. A fixture that does not exercise those branches is the wrong fixture,
and finding that out here is much cheaper than finding it out in plan 01-06.
"""
from __future__ import annotations
import datetime as dt
import json
import sys
import wave
from pathlib import Path
FIXTURES = Path(__file__).resolve().parent
REPO_ROOT = FIXTURES.parents[1]
sys.path.insert(0, str(REPO_ROOT / "src"))
SHORT_TEXT = "γγγ«γ‘γ―"
LONG_TEXT = "δ»ζ₯γ―γγ倩ζ°γ§γγγγε
¬εγζ£ζ©γγ¦γγγθ²·γη©γ«θ‘γγΎγγγ"
VOICEVOX_CORE_VERSION = "0.17.0"
VOICEVOX_VVM_VERSION = "0.17.0"
DEVOICED_VOWELS = frozenset("AIUEO")
MIN_LONG_MORAS = 20
#: 24000 Hz / 256 samples. Every phoneme is quantised to a whole number of these.
FRAMERATE = 93.75
CASES = [
("short", SHORT_TEXT, 1.0, "speech_ja.wav"),
("long", LONG_TEXT, 1.0, "speech_ja_long.wav"),
("slow", LONG_TEXT, 0.75, "speech_ja_slow.wav"),
]
def flatten_moras(query: dict) -> list[dict]:
"""Every mora in utterance order, each accent phrase's pause mora following its moras."""
moras: list[dict] = []
for phrase in query["accent_phrases"]:
moras.extend(phrase["moras"])
if phrase["pause_mora"]:
moras.append(phrase["pause_mora"])
return moras
def phoneme_lengths(query: dict) -> list[float]:
"""Unscaled phoneme durations in seconds, wrapped in the pre/post silence, in order."""
silence = {"vowel": "pau", "consonant": None, "consonant_length": None}
moras = [
{**silence, "vowel_length": query["prePhonemeLength"]},
*flatten_moras(query),
{**silence, "vowel_length": query["postPhonemeLength"]},
]
lengths: list[float] = []
for mora in moras:
if mora["consonant"] is not None:
lengths.append(mora["consonant_length"])
lengths.append(mora["vowel_length"])
return lengths
def write_wav(path: Path, wav_bytes: bytes) -> tuple[float, int]:
"""Write the WAV; return its true duration and frame count from the header just written.
``frame_count`` is in VOICEVOX frames (256 samples at 24000 Hz = 93.75 fps), not PCM samples,
because that is the unit plan 01-06's +/-1 frame no-drift tolerance is expressed in.
"""
path.write_bytes(wav_bytes)
with wave.open(str(path), "rb") as handle:
assert handle.getframerate() == 24000, handle.getframerate()
assert handle.getnchannels() == 1, handle.getnchannels()
assert handle.getsampwidth() == 2, handle.getsampwidth()
samples = handle.getnframes()
assert samples % 256 == 0, samples
return samples / float(handle.getframerate()), samples // 256
def main() -> int:
try:
import voicevox_core # noqa: F401
except ImportError:
print(
"voicevox_core is not installed, so the fixtures cannot be regenerated.\n"
"Install it with `uv sync --extra dev --extra voice`; see docs/VOICEVOX-SETUP.md.\n"
"Regeneration is a maintainer action - CI does not need it.",
file=sys.stderr,
)
return 1
from japanese_avatar.voice.tts import SPEAKER_STYLE_ID, synthesize
meta: dict = {
"generated": dt.datetime.now(dt.UTC).date().isoformat(),
"voicevox_core": VOICEVOX_CORE_VERSION,
"voicevox_vvm": VOICEVOX_VVM_VERSION,
"style_id": SPEAKER_STYLE_ID,
"cases": {},
}
for case, text, speed, wav_name in CASES:
result = synthesize(text, speed=speed)
query = result.audio_query
query_name = f"audio_query_{case}.json"
(FIXTURES / query_name).write_text(
json.dumps(query, indent=2, ensure_ascii=False, sort_keys=True) + "\n",
encoding="utf-8",
newline="\n",
)
duration, frame_count = write_wav(FIXTURES / wav_name, result.wav_bytes)
# The WAV we just wrote must agree with what synthesize() reported, bit for bit.
assert duration == result.duration, (case, duration, result.duration)
# Ground truth for plan 01-06. See the "Frame quantisation" section of
# docs/VOICEVOX-SETUP.md: quantise at speed 1.0 FIRST, then divide the frame count by
# speedScale and round again. Dividing the length by speedScale before quantising is a
# different, wrong answer that only coincides at speed 1.0.
predicted = sum(
round(round(length * FRAMERATE) / speed) for length in phoneme_lengths(query)
)
assert predicted == frame_count, (case, predicted, frame_count)
moras = flatten_moras(query)
vowels = [m["vowel"] for m in moras]
pause_moras = sum(1 for p in query["accent_phrases"] if p["pause_mora"])
print(
f"{case:>5}: moras={len(moras):<3} pause_moras={pause_moras} frames={frame_count:<5} "
f"duration={duration!r} vowels={sorted(set(vowels))}"
)
if case == "long":
if len(moras) < MIN_LONG_MORAS:
print(
f"FAIL: the long fixture has {len(moras)} moras, fewer than "
f"{MIN_LONG_MORAS}. Pick a longer sentence and record which one.",
file=sys.stderr,
)
return 1
devoiced = sorted(set(vowels) & DEVOICED_VOWELS)
if not devoiced:
print(
"FAIL: the long fixture contains no devoiced vowel (A/I/U/E/O). Its entire "
"job is to exercise that branch - pick a different sentence.",
file=sys.stderr,
)
return 1
if not pause_moras:
print(
"FAIL: the long fixture produced no pause_mora. Pick a sentence with a "
"comma so the pause-ordering branch is covered.",
file=sys.stderr,
)
return 1
print(f" devoiced vowels present: {devoiced}")
meta["cases"][case] = {
"text": text,
"speed_scale": speed,
"query": query_name,
"wav": wav_name,
"duration_seconds": duration,
"frame_count": frame_count,
"mora_count": len(moras),
"pause_mora_count": pause_moras,
"vowel_symbols": vowels,
}
# Not exactly 1/0.75: VOICEVOX re-quantises every phoneme after scaling, so the realised
# ratio lands within a fraction of a percent of it rather than on it. 2% is the tolerance
# the fixture contract is written against.
ratio = meta["cases"]["slow"]["duration_seconds"] / meta["cases"]["long"]["duration_seconds"]
print(f"slow/long duration ratio = {ratio:.6f} (expected ~{1 / 0.75:.6f})")
if abs(ratio - 1 / 0.75) >= 0.02:
print(
f"FAIL: speedScale=0.75 did not lengthen the audio by ~1/0.75x: {ratio}",
file=sys.stderr,
)
return 1
(FIXTURES / "synth_meta.json").write_text(
json.dumps(meta, indent=2, ensure_ascii=False, sort_keys=True) + "\n",
encoding="utf-8",
# Explicit LF: the default translates to CRLF on Windows, which would make the committed
# fixtures differ byte-for-byte depending on which OS last regenerated them.
newline="\n",
)
print(f"wrote {FIXTURES / 'synth_meta.json'}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
|