File size: 10,185 Bytes
aec4421 a2e33f0 ffb5614 a2e33f0 d715e26 a2e33f0 3ab3c26 a2e33f0 aec4421 ffb5614 a2e33f0 aec4421 ffb5614 a2e33f0 d715e26 a2e33f0 ffb5614 aec4421 a2e33f0 ffb5614 aec4421 a2e33f0 736226d a2e33f0 d715e26 736226d a2e33f0 d715e26 a2e33f0 d715e26 a2e33f0 d715e26 a2e33f0 d715e26 a2e33f0 d715e26 a2e33f0 ffb5614 aec4421 a2e33f0 d715e26 a2e33f0 ffb5614 aec4421 a2e33f0 d715e26 a2e33f0 ffb5614 aec4421 ffb5614 d715e26 ffb5614 a2e33f0 ffb5614 aec4421 ffb5614 aec4421 ffb5614 a2e33f0 aec4421 a2e33f0 ffb5614 a2e33f0 aec4421 | 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | document.addEventListener('DOMContentLoaded', async () => {
const voiceInput = document.getElementById('voice-input');
const voiceList = document.getElementById('voice-list');
const voiceClearBtn = document.getElementById('voice-clear-btn');
const generateBtn = document.getElementById('generate-btn');
const textInput = document.getElementById('text-input');
const outputSection = document.getElementById('output-section');
const audioPlayer = document.getElementById('audio-player');
const downloadBtn = document.getElementById('download-btn');
const streamToggle = document.getElementById('stream-toggle');
const formatSelect = document.getElementById('format-select');
let availableVoices = [];
let selectedVoiceId = null;
function updateStreamingAvailability() {
const fmt = formatSelect.value;
const supportsStreaming = ['wav', 'pcm'].includes(fmt);
const infoLabel = document.getElementById('format-info');
if (supportsStreaming) {
streamToggle.disabled = false;
streamToggle.parentElement.title = '';
if (fmt === 'pcm') {
infoLabel.textContent =
"Streaming is available for Raw PCM. Note: This format creates a specialized raw stream that will not play in the browser's audio player.";
} else {
infoLabel.textContent =
'Streaming is available for WAV. The server streams audio chunks for lower latency.';
}
} else {
streamToggle.disabled = true;
streamToggle.checked = false;
streamToggle.parentElement.title =
'Streaming is only available for WAV and PCM formats';
if (fmt === 'mp3') {
infoLabel.textContent =
'Streaming is not available for MP3 (Server limitation). A full file will be generated and played.';
} else if (['opus', 'aac', 'flac'].includes(fmt)) {
infoLabel.textContent = `Streaming is not available for ${fmt.toUpperCase()}. A full file will be generated and played.`;
} else {
infoLabel.textContent = 'Streaming is not available for this format.';
}
}
}
formatSelect.addEventListener('change', updateStreamingAvailability);
updateStreamingAvailability();
async function loadVoices() {
try {
const res = await fetch('/v1/voices');
const data = await res.json();
availableVoices = [];
if (data.data) {
data.data.forEach((voice) => {
availableVoices.push({
id: voice.id,
label: voice.name || voice.id,
display: voice.name || voice.id,
type: voice.type || 'builtin',
});
});
const defaultVoice = availableVoices.find((v) => v.id !== 'custom');
if (defaultVoice) {
selectVoice(defaultVoice.id, false);
}
}
} catch (e) {
console.error('Failed to list voices:', e);
}
}
function selectVoice(id, closeList = true) {
const voice = availableVoices.find((v) => v.id === id);
if (!voice) return;
selectedVoiceId = voice.id;
voiceInput.value = voice.label;
const idDisplay = document.getElementById('voice-id-display');
if (idDisplay) {
if (id !== 'custom') {
const idSpan = idDisplay.querySelector('.voice-id-text');
if (idSpan) {
idSpan.textContent = voice.id;
}
idDisplay.classList.remove('hidden');
} else {
idDisplay.classList.add('hidden');
}
}
voiceClearBtn.disabled = false;
if (closeList) hideVoiceList();
}
function renderVoiceList(filterText = '') {
const normalizedFilter = filterText.trim().toLowerCase();
const fragment = document.createDocumentFragment();
const isDocker = window.SUPERTONIC3_CONFIG?.isDocker || false;
const filtered = availableVoices.filter((v) => {
if (v.id === 'custom' && isDocker) return false;
if (!normalizedFilter) return true;
return (
v.id.toLowerCase().includes(normalizedFilter) ||
v.label.toLowerCase().includes(normalizedFilter) ||
(v.display && v.display.toLowerCase().includes(normalizedFilter))
);
});
voiceList.innerHTML = '';
if (filtered.length === 0) {
const emptyItem = document.createElement('li');
emptyItem.className = 'voice-list-empty';
emptyItem.textContent = 'No matching voices';
voiceList.appendChild(emptyItem);
} else {
filtered.forEach((voice) => {
const item = document.createElement('li');
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'voice-list-item';
btn.dataset.voiceId = voice.id;
const infoDiv = document.createElement('div');
infoDiv.className = 'voice-info';
const nameSpan = document.createElement('span');
nameSpan.className = 'voice-name';
nameSpan.textContent = voice.display || voice.label;
const subSpan = document.createElement('span');
subSpan.className = 'voice-sub';
if (voice.id !== 'custom') {
subSpan.textContent = voice.id;
}
infoDiv.appendChild(nameSpan);
if (subSpan.textContent) infoDiv.appendChild(subSpan);
const badgeSpan = document.createElement('span');
badgeSpan.className = 'voice-badge';
badgeSpan.textContent = 'Default';
badgeSpan.classList.add('badge-builtin');
btn.appendChild(infoDiv);
btn.appendChild(badgeSpan);
item.appendChild(btn);
fragment.appendChild(item);
});
voiceList.appendChild(fragment);
}
}
function showVoiceList() {
voiceList.classList.add('show');
renderVoiceList(
voiceInput.value === getSelectedVoiceLabel() ? '' : voiceInput.value,
);
}
function hideVoiceList() {
setTimeout(() => {
voiceList.classList.remove('show');
}, 150);
}
function getSelectedVoiceLabel() {
const v = availableVoices.find((v) => v.id === selectedVoiceId);
return v ? v.label : '';
}
voiceInput.addEventListener('focus', () => {
voiceInput.select();
showVoiceList();
});
voiceInput.addEventListener('input', () => {
voiceClearBtn.disabled = voiceInput.value.length === 0;
renderVoiceList(voiceInput.value);
voiceList.classList.add('show');
});
voiceInput.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
voiceInput.value = getSelectedVoiceLabel();
hideVoiceList();
voiceInput.blur();
} else if (e.key === 'Enter') {
e.preventDefault();
const { count, firstId } = renderVoiceList(voiceInput.value);
if (count >= 1 && firstId) {
selectVoice(firstId);
voiceInput.blur();
}
}
});
voiceInput.addEventListener('blur', () => {
setTimeout(() => {
if (!document.activeElement.classList.contains('voice-list-item')) {
const val = voiceInput.value.trim();
if (!val) {
voiceInput.value = getSelectedVoiceLabel();
hideVoiceList();
return;
}
if (val === getSelectedVoiceLabel()) {
hideVoiceList();
return;
}
const exact = availableVoices.find(
(v) =>
v.label.toLowerCase() === val.toLowerCase() ||
v.id.toLowerCase() === val.toLowerCase(),
);
if (exact) {
selectVoice(exact.id);
} else {
const { count, firstId } = renderVoiceList(val);
if (count === 1) {
selectVoice(firstId);
} else {
voiceInput.value = getSelectedVoiceLabel();
}
}
hideVoiceList();
}
}, 200);
});
voiceList.addEventListener('mousedown', (e) => {
const btn = e.target.closest('.voice-list-item');
if (btn) {
const id = btn.dataset.voiceId;
selectVoice(id);
}
});
voiceClearBtn.addEventListener('mousedown', (e) => {
e.preventDefault();
selectedVoiceId = null;
voiceInput.value = '';
voiceInput.focus();
renderVoiceList('');
showVoiceList();
voiceClearBtn.disabled = true;
const idDisplay = document.getElementById('voice-id-display');
if (idDisplay) idDisplay.classList.add('hidden');
});
const copyBtn = document.getElementById('voice-id-copy-btn');
if (copyBtn) {
copyBtn.addEventListener('click', async () => {
const idText = document.querySelector('.voice-id-text')?.textContent;
if (idText) {
try {
await navigator.clipboard.writeText(idText);
const originalHTML = copyBtn.innerHTML;
copyBtn.innerHTML = `<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="#2ea043" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>`;
copyBtn.classList.add('copied');
setTimeout(() => {
copyBtn.innerHTML = originalHTML;
copyBtn.classList.remove('copied');
}, 1500);
} catch (err) {
console.error('Failed to copy: ', err);
const input = document.createElement('textarea');
input.value = idText;
document.body.appendChild(input);
input.select();
document.execCommand('copy');
document.body.removeChild(input);
}
}
});
}
generateBtn.addEventListener('click', async () => {
const text = textInput.value.trim();
if (!text) return alert('Please enter text');
let voice = selectedVoiceId;
if (!voice) {
const val = voiceInput.value.trim();
const match = availableVoices.find(
(v) => (v.label === val || v.id === val),
);
if (match) voice = match.id;
}
if (!voice) return alert('Please choose a valid voice from the list');
const stream = streamToggle.checked;
const fmt = formatSelect.value;
generateBtn.classList.add('loading');
generateBtn.disabled = true;
outputSection.classList.remove('active');
try {
const response = await fetch('/v1/audio/speech', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'tts-1',
input: text,
voice: voice,
response_format: fmt,
stream: stream,
}),
});
if (!response.ok) {
const err = await response.json();
throw new Error(err.error || response.statusText);
}
const blob = await response.blob();
const url = URL.createObjectURL(blob);
audioPlayer.src = url;
downloadBtn.href = url;
downloadBtn.download = `generated_speech.${fmt}`;
if (fmt !== 'pcm') {
audioPlayer
.play()
.catch((e) => console.warn('Auto-play blocked or failed:', e));
}
outputSection.classList.add('active');
} catch (e) {
alert('Error generating speech: ' + e.message);
} finally {
generateBtn.classList.remove('loading');
generateBtn.disabled = false;
}
});
await loadVoices();
});
|