Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

### Text-to-speech

- **Reuse prepared reference audio for multi-sample voices.** Repeated generations reuse the combined WAV when sample content, transcripts, order and engine are unchanged; edits invalidate it and bypassing the cache still rebuilds it. Reference decoding, hashing and WAV writes run off the API event loop, and combined WAVs are published atomically.

- **Design a voice from a written description.** The profile form gains a third source, "Describe a voice", next to clone-from-audio and built-in voice: write the voice you want ("a warm, gravelly older man with a slow Scottish lilt") and the profile is created as a `designed` voice backed by the new `qwen_voice_design` engine (Qwen3-TTS-12Hz-1.7B-VoiceDesign, 10 languages, local, Apache-2.0 weights). Designed profiles work everywhere a profile does: `/generate`, `/speak`, MCP `voicebox_speak`, stories, and dictation TTS. A per-generation instruct layers onto the description instead of replacing it. — @Lvigentini ([#1012](https://github.com/jamiepine/voicebox/pull/1012))

### Dictation and agents
Expand Down
25 changes: 15 additions & 10 deletions backend/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
voice prompt combination, and model loading progress tracking.
"""

import asyncio
import logging
import os
import platform
Expand Down Expand Up @@ -276,19 +277,23 @@ async def combine_voice_prompts(
reference_texts: Corresponding transcripts.
sample_rate: If set, resample audio to this rate during loading.
"""
combined_audio = []
def combine_sync():
combined_audio = []

for path in audio_paths:
kwargs = {"sample_rate": sample_rate} if sample_rate else {}
audio, _sr = load_audio(path, **kwargs)
audio = normalize_audio(audio)
combined_audio.append(audio)
for path in audio_paths:
kwargs = {"sample_rate": sample_rate} if sample_rate else {}
audio, _sr = load_audio(path, **kwargs)
audio = normalize_audio(audio)
combined_audio.append(audio)

mixed = np.concatenate(combined_audio)
mixed = normalize_audio(mixed)
combined_text = " ".join(reference_texts)
mixed = np.concatenate(combined_audio)
mixed = normalize_audio(mixed)
combined_text = " ".join(reference_texts)

return mixed, combined_text
return mixed, combined_text

# Decoding/resampling can be slow; keep the API event loop responsive.
return await asyncio.to_thread(combine_sync)


@contextmanager
Expand Down
35 changes: 23 additions & 12 deletions backend/services/profiles.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Voice profile management module."""

import asyncio
import hashlib
import json as _json
import logging
import shutil
Expand All @@ -26,7 +28,7 @@
VoiceProfileResponse,
)
from ..utils.audio import save_audio, validate_and_load_reference_audio
from ..utils.cache import _get_cache_dir, clear_profile_cache
from ..utils.cache import _get_cache_dir, clear_profile_cache, get_cache_key
from ..utils.images import process_avatar, validate_image
from . import history

Expand Down Expand Up @@ -653,23 +655,32 @@ async def create_voice_prompt_for_profile(
audio_paths.append(str(sample_audio_path))
reference_texts = [s.reference_text for s in samples]

combined_audio, combined_text = await tts_model.combine_voice_prompts(
audio_paths,
reference_texts,
)

# Save combined audio to cache directory (persistent)
# Create a hash of sample IDs to identify this specific combination
import hashlib
# Preserve sample order and include content, transcripts and engine. Sample
# IDs alone cannot safely identify reusable audio after an edit/replacement.
def reference_keys():
return [get_cache_key(path, reference) for path, reference in zip(audio_paths, reference_texts, strict=True)]

sample_ids_str = "-".join(sorted([s.id for s in samples]))
combination_hash = hashlib.md5(sample_ids_str.encode()).hexdigest()[:12]
sample_keys = await asyncio.to_thread(reference_keys)
combination_hash = hashlib.sha256((engine + "-" + "-".join(sample_keys)).encode()).hexdigest()[:16]

cache_dir = _get_cache_dir()
cache_dir.mkdir(parents=True, exist_ok=True)
combined_path = cache_dir / f"combined_{profile_id}_{combination_hash}.wav"
combined_text = " ".join(reference_texts)

if not use_cache or not combined_path.is_file():
combined_audio, combined_text = await tts_model.combine_voice_prompts(audio_paths, reference_texts)

def save_combined_audio():
# A reader must never mistake a partially written WAV for a hit.
temporary_path = combined_path.with_name(f".{combined_path.stem}.{uuid.uuid4().hex}.tmp.wav")
try:
save_audio(combined_audio, str(temporary_path), 24000)
temporary_path.replace(combined_path)
finally:
temporary_path.unlink(missing_ok=True)

save_audio(combined_audio, str(combined_path), 24000)
await asyncio.to_thread(save_combined_audio)

voice_prompt, _ = await tts_model.create_voice_prompt(
str(combined_path),
Expand Down
204 changes: 204 additions & 0 deletions backend/tests/test_combined_voice_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
"""Multi-sample reference reuse, invalidation and non-blocking preparation."""

import asyncio
import threading
from pathlib import Path
from types import SimpleNamespace

import numpy as np
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from backend import backends, config
from backend.backends import base
from backend.database import Base, ProfileSample, VoiceProfile
from backend.services import profiles
from backend.utils.cache import clear_profile_cache


@pytest.fixture
def voice(tmp_path, monkeypatch):
engine = create_engine("sqlite://")
Base.metadata.create_all(engine)
db = sessionmaker(bind=engine)()
db.add(VoiceProfile(id="voice", name="Test", voice_type="cloned"))
paths = [tmp_path / "a.wav", tmp_path / "b.wav"]
for index, path in enumerate(paths):
path.write_bytes(f"reference audio {index}".encode())
db.add(ProfileSample(id=str(index), profile_id="voice", audio_path=str(path), reference_text=f"sample {index}"))
db.commit()
cache = tmp_path / "cache"
monkeypatch.setattr(config, "get_cache_dir", lambda: cache)
combinations, prompts = [], []

class Backend:
async def combine_voice_prompts(self, audio_paths, texts):
combinations.append((list(audio_paths), list(texts)))
return np.ones(240, dtype=np.float32), " ".join(texts)

async def create_voice_prompt(self, path, text, **kwargs):
prompts.append((path, text, kwargs))
assert Path(path).is_file()
return {"path": path, "text": text}, False

monkeypatch.setattr(backends, "get_tts_backend_for_engine", lambda _: Backend())

def generate(**kwargs):
return asyncio.run(profiles.create_voice_prompt_for_profile("voice", db, **kwargs))

yield SimpleNamespace(
db=db, paths=paths, cache=cache, combinations=combinations, prompts=prompts, generate=generate
)
db.close()
engine.dispose()


def test_repeated_generation_reuses_combined_wav(voice, monkeypatch):
first = voice.generate()
monkeypatch.setattr(profiles, "save_audio", lambda *args: pytest.fail("cache hit rewrote audio"))
assert voice.generate() == first
assert len(voice.combinations) == 1


@pytest.mark.parametrize("change", ["audio", "transcript", "engine"])
def test_changed_reference_or_engine_invalidates_combined_wav(voice, change):
first = voice.generate()
kwargs = {}
if change == "audio":
voice.paths[0].write_bytes(b"replacement audio")
elif change == "transcript":
voice.db.get(ProfileSample, "1").reference_text = "corrected transcript"
voice.db.commit()
else:
kwargs["engine"] = "luxtts"
second = voice.generate(**kwargs)
assert second["path"] != first["path"]
assert len(voice.combinations) == 2
assert voice.generate(**kwargs) == second
if change == "transcript":
assert second["text"] == "sample 0 corrected transcript"


def test_sample_order_is_part_of_cache_identity(voice):
first = voice.generate()
samples = list(reversed(voice.db.query(ProfileSample).filter_by(profile_id="voice").all()))

class ReorderedSession:
def query(self, entity):
if entity is ProfileSample:
return SimpleNamespace(filter_by=lambda **kwargs: SimpleNamespace(all=lambda: samples))
return voice.db.query(entity)

second = asyncio.run(profiles.create_voice_prompt_for_profile("voice", ReorderedSession()))
assert second["path"] != first["path"]
assert second["text"] == "sample 1 sample 0"
assert len(voice.combinations) == 2


def test_cache_bypass_rebuilds_and_is_forwarded_to_backend(voice):
first = voice.generate()
assert voice.generate(use_cache=False) == first
assert len(voice.combinations) == 2
assert voice.prompts[-1][2] == {"use_cache": False}
assert voice.generate() == first
assert len(voice.combinations) == 2


def test_missing_combined_wav_is_rebuilt(voice):
first = voice.generate()
Path(first["path"]).unlink()
assert voice.generate() == first
assert len(voice.combinations) == 2


def test_single_sample_keeps_existing_backend_path(voice):
voice.db.delete(voice.db.get(ProfileSample, "1"))
voice.db.commit()
result = voice.generate(use_cache=False)
assert result == {"path": str(voice.paths[0]), "text": "sample 0"}
assert voice.combinations == []
assert voice.prompts[-1][2] == {"use_cache": False}
assert not voice.cache.exists()


def test_profile_cache_clear_removes_combined_wavs(voice):
first = voice.generate()
voice.paths[0].write_bytes(b"replacement audio")
second = voice.generate()
assert clear_profile_cache("voice") == 2
assert not Path(first["path"]).exists()
assert not Path(second["path"]).exists()


def test_hashing_and_atomic_wav_write_run_off_event_loop(voice, monkeypatch):
event_loop_thread = threading.get_ident()
original_key, original_save = profiles.get_cache_key, profiles.save_audio
calls = []

def key(*args):
assert threading.get_ident() != event_loop_thread
calls.append("hash")
return original_key(*args)

def save(*args):
assert threading.get_ident() != event_loop_thread
assert not list(voice.cache.glob("combined_voice_*.wav"))
calls.append("save")
return original_save(*args)

monkeypatch.setattr(profiles, "get_cache_key", key)
monkeypatch.setattr(profiles, "save_audio", save)
voice.generate()
assert calls == ["hash", "hash", "save"]


def test_failed_write_does_not_publish_partial_cache_and_can_retry(voice, monkeypatch):
def fail_save(audio, path, sample_rate):
Path(path).write_bytes(b"partial WAV")
raise OSError("disk full")

with monkeypatch.context() as patch:
patch.setattr(profiles, "save_audio", fail_save)
with pytest.raises(OSError, match="disk full"):
voice.generate()
assert list(voice.cache.iterdir()) == []
assert voice.prompts == []
voice.generate()
assert len(voice.combinations) == 2


def test_failed_refresh_preserves_previous_complete_cache(voice, monkeypatch):
first = voice.generate()
previous = Path(first["path"]).read_bytes()

def fail_save(audio, path, sample_rate):
Path(path).write_bytes(b"partial WAV")
raise OSError("disk full")

with monkeypatch.context() as patch:
patch.setattr(profiles, "save_audio", fail_save)
with pytest.raises(OSError, match="disk full"):
voice.generate(use_cache=False)
assert Path(first["path"]).read_bytes() == previous
assert voice.generate() == first
assert len(list(voice.cache.iterdir())) == 1


@pytest.mark.parametrize("sample_rate", [None, 24000])
def test_audio_decoding_runs_off_event_loop(monkeypatch, sample_rate):
event_loop_thread = threading.get_ident()
calls = []

def load(path, **kwargs):
assert threading.get_ident() != event_loop_thread
calls.append((path, kwargs))
return np.ones(100, dtype=np.float32), 24000

monkeypatch.setattr(base, "load_audio", load)
monkeypatch.setattr(base, "normalize_audio", lambda audio: audio)
audio, text = asyncio.run(base.combine_voice_prompts(["a", "b"], ["Hello", "world"], sample_rate=sample_rate))
assert len(audio) == 200
assert text == "Hello world"
kwargs = {"sample_rate": sample_rate} if sample_rate else {}
assert calls == [("a", kwargs), ("b", kwargs)]