Skip to content

Clone your own voice on your own computer: speak any text in 60+ languages and dub videos into another language in your voice. Fully local. Early version.

15.6 KB
Raw Blame
from __future__ import annotations

import json
import re
import shutil
import threading
from pathlib import Path
from typing import Any

import numpy as np
import soundfile as sf

from .. import paths
from ..registry import EngineError, method
from . import pace, quality
from ._audio import HOP_S, read_audio, resample, write_wav
from .levels import level_for, message_for, recommended_epochs

TARGET_ACTIVE_RMS_DB = -20.0
MAX_PEAK = 0.9
MAX_GAIN_DB = 40.0
TRIM_PAD_S = 0.25
TRIM_MIN_FRAMES = 5  # 50 ms: ignores clicks
MAX_RATE = 48000
MEDIA_POOL = "reference_media.wav"
FILES_PART_MIN = 1000  # the app numbers a recording added from files 1000 + a timestamp, away from reading parts

_LOCK = threading.RLock()
_ID_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,79}$")
_PART_RE = re.compile(r"^part(\d+)\.wav$")


def _voice_dir(ctx: Any, params: dict[str, Any]) -> Path:
    vid = params.get("voice_id")
    if not isinstance(vid, str) or not _ID_RE.match(vid):
        raise EngineError("bad_request", "Missing or invalid 'voice_id'.")
    vdir = Path(ctx.data_dir) / "voices" / vid
    if not vdir.is_dir():
        raise EngineError("not_found", f"Voice not found: {vid}")
    return vdir


def _part_index(params: dict[str, Any]) -> int:
    n = params.get("part_index")
    if isinstance(n, bool) or not isinstance(n, int) or n < 1:
        raise EngineError("bad_request", "'part_index' must be an integer starting at 1.")
    return n


def _ddir(vdir: str | Path) -> Path:
    return Path(vdir) / "dataset"


def _files_name(ns: list[int], files: list[int]) -> str:
    if len(files) == 1:
        return "the recording from files"
    return f"recording {', '.join(str(files.index(n) + 1) for n in ns)} from files"


def _advice(bad: list[int], warn: list[int], files: list[int]) -> str:
    """Reading parts by their number; a recording added from files by its place among them, never its internal index."""
    ns, worse = (bad, True) if bad else (warn, False)
    reading = [n for n in ns if n < FILES_PART_MIN]
    from_files = [n for n in ns if n >= FILES_PART_MIN]
    out = []
    if reading:
        nums = ", ".join(map(str, reading))
        out.append(
            f"Re-record part {nums}: it has problems and is not counted."
            if worse
            else f"Part {nums} could be better; check the tips and re-record if you can."
        )
    if from_files:
        name = _files_name(from_files, files)
        name = name[0].upper() + name[1:]
        out.append(
            f"{name} has problems and is not counted; a cleaner file would help."
            if worse
            else f"{name} could be better; a cleaner file would help."
        )
    return " ".join(out)


def _part_path(vdir: str | Path, n: int) -> Path:
    return _ddir(vdir) / f"part{n}.wav"


def _meta_path(vdir: str | Path) -> Path:
    return _ddir(vdir) / "meta.json"


def _load_meta(vdir: str | Path) -> dict[str, Any]:
    try:
        with _meta_path(vdir).open(encoding="utf-8") as f:
            meta = json.load(f)
        if isinstance(meta, dict) and isinstance(meta.get("parts"), dict):
            return meta
    except (OSError, ValueError):
        pass
    return {"version": 1, "parts": {}}


def _save_meta(vdir: str | Path, meta: dict[str, Any]) -> None:
    _ddir(vdir).mkdir(parents=True, exist_ok=True)
    path = _meta_path(vdir)
    tmp = path.with_name(path.name + ".tmp")
    with tmp.open("w", encoding="utf-8") as f:
        json.dump(meta, f, indent=1, default=paths.json_default)
    tmp.replace(path)


def _existing_parts(vdir: str | Path) -> list[int]:
    try:
        names = [p.name for p in _ddir(vdir).iterdir()]
    except OSError:
        return []
    return sorted(int(m.group(1)) for n in names if (m := _PART_RE.match(n)))


def trim_silence(y: np.ndarray, sr: int, m: dict[str, Any]) -> np.ndarray:
    if m["speech_db"] <= -90.0:
        return y
    fdb = m["_frame_db"]
    thr = max(m["noise_floor_db"] + 10.0, m["speech_db"] - 35.0)
    above = fdb >= thr
    k = TRIM_MIN_FRAMES
    if len(above) < k:
        return y
    run = np.convolve(above.astype(np.int8), np.ones(k, dtype=np.int8), mode="valid") == k
    idx = np.flatnonzero(run)
    if idx.size == 0:
        return y
    hop = max(int(sr * HOP_S), 1)
    start = max(int(idx[0] * hop - TRIM_PAD_S * sr), 0)
    end = min(int((idx[-1] + k) * hop + int(sr * 0.025) + TRIM_PAD_S * sr), len(y))
    return y[start:end] if end - start > sr * 0.3 else y


def normalise(y: np.ndarray, m: dict[str, Any]) -> np.ndarray:
    fdb = m["_frame_db"]
    act = fdb >= m["_silence_thr"]
    if m["speech_db"] <= -90.0 or not act.any():
        return y
    active_rms_db = float(10.0 * np.log10(np.mean(10.0 ** (fdb[act] / 10.0))))
    gain_db = min(TARGET_ACTIVE_RMS_DB - active_rms_db, MAX_GAIN_DB)
    gain = 10.0 ** (gain_db / 20.0)
    peak = float(np.max(np.abs(y))) or 1e-9
    gain = min(gain, MAX_PEAK / peak)
    return (y * gain).astype(np.float32)


def _fade(y: np.ndarray, sr: int, sec: float = 0.01) -> np.ndarray:
    k = min(int(sr * sec), len(y) // 2)
    if k > 1:
        ramp = np.linspace(0.0, 1.0, k, dtype=np.float32)
        y = y.copy()
        y[:k] *= ramp
        y[-k:] *= ramp[::-1]
    return y


def _titles(vdir: str | Path) -> dict[int, str]:
    try:
        with (Path(vdir) / "voice.json").open(encoding="utf-8") as f:
            vmeta = json.load(f)
        preset_id = vmeta.get("preset")
        if not isinstance(preset_id, str):
            return {}
        from .presets import get_preset

        # Every language's preset has the English parts in the English order.
        preset = get_preset(preset_id, "en")
        return {p["index"]: p["title"] for p in preset["parts"]} if preset else {}
    except Exception:
        return {}


def _usable(vdir: str | Path, meta: dict[str, Any]) -> set[int]:
    have = {n for n in _existing_parts(vdir) if str(n) in meta["parts"]}
    ok = {n for n in have if meta["parts"][str(n)].get("verdict") != "bad"}
    return ok or have


def _ensure_analysed(vdir: str | Path, meta: dict[str, Any]) -> bool:
    """Analyses parts stored before the pace analysis existed. True when meta changed."""
    changed = False
    for n in _existing_parts(vdir):
        info = meta["parts"].get(str(n))
        if info is not None and "cands" not in info:
            y, sr = read_audio(_part_path(vdir, n))
            info.update(pace.analyse_part(y, sr))
            info.pop("ref", None)
            changed = True
    return changed


def rank_candidates(vdir: str | Path, meta: dict[str, Any]) -> tuple[list[dict[str, Any]], float]:
    """Best first; ``meta`` must be analysed."""
    return pace.rank(meta["parts"], _titles(vdir), _usable(vdir, meta))


def write_reference(
    vdir: str | Path, meta: dict[str, Any], part: int, start_s: float, end_s: float, **extra: Any
) -> None:
    y, sr = read_audio(_part_path(vdir, part))
    clip = _fade(y[int(start_s * sr) : int(end_s * sr)], sr)
    write_wav(Path(vdir) / "reference.wav", clip, sr)
    meta["reference"] = {
        "part": part,
        "start_s": round(start_s, 3),
        "end_s": round(end_s, 3),
        "duration_s": round(len(clip) / sr, 2),
        **extra,
    }


def cut_media(vdir: str | Path, start_s: float, end_s: float) -> tuple[np.ndarray, int]:
    """Read without loading the whole pool; ``not_found`` when there is no pool."""
    path = Path(vdir) / MEDIA_POOL
    try:
        sr = int(sf.info(path).samplerate)
        y, _ = sf.read(path, start=int(start_s * sr), stop=int(end_s * sr), dtype="float32", always_2d=False)
    except (OSError, RuntimeError):
        raise EngineError("not_found", "this voice has no combined media (reference_media.wav)") from None
    if y.ndim > 1:
        y = y.mean(axis=1)
    return _fade(np.ascontiguousarray(y, dtype=np.float32), sr), sr


def _keep_manual(vdir: str | Path, meta: dict[str, Any], cur: dict[str, Any], changed_part: int | None) -> bool:
    ref_path = Path(vdir) / "reference.wav"
    if cur.get("source") == "media":
        if ref_path.is_file():
            return True
        pool = meta.get("media") if isinstance(meta.get("media"), dict) else {}
        if not cur.get("pool") or cur.get("pool") != pool.get("pool"):
            return False
        try:
            clip, sr = cut_media(vdir, float(cur["start_s"]), float(cur["end_s"]))
        except (EngineError, KeyError, TypeError, ValueError):
            return False
        write_wav(ref_path, clip, sr)
        return True
    part = cur.get("part")
    if isinstance(part, int) and _part_path(vdir, part).is_file() and part != changed_part:
        if not ref_path.is_file():
            write_reference(vdir, meta, part, cur["start_s"], cur["end_s"], manual=True)
        return True
    return False


def _refresh_reference(vdir: str | Path, meta: dict[str, Any], changed_part: int | None = None) -> None:
    """A manual choice is kept until its part is removed or re-recorded (``changed_part``); one from the media pool
    until another clip is chosen."""
    ref_path = Path(vdir) / "reference.wav"
    cur = meta.get("reference")
    if isinstance(cur, dict) and cur.get("manual"):
        if _keep_manual(vdir, meta, cur, changed_part):
            return
        meta.pop("reference", None)
        cur = None
    _ensure_analysed(vdir, meta)
    ranked, _ = rank_candidates(vdir, meta)
    if not ranked:
        meta.pop("reference", None)
        if ref_path.is_file():
            ref_path.unlink()
        return
    best = ranked[0]
    same = (
        isinstance(cur, dict)
        and cur.get("part") == best["part"]
        and cur.get("start_s") == round(best["start_s"], 3)
        and cur.get("end_s") == round(best["end_s"], 3)
        and best["part"] != changed_part
    )
    if same and ref_path.is_file():
        return  # unchanged: rewriting would only force the speech model to condition on the speaker again
    write_reference(vdir, meta, best["part"], best["start_s"], best["end_s"])


def _analyse_stored(vdir: str | Path, n: int) -> dict[str, Any]:
    y, sr = read_audio(_part_path(vdir, n))
    m = quality.measure(y, sr)
    verdict, issues = quality.assess(m)
    return {
        "verdict": verdict,
        "issues": issues,
        "snr_db": round(m["snr_db"], 1),
        "clipping_pct": round(m["clipping_pct"], 3),
        **pace.analyse_part(y, sr),
    }


def _summary(vdir: str | Path, meta: dict[str, Any]) -> dict[str, Any]:
    changed = False
    parts = []
    for n in _existing_parts(vdir):
        info = meta["parts"].get(str(n))
        if info is None:
            info = meta["parts"][str(n)] = _analyse_stored(vdir, n)
            changed = True
        try:
            dur = sf.info(_part_path(vdir, n)).duration
        except Exception:
            dur = 0.0
        parts.append(
            {
                "index": n,
                "duration_s": round(dur, 2),
                "verdict": info.get("verdict", "ok"),
                "issues": info.get("issues", []),
            }
        )
    stale = [k for k in meta["parts"] if int(k) not in {p["index"] for p in parts}]
    for k in stale:
        del meta["parts"][k]
        changed = True
    if changed:
        _refresh_reference(vdir, meta)
        _save_meta(vdir, meta)

    total = sum(p["duration_s"] for p in parts) / 60.0
    usable = sum(p["duration_s"] for p in parts if p["verdict"] != "bad") / 60.0
    level = level_for(usable)
    bad = [p["index"] for p in parts if p["verdict"] == "bad"]
    warn = [p["index"] for p in parts if p["verdict"] == "warn"]
    extra = _advice(bad, warn, sorted(p["index"] for p in parts if p["index"] >= FILES_PART_MIN))
    if not parts:
        message = "No recordings yet. Read the first part aloud to get started."
    else:
        message = message_for(usable, level, extra)
    out: dict[str, Any] = {
        "total_minutes": round(total, 2),
        "usable_minutes": round(usable, 2),
        "parts": parts,
        "level": level,
        "message": message,
        "recommended_epochs": recommended_epochs(usable),
        "has_reference": (Path(vdir) / "reference.wav").is_file(),
    }
    if meta.get("reference"):
        out["reference"] = meta["reference"]
    return out


def _write_voice_meta(vdir: Path, meta: dict[str, Any]) -> None:
    path = vdir / "voice.json"
    tmp = path.with_name(path.name + ".tmp")
    with tmp.open("w", encoding="utf-8") as f:
        json.dump(meta, f, indent=1)
    tmp.replace(path)


def _set_language(vdir: Path, language: str) -> None:
    """The first part read into an empty voice gives it the reading language."""
    with (vdir / "voice.json").open(encoding="utf-8") as f:
        meta = json.load(f)
    if language and meta.get("language") != language and not _existing_parts(vdir):
        meta["language"] = language
        _write_voice_meta(vdir, meta)


def _start_over(vdir: Path, language: Any) -> None:
    """Recording over a voice: it keeps its id and name; recordings, references and the trained model go."""
    meta_path = vdir / "voice.json"
    with meta_path.open(encoding="utf-8") as f:
        old = json.load(f)
    for p in vdir.iterdir():
        if p.is_dir():
            shutil.rmtree(p)
        elif p != meta_path:
            p.unlink()
    meta = {k: old[k] for k in ("id", "name", "created") if k in old}
    meta["language"] = language if isinstance(language, str) and language else old.get("language")
    _write_voice_meta(vdir, meta)


@method("dataset.add")
def dataset_add(ctx: Any, params: dict[str, Any]) -> dict[str, Any]:
    vdir = _voice_dir(ctx, params)
    n = _part_index(params)
    y, sr = read_audio(params.get("path"))
    if len(y) < sr * 0.3:
        raise EngineError("bad_request", "This recording is empty or too short.")
    if sr < quality.MIN_SAMPLE_RATE:
        raise EngineError("bad_request", f"Sample rate too low ({sr} Hz): record at 44.1 or 48 kHz.")
    if sr > MAX_RATE:
        y, sr = resample(y, sr, 44100), 44100
    y = y - float(np.mean(y))
    m0 = quality.measure(y, sr)
    y = trim_silence(y, sr, m0)
    m1 = quality.measure(y, sr)
    expected = params.get("expected_s")
    verdict, issues = quality.assess(m1, float(expected) if isinstance(expected, (int, float)) else None)
    y = normalise(y, m1)
    with _LOCK:
        if params.get("start_over") is True:
            _start_over(vdir, params.get("language"))
        elif isinstance(params.get("language"), str):
            _set_language(vdir, params["language"])
        _ddir(vdir).mkdir(parents=True, exist_ok=True)
        write_wav(_part_path(vdir, n), y, sr)
        meta = _load_meta(vdir)
        others = {k: v for k, v in meta["parts"].items() if k != str(n)}
        meta["parts"][str(n)] = {
            "verdict": verdict,
            "issues": issues,
            "snr_db": round(m1["snr_db"], 1),
            "clipping_pct": round(m1["clipping_pct"], 3),
            **pace.analyse_part(y, sr, pace.voice_median_rate(others)),
        }
        _refresh_reference(vdir, meta, changed_part=n)
        _save_meta(vdir, meta)
        summary = _summary(vdir, meta)
    return {
        "part": {"index": n, "duration_s": round(len(y) / sr, 2), "verdict": verdict, "issues": issues},
        "summary": summary,
    }


@method("dataset.remove")
def dataset_remove(ctx: Any, params: dict[str, Any]) -> dict[str, Any]:
    vdir = _voice_dir(ctx, params)
    n = _part_index(params)
    with _LOCK:
        path = _part_path(vdir, n)
        if path.is_file():
            path.unlink()
        meta = _load_meta(vdir)
        meta["parts"].pop(str(n), None)
        _refresh_reference(vdir, meta, changed_part=n)
        _save_meta(vdir, meta)
        return {"summary": _summary(vdir, meta)}


@method("dataset.summary")
def dataset_summary(ctx: Any, params: dict[str, Any]) -> dict[str, Any]:
    vdir = _voice_dir(ctx, params)
    with _LOCK:
        return _summary(vdir, _load_meta(vdir))