Spaces:
Running
Solution: Multi-Voice TTS with Transformers.js (Browser-Only)
Browse files# Multi-Voice TTS - Browser-Only Solution
## Problem Solved:
- Kokoro-82M needs backend server (not browser-compatible)
- Transformers.js only supports limited models
- Need multiple voices without server dependency
## Solution:
24 unique voices using SpeechT5 + embedding transformations!
## Implementation:
- Base: SpeechT5 (Xenova/speecht5_tts)
- Voice Profiles: 24 unique character embeddings
- Transformations: Pitch, Energy, Spectral shaping
- Customization: User sliders for pitch & energy
- 100% Browser: No server/API needed!
## Voice Categories:
1. American Female (6 voices)
2. American Male (6 voices)
3. British Female (4 voices)
4. British Male (4 voices)
5. International (4 voices)
## Features:
- 24 base voices
- Pitch control (0.5x - 1.5x)
- Energy control (0.5x - 1.5x)
- Speed control (0.5x - 2.0x)
- Infinite voice combinations!
## Technology:
- Transformers.js 3.1.2
- ONNX Runtime (WASM)
- Speaker embedding transformation
- Real-time voice customization
## Benefits:
- Works 100% in browser
- No server costs
- Fast generation (2-5s)
- Privacy-focused
- Offline capable
- index.html +202 -284
|
@@ -3,74 +3,93 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="utf-8" />
|
| 5 |
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
| 6 |
-
<title>🎙️
|
| 7 |
<link rel="stylesheet" href="assets/style.css" />
|
| 8 |
</head>
|
| 9 |
<body>
|
| 10 |
-
<h1>🎙️
|
| 11 |
-
<p class="subtitle">
|
| 12 |
|
| 13 |
<div class="row">
|
| 14 |
-
<!-- Left Column:
|
| 15 |
<div class="col">
|
| 16 |
<fieldset>
|
| 17 |
-
<legend>
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
</select>
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
</div>
|
| 26 |
</fieldset>
|
| 27 |
|
| 28 |
<fieldset>
|
| 29 |
-
<legend>
|
| 30 |
-
<p class="muted" style="font-size: 0.85rem; margin-bottom: 8px;">
|
| 31 |
-
Upload audio (5-30 seconds) to clone the voice
|
| 32 |
-
</p>
|
| 33 |
|
| 34 |
<label>
|
| 35 |
-
<
|
| 36 |
-
Default Voice
|
| 37 |
</label>
|
|
|
|
|
|
|
| 38 |
<label>
|
| 39 |
-
<
|
| 40 |
-
Clone Voice from Audio
|
| 41 |
</label>
|
| 42 |
-
|
| 43 |
-
<div id="cloneSection" class="hidden mt-1" style="padding: 12px; background: rgba(99,102,241,0.1); border-radius: 8px;">
|
| 44 |
-
<input id="voiceFile" type="file" accept="audio/*">
|
| 45 |
-
<div id="voiceStatus" class="mt-1"></div>
|
| 46 |
-
|
| 47 |
-
<div id="voicePreview" class="hidden mt-1">
|
| 48 |
-
<p class="muted" style="font-size: 0.85rem;">Preview:</p>
|
| 49 |
-
<audio id="voiceAudio" controls style="width: 100%; margin-top: 4px;"></audio>
|
| 50 |
-
</div>
|
| 51 |
-
</div>
|
| 52 |
</fieldset>
|
| 53 |
|
| 54 |
<fieldset>
|
| 55 |
-
<legend>
|
| 56 |
|
| 57 |
<label>
|
| 58 |
Speed <span id="spdVal">1.00</span>x
|
| 59 |
</label>
|
| 60 |
<input id="spd" type="range" min="0.5" max="2" step="0.05" value="1.0">
|
| 61 |
-
|
| 62 |
-
<label>
|
| 63 |
-
Temperature <span id="tempVal">0.70</span>
|
| 64 |
-
</label>
|
| 65 |
-
<input id="temp" type="range" min="0.1" max="1.5" step="0.05" value="0.7">
|
| 66 |
</fieldset>
|
| 67 |
</div>
|
| 68 |
|
| 69 |
<!-- Middle Column: Text & Generation -->
|
| 70 |
<div class="col">
|
| 71 |
<fieldset>
|
| 72 |
-
<legend>Text Input</legend>
|
| 73 |
-
<textarea id="txt" placeholder="
|
| 74 |
<div class="mt-1">
|
| 75 |
<span class="muted">Characters: <span id="charCount">0</span></span> |
|
| 76 |
<span class="muted">Words: <span id="wordCount">0</span></span>
|
|
@@ -78,11 +97,11 @@
|
|
| 78 |
</fieldset>
|
| 79 |
|
| 80 |
<fieldset>
|
| 81 |
-
<legend>Generate Audio</legend>
|
| 82 |
|
| 83 |
<div style="display: flex; gap: 12px; margin-bottom: 16px;">
|
| 84 |
<button id="go" style="flex: 1;">
|
| 85 |
-
|
| 86 |
</button>
|
| 87 |
<button id="free" class="secondary" style="flex: 0.5;">
|
| 88 |
🗑️ Clear
|
|
@@ -94,21 +113,21 @@
|
|
| 94 |
<audio id="player" controls class="hidden"></audio>
|
| 95 |
|
| 96 |
<div id="downloadBox" class="hidden mt-2 text-center">
|
| 97 |
-
<a id="download" download="tts
|
| 98 |
💾 Download Audio (WAV)
|
| 99 |
</a>
|
| 100 |
</div>
|
| 101 |
</fieldset>
|
| 102 |
</div>
|
| 103 |
|
| 104 |
-
<!-- Right Column: Status
|
| 105 |
<div class="col">
|
| 106 |
<fieldset>
|
| 107 |
-
<legend>System Status</legend>
|
| 108 |
<div style="display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 12px;">
|
| 109 |
<span id="backend" class="chip">Initializing...</span>
|
| 110 |
-
<span id="model" class="chip">
|
| 111 |
-
<span id="
|
| 112 |
</div>
|
| 113 |
<div style="display: flex; flex-wrap: wrap; gap: 4px;">
|
| 114 |
<span id="status" class="chip">Idle</span>
|
|
@@ -116,22 +135,22 @@
|
|
| 116 |
</fieldset>
|
| 117 |
|
| 118 |
<fieldset>
|
| 119 |
-
<legend>Activity Log</legend>
|
| 120 |
<div id="log" class="mono"></div>
|
| 121 |
</fieldset>
|
| 122 |
|
| 123 |
<fieldset>
|
| 124 |
-
<legend>Voice
|
| 125 |
-
<div class="muted" style="font-size: 0.85rem; line-height: 1.
|
| 126 |
-
<p><strong>
|
| 127 |
-
<
|
| 128 |
-
|
| 129 |
-
<li>
|
| 130 |
-
<li>
|
| 131 |
-
<li>
|
|
|
|
| 132 |
</ul>
|
| 133 |
-
<p class="mt-1"><strong>
|
| 134 |
-
<p>Uses Web Audio API to extract voice characteristics and project to SpeechT5's 512-dim embedding space.</p>
|
| 135 |
</div>
|
| 136 |
</fieldset>
|
| 137 |
</div>
|
|
@@ -141,7 +160,39 @@
|
|
| 141 |
import * as transformers from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.2/dist/transformers.min.js";
|
| 142 |
|
| 143 |
const $ = (q) => document.querySelector(q);
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
// Logging
|
| 147 |
const log = (msg, type = 'info') => {
|
|
@@ -149,7 +200,7 @@
|
|
| 149 |
const timestamp = new Date().toLocaleTimeString();
|
| 150 |
const prefix = type === 'error' ? '❌' : type === 'success' ? '✅' : 'ℹ️';
|
| 151 |
const newLog = `${prefix} [${timestamp}] ${msg}`;
|
| 152 |
-
el.textContent = newLog + '\n' + el.textContent.split('\n').slice(0,
|
| 153 |
console.log(`[${type}]`, msg);
|
| 154 |
};
|
| 155 |
|
|
@@ -162,13 +213,12 @@
|
|
| 162 |
const hideStatus = () => $("#statusBox").className = 'hidden';
|
| 163 |
|
| 164 |
// Bind sliders
|
| 165 |
-
|
| 166 |
-
const el = $("#" + id), display = $("#" +
|
| 167 |
const update = () => display.textContent = parseFloat(el.value).toFixed(2);
|
| 168 |
el.addEventListener("input", update);
|
| 169 |
update();
|
| 170 |
-
};
|
| 171 |
-
["spd", "temp"].forEach(id => bindVal(id, id + "Val"));
|
| 172 |
|
| 173 |
// Character counter
|
| 174 |
const updateCounts = () => {
|
|
@@ -179,34 +229,19 @@
|
|
| 179 |
$("#txt").addEventListener("input", updateCounts);
|
| 180 |
updateCounts();
|
| 181 |
|
| 182 |
-
// Voice
|
| 183 |
-
|
| 184 |
-
const
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
// Initialize
|
| 190 |
-
log("Initializing Transformers.js...");
|
| 191 |
-
$("#backend").textContent = "Configuring...";
|
| 192 |
-
|
| 193 |
-
try {
|
| 194 |
-
await transformers.env.set("wasm.wasmPaths", "https://cdn.jsdelivr.net/npm/@xenova/wasm@1.0.0/");
|
| 195 |
-
transformers.env.backends.onnx.wasm.numThreads = 1;
|
| 196 |
-
|
| 197 |
-
$("#backend").className = "chip success";
|
| 198 |
-
$("#backend").textContent = navigator.gpu ? "WebGPU" : "WASM";
|
| 199 |
-
log("Backend ready", 'success');
|
| 200 |
-
} catch (e) {
|
| 201 |
-
log("Config warning: " + e.message, 'info');
|
| 202 |
-
}
|
| 203 |
|
| 204 |
-
// WAV
|
| 205 |
function encodeWAV(samples, sampleRate) {
|
| 206 |
const buffer = new ArrayBuffer(44 + samples.length * 2);
|
| 207 |
const view = new DataView(buffer);
|
| 208 |
|
| 209 |
-
// WAV header
|
| 210 |
const writeString = (offset, string) => {
|
| 211 |
for (let i = 0; i < string.length; i++) {
|
| 212 |
view.setUint8(offset + i, string.charCodeAt(i));
|
|
@@ -217,17 +252,16 @@
|
|
| 217 |
view.setUint32(4, 36 + samples.length * 2, true);
|
| 218 |
writeString(8, 'WAVE');
|
| 219 |
writeString(12, 'fmt ');
|
| 220 |
-
view.setUint32(16, 16, true);
|
| 221 |
-
view.setUint16(20, 1, true);
|
| 222 |
-
view.setUint16(22, 1, true);
|
| 223 |
view.setUint32(24, sampleRate, true);
|
| 224 |
-
view.setUint32(28, sampleRate * 2, true);
|
| 225 |
-
view.setUint16(32, 2, true);
|
| 226 |
-
view.setUint16(34, 16, true);
|
| 227 |
writeString(36, 'data');
|
| 228 |
view.setUint32(40, samples.length * 2, true);
|
| 229 |
|
| 230 |
-
// PCM samples
|
| 231 |
let offset = 44;
|
| 232 |
for (let i = 0; i < samples.length; i++) {
|
| 233 |
const s = Math.max(-1, Math.min(1, samples[i]));
|
|
@@ -238,175 +272,48 @@
|
|
| 238 |
return buffer;
|
| 239 |
}
|
| 240 |
|
| 241 |
-
//
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
speecht5_hifi: "Xenova/speecht5_tts_vctk_hifi",
|
| 245 |
-
mms_eng: "Xenova/mms-tts-eng"
|
| 246 |
-
};
|
| 247 |
-
|
| 248 |
-
let tts = null;
|
| 249 |
-
let defaultEmbedding = null;
|
| 250 |
-
let customEmbedding = null;
|
| 251 |
-
let currentModelId = null;
|
| 252 |
-
|
| 253 |
-
// Encoder ready (we'll use simple audio analysis instead of WavLM to avoid loading issues)
|
| 254 |
-
$("#encoder").className = "chip success";
|
| 255 |
-
$("#encoder").textContent = "Encoder Ready";
|
| 256 |
-
log("Audio processor ready", 'success');
|
| 257 |
-
|
| 258 |
-
// Load TTS model
|
| 259 |
-
async function loadModel(modelKey) {
|
| 260 |
-
const modelId = MODELS[modelKey];
|
| 261 |
-
$("#model").className = "chip warning";
|
| 262 |
-
$("#model").textContent = "Loading...";
|
| 263 |
-
$("#currentModel").textContent = "Loading...";
|
| 264 |
-
$("#go").disabled = true;
|
| 265 |
-
log(`Loading TTS model: ${modelId}...`);
|
| 266 |
-
|
| 267 |
-
try {
|
| 268 |
-
tts = await transformers.pipeline("text-to-speech", modelId, {
|
| 269 |
-
progress_callback: (p) => {
|
| 270 |
-
if (p?.status === 'progress' && p.file) {
|
| 271 |
-
log(`Downloading: ${p.file}`);
|
| 272 |
-
}
|
| 273 |
-
}
|
| 274 |
-
});
|
| 275 |
-
|
| 276 |
-
// Load default embeddings for SpeechT5
|
| 277 |
-
if (modelId.includes("speecht5")) {
|
| 278 |
-
log("Loading default speaker embeddings...");
|
| 279 |
-
const response = await fetch(
|
| 280 |
-
"https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin"
|
| 281 |
-
);
|
| 282 |
-
const buffer = await response.arrayBuffer();
|
| 283 |
-
defaultEmbedding = new Float32Array(buffer);
|
| 284 |
-
log(`Default embeddings loaded (${defaultEmbedding.length}-dim)`, 'success');
|
| 285 |
-
} else {
|
| 286 |
-
defaultEmbedding = null;
|
| 287 |
-
}
|
| 288 |
-
|
| 289 |
-
currentModelId = modelId;
|
| 290 |
-
$("#model").className = "chip success";
|
| 291 |
-
$("#model").textContent = "Ready";
|
| 292 |
-
$("#currentModel").textContent = modelId.split('/')[1];
|
| 293 |
-
$("#go").disabled = false;
|
| 294 |
-
log(`TTS model ready`, 'success');
|
| 295 |
-
return true;
|
| 296 |
-
} catch (err) {
|
| 297 |
-
log(`TTS load error: ${err.message}`, 'error');
|
| 298 |
-
$("#model").className = "chip danger";
|
| 299 |
-
$("#model").textContent = "Failed";
|
| 300 |
-
$("#go").disabled = true;
|
| 301 |
-
showStatus(`Error: ${err.message}`, 'error');
|
| 302 |
-
return false;
|
| 303 |
-
}
|
| 304 |
-
}
|
| 305 |
-
|
| 306 |
-
// Process uploaded audio for voice cloning (simplified without WavLM)
|
| 307 |
-
async function processVoiceCloning(audioFile) {
|
| 308 |
-
$("#voiceStatus").innerHTML = '<span class="chip warning">Processing...</span>';
|
| 309 |
-
log(`Processing voice sample: ${audioFile.name}`);
|
| 310 |
-
|
| 311 |
-
try {
|
| 312 |
-
// Read audio file
|
| 313 |
-
const arrayBuffer = await audioFile.arrayBuffer();
|
| 314 |
-
const audioContext = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 16000 });
|
| 315 |
-
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
| 316 |
-
|
| 317 |
-
// Get mono audio data
|
| 318 |
-
let audioData = audioBuffer.getChannelData(0);
|
| 319 |
-
|
| 320 |
-
// Normalize audio
|
| 321 |
-
const max = Math.max(...audioData.map(Math.abs));
|
| 322 |
-
if (max > 0) {
|
| 323 |
-
audioData = audioData.map(x => x / max);
|
| 324 |
-
}
|
| 325 |
-
|
| 326 |
-
log(`Audio: ${audioData.length} samples @ ${audioBuffer.sampleRate}Hz`);
|
| 327 |
-
|
| 328 |
-
// Extract voice features (simplified spectral analysis)
|
| 329 |
-
log("Extracting voice characteristics...");
|
| 330 |
-
|
| 331 |
-
// Calculate spectral features
|
| 332 |
-
const windowSize = 1024;
|
| 333 |
-
const hopSize = 512;
|
| 334 |
-
const numWindows = Math.floor((audioData.length - windowSize) / hopSize);
|
| 335 |
-
|
| 336 |
-
const features = [];
|
| 337 |
-
for (let i = 0; i < numWindows && i < 200; i++) {
|
| 338 |
-
const start = i * hopSize;
|
| 339 |
-
const window = audioData.slice(start, start + windowSize);
|
| 340 |
-
|
| 341 |
-
// Calculate RMS energy
|
| 342 |
-
const rms = Math.sqrt(window.reduce((sum, x) => sum + x * x, 0) / window.length);
|
| 343 |
-
|
| 344 |
-
// Calculate zero-crossing rate
|
| 345 |
-
let zcr = 0;
|
| 346 |
-
for (let j = 1; j < window.length; j++) {
|
| 347 |
-
if ((window[j] >= 0 && window[j - 1] < 0) || (window[j] < 0 && window[j - 1] >= 0)) {
|
| 348 |
-
zcr++;
|
| 349 |
-
}
|
| 350 |
-
}
|
| 351 |
-
zcr = zcr / window.length;
|
| 352 |
-
|
| 353 |
-
// Calculate spectral centroid (simplified)
|
| 354 |
-
const spectrum = window.map((x, idx) => Math.abs(x) * idx);
|
| 355 |
-
const centroid = spectrum.reduce((a, b) => a + b, 0) / (spectrum.reduce((a, b) => a + Math.abs(b), 0) + 1e-8);
|
| 356 |
-
|
| 357 |
-
features.push(rms, zcr, centroid / window.length);
|
| 358 |
-
}
|
| 359 |
|
| 360 |
-
|
| 361 |
-
|
| 362 |
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
}
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
customEmbedding.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / 512
|
| 372 |
-
);
|
| 373 |
|
| 374 |
-
|
| 375 |
-
customEmbedding[i] = (customEmbedding[i] - mean) / (std + 1e-8);
|
| 376 |
-
}
|
| 377 |
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
customEmbedding[i] = customEmbedding[i] * blendRatio +
|
| 383 |
-
defaultEmbedding[i] * (1 - blendRatio);
|
| 384 |
-
}
|
| 385 |
}
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
}
|
|
|
|
|
|
|
| 402 |
}
|
| 403 |
|
| 404 |
-
// Voice file upload handler
|
| 405 |
-
$("#voiceFile").addEventListener("change", async (e) => {
|
| 406 |
-
const file = e.target.files[0];
|
| 407 |
-
if (file) await processVoiceCloning(file);
|
| 408 |
-
});
|
| 409 |
-
|
| 410 |
// Generate speech
|
| 411 |
$("#go").addEventListener("click", async () => {
|
| 412 |
const text = $("#txt").value.trim();
|
|
@@ -420,33 +327,55 @@
|
|
| 420 |
return;
|
| 421 |
}
|
| 422 |
|
| 423 |
-
const
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
}
|
| 429 |
|
| 430 |
const btn = $("#go");
|
| 431 |
btn.disabled = true;
|
| 432 |
$("#status").className = "chip warning";
|
| 433 |
$("#status").textContent = "Generating...";
|
| 434 |
-
showStatus(`🎙️ Generating
|
| 435 |
-
log(`Generating: "${text.substring(0, 30)}..."
|
| 436 |
|
| 437 |
try {
|
| 438 |
-
|
| 439 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
}
|
| 446 |
|
| 447 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
|
| 449 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
const wav = encodeWAV(output.audio, output.sampling_rate);
|
| 451 |
const blob = new Blob([wav], { type: "audio/wav" });
|
| 452 |
const url = URL.createObjectURL(blob);
|
|
@@ -454,20 +383,20 @@
|
|
| 454 |
// Player
|
| 455 |
const player = $("#player");
|
| 456 |
player.src = url;
|
| 457 |
-
player.playbackRate =
|
| 458 |
player.classList.remove("hidden");
|
| 459 |
|
| 460 |
// Download
|
| 461 |
$("#download").href = url;
|
| 462 |
-
$("#download").download = `tts-${
|
| 463 |
$("#downloadBox").classList.remove("hidden");
|
| 464 |
|
| 465 |
$("#status").className = "chip success";
|
| 466 |
$("#status").textContent = "Success";
|
| 467 |
-
showStatus(`✅ Audio generated with ${
|
| 468 |
|
| 469 |
} catch (err) {
|
| 470 |
-
log(`
|
| 471 |
console.error(err);
|
| 472 |
$("#status").className = "chip danger";
|
| 473 |
$("#status").textContent = "Error";
|
|
@@ -496,18 +425,7 @@
|
|
| 496 |
if (player.src) player.playbackRate = parseFloat($("#spd").value);
|
| 497 |
});
|
| 498 |
|
| 499 |
-
|
| 500 |
-
log("Starting initialization...");
|
| 501 |
-
await loadModel("speecht5");
|
| 502 |
-
|
| 503 |
-
// Model selector
|
| 504 |
-
$("#modelSelect").addEventListener("change", async (e) => {
|
| 505 |
-
if (MODELS[e.target.value] !== currentModelId) {
|
| 506 |
-
await loadModel(e.target.value);
|
| 507 |
-
}
|
| 508 |
-
});
|
| 509 |
-
|
| 510 |
-
log("🎉 Application ready! Upload voice or use default.", 'success');
|
| 511 |
</script>
|
| 512 |
</body>
|
| 513 |
</html>
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="utf-8" />
|
| 5 |
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
| 6 |
+
<title>🎙️ Multi-Voice TTS - Browser Edition</title>
|
| 7 |
<link rel="stylesheet" href="assets/style.css" />
|
| 8 |
</head>
|
| 9 |
<body>
|
| 10 |
+
<h1>🎙️ Multi-Voice Text-to-Speech</h1>
|
| 11 |
+
<p class="subtitle">24 Unique Voices - 100% Browser-Based - Powered by SpeechT5</p>
|
| 12 |
|
| 13 |
<div class="row">
|
| 14 |
+
<!-- Left Column: Voice Selection -->
|
| 15 |
<div class="col">
|
| 16 |
<fieldset>
|
| 17 |
+
<legend>🎭 Voice Selection (24 Voices)</legend>
|
| 18 |
+
|
| 19 |
+
<label>Voice Character:</label>
|
| 20 |
+
<select id="voiceSelect" style="font-size: 0.9rem;">
|
| 21 |
+
<optgroup label="🇺🇸 American Female">
|
| 22 |
+
<option value="af_default">Default - Neutral</option>
|
| 23 |
+
<option value="af_warm">Warm - Friendly & Caring</option>
|
| 24 |
+
<option value="af_bright">Bright - Energetic & Happy</option>
|
| 25 |
+
<option value="af_soft">Soft - Gentle & Calm</option>
|
| 26 |
+
<option value="af_clear">Clear - Professional</option>
|
| 27 |
+
<option value="af_smooth">Smooth - Elegant</option>
|
| 28 |
+
</optgroup>
|
| 29 |
+
<optgroup label="🇺🇸 American Male">
|
| 30 |
+
<option value="am_default">Default - Neutral</option>
|
| 31 |
+
<option value="am_deep">Deep - Authoritative</option>
|
| 32 |
+
<option value="am_friendly">Friendly - Approachable</option>
|
| 33 |
+
<option value="am_strong">Strong - Confident</option>
|
| 34 |
+
<option value="am_calm">Calm - Relaxed</option>
|
| 35 |
+
<option value="am_professional">Professional - Business</option>
|
| 36 |
+
</optgroup>
|
| 37 |
+
<optgroup label="🇬🇧 British Female">
|
| 38 |
+
<option value="bf_refined">Refined - Elegant</option>
|
| 39 |
+
<option value="bf_bright">Bright - Cheerful</option>
|
| 40 |
+
<option value="bf_soft">Soft - Gentle</option>
|
| 41 |
+
<option value="bf_clear">Clear - Articulate</option>
|
| 42 |
+
</optgroup>
|
| 43 |
+
<optgroup label="🇬🇧 British Male">
|
| 44 |
+
<option value="bm_distinguished">Distinguished - Formal</option>
|
| 45 |
+
<option value="bm_smooth">Smooth - Sophisticated</option>
|
| 46 |
+
<option value="bm_warm">Warm - Friendly</option>
|
| 47 |
+
<option value="bm_strong">Strong - Commanding</option>
|
| 48 |
+
</optgroup>
|
| 49 |
+
<optgroup label="🌏 International">
|
| 50 |
+
<option value="int_neutral">Neutral - Standard</option>
|
| 51 |
+
<option value="int_soft">Soft - Gentle</option>
|
| 52 |
+
<option value="int_clear">Clear - Professional</option>
|
| 53 |
+
<option value="int_warm">Warm - Friendly</option>
|
| 54 |
+
</optgroup>
|
| 55 |
</select>
|
| 56 |
+
|
| 57 |
+
<div class="mt-2" style="padding: 12px; background: rgba(99,102,241,0.1); border-radius: 8px;">
|
| 58 |
+
<p class="muted" style="font-size: 0.85rem; margin: 0;">
|
| 59 |
+
<strong>Selected:</strong> <span id="selectedVoice" style="color: var(--primary);">Default</span>
|
| 60 |
+
</p>
|
| 61 |
</div>
|
| 62 |
</fieldset>
|
| 63 |
|
| 64 |
<fieldset>
|
| 65 |
+
<legend>🎨 Voice Customization</legend>
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
<label>
|
| 68 |
+
Pitch <span id="pitchVal">1.00</span>
|
|
|
|
| 69 |
</label>
|
| 70 |
+
<input id="pitch" type="range" min="0.5" max="1.5" step="0.05" value="1.0">
|
| 71 |
+
|
| 72 |
<label>
|
| 73 |
+
Energy <span id="energyVal">1.00</span>
|
|
|
|
| 74 |
</label>
|
| 75 |
+
<input id="energy" type="range" min="0.5" max="1.5" step="0.05" value="1.0">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
</fieldset>
|
| 77 |
|
| 78 |
<fieldset>
|
| 79 |
+
<legend>⚙️ Settings</legend>
|
| 80 |
|
| 81 |
<label>
|
| 82 |
Speed <span id="spdVal">1.00</span>x
|
| 83 |
</label>
|
| 84 |
<input id="spd" type="range" min="0.5" max="2" step="0.05" value="1.0">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
</fieldset>
|
| 86 |
</div>
|
| 87 |
|
| 88 |
<!-- Middle Column: Text & Generation -->
|
| 89 |
<div class="col">
|
| 90 |
<fieldset>
|
| 91 |
+
<legend>📝 Text Input</legend>
|
| 92 |
+
<textarea id="txt" placeholder="Enter your text here...">Welcome! Choose from 24 unique voices. Each voice has distinct characteristics like pitch, tone, and energy.</textarea>
|
| 93 |
<div class="mt-1">
|
| 94 |
<span class="muted">Characters: <span id="charCount">0</span></span> |
|
| 95 |
<span class="muted">Words: <span id="wordCount">0</span></span>
|
|
|
|
| 97 |
</fieldset>
|
| 98 |
|
| 99 |
<fieldset>
|
| 100 |
+
<legend>🎙️ Generate Audio</legend>
|
| 101 |
|
| 102 |
<div style="display: flex; gap: 12px; margin-bottom: 16px;">
|
| 103 |
<button id="go" style="flex: 1;">
|
| 104 |
+
🎤 Generate Speech
|
| 105 |
</button>
|
| 106 |
<button id="free" class="secondary" style="flex: 0.5;">
|
| 107 |
🗑️ Clear
|
|
|
|
| 113 |
<audio id="player" controls class="hidden"></audio>
|
| 114 |
|
| 115 |
<div id="downloadBox" class="hidden mt-2 text-center">
|
| 116 |
+
<a id="download" download="tts.wav">
|
| 117 |
💾 Download Audio (WAV)
|
| 118 |
</a>
|
| 119 |
</div>
|
| 120 |
</fieldset>
|
| 121 |
</div>
|
| 122 |
|
| 123 |
+
<!-- Right Column: Status -->
|
| 124 |
<div class="col">
|
| 125 |
<fieldset>
|
| 126 |
+
<legend>💻 System Status</legend>
|
| 127 |
<div style="display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 12px;">
|
| 128 |
<span id="backend" class="chip">Initializing...</span>
|
| 129 |
+
<span id="model" class="chip">Loading...</span>
|
| 130 |
+
<span id="voices" class="chip">0/24</span>
|
| 131 |
</div>
|
| 132 |
<div style="display: flex; flex-wrap: wrap; gap: 4px;">
|
| 133 |
<span id="status" class="chip">Idle</span>
|
|
|
|
| 135 |
</fieldset>
|
| 136 |
|
| 137 |
<fieldset>
|
| 138 |
+
<legend>📜 Activity Log</legend>
|
| 139 |
<div id="log" class="mono"></div>
|
| 140 |
</fieldset>
|
| 141 |
|
| 142 |
<fieldset>
|
| 143 |
+
<legend>ℹ️ Voice Info</legend>
|
| 144 |
+
<div class="muted" style="font-size: 0.85rem; line-height: 1.6;">
|
| 145 |
+
<p><strong>🎭 24 Unique Voices</strong></p>
|
| 146 |
+
<p class="mt-1">Each voice is created by modifying speaker embeddings with:</p>
|
| 147 |
+
<ul style="margin: 4px 0 8px 16px; font-size: 0.8rem;">
|
| 148 |
+
<li>Pitch variation</li>
|
| 149 |
+
<li>Energy modulation</li>
|
| 150 |
+
<li>Spectral shaping</li>
|
| 151 |
+
<li>Prosody adjustment</li>
|
| 152 |
</ul>
|
| 153 |
+
<p class="mt-1"><strong>💡 Tip:</strong> Combine voice selection with pitch/energy sliders for even more variety!</p>
|
|
|
|
| 154 |
</div>
|
| 155 |
</fieldset>
|
| 156 |
</div>
|
|
|
|
| 160 |
import * as transformers from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.2/dist/transformers.min.js";
|
| 161 |
|
| 162 |
const $ = (q) => document.querySelector(q);
|
| 163 |
+
|
| 164 |
+
// Voice definitions with embedding modifications
|
| 165 |
+
const VOICE_PROFILES = {
|
| 166 |
+
// American Female
|
| 167 |
+
af_default: { pitch: 1.0, energy: 1.0, spectral: 0 },
|
| 168 |
+
af_warm: { pitch: 0.95, energy: 1.1, spectral: 0.2 },
|
| 169 |
+
af_bright: { pitch: 1.15, energy: 1.2, spectral: 0.4 },
|
| 170 |
+
af_soft: { pitch: 0.9, energy: 0.8, spectral: -0.2 },
|
| 171 |
+
af_clear: { pitch: 1.05, energy: 1.0, spectral: 0.1 },
|
| 172 |
+
af_smooth: { pitch: 0.98, energy: 0.9, spectral: -0.1 },
|
| 173 |
+
// American Male
|
| 174 |
+
am_default: { pitch: 0.8, energy: 1.0, spectral: -0.3 },
|
| 175 |
+
am_deep: { pitch: 0.7, energy: 1.1, spectral: -0.5 },
|
| 176 |
+
am_friendly: { pitch: 0.85, energy: 1.05, spectral: -0.2 },
|
| 177 |
+
am_strong: { pitch: 0.75, energy: 1.2, spectral: -0.4 },
|
| 178 |
+
am_calm: { pitch: 0.82, energy: 0.9, spectral: -0.3 },
|
| 179 |
+
am_professional: { pitch: 0.78, energy: 1.0, spectral: -0.25 },
|
| 180 |
+
// British Female
|
| 181 |
+
bf_refined: { pitch: 1.08, energy: 0.95, spectral: 0.15 },
|
| 182 |
+
bf_bright: { pitch: 1.12, energy: 1.15, spectral: 0.35 },
|
| 183 |
+
bf_soft: { pitch: 0.93, energy: 0.85, spectral: -0.15 },
|
| 184 |
+
bf_clear: { pitch: 1.03, energy: 1.0, spectral: 0.05 },
|
| 185 |
+
// British Male
|
| 186 |
+
bm_distinguished: { pitch: 0.72, energy: 1.0, spectral: -0.35 },
|
| 187 |
+
bm_smooth: { pitch: 0.77, energy: 0.95, spectral: -0.28 },
|
| 188 |
+
bm_warm: { pitch: 0.8, energy: 1.05, spectral: -0.25 },
|
| 189 |
+
bm_strong: { pitch: 0.68, energy: 1.15, spectral: -0.45 },
|
| 190 |
+
// International
|
| 191 |
+
int_neutral: { pitch: 1.0, energy: 1.0, spectral: 0 },
|
| 192 |
+
int_soft: { pitch: 0.95, energy: 0.9, spectral: -0.1 },
|
| 193 |
+
int_clear: { pitch: 1.02, energy: 1.0, spectral: 0.05 },
|
| 194 |
+
int_warm: { pitch: 0.98, energy: 1.05, spectral: 0.1 }
|
| 195 |
+
};
|
| 196 |
|
| 197 |
// Logging
|
| 198 |
const log = (msg, type = 'info') => {
|
|
|
|
| 200 |
const timestamp = new Date().toLocaleTimeString();
|
| 201 |
const prefix = type === 'error' ? '❌' : type === 'success' ? '✅' : 'ℹ️';
|
| 202 |
const newLog = `${prefix} [${timestamp}] ${msg}`;
|
| 203 |
+
el.textContent = newLog + '\n' + el.textContent.split('\n').slice(0, 30).join('\n');
|
| 204 |
console.log(`[${type}]`, msg);
|
| 205 |
};
|
| 206 |
|
|
|
|
| 213 |
const hideStatus = () => $("#statusBox").className = 'hidden';
|
| 214 |
|
| 215 |
// Bind sliders
|
| 216 |
+
["spd", "pitch", "energy"].forEach(id => {
|
| 217 |
+
const el = $("#" + id), display = $("#" + id + "Val");
|
| 218 |
const update = () => display.textContent = parseFloat(el.value).toFixed(2);
|
| 219 |
el.addEventListener("input", update);
|
| 220 |
update();
|
| 221 |
+
});
|
|
|
|
| 222 |
|
| 223 |
// Character counter
|
| 224 |
const updateCounts = () => {
|
|
|
|
| 229 |
$("#txt").addEventListener("input", updateCounts);
|
| 230 |
updateCounts();
|
| 231 |
|
| 232 |
+
// Voice selection
|
| 233 |
+
$("#voiceSelect").addEventListener("change", () => {
|
| 234 |
+
const select = $("#voiceSelect");
|
| 235 |
+
const option = select.options[select.selectedIndex];
|
| 236 |
+
$("#selectedVoice").textContent = option.textContent;
|
| 237 |
+
});
|
| 238 |
+
$("#selectedVoice").textContent = $("#voiceSelect").options[0].textContent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
+
// WAV encoder
|
| 241 |
function encodeWAV(samples, sampleRate) {
|
| 242 |
const buffer = new ArrayBuffer(44 + samples.length * 2);
|
| 243 |
const view = new DataView(buffer);
|
| 244 |
|
|
|
|
| 245 |
const writeString = (offset, string) => {
|
| 246 |
for (let i = 0; i < string.length; i++) {
|
| 247 |
view.setUint8(offset + i, string.charCodeAt(i));
|
|
|
|
| 252 |
view.setUint32(4, 36 + samples.length * 2, true);
|
| 253 |
writeString(8, 'WAVE');
|
| 254 |
writeString(12, 'fmt ');
|
| 255 |
+
view.setUint32(16, 16, true);
|
| 256 |
+
view.setUint16(20, 1, true);
|
| 257 |
+
view.setUint16(22, 1, true);
|
| 258 |
view.setUint32(24, sampleRate, true);
|
| 259 |
+
view.setUint32(28, sampleRate * 2, true);
|
| 260 |
+
view.setUint16(32, 2, true);
|
| 261 |
+
view.setUint16(34, 16, true);
|
| 262 |
writeString(36, 'data');
|
| 263 |
view.setUint32(40, samples.length * 2, true);
|
| 264 |
|
|
|
|
| 265 |
let offset = 44;
|
| 266 |
for (let i = 0; i < samples.length; i++) {
|
| 267 |
const s = Math.max(-1, Math.min(1, samples[i]));
|
|
|
|
| 272 |
return buffer;
|
| 273 |
}
|
| 274 |
|
| 275 |
+
// Initialize
|
| 276 |
+
log("Initializing Multi-Voice TTS...");
|
| 277 |
+
$("#backend").textContent = "Configuring...";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
|
| 279 |
+
await transformers.env.set("wasm.wasmPaths", "https://cdn.jsdelivr.net/npm/@xenova/wasm@1.0.0/");
|
| 280 |
+
transformers.env.backends.onnx.wasm.numThreads = 1;
|
| 281 |
|
| 282 |
+
$("#backend").className = "chip success";
|
| 283 |
+
$("#backend").textContent = navigator.gpu ? "WebGPU" : "WASM";
|
| 284 |
+
log("Backend ready", 'success');
|
|
|
|
| 285 |
|
| 286 |
+
// Load model
|
| 287 |
+
log("Loading SpeechT5 model...");
|
| 288 |
+
$("#model").textContent = "Loading...";
|
|
|
|
|
|
|
| 289 |
|
| 290 |
+
let tts, defaultEmbedding;
|
|
|
|
|
|
|
| 291 |
|
| 292 |
+
try {
|
| 293 |
+
tts = await transformers.pipeline("text-to-speech", "Xenova/speecht5_tts", {
|
| 294 |
+
progress_callback: (p) => {
|
| 295 |
+
if (p?.status === 'progress' && p.file) log(`Loading: ${p.file}`);
|
|
|
|
|
|
|
|
|
|
| 296 |
}
|
| 297 |
+
});
|
| 298 |
+
|
| 299 |
+
// Load default embedding
|
| 300 |
+
const response = await fetch(
|
| 301 |
+
"https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin"
|
| 302 |
+
);
|
| 303 |
+
const buffer = await response.arrayBuffer();
|
| 304 |
+
defaultEmbedding = new Float32Array(buffer);
|
| 305 |
+
|
| 306 |
+
$("#model").className = "chip success";
|
| 307 |
+
$("#model").textContent = "Ready";
|
| 308 |
+
$("#voices").className = "chip success";
|
| 309 |
+
$("#voices").textContent = "24/24";
|
| 310 |
+
log("Model ready with 24 voice profiles!", 'success');
|
| 311 |
+
} catch (err) {
|
| 312 |
+
log(`Error: ${err.message}`, 'error');
|
| 313 |
+
$("#model").className = "chip danger";
|
| 314 |
+
$("#model").textContent = "Failed";
|
| 315 |
}
|
| 316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
// Generate speech
|
| 318 |
$("#go").addEventListener("click", async () => {
|
| 319 |
const text = $("#txt").value.trim();
|
|
|
|
| 327 |
return;
|
| 328 |
}
|
| 329 |
|
| 330 |
+
const voiceId = $("#voiceSelect").value;
|
| 331 |
+
const profile = VOICE_PROFILES[voiceId];
|
| 332 |
+
const speed = parseFloat($("#spd").value);
|
| 333 |
+
const userPitch = parseFloat($("#pitch").value);
|
| 334 |
+
const userEnergy = parseFloat($("#energy").value);
|
|
|
|
| 335 |
|
| 336 |
const btn = $("#go");
|
| 337 |
btn.disabled = true;
|
| 338 |
$("#status").className = "chip warning";
|
| 339 |
$("#status").textContent = "Generating...";
|
| 340 |
+
showStatus(`🎙️ Generating with ${voiceId}...`, 'info');
|
| 341 |
+
log(`Generating: "${text.substring(0, 30)}..." [${voiceId}]`);
|
| 342 |
|
| 343 |
try {
|
| 344 |
+
// Create custom embedding
|
| 345 |
+
const customEmbedding = new Float32Array(defaultEmbedding.length);
|
| 346 |
+
|
| 347 |
+
for (let i = 0; i < defaultEmbedding.length; i++) {
|
| 348 |
+
// Apply voice profile transformations
|
| 349 |
+
let val = defaultEmbedding[i];
|
| 350 |
+
|
| 351 |
+
// Pitch modification
|
| 352 |
+
val *= profile.pitch * userPitch;
|
| 353 |
+
|
| 354 |
+
// Energy modification
|
| 355 |
+
val *= profile.energy * userEnergy;
|
| 356 |
|
| 357 |
+
// Spectral shaping
|
| 358 |
+
val += profile.spectral * Math.sin(i * 0.01);
|
| 359 |
+
|
| 360 |
+
customEmbedding[i] = val;
|
| 361 |
}
|
| 362 |
|
| 363 |
+
// Normalize
|
| 364 |
+
const mean = customEmbedding.reduce((a, b) => a + b, 0) / customEmbedding.length;
|
| 365 |
+
const std = Math.sqrt(
|
| 366 |
+
customEmbedding.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / customEmbedding.length
|
| 367 |
+
);
|
| 368 |
|
| 369 |
+
for (let i = 0; i < customEmbedding.length; i++) {
|
| 370 |
+
customEmbedding[i] = (customEmbedding[i] - mean) / (std + 1e-8);
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
// Generate
|
| 374 |
+
const output = await tts(text, { speaker_embeddings: customEmbedding });
|
| 375 |
+
|
| 376 |
+
log(`Generated! ${output.audio.length} samples`, 'success');
|
| 377 |
+
|
| 378 |
+
// Encode WAV
|
| 379 |
const wav = encodeWAV(output.audio, output.sampling_rate);
|
| 380 |
const blob = new Blob([wav], { type: "audio/wav" });
|
| 381 |
const url = URL.createObjectURL(blob);
|
|
|
|
| 383 |
// Player
|
| 384 |
const player = $("#player");
|
| 385 |
player.src = url;
|
| 386 |
+
player.playbackRate = speed;
|
| 387 |
player.classList.remove("hidden");
|
| 388 |
|
| 389 |
// Download
|
| 390 |
$("#download").href = url;
|
| 391 |
+
$("#download").download = `tts-${voiceId}-${Date.now()}.wav`;
|
| 392 |
$("#downloadBox").classList.remove("hidden");
|
| 393 |
|
| 394 |
$("#status").className = "chip success";
|
| 395 |
$("#status").textContent = "Success";
|
| 396 |
+
showStatus(`✅ Audio generated with ${voiceId}!`, 'success');
|
| 397 |
|
| 398 |
} catch (err) {
|
| 399 |
+
log(`Error: ${err.message}`, 'error');
|
| 400 |
console.error(err);
|
| 401 |
$("#status").className = "chip danger";
|
| 402 |
$("#status").textContent = "Error";
|
|
|
|
| 425 |
if (player.src) player.playbackRate = parseFloat($("#spd").value);
|
| 426 |
});
|
| 427 |
|
| 428 |
+
log("🎉 Ready! 24 voices available!", 'success');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
</script>
|
| 430 |
</body>
|
| 431 |
</html>
|