Edit photos by describing the change, on your own GPU. A free, open-source alternative to Nano Banana and ChatGPT image editing: photos stay on your computer, no daily limit. One installer for Windows, Mac and Linux sets up the best open model (Qwen-Image 2.1). MCP server for AI agents.
TypeScript ai-photo-editorcomfyui-alternativegenerative-fillimage-editingimage-generationinpaintinglinuxlocal-aimacosmcpmcp-servernano-banana-alternativeofflinephoto-editorprivacyqwen-imageqwen-image-2-1stable-diffusion-cppvulkanwindows
"""Voiceover takes for the launch video, generated locally with Chatterbox (MIT, Resemble AI).
D:/AI/tts/.venv/Scripts/python gen_voice.py [take ...] -> voice/<take>.wav
D:/AI/tts/.venv/Scripts/python gen_voice.py --ref me.wav [take ...]
D:/AI/tts/.venv/Scripts/python gen_voice.py final -> voice/final/NN.wav (approved take)
D:/AI/tts/.venv/Scripts/python gen_voice.py redo 3 --keep 0:0,4:2 [--seeds 6]
-> voice/redo-<stamp>/: takes of line 3's new sentences
D:/AI/tts/.venv/Scripts/python gen_voice.py splice 3 --keep 0:0,4:2 --pick 1:13,2:22 --from voice/redo-<stamp>
-> line 3 rebuilt from kept and picked sentences
The "Kokon 0.10" update video has its own script (voiceover-0.10.md) and its own files, so the
launch take is never touched:
D:/AI/tts/.venv/Scripts/python gen_voice.py audition [--seeds 4]
-> voice/update-0.10/audition-<stamp>/: takes of every sentence
D:/AI/tts/.venv/Scripts/python gen_voice.py build --from voice/update-0.10/audition-<stamp> --pick 1.0:11,2.0:23,...
-> voice/update-0.10/final-<stamp>/NN.wav, copied to
video/public/voice-0.10 with video/src/app/voice-0.10.json
The "use cases" video (voiceover-usecases.md) works the same way, with a tone per line:
D:/AI/tts/.venv/Scripts/python gen_voice.py audition-usecases [--seeds 4]
D:/AI/tts/.venv/Scripts/python gen_voice.py build-usecases --from voice/usecases/audition-<stamp> --pick 1.0:11,...
redo/splice change single sentences of the approved take: `--keep NEW:OLD` reuses old sentence
OLD (cut from voice/final/raw by raw/spans.json) as new sentence NEW; the other sentences of the
line in voiceover.md get takes to audition, and `--pick NEW:SEED` chooses one each (the auditioned file itself). splice backs
voice/final up first and matches the line's loudness to the old file.
Each take reads the whole script (voiceover.md, one file per line too, in voice/<take>/NN.wav)
so takes can be compared on the same text. Without --ref the model's built-in voice is used;
--ref clones the timbre of a clean recording of at least 5-10 s (only use a voice you have the
right to use, e.g. your own).
"""
import argparse
import json
import re
import shutil
import subprocess
import time
from pathlib import Path
import numpy as np
import soundfile as sf
import torch
import torchaudio.functional as F
HERE = Path(__file__).parent
OUT = HERE / "voice"
VIDEO = HERE.parent / "video"
SR = 24000
PAUSE = 0.9
# name: (model, exaggeration, cfg_weight, temperature)
TAKES = {
"calm": ("base", 0.35, 0.5, 0.7),
"warm": ("base", 0.5, 0.4, 0.8),
"expressive": ("base", 0.7, 0.3, 0.8),
"turbo": ("turbo", 0.0, 0.0, 0.8),
"calmer": ("base", 0.55, 0.2, 0.8),
# The "use cases" video's tones (voiceover-usecases.md, column Tone). "playful" is Chatterbox
# Turbo, which reads paralinguistic tags ([chuckle], [happy], [laugh]...); it has no voice of its
# own here, so it clones the launch voice from TURBO_REF.
"punchy": ("base", 0.75, 0.35, 0.8),
"playful": ("turbo", 0.0, 0.0, 0.8),
"tender": ("base", 0.3, 0.45, 0.65),
"intimate": ("base", 0.25, 0.5, 0.55),
# The 0.12 end line, slower (Gabriele: the first takes too fast): low exaggeration and cfg read slowest.
"slow": ("base", 0.3, 0.2, 0.7),
}
# A clean line of the launch take (12.8 s), the voice Turbo copies.
TURBO_REF = HERE.parent / "video" / "public" / "voice" / "03.wav"
# The approved take (`gen_voice.py final`), picked by ear line by line. Each line is read one
# sentence at a time (seed + i for sentence i) with a pause in between, which slows the
# delivery down. Per line: (take, seed[, {sentence index: (take, seed)}[, pauses]]).
FINAL = {
1: ("calmer", 61),
2: ("calmer", 11),
3: ("expressive", 11),
4: ("expressive", 11),
5: ("calmer", 11),
6: ("expressive", 31),
7: ("expressive", 21),
8: ("calmer", 11),
9: ("expressive", 31, {2: ("calmer", 71)}, [0.4, 0.55, 0.55]),
}
SENTENCE_PAUSE = 0.4
# Light voice-over master: rumble cut, de-ess, gentle compression. Loudness is set afterwards
# with one gain for all lines, so their relative levels stay as read.
MASTER = "aresample=48000,highpass=f=80,deesser=i=0.4,acompressor=threshold=-20dB:ratio=3:attack=5:release=90"
LUFS = -16.0
def script_rows(script: str) -> list[list[str]]:
rows = [l for l in (HERE / script).read_text(encoding="utf-8").splitlines() if re.match(r"^\| \d+ \|", l)]
return [[c.strip() for c in r.split("|")] for r in rows]
def script_lines(script: str = "voiceover.md") -> list[str]:
return [r[4] for r in script_rows(script)]
_models: dict = {}
def model(kind: str):
if kind not in _models:
if kind == "turbo":
from chatterbox.tts_turbo import ChatterboxTurboTTS as M
else:
from chatterbox.tts import ChatterboxTTS as M
_models[kind] = M.from_pretrained(device="cuda")
return _models[kind]
def say(take: str, text: str, ref: Path | None, seed: int) -> np.ndarray:
kind, exag, cfg, temp = TAKES[take]
m = model(kind)
torch.manual_seed(seed)
if kind == "turbo" and ref is None:
ref = TURBO_REF
kw = dict(audio_prompt_path=str(ref) if ref else None, temperature=temp)
if kind == "base":
kw.update(exaggeration=exag, cfg_weight=cfg)
with torch.inference_mode():
wav = m.generate(text, **kw)
return F.resample(wav, m.sr, SR).squeeze(0).cpu().numpy()
def read(take: str, ref: Path | None, seed: int) -> None:
lines = script_lines()
gap = np.zeros(int(PAUSE * SR), dtype=np.float32)
folder = OUT / take
folder.mkdir(parents=True, exist_ok=True)
parts = []
for i, line in enumerate(lines, 1):
a = say(take, line, ref, seed + i)
sf.write(folder / f"{i:02d}.wav", a, SR)
parts += [a, gap]
audio = np.concatenate(parts).astype(np.float32)
audio /= max(1e-6, np.abs(audio).max()) / 0.89
sf.write(OUT / f"{take}.wav", audio, SR)
print(f"{take}.wav {audio.size / SR:.1f} s", flush=True)
def sentences(line: str) -> list[str]:
return [s for s in re.split(r"(?<=[.?!])\s+", line) if s]
def master(src: Path, dst: Path, gain_db: float = 0.0) -> None:
# alimiter at -1.5 dBFS: the loudness gain would otherwise clip the peaks.
af = MASTER if gain_db == 0 else f"volume={gain_db:.2f}dB,alimiter=limit=0.84:attack=5:release=50:level=0"
subprocess.run(["ffmpeg", "-v", "error", "-y", "-i", str(src), "-af", af, str(dst)], check=True)
def lufs(path: Path) -> float:
r = subprocess.run(["ffmpeg", "-hide_banner", "-i", str(path), "-af", "ebur128", "-f", "null", "-"],
capture_output=True, text=True)
return float(re.findall(r"I:\s+(-?[\d.]+) LUFS", r.stderr)[-1])
def final() -> None:
"""voice/final/NN.wav, one mastered file per line at -16 LUFS, plus final.wav to preview them in a row.
Also copies the lines to the video (public/voice/NN.wav) with src/app/voice.json: each line's
length and where each sentence starts and ends in it, so scenes can place single sentences.
"""
folder = OUT / "final"
(folder / "raw").mkdir(parents=True, exist_ok=True)
spans: dict[int, dict] = {}
for n, line in enumerate(script_lines(), 1):
spec = FINAL[n]
take, seed = spec[:2]
swap = spec[2] if len(spec) > 2 else {}
pauses = spec[3] if len(spec) > 3 else None
parts = sentences(line)
pauses = pauses or [SENTENCE_PAUSE] * (len(parts) - 1)
chunks, marks, pos = [], [], 0
for i, s in enumerate(parts):
t, sd = swap.get(i, (take, seed + i))
chunks.append(say(t, s, None, sd))
marks.append([round(pos / SR, 3), round((pos + chunks[-1].size) / SR, 3)])
pos += chunks[-1].size
if i < len(pauses):
chunks.append(np.zeros(int(pauses[i] * SR), dtype=np.float32))
pos += chunks[-1].size
spans[n] = {"dur": round(pos / SR, 3), "sentences": marks}
raw = folder / "raw" / f"{n:02d}.wav"
sf.write(raw, np.concatenate(chunks), SR)
master(raw, folder / f"{n:02d}.wav")
print(f"{n:02d} {sum(c.size for c in chunks) / SR:4.1f} s {line}", flush=True)
files = [folder / f"{n:02d}.wav" for n in FINAL]
gap = np.zeros(int(PAUSE * 48000), dtype=np.float32)
preview = np.concatenate([x for f in files for x in (sf.read(f, dtype="float32")[0], gap)])
sf.write(folder / "final.wav", preview, 48000)
gain = LUFS - lufs(folder / "final.wav")
for f in [*files, folder / "final.wav"]:
tmp = f.with_suffix(".tmp.wav")
master(f, tmp, gain)
tmp.replace(f)
print(f"final.wav {preview.size / 48000:.1f} s, gain {gain:+.1f} dB -> {lufs(folder / 'final.wav'):.1f} LUFS")
(VIDEO / "public" / "voice").mkdir(parents=True, exist_ok=True)
for f in files:
shutil.copy(f, VIDEO / "public" / "voice" / f.name)
(VIDEO / "src" / "app" / "voice.json").write_text(json.dumps(spans, indent=1) + "\n", encoding="utf-8")
print("copied to video/public/voice, timings in video/src/app/voice.json")
def pairs(arg: str | None) -> dict[int, int]:
return {int(a): int(b) for a, b in (p.split(":") for p in arg.split(","))} if arg else {}
def redo(n: int, keep: dict[int, int], seeds: int, take: str, base: int) -> None:
"""Takes of the sentences of line `n` that are not kept, for picking by ear."""
parts = sentences(script_lines()[n - 1])
folder = OUT / f"redo-{time.strftime('%Y%m%d-%H%M%S')}"
folder.mkdir(parents=True)
for i, text in enumerate(parts):
if i in keep:
continue
for k in range(seeds):
seed = base + 10 * i + k
a = say(take, text, None, seed)
raw = folder / f"{n:02d}-s{i}-seed{seed}.raw.wav"
sf.write(raw, a, SR)
master(raw, folder / f"{n:02d}-s{i}-seed{seed}.wav")
print(f"{n:02d} s{i} seed {seed} {a.size / SR:4.1f} s {text}", flush=True)
print(f"takes ({take}) in {folder}")
def splice(n: int, keep: dict[int, int], pick: dict[int, int], src: Path) -> None:
"""Line `n` rebuilt: kept sentences cut from the approved raw file, picked takes from `src`
(the raw files redo wrote), 0.4 s between sentences, mastered to the old file's loudness."""
parts = sentences(script_lines()[n - 1])
missing = [i for i in range(len(parts)) if i not in keep and i not in pick]
if missing:
raise SystemExit(f"no take picked for sentences {missing}")
folder = OUT / "final"
spans_file = folder / "raw" / "spans.json"
spans = json.loads(spans_file.read_text(encoding="utf-8"))
old_raw, sr = sf.read(folder / "raw" / f"{n:02d}.wav", dtype="float32")
assert sr == SR
backup = OUT / f"final-backup-{time.strftime('%Y%m%d-%H%M%S')}"
shutil.copytree(folder, backup)
target = lufs(folder / f"{n:02d}.wav")
chunks, marks, pos = [], [], 0
for i, text in enumerate(parts):
if i in keep:
a, b = spans[str(n)]["sentences"][keep[i]]
c = old_raw[int(round(a * SR)):int(round(b * SR))]
else:
c, sr = sf.read(src / f"{n:02d}-s{i}-seed{pick[i]}.raw.wav", dtype="float32")
assert sr == SR
chunks.append(c)
marks.append([round(pos / SR, 3), round((pos + c.size) / SR, 3)])
pos += c.size
if i < len(parts) - 1:
chunks.append(np.zeros(int(SENTENCE_PAUSE * SR), dtype=np.float32))
pos += chunks[-1].size
raw = folder / "raw" / f"{n:02d}.wav"
sf.write(raw, np.concatenate(chunks), SR)
spans[str(n)] = {"dur": round(pos / SR, 3), "sentences": marks}
spans_file.write_text(json.dumps(spans, indent=1) + "\n", encoding="utf-8")
tmp = folder / f"{n:02d}.tmp.wav"
master(raw, tmp)
master(tmp, folder / f"{n:02d}.wav", target - lufs(tmp))
tmp.unlink()
shutil.copy(folder / f"{n:02d}.wav", VIDEO / "public" / "voice" / f"{n:02d}.wav")
vj = VIDEO / "src" / "app" / "voice.json"
video = json.loads(vj.read_text(encoding="utf-8"))
video[str(n)] = spans[str(n)]
vj.write_text(json.dumps(video, indent=1) + "\n", encoding="utf-8")
print(f"{n:02d} {pos / SR:4.1f} s at {lufs(folder / f'{n:02d}.wav'):.1f} LUFS (was {target:.1f}); backup in {backup}")
# The "Kokon 0.10" update video (voiceover-0.10.md): each line's model settings, as in FINAL.
UPDATE = {"script": "voiceover-0.10.md", "dir": OUT / "update-0.10", "public": "voice-0.10", "json": "voice-0.10.json"}
UPDATE_TAKES = {1: "calmer", 2: "expressive", 3: "expressive", 4: "expressive", 5: "calmer", 6: "expressive"}
# build: seconds between a line's sentences where not 0.4 (line 3, Prompt+'s model, read slower),
# and silences put inside a sentence ("LINE.SENTENCE": [(seconds into the take, seconds)]): the
# micro-pause in "One model, two jobs." falls in the take's own silence before "two".
UPDATE_PAUSES = {3: 0.7}
UPDATE_SPLITS = {"3.0": [(0.93, 0.35)]}
# The "use cases" video (voiceover-usecases.md): each line's tone is in the script's Tone column.
USECASES = {"script": "voiceover-usecases.md", "dir": OUT / "usecases", "public": "voice-usecases", "json": "voice-usecases.json"}
USECASES_TONES = {n: r[3] for n, r in enumerate(script_rows(USECASES["script"]), 1)}
# The "Kokon 0.11" video (voiceover-0.11.md): the 0.10 voice, each sentence in both of its tones
# (seeds k of the second tone are offset by 5, so every take keeps its own file name).
V011 = {"script": "voiceover-0.11.md", "dir": OUT / "update-0.11", "public": "voice-0.11", "json": "voice-0.11.json"}
V011_TONES = {n: ["calmer", "expressive"] for n in range(1, len(script_rows(V011["script"])) + 1)}
# The "Kokon 0.12" video (voiceover-0.12.md): the same voice and tones as 0.11. Round 2 (line 20
# added) and round 3 (Gabriele's picks) go to their own folders; earlier files in public/ stay as they were.
V012 = {"script": "voiceover-0.12.md", "dir": OUT / "update-0.12", "public": "voice-0.12-v3", "json": "voice-0.12-v3.json"}
V012_TONES = {n: ["calmer", "expressive"] for n in range(1, len(script_rows(V012["script"])) + 1)}
V012_TONES[20] = ["slow", "calmer", "calm"]
# build-012: seconds between the end line's sentences (the default 0.4 made it sound rushed).
V012_PAUSES = {20: 0.8}
PROJECTS = {"update": (UPDATE, UPDATE_TAKES), "usecases": (USECASES, USECASES_TONES), "011": (V011, V011_TONES), "012": (V012, V012_TONES)}
# build-011: sentences cut into parts that the video places one by one ("LINE.SENTENCE": about
# where, in seconds into the take; each cut moves to the quietest 10 ms within 0.15 s). "Scan the
# code, / sign in once, / and add Kokon to your home screen." goes with three shots; "Pick a photo, /
# describe the change." gets a pause at the comma (the takes read it in one breath).
V011_CUTS = {"3.1": [0.95, 1.78], "4.0": [1.0]}
def quiet_cuts(c: np.ndarray, near: list[float]) -> list[int]:
"""Sample positions of the quietest 10 ms windows around each time in `near`."""
w, out = int(0.01 * SR), []
for t0 in near:
lo, hi = int((t0 - 0.15) * SR), int((t0 + 0.15) * SR)
best = min(range(lo, hi - w, w // 2), key=lambda i: float(np.abs(c[i:i + w]).mean()))
out.append(best + w // 2)
return out
def audition(seeds: int, base: int, project: str = "update", lines: set[int] | None = None, into: Path | None = None) -> None:
"""`seeds` takes of every sentence of a script, read one at a time, for picking by ear. `lines`:
only these lines; `into`: add the takes to an earlier audition folder (a changed line, or more
seeds of one: pass another --seed so the names don't collide)."""
proj, takes = PROJECTS[project]
folder = into or proj["dir"] / f"audition-{time.strftime('%Y%m%d-%H%M%S')}"
folder.mkdir(parents=True, exist_ok=into is not None)
for n, line in enumerate(script_lines(proj["script"]), 1):
if lines and n not in lines:
continue
tones = takes.get(n, "expressive")
tones = [tones] if isinstance(tones, str) else tones
for i, text in enumerate(sentences(line)):
for k, take in ((k + 5 * j, t) for j, t in enumerate(tones) for k in range(seeds)):
seed = base + 10 * i + k
a = say(take, text, None, seed)
raw = folder / f"{n:02d}-s{i}-seed{seed}.raw.wav"
sf.write(raw, a, SR)
master(raw, folder / f"{n:02d}-s{i}-seed{seed}.wav")
print(f"{n:02d} s{i} seed {seed} {take} {a.size / SR:4.1f} s {text}", flush=True)
print(f"takes in {folder}")
def build(src: Path, pick: dict[str, int], project: str = "update") -> None:
"""The update's lines from the picked sentence takes (`pick` "LINE.SENTENCE" -> seed), 0.4 s
between sentences (UPDATE_PAUSES, UPDATE_SPLITS), mastered like the launch take (-16 LUFS over all lines). Each build goes to
a new folder; the video gets the files and their sentence spans."""
proj = PROJECTS[project][0]
pauses = UPDATE_PAUSES if project == "update" else V012_PAUSES if project == "012" else {}
splits = UPDATE_SPLITS if project == "update" else {}
lines = script_lines(proj["script"])
missing = [f"{n}.{i}" for n, l in enumerate(lines, 1) for i in range(len(sentences(l))) if f"{n}.{i}" not in pick]
if missing:
raise SystemExit(f"no take picked for {', '.join(missing)}")
folder = proj["dir"] / f"final-{time.strftime('%Y%m%d-%H%M%S')}"
(folder / "raw").mkdir(parents=True)
spans: dict[int, dict] = {}
files = []
for n, line in enumerate(lines, 1):
chunks, marks, pos = [], [], 0
parts = sentences(line)
for i in range(len(parts)):
c, sr = sf.read(src / f"{n:02d}-s{i}-seed{pick[f'{n}.{i}']}.raw.wav", dtype="float32")
assert sr == SR
for t, gap in sorted(splits.get(f"{n}.{i}", []), reverse=True):
c = np.concatenate([c[:int(t * SR)], np.zeros(int(gap * SR), dtype=np.float32), c[int(t * SR):]])
cuts = quiet_cuts(c, V011_CUTS.get(f"{n}.{i}", [])) if project == "011" else []
for a, b in zip([0, *cuts], [*cuts, c.size]):
if a:
chunks.append(np.zeros(int(SENTENCE_PAUSE * SR), dtype=np.float32))
pos += chunks[-1].size
chunks.append(c[a:b])
marks.append([round(pos / SR, 3), round((pos + b - a) / SR, 3)])
pos += b - a
if i < len(parts) - 1:
chunks.append(np.zeros(int(pauses.get(n, SENTENCE_PAUSE) * SR), dtype=np.float32))
pos += chunks[-1].size
spans[n] = {"dur": round(pos / SR, 3), "sentences": marks}
raw = folder / "raw" / f"{n:02d}.wav"
sf.write(raw, np.concatenate(chunks), SR)
master(raw, folder / f"{n:02d}.wav")
files.append(folder / f"{n:02d}.wav")
print(f"{n:02d} {pos / SR:4.1f} s {line}", flush=True)
gap = np.zeros(int(PAUSE * 48000), dtype=np.float32)
preview = np.concatenate([x for f in files for x in (sf.read(f, dtype="float32")[0], gap)])
sf.write(folder / "all.wav", preview, 48000)
gain = LUFS - lufs(folder / "all.wav")
for f in [*files, folder / "all.wav"]:
tmp = f.with_suffix(".tmp.wav")
master(f, tmp, gain)
tmp.replace(f)
(folder / "picks.json").write_text(json.dumps({"from": str(src), "pick": pick}, indent=1) + "\n", encoding="utf-8")
dst = VIDEO / "public" / proj["public"]
dst.mkdir(parents=True, exist_ok=True)
for f in files:
shutil.copy(f, dst / f.name)
(VIDEO / "src" / "app" / proj["json"]).write_text(json.dumps(spans, indent=1) + "\n", encoding="utf-8")
print(f"{folder}: all.wav {preview.size / 48000:.1f} s at {lufs(folder / 'all.wav'):.1f} LUFS; copied to video/public/{proj['public']}, spans in video/src/app/{proj['json']}")
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("takes", nargs="*", default=list(TAKES))
ap.add_argument("--ref", type=Path)
ap.add_argument("--seed", type=int, default=11)
ap.add_argument("--keep", help="redo/splice: NEW:OLD sentence pairs reused from the approved take")
ap.add_argument("--pick", help="splice: NEW:SEED, the take chosen for each new sentence")
ap.add_argument("--seeds", type=int, default=6, help="redo: takes per new sentence")
ap.add_argument("--take", help="redo: model settings (default: the line's approved take in FINAL)")
ap.add_argument("--from", dest="src", type=Path)
ap.add_argument("--lines", help="audition: only these lines, e.g. 4,6")
ap.add_argument("--into", type=Path, help="audition: add to this audition folder")
a = ap.parse_args()
for proj in PROJECTS:
suffix = "" if proj == "update" else f"-{proj}"
if a.takes[:1] == [f"audition{suffix}"]:
raise SystemExit(audition(a.seeds, a.seed, proj, {int(x) for x in a.lines.split(",")} if a.lines else None, a.into))
if a.takes[:1] == [f"build{suffix}"]:
raise SystemExit(build(a.src, {k: int(v) for k, v in (p.split(":") for p in a.pick.split(","))}, proj))
if a.takes[:1] == ["redo"]:
n = int(a.takes[1])
raise SystemExit(redo(n, pairs(a.keep), a.seeds, a.take or FINAL[n][0], a.seed))
if a.takes[:1] == ["splice"]:
raise SystemExit(splice(int(a.takes[1]), pairs(a.keep), pairs(a.pick), a.src))
for t in a.takes:
final() if t == "final" else read(t, a.ref, a.seed)