// settings.voice — the voice ops (release 39): enhance and convert a speech // recording (lib/voiceApps.ts; controller/voiceFile.ts; `pnpm ops // voice-enhance` / `voice-convert`). // // Pure: the type, its defaults, the sanitizer settingsSchema.ts runs it // through, the per-field docs SETTINGS.md renders, and the resolver that turns // a blank `home` into paths.voiceHome. import os from "node:os"; import path from "node:path"; import type { FieldDocs } from "./fieldDocs"; import type { Paths } from "./paths"; import { DEFAULT_VOICE_ENGINE, DEFAULT_VOICE_TARGET, VOICE_ENGINES, DEFAULT_VOICE_PITCH, VOICE_PITCH_MODES, VOICE_SEMITONES_MAX, getVoiceEngine, parseStockTarget, type VoiceOp, type VoicePitch, } from "./voiceApps"; export type VoiceEnhanceSettings = { engine: string }; export type VoiceConvertSettings = { engine: string; target: string; pitch: VoicePitch; semitones: number }; export type VoiceSettings = { home: string; threads: number; enhance: VoiceEnhanceSettings; convert: VoiceConvertSettings; // Per engine id: the memory its job declares to the heavy slot, in MB. heavyEstimateMB: Record; }; // An engine's own estimate, and — for one that can follow the input's pitch on // another model — that run's, under the engine's `pitch.estimateKey`. export const DEFAULT_VOICE_HEAVY_ESTIMATES: Record = Object.fromEntries( Object.values(VOICE_ENGINES).flatMap((e) => [ [e.id, e.defaultEstimateMB] as const, ...(e.pitch ? [[e.pitch.estimateKey, e.pitch.defaultEstimateMB] as const] : []), ]), ); export const DEFAULT_VOICE_SETTINGS: VoiceSettings = { home: "", threads: 4, enhance: { engine: DEFAULT_VOICE_ENGINE.enhance }, convert: { engine: DEFAULT_VOICE_ENGINE.convert, target: DEFAULT_VOICE_TARGET, ...DEFAULT_VOICE_PITCH }, heavyEstimateMB: DEFAULT_VOICE_HEAVY_ESTIMATES, }; export const VOICE_SETTINGS_FIELD_DOCS: FieldDocs = { home: "Where `archilyzer voice setup` installs the voice engines, one directory per engine " + "(`/deepfilternet`: its binary; `/openvoice`, `/seed-vc`: a python venv, " + "a pinned checkout and the weights), and where the stock targets' reference clips are kept " + "(`/targets/`). Blank = `~/.local/share/archilyzer/voice`. Absolute, or `~/`-relative.", threads: "CPU threads a torch engine (openvoice, seed-vc) may use. 1–16.", enhance: "The enhance op's default engine. See VoiceEnhanceSettings (`voice.enhance` below).", convert: "The convert op's default engine, target and pitch. See VoiceConvertSettings (`voice.convert` below).", heavyEstimateMB: "Per engine id: the memory (MB) its job is admitted to the machine's heavy slot with " + "(`queue-lock --heavy --estimate`) — it runs only when that much is free above the floor. " + "The defaults are the 2026-11-03 bake-off's measured peaks plus a margin; `seed-vc-f0` is seed-vc's " + "run when it follows the input's pitch (`pitch: source | contour` — another, bigger model; measured " + "2026-11-11 over a take long enough to fill a 30 s window: 5.8 GB). An engine left out " + "takes its default; 0 declares nothing (the job then runs ALONE in the slot).", }; export const VOICE_ENHANCE_SETTINGS_FIELD_DOCS: FieldDocs = { engine: "The engine `voice-enhance` uses when the request names none: `deepfilternet` (DeepFilterNet 3, " + "MIT/Apache-2.0, CPU, its static binary — `archilyzer voice setup --engine deepfilternet`) or " + "`rnnoise` (ffmpeg's arnndn with a model shipped in the repo: nothing to install, cruder). " + "An unknown id falls back to the default.", }; export const VOICE_CONVERT_SETTINGS_FIELD_DOCS: FieldDocs = { engine: "The engine `voice-convert` uses when the request names none: `openvoice` (OpenVoice v2's " + "tone-colour converter, MIT, light) or `seed-vc` (Seed-VC v1, GPL-3.0, a closer timbre match, " + "slower). Both run on the CPU; `archilyzer voice setup --engine ` installs one. An unknown id " + "falls back to the default.", target: "The stock synthetic voice a conversion lands on when the request names none: " + "`:` (`orpheus:zac`, `orpheus:leah`, `kokoro:am_michael`, …). Never a " + "file or a recording — no real person's voice is a target. Its reference clip is synthesized " + "once through the `tts` op and kept under `home`. Anything else falls back to `orpheus:zac`.", pitch: "The pitch a conversion lands on when the request names none — for an engine that can follow the " + "input's (seed-vc, with its optional f0 model: `archilyzer voice setup --engine seed-vc --with f0`; " + "openvoice always lands on the target's). `target`: the stock voice's own pitch — the read's " + "intonation is flattened to it (what there was before). `source`: the pitch the take was read at — " + "raise your voice and the result rises. `contour`: the read's rises and falls, moved as a whole so " + "their median sits on the stock voice's. Anything else falls back to `target`.", semitones: "Semitones a FOLLOWED pitch (`source`, `contour`) is moved by when the request names none: -12 to 12, " + "0 = as followed. Ignored for `target`. It applies to a request that names its own `pitch` too: " + "`{\"pitch\": \"source\"}` with no `semitones` is moved by this many — send `\"semitones\": 0` for none.", }; // "" (the default), an absolute path, or a `~/` one; anything else is "". function dirSetting(value: unknown): string { if (typeof value !== "string") return ""; const s = value.trim(); if (s === "" || path.isAbsolute(s) || s.startsWith("~/")) return s; return ""; } function engineFor(op: VoiceOp, value: unknown): string { const e = getVoiceEngine(typeof value === "string" ? value.trim() : undefined); return e && e.op === op ? e.id : DEFAULT_VOICE_ENGINE[op]; } export function sanitizeVoice(value: unknown): VoiceSettings { const r = value && typeof value === "object" ? (value as Record) : {}; const en = r.enhance && typeof r.enhance === "object" ? (r.enhance as Record) : {}; const cv = r.convert && typeof r.convert === "object" ? (r.convert as Record) : {}; const target = parseStockTarget(cv.target); const est = r.heavyEstimateMB && typeof r.heavyEstimateMB === "object" ? (r.heavyEstimateMB as Record) : {}; const heavyEstimateMB: Record = {}; for (const [id, d] of Object.entries(DEFAULT_VOICE_HEAVY_ESTIMATES)) { const v = est[id]; heavyEstimateMB[id] = typeof v === "number" && Number.isFinite(v) ? Math.min(65536, Math.max(0, Math.round(v))) : d; } const threads = typeof r.threads === "number" && Number.isFinite(r.threads) ? Math.round(r.threads) : DEFAULT_VOICE_SETTINGS.threads; return { home: dirSetting(r.home), threads: Math.min(16, Math.max(1, threads)), enhance: { engine: engineFor("enhance", en.engine) }, convert: { engine: engineFor("convert", cv.engine), target: target.ok ? target.value.id : DEFAULT_VOICE_TARGET, pitch: VOICE_PITCH_MODES.includes(cv.pitch as VoicePitch) ? (cv.pitch as VoicePitch) : DEFAULT_VOICE_PITCH.pitch, semitones: typeof cv.semitones === "number" && Number.isFinite(cv.semitones) ? Math.min(VOICE_SEMITONES_MAX, Math.max(-VOICE_SEMITONES_MAX, Math.round(cv.semitones * 100) / 100)) : DEFAULT_VOICE_PITCH.semitones, }, heavyEstimateMB, }; } function expand(p: string): string { return p.startsWith("~/") ? path.join(os.homedir(), p.slice(2)) : path.resolve(p); } /** Where voice engines are installed: settings.voice.home, else paths.voiceHome. */ export function resolveVoiceHome(voice: Pick, paths: Pick): string { return voice.home ? expand(voice.home) : paths.voiceHome; }