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();
});