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.

2.5 KB
Raw Blame
import json

import numpy as np
from mimvox_engine import voices
from mimvox_engine.audio_util import write_wav
from mimvox_engine.dataset import store


class Ctx:
    def __init__(self, path):
        self.data_dir = str(path)


def tone(path, seconds=2.0):
    t = np.linspace(0, seconds, int(48000 * seconds), endpoint=False)
    write_wav(path, (0.2 * np.sin(2 * np.pi * 220 * t)).astype(np.float32), 48000)
    return path


def test_recording_over_a_voice_keeps_only_its_name(tmp_path):
    ctx = Ctx(tmp_path / "data")
    vid = voices.voices_create(ctx, {"name": "Huyang", "language": "zh"})["voice"]["id"]
    vdir = store._voice_dir(ctx, {"voice_id": vid})
    for n in (1, 2, 1000123):
        tone(store._part_path(vdir, n))
    (vdir / "rvc").mkdir()
    (vdir / "rvc" / "model.pth").write_bytes(b"model")
    tone(vdir / "reference.wav")
    meta = json.loads((vdir / "voice.json").read_text(encoding="utf-8"))
    meta.update(trained=True, trained_epochs=450)
    (vdir / "voice.json").write_text(json.dumps(meta), encoding="utf-8")

    take = tone(tmp_path / "take.wav", 3.0)
    store.dataset_add(ctx, {"voice_id": vid, "part_index": 1, "path": str(take), "start_over": True, "language": "fr"})

    assert store._existing_parts(vdir) == [1]
    assert not (vdir / "rvc").exists()
    after = json.loads((vdir / "voice.json").read_text(encoding="utf-8"))
    assert (after["name"], after["language"]) == ("Huyang", "fr")
    assert "trained" not in after
    assert voices.describe(voices.config.data_dir(ctx), vid)["trained"] is False


def test_a_plain_take_keeps_the_others(tmp_path):
    ctx = Ctx(tmp_path / "data")
    vid = voices.voices_create(ctx, {"name": "Anna", "language": "en"})["voice"]["id"]
    vdir = store._voice_dir(ctx, {"voice_id": vid})
    tone(store._part_path(vdir, 2))
    store.dataset_add(ctx, {"voice_id": vid, "part_index": 1, "path": str(tone(tmp_path / "t.wav", 3.0))})
    assert store._existing_parts(vdir) == [1, 2]


def test_an_empty_voice_takes_the_language_of_its_first_part_only(tmp_path):
    ctx = Ctx(tmp_path / "data")
    vid = voices.voices_create(ctx, {"name": "Nova", "language": "zh"})["voice"]["id"]
    vdir = store._voice_dir(ctx, {"voice_id": vid})
    take = tone(tmp_path / "t.wav", 3.0)
    store.dataset_add(ctx, {"voice_id": vid, "part_index": 1, "path": str(take), "language": "fr"})
    store.dataset_add(ctx, {"voice_id": vid, "part_index": 2, "path": str(take), "language": "de"})
    assert json.loads((vdir / "voice.json").read_text(encoding="utf-8"))["language"] == "fr"