Spaces:
Paused
Paused
Feat: Seamless TTS preloading between chunks (BUILD 1.5.18)
Browse files- index.html +1 -1
- src/services/tts-service.js +119 -25
index.html
CHANGED
|
@@ -51,7 +51,7 @@
|
|
| 51 |
</head>
|
| 52 |
<body class="dark-theme in-chat-view">
|
| 53 |
|
| 54 |
-
<div id="live-version-badge" style="position: fixed; top: 50px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--text-secondary); opacity: 0.6; z-index: 99999; pointer-events: none; font-weight: 500; font-family: monospace; letter-spacing: 1px; background: rgba(0,0,0,0.2); padding: 2px 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.05);">BUILD 1.5.
|
| 55 |
|
| 56 |
<!-- SVG Filters for Liquid Glass Refraction -->
|
| 57 |
<svg width="0" height="0" style="position: absolute; pointer-events: none;">
|
|
|
|
| 51 |
</head>
|
| 52 |
<body class="dark-theme in-chat-view">
|
| 53 |
|
| 54 |
+
<div id="live-version-badge" style="position: fixed; top: 50px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--text-secondary); opacity: 0.6; z-index: 99999; pointer-events: none; font-weight: 500; font-family: monospace; letter-spacing: 1px; background: rgba(0,0,0,0.2); padding: 2px 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.05);">BUILD 1.5.18</div>
|
| 55 |
|
| 56 |
<!-- SVG Filters for Liquid Glass Refraction -->
|
| 57 |
<svg width="0" height="0" style="position: absolute; pointer-events: none;">
|
src/services/tts-service.js
CHANGED
|
@@ -9,7 +9,9 @@ class TTSService {
|
|
| 9 |
this.currentText = '';
|
| 10 |
|
| 11 |
this.chunks = [];
|
|
|
|
| 12 |
this.currentChunkIndex = 0;
|
|
|
|
| 13 |
|
| 14 |
this.audio.addEventListener('ended', () => {
|
| 15 |
if (this.hasActiveSession && this.isPlaying) {
|
|
@@ -50,25 +52,75 @@ class TTSService {
|
|
| 50 |
}
|
| 51 |
|
| 52 |
splitIntoChunks(text) {
|
| 53 |
-
|
| 54 |
-
const
|
| 55 |
-
let
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
} else {
|
| 63 |
-
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
|
| 74 |
play(text) {
|
|
@@ -77,6 +129,7 @@ class TTSService {
|
|
| 77 |
if (!this.currentText) return;
|
| 78 |
|
| 79 |
this.chunks = this.splitIntoChunks(this.currentText);
|
|
|
|
| 80 |
this.currentChunkIndex = 0;
|
| 81 |
this.hasActiveSession = true;
|
| 82 |
|
|
@@ -92,24 +145,37 @@ class TTSService {
|
|
| 92 |
return;
|
| 93 |
}
|
| 94 |
|
| 95 |
-
|
| 96 |
-
this.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
const url = `${baseUrl}/api/tts?text=${encodeURIComponent(text)}&rate=${this.rate}`;
|
| 101 |
|
| 102 |
-
this.audio.src =
|
| 103 |
this.audio.playbackRate = this.rate;
|
| 104 |
this.audio.load();
|
| 105 |
this.audio.play().then(() => {
|
| 106 |
this.isPlaying = true;
|
| 107 |
-
this.isLoading = false;
|
| 108 |
this.notifyState();
|
| 109 |
}).catch(err => {
|
| 110 |
console.error("Playback failed", err);
|
| 111 |
-
|
| 112 |
-
// Skip chunk on error
|
| 113 |
if (this.hasActiveSession && this.isPlaying) {
|
| 114 |
this.currentChunkIndex++;
|
| 115 |
this.playChunk();
|
|
@@ -126,8 +192,14 @@ class TTSService {
|
|
| 126 |
resume() {
|
| 127 |
if (!this.hasActiveSession) return;
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
this.audio.play().then(() => {
|
| 130 |
-
this.isPlaying = true;
|
| 131 |
this.notifyState();
|
| 132 |
}).catch(err => {
|
| 133 |
console.error("Playback failed", err);
|
|
@@ -148,8 +220,18 @@ class TTSService {
|
|
| 148 |
this.isPlaying = false;
|
| 149 |
this.isLoading = false;
|
| 150 |
this.hasActiveSession = false;
|
|
|
|
| 151 |
this.currentText = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
this.chunks = [];
|
|
|
|
| 153 |
this.currentChunkIndex = 0;
|
| 154 |
this.notifyState();
|
| 155 |
}
|
|
@@ -157,6 +239,18 @@ class TTSService {
|
|
| 157 |
setRate(newRate) {
|
| 158 |
this.rate = newRate;
|
| 159 |
this.audio.playbackRate = this.rate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
this.notifyState();
|
| 161 |
}
|
| 162 |
|
|
@@ -186,7 +280,7 @@ class TTSService {
|
|
| 186 |
this.playChunk();
|
| 187 |
} else {
|
| 188 |
this.audio.pause();
|
| 189 |
-
this.playChunk(); // Restart
|
| 190 |
}
|
| 191 |
}
|
| 192 |
|
|
|
|
| 9 |
this.currentText = '';
|
| 10 |
|
| 11 |
this.chunks = [];
|
| 12 |
+
this.audioUrls = [];
|
| 13 |
this.currentChunkIndex = 0;
|
| 14 |
+
this.isWaitingForPreload = false;
|
| 15 |
|
| 16 |
this.audio.addEventListener('ended', () => {
|
| 17 |
if (this.hasActiveSession && this.isPlaying) {
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
splitIntoChunks(text) {
|
| 55 |
+
// Cut at sentence boundaries (. ! ?) to keep intonation natural
|
| 56 |
+
const regex = /[^.!?\n]+[.!?\n]+/g;
|
| 57 |
+
let chunks = text.match(regex);
|
| 58 |
+
|
| 59 |
+
if (!chunks) chunks = [text];
|
| 60 |
+
else if (chunks.join('').length < text.length) {
|
| 61 |
+
chunks.push(text.substring(chunks.join('').length));
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
// Trim and filter out empty chunks
|
| 65 |
+
chunks = chunks.map(c => c.trim()).filter(c => c.length > 0);
|
| 66 |
+
|
| 67 |
+
// If a chunk is extremely long (>200 chars), we should split it by commas or spaces to prevent timeouts
|
| 68 |
+
let finalChunks = [];
|
| 69 |
+
for (let chunk of chunks) {
|
| 70 |
+
if (chunk.length > 200) {
|
| 71 |
+
const subChunks = chunk.split(/(?<=[,;:])\s+/);
|
| 72 |
+
for (let sub of subChunks) {
|
| 73 |
+
if (sub.length > 200) {
|
| 74 |
+
// Hard split by words
|
| 75 |
+
const words = sub.split(/\s+/);
|
| 76 |
+
let current = '';
|
| 77 |
+
for (let w of words) {
|
| 78 |
+
if ((current + ' ' + w).length > 150) {
|
| 79 |
+
finalChunks.push(current.trim());
|
| 80 |
+
current = w;
|
| 81 |
+
} else {
|
| 82 |
+
current += ' ' + w;
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
if (current.trim()) finalChunks.push(current.trim());
|
| 86 |
+
} else {
|
| 87 |
+
finalChunks.push(sub.trim());
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
} else {
|
| 91 |
+
finalChunks.push(chunk);
|
| 92 |
}
|
| 93 |
}
|
| 94 |
|
| 95 |
+
return finalChunks;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
async preloadChunk(index) {
|
| 99 |
+
if (index >= this.chunks.length) return;
|
| 100 |
+
if (this.audioUrls[index]) return; // Already loaded or loading
|
| 101 |
|
| 102 |
+
this.audioUrls[index] = 'loading'; // Mark as loading
|
| 103 |
+
|
| 104 |
+
try {
|
| 105 |
+
const text = this.chunks[index];
|
| 106 |
+
const baseUrl = window.location.origin.includes('localhost') ? 'http://localhost:3000' : '';
|
| 107 |
+
const url = `${baseUrl}/api/tts?text=${encodeURIComponent(text)}&rate=${this.rate}`;
|
| 108 |
+
|
| 109 |
+
const response = await fetch(url);
|
| 110 |
+
if (!response.ok) throw new Error('Network response was not ok');
|
| 111 |
+
const blob = await response.blob();
|
| 112 |
+
const objectUrl = URL.createObjectURL(blob);
|
| 113 |
+
|
| 114 |
+
this.audioUrls[index] = objectUrl;
|
| 115 |
+
|
| 116 |
+
// If the player is waiting for this specific chunk, play it immediately
|
| 117 |
+
if (this.currentChunkIndex === index && this.isWaitingForPreload && this.isPlaying) {
|
| 118 |
+
this.playReadyChunk(index);
|
| 119 |
+
}
|
| 120 |
+
} catch (e) {
|
| 121 |
+
console.error("Preload failed", e);
|
| 122 |
+
this.audioUrls[index] = null;
|
| 123 |
+
}
|
| 124 |
}
|
| 125 |
|
| 126 |
play(text) {
|
|
|
|
| 129 |
if (!this.currentText) return;
|
| 130 |
|
| 131 |
this.chunks = this.splitIntoChunks(this.currentText);
|
| 132 |
+
this.audioUrls = new Array(this.chunks.length).fill(null);
|
| 133 |
this.currentChunkIndex = 0;
|
| 134 |
this.hasActiveSession = true;
|
| 135 |
|
|
|
|
| 145 |
return;
|
| 146 |
}
|
| 147 |
|
| 148 |
+
// Start preloading current and next 2 chunks in the background
|
| 149 |
+
this.preloadChunk(this.currentChunkIndex);
|
| 150 |
+
this.preloadChunk(this.currentChunkIndex + 1);
|
| 151 |
+
this.preloadChunk(this.currentChunkIndex + 2);
|
| 152 |
+
|
| 153 |
+
const url = this.audioUrls[this.currentChunkIndex];
|
| 154 |
+
if (url && url !== 'loading') {
|
| 155 |
+
this.playReadyChunk(this.currentChunkIndex);
|
| 156 |
+
} else {
|
| 157 |
+
// Show spinner and wait
|
| 158 |
+
this.isWaitingForPreload = true;
|
| 159 |
+
this.isLoading = true;
|
| 160 |
+
this.notifyState();
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
playReadyChunk(index) {
|
| 165 |
+
if (!this.hasActiveSession || this.currentChunkIndex !== index) return;
|
| 166 |
|
| 167 |
+
this.isWaitingForPreload = false;
|
| 168 |
+
this.isLoading = false;
|
|
|
|
| 169 |
|
| 170 |
+
this.audio.src = this.audioUrls[index];
|
| 171 |
this.audio.playbackRate = this.rate;
|
| 172 |
this.audio.load();
|
| 173 |
this.audio.play().then(() => {
|
| 174 |
this.isPlaying = true;
|
|
|
|
| 175 |
this.notifyState();
|
| 176 |
}).catch(err => {
|
| 177 |
console.error("Playback failed", err);
|
| 178 |
+
// Skip on error
|
|
|
|
| 179 |
if (this.hasActiveSession && this.isPlaying) {
|
| 180 |
this.currentChunkIndex++;
|
| 181 |
this.playChunk();
|
|
|
|
| 192 |
resume() {
|
| 193 |
if (!this.hasActiveSession) return;
|
| 194 |
|
| 195 |
+
// If we were waiting for a chunk, just ensure state is right
|
| 196 |
+
this.isPlaying = true;
|
| 197 |
+
if (this.isWaitingForPreload) {
|
| 198 |
+
this.notifyState();
|
| 199 |
+
return;
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
this.audio.play().then(() => {
|
|
|
|
| 203 |
this.notifyState();
|
| 204 |
}).catch(err => {
|
| 205 |
console.error("Playback failed", err);
|
|
|
|
| 220 |
this.isPlaying = false;
|
| 221 |
this.isLoading = false;
|
| 222 |
this.hasActiveSession = false;
|
| 223 |
+
this.isWaitingForPreload = false;
|
| 224 |
this.currentText = '';
|
| 225 |
+
|
| 226 |
+
// Cleanup Object URLs to free memory
|
| 227 |
+
if (this.audioUrls) {
|
| 228 |
+
this.audioUrls.forEach(url => {
|
| 229 |
+
if (url && url !== 'loading') URL.revokeObjectURL(url);
|
| 230 |
+
});
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
this.chunks = [];
|
| 234 |
+
this.audioUrls = [];
|
| 235 |
this.currentChunkIndex = 0;
|
| 236 |
this.notifyState();
|
| 237 |
}
|
|
|
|
| 239 |
setRate(newRate) {
|
| 240 |
this.rate = newRate;
|
| 241 |
this.audio.playbackRate = this.rate;
|
| 242 |
+
// Note: To apply rate to preloading, we'd need to re-fetch, but typically
|
| 243 |
+
// changing rate clears the preloaded URLs. For simplicity, we just clear them!
|
| 244 |
+
if (this.audioUrls) {
|
| 245 |
+
for (let i = this.currentChunkIndex + 1; i < this.audioUrls.length; i++) {
|
| 246 |
+
if (this.audioUrls[i] && this.audioUrls[i] !== 'loading') {
|
| 247 |
+
URL.revokeObjectURL(this.audioUrls[i]);
|
| 248 |
+
this.audioUrls[i] = null;
|
| 249 |
+
}
|
| 250 |
+
}
|
| 251 |
+
}
|
| 252 |
+
// Re-trigger preload for next chunks
|
| 253 |
+
this.preloadChunk(this.currentChunkIndex + 1);
|
| 254 |
this.notifyState();
|
| 255 |
}
|
| 256 |
|
|
|
|
| 280 |
this.playChunk();
|
| 281 |
} else {
|
| 282 |
this.audio.pause();
|
| 283 |
+
this.playChunk(); // Restart current
|
| 284 |
}
|
| 285 |
}
|
| 286 |
|