kale-tts-1-tiny / recipe /export /apply_voice_values.py
NERDDISCO's picture
kale-tts-1
f21d6eb
Raw History Blame Contribute Delete
1.64 kB
"""write voice_values.json (the single source of per-voice voice-file values) into a model dir's voices/<id>.extra.bin.
fields: floor -> voice_pitch_floor, tail -> voice_clamp_tail, thr -> voice_clamp_thr, unvoice_hz -> voice_unvoice_hz (only once the graph has
that input; until then every voice must stay at the graph's fixed 80 Hz). prints every value it changes.
usage: python apply_voice_values.py <model dir> <tiny|nano> [voice_values.json]"""
import json, math, sys
from pathlib import Path
import numpy as np
md, size = Path(sys.argv[1]), sys.argv[2]
vals = json.loads(Path(sys.argv[3] if len(sys.argv) > 3 else Path(__file__).with_name("voice_values.json")).read_text())[size]
vj = json.loads((md / "voices.json").read_text())
off, slot = 0, {}
for vi in vj["voice_inputs"]:
n = math.prod(vi["dims"]); slot[vi["name"]] = (off, n); off += n
FIELDS = {"floor": "voice_pitch_floor", "tail": "voice_clamp_tail", "thr": "voice_clamp_thr", "unvoice_hz": "voice_unvoice_hz"}
assert {v["id"] for v in vj["voices"]} == set(vals), "voice_values.json must list exactly the model's voices"
for v in vj["voices"]:
p = md / "voices" / f"{v['id']}.extra.bin"; e = np.fromfile(p, np.float32); assert e.size == off, (p, e.size, off)
for f, inp in FIELDS.items():
want = np.float32(vals[v["id"]][f])
if inp not in slot:
assert f == "unvoice_hz" and want == 80.0, f"{v['id']}: {f}={want} needs a graph with {inp}"
continue
o, n = slot[inp]; assert n == 1
if e[o] != want: print(f"{size} {v['name']:8s} {f}: {e[o]:g} -> {want:g}"); e[o] = want
e.tofile(p)
print(size, "ok")