Skip to content

VoiceStudio is the open-source, fully-local ElevenLabs alternative — voice cloning, voice design, video dubbing, dictation, transcription & audiobook creation in 646 languages.

1.9 KB
Raw Blame
from pathlib import Path
from types import SimpleNamespace

import pytest

from services.diarization_native import (
    MAX_V1_AUDIO_SECONDS,
    SORTFORMER_FRAME_SAMPLES,
    NativeSortformer,
    _sortformer_command,
    _validated_turn,
)


def test_native_sortformer_uses_bounded_growing_graph():
    command = _sortformer_command(
        Path("audiocpp_cli"),
        Path("sortformer.gguf"),
        SimpleNamespace(backend="vulkan", index=1),
        Path("input.wav"),
        Path("turns.json"),
    )

    assert command[-2:] == ["--session-option", "graph_capacity_mode=grow"]
    assert command[command.index("--backend") + 1] == "vulkan"
    assert command[command.index("--device") + 1] == "1"
    assert MAX_V1_AUDIO_SECONDS == 120.0


def test_native_sortformer_rejects_unbounded_v1_recording(monkeypatch, tmp_path):
    import soundfile

    adapter = object.__new__(NativeSortformer)
    adapter.model = tmp_path / "sortformer.gguf"
    adapter.binary = tmp_path / "audiocpp_cli"
    monkeypatch.setattr(
        soundfile,
        "info",
        lambda _path: SimpleNamespace(duration=MAX_V1_AUDIO_SECONDS + 0.01),
    )

    with pytest.raises(ValueError, match="select pyannote for longer recordings"):
        adapter(tmp_path / "long.wav")


def test_native_sortformer_clamps_one_frame_of_decoder_padding():
    frames = 560_000

    assert _validated_turn(
        {
            "start_sample": 373_760,
            "end_sample": frames + 480,
            "speaker_id": "SPEAKER_03",
        },
        frames,
    ) == (373_760, frames, "SPEAKER_03")


def test_native_sortformer_rejects_large_boundary_overshoot():
    frames = 560_000

    with pytest.raises(ValueError, match="Invalid native speaker-turn boundaries"):
        _validated_turn(
            {
                "start_sample": 373_760,
                "end_sample": frames + SORTFORMER_FRAME_SAMPLES + 1,
                "speaker_id": "SPEAKER_03",
            },
            frames,
        )