// Ponte do painel com o serviço de voz local (voice/, 127.0.0.1:4510). // O painel lê a chave e a porta de voice/.env, fala com o serviço por HTTP e cuida do que é arquivo: // vozes de personagem (data/voice/user-voices.json + .pth/.index) e configurações (voice/.env). // Mudou o voice/.env → o `pnpm dev` (scripts/dev.ts) reinicia o serviço sozinho. import { spawn } from "node:child_process"; import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; import { REPO_ROOT, slugify } from "./paths"; import { UserError } from "./stories"; export const VOICE_DIR = path.join(REPO_ROOT, "voice"); const ENV_FILE = path.join(VOICE_DIR, ".env"); const LOCK_FILE = path.join(VOICE_DIR, "models.lock.json"); const APPLIO_INFER = path.join(VOICE_DIR, "vendor", "applio", "rvc", "infer", "infer.py"); const VENV_PYTHON = path.join(VOICE_DIR, ".venv", process.platform === "win32" ? "Scripts/python.exe" : "bin/python"); export const DEVICES = ["auto", "cuda", "cpu"] as const; export const STT_MODELS = ["base", "small", "medium", "large-v3-turbo"] as const; export const F0_METHODS = ["rmvpe", "fcpe", "crepe"] as const; export const LANGUAGES = ["pt-BR", "en", "es"] as const; export const DETAILS = ["text", "segments", "words"] as const; const VOICE_ID_RE = /^[a-z0-9][a-z0-9-]{1,47}$/; // mesmo padrão do serviço (schemas.py) const MAX_MODEL_BYTES = 1024 * 1024 * 1024; type Device = (typeof DEVICES)[number]; type SttModel = (typeof STT_MODELS)[number]; export type VoiceSettings = { device: Device; sttModel: SttModel; enableRvc: boolean; sttModelsInstalled: string[]; /** Azure Speech: só diz se há chave; a chave nunca sai do servidor. */ azureConfigured: boolean; azureRegion: string; }; export type VoiceJob = { kind: "download"; status: "running" | "done" | "error"; message: string }; // ---------- voice/.env ---------- export function readVoiceEnv(): Record { if (!fs.existsSync(ENV_FILE)) return {}; const env: Record = {}; for (const line of fs.readFileSync(ENV_FILE, "utf8").split(/\r?\n/)) { const at = line.indexOf("="); if (at <= 0 || line.trimStart().startsWith("#")) continue; env[line.slice(0, at).trim()] = line.slice(at + 1).trim(); } return env; } /** Troca só as chaves pedidas, preservando comentários e a ordem do arquivo. */ function writeVoiceEnv(values: Record): void { if (!fs.existsSync(ENV_FILE)) throw new UserError("voice/.env não existe: rode o setup.bat."); const lines = fs.readFileSync(ENV_FILE, "utf8").split(/\r?\n/); const pending = new Map(Object.entries(values)); const out = lines.map((line) => { const name = line.split("=", 1)[0].trim(); if (!line.trimStart().startsWith("#") && pending.has(name)) { const value = pending.get(name)!; pending.delete(name); return `${name}=${value}`; } return line; }); for (const [name, value] of pending) out.push(`${name}=${value}`); const tmp = `${ENV_FILE}.tmp`; fs.writeFileSync(tmp, out.join("\n"), "utf8"); fs.renameSync(tmp, ENV_FILE); } function resolveFromRoot(value: string | undefined, fallback: string): string { if (!value) return fallback; return path.isAbsolute(value) ? value : path.join(REPO_ROOT, value); } export const modelsDir = () => resolveFromRoot(readVoiceEnv().VOICE_MODELS_DIR, path.join(REPO_ROOT, "data", "voice", "models")); const userCatalogPath = () => resolveFromRoot(readVoiceEnv().VOICE_USER_CATALOG, path.join(REPO_ROOT, "data", "voice", "user-voices.json")); export const applioInstalled = () => fs.existsSync(APPLIO_INFER); const CLONE_PYTHON = path.join(VOICE_DIR, ".venv-clone", process.platform === "win32" ? "Scripts/python.exe" : "bin/python"); /** Clonagem de voz instalada (setup.bat -Clone): venv próprio com o Chatterbox. */ export const cloneInstalled = () => fs.existsSync(CLONE_PYTHON); // ---------- HTTP com o serviço ---------- function serviceUrl(pathname: string): { url: string; key: string } { const env = readVoiceEnv(); if (!env.VOICE_SERVICE_KEY) throw new UserError("Serviço de voz não instalado: rode o setup.bat."); return { url: `http://127.0.0.1:${env.VOICE_PORT || "4510"}${pathname}`, key: env.VOICE_SERVICE_KEY }; } async function voiceFetch(pathname: string, init: RequestInit = {}, timeoutMs = 30_000): Promise { const { url, key } = serviceUrl(pathname); const headers = new Headers(init.headers); headers.set("X-Service-Key", key); try { return await fetch(url, { ...init, headers, signal: AbortSignal.timeout(timeoutMs), cache: "no-store" }); } catch { throw new UserError("Serviço de voz fora do ar (127.0.0.1). Suba com start.bat ou pnpm dev."); } } /** Erro do serviço `{error:{code,message}}` → UserError com a mensagem dele. */ async function serviceError(response: Response): Promise { try { const body = (await response.json()) as { error?: { code?: string; message?: string } }; return new UserError(body.error?.message ?? `Serviço de voz respondeu ${response.status}.`); } catch { return new UserError(`Serviço de voz respondeu ${response.status}.`); } } // ---------- configurações ---------- function installedSttModels(): string[] { const lock = JSON.parse(fs.readFileSync(LOCK_FILE, "utf8")) as { files: { path: string; group?: string }[] }; const dir = modelsDir(); return STT_MODELS.filter((model) => { const files = lock.files.filter((f) => f.group === `stt-${model}`); // Existência e tamanho; o sha256 completo o serviço confere na subida. return files.length > 0 && files.every((f) => fs.existsSync(path.join(dir, f.path)) && fs.statSync(path.join(dir, f.path)).size > 0); }); } export function readSettings(): VoiceSettings { const env = readVoiceEnv(); const device = (DEVICES as readonly string[]).includes(env.VOICE_DEVICE) ? (env.VOICE_DEVICE as Device) : "auto"; const sttModel = (STT_MODELS as readonly string[]).includes(env.VOICE_STT_MODEL) ? (env.VOICE_STT_MODEL as SttModel) : "small"; return { device, sttModel, enableRvc: env.VOICE_ENABLE_RVC === "1", sttModelsInstalled: installedSttModels(), azureConfigured: Boolean(env.VOICE_AZURE_KEY), azureRegion: env.VOICE_AZURE_REGION || "eastus", }; } // Download de modelo em andamento (um por vez). Vive na memória do processo do painel. let job: VoiceJob | null = null; export function updateSettings(input: { device?: unknown; sttModel?: unknown; enableRvc?: unknown }): { download: boolean } { const device = String(input.device); const sttModel = String(input.sttModel); const enableRvc = input.enableRvc === true; if (!(DEVICES as readonly string[]).includes(device)) throw new UserError("Dispositivo inválido."); if (!(STT_MODELS as readonly string[]).includes(sttModel)) throw new UserError("Modelo do Whisper inválido."); if (enableRvc && !applioInstalled()) throw new UserError("O Applio não está instalado: rode o setup.bat sem -NoRvc."); if (job?.status === "running") throw new UserError("Já há um download de modelo em andamento."); const values = { VOICE_DEVICE: device, VOICE_ENABLE_RVC: enableRvc ? "1" : "0", VOICE_STT_MODEL: sttModel }; if (installedSttModels().includes(sttModel)) { writeVoiceEnv(values); return { download: false }; } // Modelo ausente: baixa primeiro (com sha256) e só então grava o .env — senão o serviço não sobe. job = { kind: "download", status: "running", message: `Baixando Whisper ${sttModel}…` }; const child = spawn(VENV_PYTHON, [path.join(VOICE_DIR, "scripts", "fetch_models.py"), "--group", `stt-${sttModel}`], { cwd: VOICE_DIR, windowsHide: true, env: { ...process.env, VOICE_MODELS_DIR: modelsDir(), PYTHONUTF8: "1" }, }); let tail = ""; const keep = (chunk: Buffer) => { tail = (tail + chunk.toString("utf8")).slice(-400); }; child.stdout.on("data", keep); child.stderr.on("data", keep); child.on("error", () => { job = { kind: "download", status: "error", message: "Não consegui iniciar o Python do voice/.venv; rode o setup.bat." }; }); child.on("exit", (code) => { if (code === 0) { writeVoiceEnv(values); job = { kind: "download", status: "done", message: `Whisper ${sttModel} instalado; o serviço de voz está reiniciando.` }; } else { job = { kind: "download", status: "error", message: `Download falhou: ${tail.trim().split("\n").pop() ?? code}` }; } }); return { download: true }; } // ---------- Azure Speech ---------- const AZURE_REGION_RE = /^[a-z0-9]{2,32}$/; const AZURE_KEY_RE = /^[A-Za-z0-9]{20,128}$/; /** * Grava região e chave do Azure no voice/.env (o pnpm dev reinicia o serviço sozinho). * `key` vazio mantém a chave atual; `clear: true` apaga a chave (as vozes Azure somem da lista). */ export function updateAzure(input: { region?: unknown; key?: unknown; clear?: unknown }): void { const region = String(input.region ?? "").trim().toLowerCase(); const key = String(input.key ?? "").trim(); if (!AZURE_REGION_RE.test(region)) throw new UserError("Região inválida (ex.: eastus, brazilsouth)."); if (key && !AZURE_KEY_RE.test(key)) throw new UserError("A chave do Azure tem só letras e números (copie a \"KEY 1\" do recurso de Fala)."); const values: Record = { VOICE_AZURE_REGION: region }; if (input.clear === true) values.VOICE_AZURE_KEY = ""; else if (key) values.VOICE_AZURE_KEY = key; else if (!readVoiceEnv().VOICE_AZURE_KEY) throw new UserError("Cole a chave do Azure."); writeVoiceEnv(values); } // ---------- estado ---------- export async function getStatus() { const base = { applioInstalled: applioInstalled(), cloneInstalled: cloneInstalled(), settings: readSettings(), job }; try { const [status, voices] = await Promise.all([voiceFetch("/v1/status", {}, 5_000), voiceFetch("/v1/voices?limit=100", {}, 5_000)]); if (!status.ok) throw await serviceError(status); if (!voices.ok) throw await serviceError(voices); const s = (await status.json()) as { device: unknown; rvc: boolean; engines: unknown }; const v = (await voices.json()) as { items: unknown[] }; return { ...base, online: true, device: s.device, rvc: s.rvc, engines: s.engines, voices: v.items }; } catch (error) { // Fora do ar, as vozes de personagem ainda aparecem (lidas do arquivo local) para poder removê-las. let voices: unknown[] = []; try { voices = readUserCatalog().voices.map((v) => v.kind === "clone" ? { id: v.id, name: v.name, languages: ["pt-BR"], engine: "tts", default: false, description: v.description ?? null, gender: v.gender ?? null, ageRange: null, provider: "clone", baseVoice: null, user: true, offline: true, rvc: null, } : { id: v.id, name: v.name, languages: ["pt-BR"], engine: "tts+rvc", default: false, description: v.description ?? null, gender: null, ageRange: null, provider: "edge", baseVoice: v.baseVoice ?? null, user: true, offline: false, rvc: { pitch: Number(v.rvc?.pitch ?? 0), f0Method: String(v.rvc?.f0Method ?? "rmvpe"), indexRate: Number(v.rvc?.indexRate ?? 0.5), protect: Number(v.rvc?.protect ?? 0.33), hasIndex: Boolean(v.rvc?.index), }, }); } catch { // catálogo local ilegível: segue sem vozes } return { ...base, online: false, error: error instanceof Error ? error.message : String(error), voices }; } } // ---------- síntese ---------- export async function synthesize(input: { voiceId?: unknown; text?: unknown; speed?: unknown }): Promise { const voiceId = String(input.voiceId ?? ""); const text = String(input.text ?? "").trim(); const speed = Number(input.speed ?? 1); if (!VOICE_ID_RE.test(voiceId)) throw new UserError("Voz inválida."); if (!text) throw new UserError("Escreva um texto para ouvir."); if (!(speed >= 0.5 && speed <= 2)) throw new UserError("Velocidade precisa estar entre 0,5 e 2."); const response = await voiceFetch( "/v1/synthesize", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ requestId: `panel-${crypto.randomUUID()}`.slice(0, 64), text, voiceId, params: { speed, format: "ogg_opus" } }), }, 300_000, ); if (!response.ok) throw await serviceError(response); return new Response(response.body, { headers: { "Content-Type": "audio/ogg", "Cache-Control": "no-store", "X-Audio-Duration-Ms": response.headers.get("X-Audio-Duration-Ms") ?? "" }, }); } // ---------- transcrição / legenda ---------- type Segment = { start: number; end: number; text: string; words?: { start: number; end: number; text: string; p: number }[] }; function srtTime(seconds: number): string { const ms = Math.max(0, Math.round(seconds * 1000)); const h = Math.floor(ms / 3_600_000); const m = Math.floor((ms % 3_600_000) / 60_000); const s = Math.floor((ms % 60_000) / 1000); const pad = (n: number, w = 2) => String(n).padStart(w, "0"); return `${pad(h)}:${pad(m)}:${pad(s)},${pad(ms % 1000, 3)}`; } /** Segmentos com tempo → SubRip (.srt). */ export function toSrt(segments: Segment[]): string { return segments .filter((seg) => seg.text.trim()) .map((seg, i) => `${i + 1}\n${srtTime(seg.start)} --> ${srtTime(seg.end)}\n${seg.text.trim()}\n`) .join("\n"); } export async function transcribe(file: File, language: string, detail: string) { if (!(LANGUAGES as readonly string[]).includes(language)) throw new UserError("Idioma inválido."); if (!(DETAILS as readonly string[]).includes(detail)) throw new UserError("Detalhe inválido."); if (file.size === 0) throw new UserError("Arquivo de áudio vazio."); const audio = Buffer.from(await file.arrayBuffer()); let response = await voiceFetch( "/v1/transcribe", { method: "POST", body: audio, headers: { "Content-Type": file.type && file.type.includes("/") ? file.type : "application/octet-stream", "X-Idempotency-Key": `panel-${crypto.randomUUID()}`, "X-Language": language, "X-Detail": detail, }, }, 120_000, ); // 202 = ainda transcrevendo: o painel espera o job (até 20 min, o teto do serviço para 30 min de áudio na CPU). const deadline = Date.now() + 20 * 60_000; while (response.status === 202 && Date.now() < deadline) { const pending = (await response.json()) as { jobRef: string; retryAfterMs: number }; await new Promise((resolve) => setTimeout(resolve, Math.min(10_000, Math.max(500, pending.retryAfterMs)))); response = await voiceFetch(`/v1/transcribe/jobs/${encodeURIComponent(pending.jobRef)}`, {}, 30_000); } if (response.status === 202) throw new UserError("A transcrição passou de 20 minutos; tente um áudio menor."); if (!response.ok) throw await serviceError(response); const result = (await response.json()) as { text: string; noSpeechProb: number; durationS?: number; segments?: Segment[] }; return { ...result, srt: result.segments ? toSrt(result.segments) : undefined }; } // ---------- vozes de personagem ---------- type UserVoiceEntry = { id: string; name: string; /** "clone" = voz clonada de uma gravação; ausente = voz de personagem RVC. */ kind?: "clone"; baseVoice?: string; description?: string; gender?: "female" | "male"; source: string; license: string; consent?: boolean; consentAt?: string; rvc?: Record; clone?: Record; }; function readUserCatalog(): { voices: UserVoiceEntry[] } { const file = userCatalogPath(); if (!fs.existsSync(file)) return { voices: [] }; return JSON.parse(fs.readFileSync(file, "utf8")) as { voices: UserVoiceEntry[] }; } /** * A voz é do catálogo do usuário (personagem RVC ou voz clonada)? Usado no rótulo "feito com IA" da publicação * (ai_label = auto). Sem catálogo = nenhuma voz do usuário; catálogo ilegível LANÇA erro — quem chama decide * com cautela (a publicação liga o rótulo de IA). */ export function isUserVoice(voiceId: string): boolean { if (!voiceId) return false; return readUserCatalog().voices.some((v) => v.id === voiceId); } function writeUserCatalog(catalog: { voices: UserVoiceEntry[] }): void { const file = userCatalogPath(); fs.mkdirSync(path.dirname(file), { recursive: true }); const tmp = `${file}.tmp`; fs.writeFileSync(tmp, JSON.stringify(catalog, null, 2) + "\n", "utf8"); fs.renameSync(tmp, file); } async function reloadService(): Promise { try { const response = await voiceFetch("/v1/voices/reload", { method: "POST" }, 120_000); if (!response.ok) throw await serviceError(response); } catch { // Serviço fora do ar: ele lê o catálogo novo quando subir. } } function numberIn(raw: FormDataEntryValue | null, fallback: number, min: number, max: number, label: string): number { const value = raw === null || raw === "" ? fallback : Number(raw); if (!Number.isFinite(value) || value < min || value > max) throw new UserError(`${label} precisa estar entre ${min} e ${max}.`); return value; } /** .pth do RVC é um checkpoint do torch: zip (formato atual) ou pickle (formato antigo). */ function looksLikeTorchCheckpoint(head: Buffer): boolean { const zip = head[0] === 0x50 && head[1] === 0x4b && head[2] === 0x03 && head[3] === 0x04; const pickle = head[0] === 0x80 && head[1] >= 2 && head[1] <= 5; return zip || pickle; } export async function addUserVoice(form: FormData): Promise<{ id: string }> { if (!applioInstalled()) throw new UserError("O Applio não está instalado: rode o setup.bat sem -NoRvc."); const text = (key: string) => String(form.get(key) ?? "").trim(); const name = text("name"); const baseVoice = text("baseVoice"); const source = text("source"); const license = text("license"); if (name.length < 2 || name.length > 60) throw new UserError("Nome precisa ter entre 2 e 60 caracteres."); if (!source || !license) throw new UserError("Informe a origem e a licença da voz."); if (!VOICE_ID_RE.test(baseVoice)) throw new UserError("Escolha a voz base."); const f0Method = text("f0Method") || "rmvpe"; if (!(F0_METHODS as readonly string[]).includes(f0Method)) throw new UserError("Método de f0 inválido."); const pitch = Math.round(numberIn(form.get("pitch"), 0, -24, 24, "Pitch")); const indexRate = numberIn(form.get("indexRate"), 0.5, 0, 1, "Index rate"); const protect = numberIn(form.get("protect"), 0.33, 0, 0.5, "Protect"); const model = form.get("model"); const index = form.get("index"); if (!(model instanceof File) || !model.name.toLowerCase().endsWith(".pth")) throw new UserError("Envie o modelo .pth."); if (model.size > MAX_MODEL_BYTES) throw new UserError("Modelo acima de 1 GB."); const hasIndex = index instanceof File && index.size > 0; if (hasIndex && !index.name.toLowerCase().endsWith(".index")) throw new UserError("O arquivo de índice precisa ser .index."); if (hasIndex && index.size > MAX_MODEL_BYTES) throw new UserError("Índice acima de 1 GB."); const catalog = readUserCatalog(); const status = await getStatus(); const known = new Set([...catalog.voices.map((v) => v.id), ...(status.voices as { id: string }[]).map((v) => v.id)]); if (status.online && !known.has(baseVoice)) throw new UserError("Voz base desconhecida."); let id = slugify(name); if (!VOICE_ID_RE.test(id)) throw new UserError("Nome precisa ter letras ou números."); for (let n = 2; known.has(id); n++) id = `${slugify(name).slice(0, 44)}-${n}`; const modelBytes = Buffer.from(await model.arrayBuffer()); if (!looksLikeTorchCheckpoint(modelBytes.subarray(0, 4))) throw new UserError("O .pth não parece um modelo do torch/RVC."); const folder = path.join(modelsDir(), "rvc", id); fs.mkdirSync(folder, { recursive: true }); const sha = (data: Buffer) => crypto.createHash("sha256").update(data).digest("hex"); fs.writeFileSync(path.join(folder, `${id}.pth`), modelBytes); const rvc: Record = { model: `rvc/${id}/${id}.pth`, modelSha256: sha(modelBytes), pitch, f0Method, indexRate, protect, }; if (hasIndex) { const indexBytes = Buffer.from(await (index as File).arrayBuffer()); fs.writeFileSync(path.join(folder, `${id}.index`), indexBytes); rvc.index = `rvc/${id}/${id}.index`; rvc.indexSha256 = sha(indexBytes); } const description = text("description"); catalog.voices.push({ id, name, baseVoice, ...(description ? { description } : {}), source, license, rvc }); writeUserCatalog(catalog); await reloadService(); return { id }; } const CLONE_AUDIO_EXT = [".wav", ".mp3", ".ogg", ".m4a", ".flac", ".webm", ".opus"]; const MAX_CLONE_BYTES = 30 * 1024 * 1024; /** * Voz clonada: grava a referência em models/clone// (com sha256) e registra no user-voices.json. * Exige consentimento explícito: a pessoa da gravação autorizou o uso da voz. */ export async function addCloneVoice(form: FormData): Promise<{ id: string }> { if (!cloneInstalled()) throw new UserError("A clonagem não está instalada: rode setup.bat -Clone e reinicie o start.bat."); const text = (key: string) => String(form.get(key) ?? "").trim(); const name = text("name"); const source = text("source"); const gender = text("gender"); if (name.length < 2 || name.length > 60) throw new UserError("Nome precisa ter entre 2 e 60 caracteres."); if (!source) throw new UserError("Diga de quem é a voz e de onde veio a gravação."); if (form.get("consent") !== "on") throw new UserError("Confirme que a pessoa da gravação autorizou o uso da voz."); if (gender !== "female" && gender !== "male") throw new UserError("Escolha se a voz é feminina ou masculina."); const exaggeration = numberIn(form.get("exaggeration"), 0.5, 0.25, 1.5, "Expressividade"); const cfgWeight = numberIn(form.get("cfgWeight"), 0.5, 0, 1, "Ritmo"); const audio = form.get("audio"); if (!(audio instanceof File) || audio.size === 0) throw new UserError("Envie a gravação da voz."); const ext = path.extname(audio.name).toLowerCase(); if (!CLONE_AUDIO_EXT.includes(ext)) throw new UserError(`Formato não aceito (${CLONE_AUDIO_EXT.join(", ")}).`); if (audio.size > MAX_CLONE_BYTES) throw new UserError("Gravação acima de 30 MB: 10 a 30 segundos bastam."); if (audio.size < 30 * 1024) throw new UserError("Gravação muito curta: envie de 10 a 30 segundos de fala limpa."); const catalog = readUserCatalog(); const status = await getStatus(); const known = new Set([...catalog.voices.map((v) => v.id), ...(status.voices as { id: string }[]).map((v) => v.id)]); const slug = slugify(name); let id = `clone-${slug}`.slice(0, 48); if (!VOICE_ID_RE.test(id)) throw new UserError("Nome precisa ter letras ou números."); for (let n = 2; known.has(id); n++) id = `clone-${slug.slice(0, 38)}-${n}`; const bytes = Buffer.from(await audio.arrayBuffer()); const folder = path.join(modelsDir(), "clone", id); fs.mkdirSync(folder, { recursive: true }); fs.writeFileSync(path.join(folder, `reference${ext}`), bytes); const description = text("description"); catalog.voices.push({ id, name, kind: "clone", gender, ...(description ? { description } : {}), source, license: "Voz clonada com autorização da pessoa da gravação (registrada no painel).", consent: true, consentAt: new Date().toISOString(), clone: { reference: `clone/${id}/reference${ext}`, referenceSha256: crypto.createHash("sha256").update(bytes).digest("hex"), language: "pt", exaggeration, cfgWeight, }, }); writeUserCatalog(catalog); await reloadService(); return { id }; } export async function removeUserVoice(id: string): Promise { if (!VOICE_ID_RE.test(id)) throw new UserError("Voz inválida."); const catalog = readUserCatalog(); if (!catalog.voices.some((v) => v.id === id)) throw new UserError("Só vozes de personagem adicionadas pelo painel podem ser removidas."); writeUserCatalog({ voices: catalog.voices.filter((v) => v.id !== id) }); fs.rmSync(path.join(modelsDir(), "rvc", id), { recursive: true, force: true }); fs.rmSync(path.join(modelsDir(), "clone", id), { recursive: true, force: true }); await reloadService(); }