/** Text zu Sprache – TTS + RVC-Umwandlung */ import { api } from "../api.js"; import { escapeHtml, formatDuration, toast, progressHtml, } from "../app.js"; let state = { step: 1, text: "", ttsVoice: "de-DE-KatjaNeural", model: null, preset: "sprache-neutral", params: {}, pitchCustomized: false, job: null, }; const STEPS = [ { n: 1, label: "Text" }, { n: 2, label: "TTS-Stimme" }, { n: 3, label: "RVC-Modell" }, { n: 4, label: "Einstellungen" }, { n: 5, label: "Ergebnis" }, ]; export async function renderTts(content) { const [models, presets] = await Promise.all([api.models(), api.presets()]); const presetList = presets.presets || []; if (!Object.keys(state.params).length) { const initialPreset = presetList.find((p) => p.id === state.preset); if (initialPreset) state.params = { ...initialPreset.parameters }; } window.__ttsState = { models: models.models || [], presets: presetList }; content.innerHTML = `
`; renderStep(); } function renderStepper() { const el = document.getElementById("stepper"); el.innerHTML = STEPS.map((s, i) => { const cls = s.n < state.step ? "stepper__step--done" : s.n === state.step ? "stepper__step--active" : ""; const num = s.n < state.step ? "✓" : s.n; const conn = i < STEPS.length - 1 ? `
` : ""; return `
${num}${s.label}
${conn}`; }).join(""); } function renderStep() { renderStepper(); const el = document.getElementById("step-content"); switch (state.step) { case 1: return renderStep1(el); case 2: return renderStep2(el); case 3: return renderStep3(el); case 4: return renderStep4(el); case 5: return renderStep5(el); } } function renderStep1(el) { el.innerHTML = `
${state.text.length} Zeichen
`; const ta = document.getElementById("tts-text"); const count = document.getElementById("char-count"); const next = document.getElementById("next-1"); ta.addEventListener("input", () => { state.text = ta.value; count.textContent = `${ta.value.length} Zeichen`; next.disabled = !ta.value.trim(); }); next.addEventListener("click", () => { state.step = 2; renderStep(); }); } function renderStep2(el) { el.innerHTML = `
Diese Stimme erzeugt das Roh-Audio. Die eigentliche Zielstimme wird im nächsten Schritt per RVC gewählt.
`; document.getElementById("tts-voice").addEventListener("change", (e) => { state.ttsVoice = e.target.value; }); document.getElementById("back-2").addEventListener("click", () => { state.step = 1; renderStep(); }); document.getElementById("next-2").addEventListener("click", () => { state.step = 3; renderStep(); }); } function renderStep3(el) { const { models } = window.__ttsState; if (!models.length) { el.innerHTML = `
🧩
Keine Modelle gefunden
Zum Training
`; return; } el.innerHTML = `
${models.map((m) => `
${escapeHtml(m.name)}
Pitch${m.recommended_pitch == null ? "–" : `${m.recommended_pitch >= 0 ? "+" : ""}${m.recommended_pitch}`}
Index${m.index_path ? "✓" : "✗"}
`).join("")}
`; el.querySelectorAll("[data-model]").forEach((card) => { card.addEventListener("click", () => { const name = card.dataset.model; state.model = models.find((m) => m.name === name); if (state.model.recommended_pitch != null && !state.pitchCustomized) { state.params.pitch = state.model.recommended_pitch; } renderStep(); }); }); document.getElementById("back-3").addEventListener("click", () => { state.step = 2; renderStep(); }); document.getElementById("next-3").addEventListener("click", () => { state.step = 4; renderStep(); }); } function renderStep4(el) { const { presets } = window.__ttsState; el.innerHTML = `
${state.params.pitch ?? 0}
ℹ️
Die Synthese kann je nach Textlänge einige Minuten dauern – die Fortschrittsanzeige bleibt währenddessen aktiv.
`; document.getElementById("tts-preset").addEventListener("change", (e) => { state.preset = e.target.value; const preset = presets.find((p) => p.id === state.preset); if (preset) { state.params = { ...preset.parameters }; state.pitchCustomized = true; } }); const pitchInput = document.getElementById("tts-pitch"); const pitchVal = document.getElementById("tts-pitch-val"); pitchInput.addEventListener("input", () => { state.params.pitch = parseInt(pitchInput.value, 10); state.pitchCustomized = true; pitchVal.textContent = pitchInput.value; }); document.getElementById("back-4").addEventListener("click", () => { state.step = 3; renderStep(); }); document.getElementById("start-4").addEventListener("click", async () => { try { const p = state.params; const payload = { text: state.text, tts_voice: state.ttsVoice, model_name: state.model.name, preset_id: state.preset, pitch: p.pitch ?? 0, f0_method: p.f0_method || "rmvpe", index_rate: p.index_rate ?? 0.7, protect: p.protect ?? 0.33, export_format: (p.export_format || "wav").toUpperCase(), }; const res = await api.createTtsJob(payload); state.job = await api.job(res.job_id); state.step = 5; renderStep(); } catch (err) { toast(err.message, "error"); } }); } function renderStep5(el) { const job = state.job; el.innerHTML = `
Text-Synthese & Umwandlung
`; const startedAt = Date.now(); const poll = async () => { try { const j = await api.job(job.id); const progBox = document.getElementById("job-progress"); if (progBox) progBox.innerHTML = progressHtml(j); const elapsed = document.getElementById("job-elapsed"); if (elapsed) { const secs = Math.round((Date.now() - startedAt) / 1000); elapsed.textContent = `Laufzeit: ${formatDuration(secs)} · Phase: ${j.progress_message || j.phase}`; } const logBox = document.getElementById("job-log-box"); if (logBox && j.log?.length) { logBox.innerHTML = `
${j.log.slice(-3).map(escapeHtml).join("\n")}
`; } if (j.status === "erfolgreich") { const resultArea = document.getElementById("result-area"); const url = api.jobDownload(j.id, 0); resultArea.innerHTML = `
✓
Ergebnis ist bereit
🎧 Ergebnis
⬇ Download
`; document.getElementById("again-5").addEventListener("click", () => { state = { step: 1, text: "", ttsVoice: state.ttsVoice, model: state.model, preset: state.preset, params: { ...state.params }, pitchCustomized: state.pitchCustomized, job: null }; renderStep(); }); return; } if (j.status === "fehlgeschlagen" || j.status === "abgebrochen") { const resultArea = document.getElementById("result-area"); resultArea.innerHTML = `
⚠️
${j.status === "abgebrochen" ? "Abgebrochen" : "Fehlgeschlagen"}
${escapeHtml(j.error || "")}
`; return; } setTimeout(poll, 2000); } catch { setTimeout(poll, 3000); } }; document.getElementById("cancel-5").addEventListener("click", async () => { try { await api.cancelJob(job.id); toast("Abbruch angefordert", "info"); } catch (err) { toast(err.message, "error"); } }); poll(); }