-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
299 lines (261 loc) · 9.65 KB
/
Copy pathscript.js
File metadata and controls
299 lines (261 loc) · 9.65 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const LANGUAGE_LABELS = {
"🇨🇳": "Chinese",
"🇺🇸": "English",
"🇯🇵": "Japanese",
"🇰🇷": "Korean",
"🇪🇸": "Spanish",
"🇫🇷": "French",
"🇩🇪": "German",
"🇮🇹": "Italian",
"🇭🇺": "Hungarian",
"🇷🇺": "Russian",
"🇮🇷": "Persian",
"🇸🇦": "Arabic",
"🇵🇱": "Polish",
"🇵🇹": "Portuguese",
"🇨🇿": "Czech",
"🇩🇰": "Danish",
"🇸🇪": "Swedish",
"🇬🇷": "Greek",
"🇹🇷": "Turkish",
};
const SAMPLE_CONTAINERS = {
chinese: document.querySelector("#chineseSamples"),
english: document.querySelector("#englishSamples"),
multilingual: document.querySelector("#multilingualSamples"),
};
// ─── Reveal observer ──────────────────────────────────────
const revealObserver =
typeof IntersectionObserver === "function"
? new IntersectionObserver(
(entries, obs) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
entry.target.classList.add("is-visible");
obs.unobserve(entry.target);
});
},
{ threshold: 0, rootMargin: "0px 0px -6% 0px" },
)
: null;
function observeReveal(el) {
if (el.dataset.revealBound) return;
el.dataset.revealBound = "true";
if (!revealObserver) {
el.classList.add("is-visible");
return;
}
revealObserver.observe(el);
}
function setupRevealAnimations() {
document.querySelectorAll(".reveal").forEach(observeReveal);
}
// ─── Sidebar nav active state ─────────────────────────────
function setupSidebarActive() {
const links = document.querySelectorAll(".sidebar-link");
if (!links.length || typeof IntersectionObserver !== "function") return;
const sectionIds = ["overview", "introduction", "architecture", "demo", "languages"];
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
const id = entry.target.id;
links.forEach((link) => {
const href = link.getAttribute("href");
const match =
href === `#${id}` ||
(href === "#overview" && id === "introduction");
link.classList.toggle("active", match);
});
});
},
{ threshold: 0.3, rootMargin: "-80px 0px -40% 0px" },
);
sectionIds.forEach((id) => {
const el = document.getElementById(id);
if (el) observer.observe(el);
});
}
// ─── Tab switching ────────────────────────────────────────
function setupTabs() {
const tabs = document.querySelectorAll(".demo-tab");
const panels = {
chinese: document.getElementById("chineseSamples"),
english: document.getElementById("englishSamples"),
multilingual: document.getElementById("multilingualSamples"),
};
function showTab(targetKey) {
tabs.forEach((t) => {
const isActive = t.dataset.tab === targetKey;
t.classList.toggle("active", isActive);
t.setAttribute("aria-selected", isActive ? "true" : "false");
});
Object.entries(panels).forEach(([key, panel]) => {
if (!panel) return;
panel.style.display = key === targetKey ? "grid" : "none";
});
setupRevealAnimations();
}
// Initialize: hide non-active panels
showTab("chinese");
tabs.forEach((tab) => {
tab.addEventListener("click", () => showTab(tab.dataset.tab));
});
}
// ─── Asset URL ────────────────────────────────────────────
function buildAssetUrl(assetPath) {
return encodeURI(`./assets/${assetPath}`);
}
function getCategory(flag) {
if (flag === "🇨🇳") return "chinese";
if (flag === "🇺🇸") return "english";
return "multilingual";
}
// ─── DOM helpers ──────────────────────────────────────────
function el(tag, className, text) {
const node = document.createElement(tag);
if (className) node.className = className;
if (typeof text === "string") node.textContent = text;
return node;
}
function createAudioPanel(label, src, options = {}) {
const { collapsible = false } = options;
const wrap = el("div", "audio-panel");
const audio = document.createElement("audio");
audio.controls = true;
audio.preload = "none";
audio.src = src;
if (collapsible) {
wrap.classList.add("audio-panel-collapsible");
audio.hidden = true;
const toggle = document.createElement("button");
toggle.type = "button";
toggle.className = "audio-label audio-label-button";
toggle.textContent = label;
toggle.setAttribute("aria-expanded", "false");
toggle.setAttribute("aria-label", `Show ${label.toLowerCase()} audio`);
toggle.addEventListener("click", () => {
const isExpanded = toggle.getAttribute("aria-expanded") === "true";
const nextExpanded = !isExpanded;
audio.hidden = !nextExpanded;
toggle.setAttribute("aria-expanded", String(nextExpanded));
toggle.setAttribute(
"aria-label",
`${nextExpanded ? "Hide" : "Show"} ${label.toLowerCase()} audio`,
);
if (!nextExpanded) audio.pause();
});
wrap.append(toggle, audio);
return wrap;
}
const lbl = el("p", "audio-label", label);
wrap.append(lbl, audio);
return wrap;
}
function createTextPanel(text) {
const wrap = el("div", "text-panel");
const lbl = el("p", "audio-label", "Text");
const body = el("div", "sample-text", text);
wrap.append(lbl, body);
return wrap;
}
function createSampleCard(sample, index) {
const compact = sample.category === "multilingual";
const card = el("article", compact ? "sample-card compact reveal" : "sample-card reveal");
card.style.setProperty("--delay", `${Math.min(index * 55, 330)}ms`);
// Header
const header = el("header", "sample-header");
const copy = document.createElement("div");
const lang = el("p", "sample-lang", sample.languageLabel);
const title = el("h4", "sample-title", sample.name);
const chip = el("span", "status-chip", compact ? "Multilingual" : "Featured");
copy.append(lang, title);
header.append(copy, chip);
// Body
card.append(
header,
createAudioPanel("Prompt speech", sample.promptSpeech, { collapsible: true }),
createTextPanel(sample.text),
createAudioPanel("Generated speech", sample.generatedSpeech),
);
return card;
}
// ─── Data parsing ─────────────────────────────────────────
function parseSample(line) {
const raw = JSON.parse(line);
const [flag, ...rest] = raw.name.trim().split(/\s+/);
return {
name: raw.name,
title: rest.join(" ").trim(),
flag,
languageLabel: LANGUAGE_LABELS[flag] ?? "Multilingual",
text: raw.text,
promptSpeech: buildAssetUrl(raw.prompt_speech),
generatedSpeech: buildAssetUrl(raw.speech),
category: getCategory(flag),
};
}
// ─── Stats & badges ───────────────────────────────────────
function updateStats(samples) {
const langCount = new Set(samples.map((s) => s.flag)).size;
const langEl = document.querySelector("#languageCount");
const demoEl = document.querySelector("#demoCount");
if (langEl) langEl.textContent = String(langCount);
if (demoEl) demoEl.textContent = String(samples.length);
}
function renderLanguageBadges(samples) {
const container = document.querySelector("#languageBadges");
if (!container) return;
const seen = new Set();
container.textContent = "";
samples.forEach((s) => {
if (seen.has(s.flag)) return;
seen.add(s.flag);
container.append(el("span", "badge", `${s.flag} ${s.languageLabel}`));
});
}
// ─── Sample rendering ─────────────────────────────────────
function renderSamples(samples) {
// Clear containers
Object.values(SAMPLE_CONTAINERS).forEach((c) => {
if (c) c.textContent = "";
});
const indexes = { chinese: 0, english: 0, multilingual: 0 };
samples.forEach((sample) => {
const target = SAMPLE_CONTAINERS[sample.category];
if (!target) return;
const card = createSampleCard(sample, indexes[sample.category]++);
target.append(card);
});
}
function renderError(message) {
Object.values(SAMPLE_CONTAINERS).forEach((c) => {
if (!c) return;
c.textContent = "";
c.append(el("article", "notice-card", message));
});
}
// ─── Data loading ─────────────────────────────────────────
async function loadSamples() {
const response = await fetch("./assets/metadata.jsonl");
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const text = await response.text();
return text.trim().split(/\r?\n/).filter(Boolean).map(parseSample);
}
// ─── Init ─────────────────────────────────────────────────
async function init() {
setupRevealAnimations();
setupSidebarActive();
setupTabs();
try {
const samples = await loadSamples();
updateStats(samples);
renderLanguageBadges(samples);
renderSamples(samples);
setupRevealAnimations(); // observe newly created cards
} catch (err) {
console.error(err);
renderError("Unable to load the demo metadata right now.");
}
}
init();