diff --git a/src/voice_worker/melo_child.py b/src/voice_worker/melo_child.py index 3b9ecd5..cdcc2ac 100644 --- a/src/voice_worker/melo_child.py +++ b/src/voice_worker/melo_child.py @@ -24,12 +24,23 @@ import argparse import importlib import json import os +import re import struct import sys import types MODEL_SPEED_MAX = 1.25 +# MeloTTS's Korean front end (g2pkk) reads a number with native numerals only before these counters, +# and only when MeCab tags the counter. Any other number is spelled out only when MeCab tags what follows +# as a bound noun, which differs between MeCab builds (on Windows "60%" was read "육영퍼센트"); the rest +# goes digit by digit. Its sentence splitter also breaks at every dot and comma ("4.2" as "사. 이"). +# Same list as g2pkk's BOUND_NOUNS: numbers before them are left for its counter rule. +NATIVE_COUNTERS = tuple("군데 권 개 그루 닢 대 두 마리 모 모금 뭇 발 발짝 방 번 벌 보루 살 수 술 시 쌈 움큼 " + "정 짝 채 척 첩 축 켤레 톨 통".split()) +_DIGITS = "영일이삼사오육칠팔구" +_NUMBER = re.compile(r"(? tuple[float, float]: """(the model's speed, the stretch factor after it); their product is ``speed``.""" @@ -65,6 +76,39 @@ def stretch(audio, factor: float, rate: int, frame_ms: float = 30.0, search_ms: return y[:n_out] +def sino_korean(num: str) -> str: + """Sino-Korean reading of a whole number: 1234 is 천이백삼십사, 10000 is 만, 100000000 is 일억.""" + n = int(num) + if n == 0: + return "영" + out = "" + for power, big in reversed(list(enumerate(("", "만", "억", "조", "경")))): + group = n // 10 ** (4 * power) % 10000 + if not group: + continue + part = "" + for digit, unit in zip(f"{group:04d}", ("천", "백", "십", "")): + d = int(digit) + if d: + part += ("" if d == 1 and unit else _DIGITS[d]) + unit + out += ("" if part == "일" and big == "만" else part) + big + return out + + +def spell_numbers_kr(text: str) -> str: + """Spell out the numbers MeloTTS would misread: decimals ("4.2" is 사점이) and every whole number + not followed by a native counter (those are left for MeloTTS, which reads "3시" as 세시).""" + def spell(m: re.Match) -> str: + whole, frac, after = m.group(1), m.group(2), m.group(3) + if frac is None and after.startswith(NATIVE_COUNTERS): + return m.group(0) + spoken = sino_korean(whole.replace(",", "")) + if frac is not None: + spoken += "점" + "".join(_DIGITS[int(d)] for d in frac) + return spoken + after + return _NUMBER.sub(spell, text) + + def korean_only_patches() -> None: """MeloTTS imports its Japanese front end (module-level MeCab.Tagger()) even for Korean, and on Windows g2pkk asks for ``eunjeon`` instead of python-mecab-ko. Same fix as the M0 kit.""" @@ -132,7 +176,10 @@ def main() -> int: req = json.loads(line) rate = int(req["rate"]) model_speed, factor = split_speed(float(req.get("speed", args.speed))) - audio = model.tts_to_file(str(req["text"]), speaker, None, speed=model_speed, quiet=True) + text = str(req["text"]) + if args.language == "KR": + text = spell_numbers_kr(text) + audio = model.tts_to_file(text, speaker, None, speed=model_speed, quiet=True) audio = stretch(audio, factor, native) if rate != native: from math import gcd diff --git a/src/voice_worker/voice_worker.spec.md b/src/voice_worker/voice_worker.spec.md index f7399b9..1dcea73 100644 --- a/src/voice_worker/voice_worker.spec.md +++ b/src/voice_worker/voice_worker.spec.md @@ -192,6 +192,14 @@ is ready. Requests are served first come, first served by one TTS engine. `tts_c request's unsent sentences, and `tts_done` follows with `cancelled: true`. A sentence that fails to synthesise is reported with `error` (`code: "tts_failed"`) and the request ends with `failed: true`. +With `--melo-language KR` the MeloTTS child spells numbers out in Hangul before MeloTTS sees them, +because its Korean front end misreads them: a number it cannot tag as a counter is read digit by digit +(the tag depends on the MeCab build; on Windows "60%" was read "육영퍼센트"), and its own splitter +breaks at every dot and comma ("4.2미터" as "사. 이미터"). Decimals are read with 점 and one name per +digit after it (사점이), thousands commas are dropped, and every other whole number gets its +Sino-Korean reading (육십, 이천이십육), except before a counter MeloTTS reads with native numerals +(g2pkk's list: 시, 개, 마리, 살, ...), which stays for MeloTTS ("3시" is 세시). + Speed starts at `--melo-speed` and `tts_speed` changes it for every sentence made after it (a speed outside 0.5-2.0 or not a number is `bad_message` and the last good one stays). MeloTTS itself speaks up to 1.25: its own speed shortens every sound, and past that it drops them (five Korean sentences, diff --git a/tests/test_voice_worker_melo_numbers.py b/tests/test_voice_worker_melo_numbers.py new file mode 100644 index 0000000..bdd024e --- /dev/null +++ b/tests/test_voice_worker_melo_numbers.py @@ -0,0 +1,87 @@ +"""Numbers MeloTTS's Korean front end reads wrongly are spelled out before it sees them: a number it +cannot tag as a counter is read digit by digit ("60%" as "육영퍼센트"), and every dot or comma is a +sentence break for it ("4.2미터" as "사. 이미터").""" + +from __future__ import annotations + +import json +import os +import struct +import subprocess +import sys +import textwrap + +import pytest + +from voice_worker import melo_child +from voice_worker.melo_child import spell_numbers_kr + + +@pytest.mark.unit +@pytest.mark.parametrize("text, spoken", [ + ("습도 60%입니다.", "습도 육십%입니다."), + ("습도 60퍼센트", "습도 육십퍼센트"), + ("바람 초속 4.2미터", "바람 초속 사점이미터"), + ("최고 25.5도, 최저 0.5도", "최고 이십오점오도, 최저 영점오도"), + ("11월 19일, 2026년", "십일월 십구일, 이천이십육년"), + ("1,234명", "천이백삼십사명"), + ("10 100 1000 10000 110 20만 100000000", "십 백 천 만 백십 이십만 일억"), + ("0, 7", "영, 칠"), +]) +def test_numbers_are_spelled_as_they_are_read(text, spoken): + assert spell_numbers_kr(text) == spoken + + +@pytest.mark.unit +def test_numbers_before_native_counters_are_left_for_melo_to_read_natively(): + # "3시" is "세시", "10개" is "열개", "5시간" is "다섯시간": MeloTTS's own counter rule reads these. + text = "3시 10개 5시간 2마리" + assert spell_numbers_kr(text) == text + + +FAKE_MELO = textwrap.dedent(""" + import os + import numpy as np + + class _Hps: + class data: + spk2id = {"KR": 0} + sampling_rate = 48000 + + class TTS: + def __init__(self, language, device): + self.hps = _Hps + + def tts_to_file(self, text, speaker, path, speed=1.0, quiet=True): + with open(os.environ["FAKE_MELO_LOG"], "a", encoding="utf-8") as f: + f.write(text + "\\n") + return np.zeros(10) +""") + + +def read_frame(stream) -> dict: + (n,) = struct.unpack("