Files
VoiceStudio/scripts/render_demos_omnivoice.py
T
Palash DebnathandClaude Opus 5 41c098e009 feat(demos): ship the demo audio and video the app already advertises (#1517)
* feat(demos): ship the demo audio and video the app already advertises

Every demo asset in the app was a dead link on anything but a Mac.

`personalities.py` has carried a `preview_url` for each of the seven
voice-design presets since they were added; DictationDemo.jsx posts three
bundled WAVs to /transcribe so the feature can be shown without microphone
permission; the Dub workspace reads a manifest and plays a source video plus
four dubbed languages. None of those files were committed, because the tooling
that renders them (scripts/build_demos.sh, scripts/build_dub_demo.sh) hard-
requires macOS `say` — it even carries a `TODO: add espeak-ng path for Linux
contributors`. So the presets returned 404, the replay buttons did nothing, and
the dubbing demo never loaded.

Rendered with VoiceStudio's own engine, which runs wherever the app does:

- 7 voice-design previews (2.2 MB)
- 3 dictation replay clips (1.1 MB) — verified by transcribing them back:
  the conversational and French clips round-trip exactly
- dubbing demo: source + 4 dubbed videos with subtitles and manifest (9.6 MB)

Tooling fixes this turned up:

- build_dub_demo.sh wrote to backend/assets/demo/dubbing, but main.py mounts
  backend/assets/samples at /demo_audio — so the frontend's
  /demo_audio/demo/dubbing/manifest.json could never have resolved even after
  a successful Mac build. Output moved under the mount.
- `say` is now the fallback rather than the requirement: the new
  scripts/render_dub_demo_audio.py renders the five tracks with the engine and
  the shell script picks them up.
- The five demo paragraphs lived in two files. They are now one JSON both read
  — two copies is one edit away from a video whose subtitles disagree with it.
- render_demos_omnivoice.py peak-normalized, which a single-sample transient
  defeats: the Helpdesk preset landed at -30 dB RMS against -17 dB for its
  neighbours, so the preview row played at wildly different volumes. Now EBU
  R128 at -18 LUFS with a -1.5 dBTP ceiling.
- …and pinning the output rate, because loudnorm resamples to 192 kHz
  internally and writes there unless told otherwise, which turned 2.1 MB of
  previews into 17.5 MB of identical-sounding audio.
- update_manifest() looked for a manifest at a path nothing writes, so it
  always printed "not found" and did nothing.
- Dictation is rendered here now too. It was excluded on the grounds that
  `say` was good enough and engine TTS was overkill — true only on macOS.

tests/test_demo_assets_exist.py resolves every advertised URL against the
directory main.py actually mounts, and checks each dubbing subtitle matches the
script its manifest entry claims. A missing static file is not an import error
and not a failing request; nothing would have caught this otherwise.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* docs(changelog): stamp the demo-asset entries with their PR ref

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(demos): watermark rendered demo audio, and harden the render scripts

Review findings on #1517:

- Greptile P1: the renderers wrote engine output straight to disk, so a
  re-render shipped demo audio with no provenance mark. These clips play
  back to users as VoiceStudio output — they are synthetic audio leaving
  the app like any other, and now go through mark_synthetic (#1169), the
  one chokepoint every producing route uses. It runs on the file AFTER
  loudnorm, since loudnorm re-encodes what it is handed, and says so
  loudly when marking is unavailable rather than committing an unmarked
  asset. The dubbing renderer shares the same helper.
- CodeRabbit: build_dub_demo.sh checked only source.src.wav before
  deciding it could run without macOS `say`, so a Linux or Windows run
  with four of five tracks present reached a missing one, called `say`,
  and left a half-built bundle. It now requires all five.
- CodeRabbit: shutil.move over an existing path delegates to os.rename,
  which raises FileExistsError on Windows — os.replace overwrites
  atomically everywhere.
- CodeRabbit: the preview test discovered presets in a parametrize
  argument, importing app code at collection time and leaving
  core.personalities in sys.modules for later tests. Discovery moved into
  the test body.

CI: the rendered dub bundle's zh/ja subtitles, its manifest and the
script source are dubbing CONTENT, not UI strings — allowlisted in
test_no_hardcoded_cjk.py with that justification.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(demos): a render that cannot be watermarked fails instead of warning

CodeRabbit and Greptile, #1517: mark_synthetic degrades rather than
raising — correct for generation, wrong for a render script, whose whole
job is to produce files a human then commits. A printed warning on a
scrolling console is not a gate, so both scripts exited 0 with unmarked
assets sitting on disk ready to commit. They now raise, with the reason
and the fix; OMNIVOICE_DEMO_ALLOW_UNMARKED=1 stays for a local listen.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* ci: stop a flaky dependency fetch from failing green runs

en-core-web-sm resolves to a direct GitHub release URL, and github.com
intermittently answers `http2 error: refused stream before processing
any application logic`. uv's own three retries all land within the same
few seconds and fail together, so the whole job dies on a dependency
that has nothing to do with the change under test — it cost #1518 and
#1517 an otherwise-green run tonight.

Two changes: back off between whole `uv sync` attempts, which is what
actually clears it, and pass --no-sync to the pytest steps. `uv run`
re-resolves the environment before running, so every test step was a
fresh chance to hit the same fetch even though the install step had
already synced — that is exactly how #1518 failed, in the isolated
backend/tests step, with all 5467 tests already passed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* ci: one retry seam for every uv sync, not just the job that failed last

en-core-web-sm resolves to a direct GitHub *release* URL rather than a
package index, and github.com intermittently answers `http2 error:
refused stream before processing any application logic`. uv's own
retries all land inside the same ~10 seconds and fail together, so a job
dies on a dependency unrelated to the change under test. Tonight that
cost four otherwise-green runs across #1515, #1517 and #1518 — and the
first fix only covered the Tests job, so the next failure simply moved
to Smoke (Linux), which syncs separately.

The fetch is per-job, so the fix has to be per-job: scripts/uv-sync-retry.sh
backs off between whole attempts (15s, 45s, 90s) and every workflow that
syncs now goes through it — ci.yml (tests + the platform matrix),
release.yml, security.yml, evals.yml. It still fails loudly after four
attempts, so a genuinely broken lockfile is not disguised as a flake.

The Tests job also lacked the UV_HTTP_TIMEOUT / UV_HTTP_RETRIES the smoke
matrix has always set, which is part of why it was the one that kept
dying; it has them now.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test(ci): pin the Intel-Mac contract by intent, not by command spelling

test_ci_verifies_intel_mac_as_the_documented_remote_only_host asserted
the literal line `run: uv sync --extra pockettts`, so routing every sync
through scripts/uv-sync-retry.sh read as a broken Intel-Mac contract. The
contract it exists to protect is that the pockettts extra installs ONLY
on backend_supported legs — which the regex now pins, while leaving how
the sync is invoked free to change.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* ci: keep every uv run out of the resolver, and bound the retry budget

CodeRabbit, #1517:

- `uv run` re-resolves before running, so the smoke suite, the
  worker-artifact tests, the release test run and the eval run were each
  a fresh chance to hit the flaky direct-URL fetch outside the retry
  loop. All of them pass --no-sync now; the environment is already
  synced by the step that owns the retries. security.yml's
  `uv run --with pip-audit` is deliberately left alone — it layers an
  ephemeral package rather than running the project's own tests.
- The retry count multiplied uv's own budget (UV_HTTP_RETRIES=5 with a
  120 s timeout on the smoke matrix). Three attempts and 60 s of total
  backoff outlast the refusals actually observed while staying well
  inside the jobs' timeout-minutes.
- The Intel-Mac contract test pinned the smoke command literally too, so
  --no-sync tripped it exactly like the sync line did. Same fix: assert
  the contract (smoke runs only on backend_supported legs), not its
  spelling.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-12 23:29:19 +00:00

395 lines
16 KiB
Python
Executable File

#!/usr/bin/env python3
"""Re-render the demo bundle using the real VoiceStudio TTS engine.
This is the production-quality counterpart to scripts/build_demos.sh, which
uses macOS `say` to bootstrap the demo bundle. Run this once on a machine
with VoiceStudio model weights cached (typically your dev box) to replace the
`say`-rendered placeholders with engine output. Commit the resulting WAVs.
Prerequisites:
* The project's .venv exists and is activated (`uv sync`).
* VoiceStudio model weights cached under $HF_HUB_CACHE (the first
`model.generate()` call will download them otherwise — ~5 GB).
* Run from the repo root: `python3 scripts/render_demos_omnivoice.py`.
What it produces:
* backend/assets/samples/demo_voice.wav (clone reference)
* backend/assets/samples/demo_clone_output.wav (clone pre-rendered)
* backend/assets/samples/voice_design/demo_voice_design_<slug>.wav (7)
* Updated manifest with rendered_by="omnivoice@<git_sha>"
* backend/assets/samples/dictation/*.wav (3 replay scripts)
Not regenerated by this script:
* backend/assets/demo/dubbing/*.mp4 — see scripts/build_dub_demo.sh.
Dictation used to be excluded here on the grounds that `say` was good enough
and engine TTS was overkill. That reasoning only held on macOS: `say` does not
exist on Linux or Windows, so on every other platform the samples simply were
never rendered and the replay buttons pointed at 404s. The engine runs
everywhere the app does, which makes it the portable answer.
Reproducibility:
* seed=42 fixed across all renders so re-running the script regenerates
the same audio byte-for-byte (modulo torch nondeterminism).
"""
from __future__ import annotations
import argparse
import json
import os
import subprocess
import sys
import time
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
BACKEND_DIR = REPO_ROOT / "backend"
SAMPLES_DIR = BACKEND_DIR / "assets" / "samples"
VOICE_DESIGN_DIR = SAMPLES_DIR / "voice_design"
DICTATION_DIR = SAMPLES_DIR / "dictation"
# Make `backend/` importable so we can pull personalities + the engine.
sys.path.insert(0, str(BACKEND_DIR))
# Cloning demo — must match scripts/build_demos.sh exactly so the manifest
# stays in sync with what the bootstrap script produced.
CLONE_REF_TEXT = (
"Hey. I'm the VoiceStudio demo voice. I was made right here, on your "
"machine: private, local, and ready whenever you are."
)
CLONE_OUTPUT_TEXT = (
"Welcome aboard. I was just a three-second clip a moment ago. Now I can "
"say anything you'd like, in your voice or mine."
)
# Original demo identity: a warm, smoky cinematic alto expressed only through
# OmniVoice's supported taxonomy. It is deliberately not based on, trained on,
# or named after a real performer.
CLONE_VOICE_INSTRUCT = "female, young adult, low pitch, american accent"
CLONE_RENDER_STEPS = 48
# Dictation replay scripts. `text` MUST match SCRIPTS in
# frontend/src/components/DictationDemo.jsx verbatim — the card shows that
# string as "what you would say" and then shows what the recogniser heard, so
# any drift between the two reads as a transcription error.
DICTATION_SCRIPTS = [
{
"id": "en_conversational",
"language": "English",
"instruct": "female, young adult, moderate pitch, american accent",
"text": (
"Schedule a meeting with Pat for Tuesday at three PM and remind me "
"to bring the quarterly report."
),
},
{
"id": "en_technical",
"language": "English",
"instruct": "male, young adult, moderate pitch, american accent",
"text": (
"Patch the WebGPU shader in renderer.tsx, then bump pnpm to nine "
"point fifteen and rerun the Vitest suite."
),
},
{
"id": "fr_reservation",
"language": "French",
"instruct": "female, young adult, moderate pitch",
"text": "Bonjour, je voudrais réserver une table pour deux personnes à vingt heures.",
},
]
def _git_sha() -> str:
try:
return subprocess.check_output(
["git", "rev-parse", "--short", "HEAD"],
cwd=REPO_ROOT, text=True,
).strip()
except Exception:
return "unknown"
def watermark_file(path: Path, sample_rate: int, *, context: str) -> None:
"""Stamp the provenance watermark into an already-written WAV (#1169).
These clips ship inside the app and play back to users as VoiceStudio
output, so they are synthetic audio leaving the app exactly like a
generated clip — they go through `mark_synthetic`, the one chokepoint
every producing route uses. `force=True` because a render script runs
outside the request path that carries the user's watermark preference.
It runs on the FILE, after loudness normalization, rather than on the
tensor before it: `loudnorm` re-encodes what it is given, and marking
first would put the watermark through a gain and true-peak limiter on
its way to disk.
"""
import torch
import torchaudio
from services.watermark import mark_synthetic
waveform, rate = torchaudio.load(str(path))
marked = mark_synthetic(waveform, rate, context=context, force=True)
if marked is waveform and os.environ.get("OMNIVOICE_DEMO_ALLOW_UNMARKED") == "1":
print(f" ! {path.name} is NOT watermarked (OMNIVOICE_DEMO_ALLOW_UNMARKED=1)")
return
if marked is waveform:
# `mark_synthetic` never raises — it degrades, so generation can't be
# broken by watermarking. A RENDER SCRIPT is the one caller where that
# is wrong: it exists to produce files a human then commits, and a
# warning on a scrolling console is not a gate. Fail, so the unmarked
# file cannot be mistaken for a finished asset.
raise RuntimeError(
f"{path.name} could not be watermarked, so it must not be committed. "
"AudioSeal is missing or its weights are not cached on this machine "
"(`uv sync --all-extras`, then re-run with the model cache warm). "
"Set OMNIVOICE_DEMO_ALLOW_UNMARKED=1 only for a local listen — never "
"for a render you intend to commit."
)
torchaudio.save(
str(path),
marked.to(torch.float32),
rate,
encoding="PCM_S", bits_per_sample=16,
)
def _save_wav(audio_tensor, sample_rate: int, out_path: Path):
"""Save a torch tensor (C, T) or (T,) to a 16-bit PCM WAV."""
import torch
import torchaudio
if audio_tensor.dim() == 1:
audio_tensor = audio_tensor.unsqueeze(0)
# Ensure mono — most VoiceStudio outputs are mono already.
if audio_tensor.shape[0] > 1:
audio_tensor = audio_tensor.mean(dim=0, keepdim=True)
out_path.parent.mkdir(parents=True, exist_ok=True)
# Peak-normalize for headroom only; perceived level is set by the loudness
# pass below. Peak alone is not enough on its own: diffusion TTS emits the
# occasional single-sample transient, and one click is all it takes to hold
# the rest of the clip down — the Helpdesk preset landed at -30 dB RMS
# against -17 dB for its neighbours that way, so a preview row played at
# wildly different volumes depending on which preset you clicked.
peak = audio_tensor.abs().max().item()
if peak > 0:
audio_tensor = audio_tensor / peak * 0.97
torchaudio.save(
str(out_path),
audio_tensor.to(torch.float32),
sample_rate,
encoding="PCM_S", bits_per_sample=16,
)
_normalize_loudness(out_path, sample_rate)
watermark_file(out_path, sample_rate, context=f"demo:{out_path.stem}")
# Preview clips are played back to back in a picker, so they have to sit at the
# same perceived level — which peak normalization does not give you: a clip
# whose speech is quiet under one loud transient normalizes to the same peak as
# a clip that is loud throughout, and plays 12 dB softer. EBU R128 measures
# loudness rather than amplitude, and its true-peak ceiling keeps the transient
# legal without pulling the body of the clip down with it.
#
# ffmpeg is already a documented prerequisite of the demo tooling (see
# scripts/build_demos.sh). If it is missing, the raw render is still written and
# usable — the clips are simply not level-matched, which is a cosmetic loss, not
# a broken asset.
_LOUDNESS_TARGET_LUFS = -18.0
_TRUE_PEAK_CEILING_DBTP = -1.5
def _normalize_loudness(path: Path, sample_rate: int) -> None:
import shutil
import tempfile
if shutil.which("ffmpeg") is None:
print(f" ! ffmpeg not found — {path.name} left un-levelled")
return
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as handle:
tmp = Path(handle.name)
try:
result = subprocess.run(
[
"ffmpeg", "-y", "-loglevel", "error", "-i", str(path),
"-af",
f"loudnorm=I={_LOUDNESS_TARGET_LUFS}:TP={_TRUE_PEAK_CEILING_DBTP}:LRA=11",
# `loudnorm` resamples to 192 kHz internally for true-peak
# measurement and will happily WRITE at 192 kHz if the output
# rate is not pinned — which turned 2.1 MB of previews into
# 17.5 MB of identical-sounding audio the first time this ran.
"-ar", str(sample_rate),
"-c:a", "pcm_s16le", str(tmp),
],
capture_output=True, text=True,
)
if result.returncode != 0 or not tmp.exists() or tmp.stat().st_size == 0:
print(f" ! loudnorm failed for {path.name}: {result.stderr.strip()[:120]}")
return
# os.replace, not shutil.move: `path` already exists, so move delegates
# to os.rename — which raises FileExistsError on Windows and fails the
# render there. os.replace overwrites atomically on every platform.
os.replace(str(tmp), str(path))
finally:
tmp.unlink(missing_ok=True)
def render_cloning(model, args):
"""Render the cloning demo: reference clip + pre-rendered output.
The reference clip is itself synthesized — chicken-and-egg, but the
VoiceStudio engine in non-zero-shot mode (no ref_audio) accepts a plain
`instruct=` taxonomy string and produces a clean voice.
"""
print("── Cloning demo ─────────────────────────────────────")
sr = getattr(model, "sampling_rate", 24000)
from omnivoice.utils.common import fix_random_seed
# 1) Reference clip — synthesized as an original cinematic alto so the
# timbre is distinctive while remaining reproducible and rights-safe.
out_ref = SAMPLES_DIR / "demo_voice.wav"
if args.skip_existing and out_ref.exists():
print(f" · skip (exists): {out_ref.name}")
else:
fix_random_seed(42)
audios = model.generate(
text=CLONE_REF_TEXT,
instruct=CLONE_VOICE_INSTRUCT,
num_step=CLONE_RENDER_STEPS,
)
_save_wav(audios[0], sr, out_ref)
print(f" ✓ {out_ref.name} ({sr} Hz, omnivoice)")
# 2) Pre-rendered clone output — same voice, different text. Use the
# reference clip we just rendered as the speaker reference so this
# actually demonstrates cloning rather than independent synthesis.
out_clone = SAMPLES_DIR / "demo_clone_output.wav"
if args.skip_existing and out_clone.exists():
print(f" · skip (exists): {out_clone.name}")
else:
fix_random_seed(42)
audios = model.generate(
text=CLONE_OUTPUT_TEXT,
ref_audio=str(out_ref),
ref_text=CLONE_REF_TEXT,
num_step=CLONE_RENDER_STEPS,
)
_save_wav(audios[0], sr, out_clone)
print(f" ✓ {out_clone.name} ({sr} Hz, cloned)")
def render_voice_design(model, args):
"""Re-render the 7 voice-design preset previews."""
print("\n── Voice design presets ─────────────────────────────")
from core.personalities import PERSONALITIES
sr = getattr(model, "sampling_rate", 24000)
demos = [p for p in PERSONALITIES if p.get("is_demo")]
if not demos:
print(" No is_demo presets found in personalities.py")
return
for preset in demos:
slug = preset["id"]
out = VOICE_DESIGN_DIR / f"demo_voice_design_{slug}.wav"
if args.skip_existing and out.exists():
print(f" · skip (exists): {out.name}")
continue
audios = model.generate(
text=preset["script"],
instruct=preset["instruct"],
language=preset.get("language"),
num_step=24,
)
_save_wav(audios[0], sr, out)
print(f" ✓ {out.name} ({preset['name']})")
def render_dictation(model, args):
"""Render the three dictation replay clips.
The replay path posts these to /transcribe and shows the recognized text,
so the demo works without microphone permission — on a VM, in CI, or before
the user has granted access. That makes the clip content load-bearing: it
has to be what the card says it is, or the demo shows a mismatch.
"""
print("\n── Dictation replay clips ───────────────────────────")
sr = getattr(model, "sampling_rate", 24000)
for script in DICTATION_SCRIPTS:
out = DICTATION_DIR / f"{script['id']}.wav"
if args.skip_existing and out.exists():
print(f" · skip (exists): {out.name}")
continue
audios = model.generate(
text=script["text"],
instruct=script["instruct"],
language=script["language"],
num_step=32,
)
_save_wav(audios[0], sr, out)
print(f" ✓ {out.name} ({script['language']})")
def update_manifest(args):
"""Update the existing manifest with rendered_by + rendered_at."""
print("\n── Manifest ─────────────────────────────────────────")
mpath = SAMPLES_DIR / "demo" / "dubbing" / "manifest.json"
if not mpath.exists():
print(f" ! manifest not found at {mpath} — run scripts/build_dub_demo.sh first")
return
data = json.loads(mpath.read_text())
data["rendered_by"] = f"omnivoice@{_git_sha()}"
data["rendered_at"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
mpath.write_text(json.dumps(data, indent=2, ensure_ascii=False))
print(f" ✓ {mpath.name}")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--skip-existing", action="store_true",
help="Don't re-render files that already exist on disk.",
)
parser.add_argument(
"--only", choices=["cloning", "design", "dictation", "manifest"],
help="Render only a subset (default: all).",
)
args = parser.parse_args()
print("Loading VoiceStudio engine (this can take 30-60 s on first run)…")
try:
import asyncio
from services.model_manager import get_model
try:
asyncio.get_running_loop()
raise RuntimeError("Run this script outside an async context.")
except RuntimeError:
model = asyncio.run(get_model())
except Exception as e:
print(f"\nERROR: Could not load VoiceStudio engine: {e}\n")
print("Check that:")
print(" 1. You're running inside the project venv (uv sync first).")
print(" 2. The omnivoice package is importable: `python -c 'import omnivoice'`.")
print(" 3. Model weights are downloaded (~5 GB on first synthesis).")
sys.exit(1)
print("Engine loaded.\n")
if args.only in (None, "cloning"):
render_cloning(model, args)
if args.only in (None, "design"):
render_voice_design(model, args)
if args.only in (None, "dictation"):
render_dictation(model, args)
if args.only in (None, "manifest"):
update_manifest(args)
print("\nDone. Dubbing videos are built separately — scripts/build_dub_demo.sh.")
if __name__ == "__main__":
main()