File size: 11,338 Bytes
80ecb01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | /* ββ VOX ANI TTS β Frontend Logic ββ */
// βββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββ
function apiKey() {
return document.getElementById("api-key").value.trim();
}
function setStatus(id, msg, type = "") {
const el = document.getElementById(id);
el.textContent = msg;
el.className = "status" + (type ? " " + type : "");
}
function setLoading(btnId, loading, label) {
const btn = document.getElementById(btnId);
btn.disabled = loading;
btn.innerHTML = loading
? `<span class="spin">β³</span> ${label}`
: btn.dataset.label;
}
function initBtn(btnId, label) {
const btn = document.getElementById(btnId);
btn.dataset.label = label;
}
// βββ Tabs βββββββββββββββββββββββββββββββββββββββββββββββββββ
function showTab(name) {
document.querySelectorAll(".tab-panel").forEach((p) => p.classList.remove("active"));
document.querySelectorAll(".tab-btn").forEach((b) => b.classList.remove("active"));
document.getElementById("tab-" + name).classList.add("active");
const map = { synth: 0, clone: 1, manage: 2, encode: 3 };
document.querySelectorAll(".tab-btn")[map[name]].classList.add("active");
}
// βββ Load voices βββββββββββββββββββββββββββββββββββββββββββββ
async function loadVoices() {
const key = apiKey();
if (!key) return;
try {
const res = await fetch(`/voices?api_key=${encodeURIComponent(key)}`);
if (!res.ok) { clearVoiceDropdowns(); return; }
const data = await res.json();
const voices = data.voices || [];
populateDropdown("voice-select", voices, true);
const cloned = voices.filter((v) => v.type === "cloned");
populateDropdown("dl-voice", cloned, false);
populateDropdown("del-voice", cloned, false);
renderVoiceList(cloned);
} catch {
clearVoiceDropdowns();
}
}
function populateDropdown(id, voices, includeAll) {
const el = document.getElementById(id);
el.innerHTML = includeAll
? voices.map((v) => `<option value="${v.id}">${v.name}</option>`).join("")
: `<option value="">β ΠΈΠ·Π±Π΅ΡΠΈ β</option>` +
voices.map((v) => `<option value="${v.id}">${v.name} (${v.id})</option>`).join("");
}
function clearVoiceDropdowns() {
["voice-select", "dl-voice", "del-voice"].forEach((id) => {
document.getElementById(id).innerHTML = `<option value="">β Π½Π΅Π²Π°Π»ΠΈΠ΄Π΅Π½ ΠΊΠ»ΡΡ β</option>`;
});
["voices-list", "manage-list"].forEach((id) => {
document.getElementById(id).textContent = "π ΠΡΠ²Π΅Π΄ΠΈ Π²Π°Π»ΠΈΠ΄Π΅Π½ API ΠΊΠ»ΡΡ";
});
}
function renderVoiceList(cloned) {
const text = cloned.length
? cloned.map((v) => `β’ ${v.name} (ID: ${v.id})`).join("\n")
: "ΠΡΠΌΠ° Π·Π°ΠΏΠ°Π·Π΅Π½ΠΈ Π³Π»Π°ΡΠΎΠ²Π΅";
document.getElementById("voices-list").textContent = text;
document.getElementById("manage-list").textContent = text;
}
function onKeyChange() {
clearTimeout(window._keyTimer);
window._keyTimer = setTimeout(loadVoices, 500);
}
// βββ Enhance toggle ββββββββββββββββββββββββββββββββββββββββββ
function toggleEnhance() {
const on = document.getElementById("do-enhance").checked;
document.getElementById("enhance-sliders").style.opacity = on ? "1" : "0.4";
document.getElementById("enhance-sliders").style.pointerEvents = on ? "" : "none";
}
// βββ Synthesize ββββββββββββββββββββββββββββββββββββββββββββββ
async function synthesize() {
const key = apiKey();
const text = document.getElementById("synth-text").value.trim();
const voice = document.getElementById("voice-select").value;
const refFile = document.getElementById("ref-audio-synth").files[0];
if (!key) return setStatus("synth-status", "β οΈ ΠΡΠ²Π΅Π΄ΠΈ API ΠΊΠ»ΡΡ", "warn");
if (!text) return setStatus("synth-status", "β οΈ ΠΡΠ²Π΅Π΄ΠΈ ΡΠ΅ΠΊΡΡ", "warn");
setStatus("synth-status", "β³ ΠΠ΅Π½Π΅ΡΠΈΡΠ°Π½Π΅β¦");
document.getElementById("synth-btn").disabled = true;
try {
let url;
if (refFile) {
// Encode reference audio β embedding β synthesize with embedding
setStatus("synth-status", "β³ Encode Π½Π° reference Π°ΡΠ΄ΠΈΠΎβ¦");
const form = new FormData();
form.append("file", refFile);
const encRes = await fetch(
`/encode_voice?api_key=${encodeURIComponent(key)}&enhance=false`,
{ method: "POST", body: form }
);
if (!encRes.ok) throw new Error(await encRes.text());
const { embedding } = await encRes.json();
setStatus("synth-status", "β³ Π‘ΠΈΠ½ΡΠ΅Π·β¦");
url =
`/synthesize_with_embedding?api_key=${encodeURIComponent(key)}` +
`&text=${encodeURIComponent(text)}` +
`&embedding=${encodeURIComponent(JSON.stringify(embedding))}`;
} else {
if (!voice) return setStatus("synth-status", "β οΈ ΠΠ·Π±Π΅ΡΠΈ Π³Π»Π°Ρ", "warn");
url =
`/synthesize?api_key=${encodeURIComponent(key)}` +
`&text=${encodeURIComponent(text)}` +
`&voice=${encodeURIComponent(voice)}`;
}
const res = await fetch(url);
if (!res.ok) throw new Error(await res.text());
const blob = await res.blob();
const audioEl = document.getElementById("synth-audio");
audioEl.src = URL.createObjectURL(blob);
audioEl.style.display = "block";
audioEl.play();
setStatus("synth-status", "β
ΠΠΎΡΠΎΠ²ΠΎ", "ok");
} catch (e) {
setStatus("synth-status", "β ΠΡΠ΅ΡΠΊΠ°: " + e.message, "err");
} finally {
document.getElementById("synth-btn").disabled = false;
}
}
// βββ Clone voice βββββββββββββββββββββββββββββββββββββββββββββ
async function cloneVoice() {
const key = apiKey();
const name = document.getElementById("voice-name").value.trim();
const fileEl = document.getElementById("ref-audio-clone");
const file = fileEl.files[0];
const enhance = document.getElementById("do-enhance").checked;
const denoise = document.getElementById("denoise").value;
const deess = document.getElementById("deess").value;
const warm = document.getElementById("warm").value;
if (!key) return setStatus("clone-status", "β οΈ ΠΡΠ²Π΅Π΄ΠΈ API ΠΊΠ»ΡΡ", "warn");
if (!file) return setStatus("clone-status", "β οΈ ΠΠ°ΡΠΈ Π°ΡΠ΄ΠΈΠΎ ΡΠ°ΠΉΠ»", "warn");
setStatus("clone-status", "β³ ΠΠ»ΠΎΠ½ΠΈΡΠ°Π½Π΅β¦");
document.getElementById("clone-btn").disabled = true;
try {
const form = new FormData();
form.append("file", file);
const params = new URLSearchParams({
api_key: key,
name: name || "",
enhance: enhance,
denoise_strength: denoise,
deess_db: deess,
warm_db: warm,
});
const res = await fetch(`/clone_voice?${params}`, {
method: "POST",
body: form,
});
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
setStatus("clone-status", `β
ΠΠ»Π°ΡΡΡ '${data.name}' Π΅ Π·Π°ΠΏΠ°Π·Π΅Π½!`, "ok");
fileEl.value = "";
document.getElementById("voice-name").value = "";
await loadVoices();
} catch (e) {
setStatus("clone-status", "β ΠΡΠ΅ΡΠΊΠ°: " + e.message, "err");
} finally {
document.getElementById("clone-btn").disabled = false;
}
}
// βββ Download voice ββββββββββββββββββββββββββββββββββββββββββ
async function downloadVoice() {
const key = apiKey();
const voiceId = document.getElementById("dl-voice").value;
if (!key) return setStatus("dl-status", "β οΈ ΠΡΠ²Π΅Π΄ΠΈ API ΠΊΠ»ΡΡ", "warn");
if (!voiceId) return setStatus("dl-status", "β οΈ ΠΠ·Π±Π΅ΡΠΈ Π³Π»Π°Ρ", "warn");
setStatus("dl-status", "β³ ΠΠ·ΡΠ΅Π³Π»ΡΠ½Π΅β¦");
try {
const res = await fetch(
`/voices/${encodeURIComponent(voiceId)}/download?api_key=${encodeURIComponent(key)}`
);
if (!res.ok) throw new Error(await res.text());
const blob = await res.blob();
const cd = res.headers.get("content-disposition") || "";
const match = cd.match(/filename="?([^"]+)"?/);
const fname = match ? match[1] : `voice_${voiceId}.json`;
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = fname;
a.click();
setStatus("dl-status", `β
${fname} ΠΈΠ·ΡΠ΅Π³Π»Π΅Π½`, "ok");
} catch (e) {
setStatus("dl-status", "β ΠΡΠ΅ΡΠΊΠ°: " + e.message, "err");
}
}
// βββ Delete voice ββββββββββββββββββββββββββββββββββββββββββββ
async function deleteVoice() {
const key = apiKey();
const voiceId = document.getElementById("del-voice").value;
if (!key) return setStatus("del-status", "β οΈ ΠΡΠ²Π΅Π΄ΠΈ API ΠΊΠ»ΡΡ", "warn");
if (!voiceId) return setStatus("del-status", "β οΈ ΠΠ·Π±Π΅ΡΠΈ Π³Π»Π°Ρ", "warn");
if (!confirm("Π‘ΠΈΠ³ΡΡΠ΅Π½ Π»ΠΈ ΡΠΈ, ΡΠ΅ ΠΈΡΠΊΠ°Ρ Π΄Π° ΠΈΠ·ΡΡΠΈΠ΅Ρ ΡΠΎΠ·ΠΈ Π³Π»Π°Ρ?")) return;
setStatus("del-status", "β³ ΠΠ·ΡΡΠΈΠ²Π°Π½Π΅β¦");
document.getElementById("del-btn").disabled = true;
try {
const res = await fetch(
`/voices/${encodeURIComponent(voiceId)}?api_key=${encodeURIComponent(key)}`,
{ method: "DELETE" }
);
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
setStatus("del-status", `β
'${data.name}' Π΅ ΠΈΠ·ΡΡΠΈΡ`, "ok");
await loadVoices();
} catch (e) {
setStatus("del-status", "β ΠΡΠ΅ΡΠΊΠ°: " + e.message, "err");
} finally {
document.getElementById("del-btn").disabled = false;
}
}
// βββ Encode / Decode API Key ββββββββββββββββββββββββββββββββ
function encodeKey() {
const key = document.getElementById("raw-key").value.trim();
if (!key) return;
const encoded = btoa(key).split("").reverse().join("");
document.getElementById("encoded-out").value = encoded;
}
function decodeKey() {
const encoded = document.getElementById("encoded-key").value.trim();
if (!encoded) return;
try {
const decoded = atob(encoded.split("").reverse().join(""));
document.getElementById("decoded-out").value = decoded;
} catch {
document.getElementById("decoded-out").value = "β ΠΠ΅Π²Π°Π»ΠΈΠ΄Π΅Π½ ΠΊΠ»ΡΡ";
}
}
// βββ Init ββββββββββββββββββββββββββββββββββββββββββββββββββββ
document.addEventListener("DOMContentLoaded", () => {
toggleEnhance();
// Keyboard shortcut: Ctrl+Enter to synthesize
document.getElementById("synth-text").addEventListener("keydown", (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === "Enter") synthesize();
});
// If there's already a key on load (browser autofill), fetch voices
if (apiKey()) loadVoices();
});
|