Spaces:
Paused
Paused
Fix: Chunking TTS locally and added loading spinner (BUILD 1.5.17)
Browse files- src/app.js +7 -1
- src/services/tts-service.js +88 -36
src/app.js
CHANGED
|
@@ -925,12 +925,18 @@ const initApp = async () => {
|
|
| 925 |
if (scrollBtn) scrollBtn.style.top = '-46px';
|
| 926 |
}
|
| 927 |
|
| 928 |
-
if (state.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 929 |
ttsPlayer.classList.add('playing');
|
| 930 |
ttsPlayIcon.innerHTML = '<rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect>';
|
|
|
|
| 931 |
} else {
|
| 932 |
ttsPlayer.classList.remove('playing');
|
| 933 |
ttsPlayIcon.innerHTML = '<polygon points="7 3 21 12 7 21 7 3"></polygon>';
|
|
|
|
| 934 |
}
|
| 935 |
|
| 936 |
ttsRateBtn.innerText = state.rate + 'x';
|
|
|
|
| 925 |
if (scrollBtn) scrollBtn.style.top = '-46px';
|
| 926 |
}
|
| 927 |
|
| 928 |
+
if (state.isLoading) {
|
| 929 |
+
ttsPlayer.classList.add('playing');
|
| 930 |
+
ttsPlayIcon.innerHTML = '<circle cx="12" cy="12" r="8" stroke="currentColor" stroke-width="3" fill="none" stroke-dasharray="25 25" stroke-linecap="round"><animateTransform attributeName="transform" type="rotate" from="0 12 12" to="360 12 12" dur="1s" repeatCount="indefinite"/></circle>';
|
| 931 |
+
ttsPlayBtn.style.opacity = '0.5';
|
| 932 |
+
} else if (state.isPlaying) {
|
| 933 |
ttsPlayer.classList.add('playing');
|
| 934 |
ttsPlayIcon.innerHTML = '<rect x="6" y="4" width="4" height="16"></rect><rect x="14" y="4" width="4" height="16"></rect>';
|
| 935 |
+
ttsPlayBtn.style.opacity = '1';
|
| 936 |
} else {
|
| 937 |
ttsPlayer.classList.remove('playing');
|
| 938 |
ttsPlayIcon.innerHTML = '<polygon points="7 3 21 12 7 21 7 3"></polygon>';
|
| 939 |
+
ttsPlayBtn.style.opacity = '1';
|
| 940 |
}
|
| 941 |
|
| 942 |
ttsRateBtn.innerText = state.rate + 'x';
|
src/services/tts-service.js
CHANGED
|
@@ -2,25 +2,29 @@ class TTSService {
|
|
| 2 |
constructor() {
|
| 3 |
this.audio = new Audio();
|
| 4 |
this.isPlaying = false;
|
|
|
|
| 5 |
this.rate = 1.0;
|
| 6 |
this.onStateChange = null;
|
| 7 |
-
this.progressInterval = null;
|
| 8 |
this.hasActiveSession = false;
|
| 9 |
this.currentText = '';
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
this.audio.addEventListener('ended', () => {
|
| 12 |
-
this.
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
});
|
| 17 |
|
| 18 |
this.audio.addEventListener('error', (e) => {
|
| 19 |
console.error('Audio playback error:', e);
|
| 20 |
-
this.
|
| 21 |
-
this.hasActiveSession
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
});
|
| 25 |
}
|
| 26 |
|
|
@@ -45,25 +49,77 @@ class TTSService {
|
|
| 45 |
return text.trim();
|
| 46 |
}
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
play(text) {
|
| 49 |
this.stop();
|
| 50 |
this.currentText = this.cleanText(text);
|
| 51 |
if (!this.currentText) return;
|
| 52 |
|
|
|
|
|
|
|
| 53 |
this.hasActiveSession = true;
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
this.audio.src = url;
|
|
|
|
| 59 |
this.audio.load();
|
| 60 |
-
this.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
|
| 63 |
pause() {
|
| 64 |
this.audio.pause();
|
| 65 |
this.isPlaying = false;
|
| 66 |
-
this.stopProgressTracking();
|
| 67 |
this.notifyState();
|
| 68 |
}
|
| 69 |
|
|
@@ -72,7 +128,6 @@ class TTSService {
|
|
| 72 |
|
| 73 |
this.audio.play().then(() => {
|
| 74 |
this.isPlaying = true;
|
| 75 |
-
this.startProgressTracking();
|
| 76 |
this.notifyState();
|
| 77 |
}).catch(err => {
|
| 78 |
console.error("Playback failed", err);
|
|
@@ -89,12 +144,13 @@ class TTSService {
|
|
| 89 |
|
| 90 |
stop() {
|
| 91 |
this.audio.pause();
|
| 92 |
-
this.audio.currentTime = 0;
|
| 93 |
this.audio.src = '';
|
| 94 |
this.isPlaying = false;
|
|
|
|
| 95 |
this.hasActiveSession = false;
|
| 96 |
this.currentText = '';
|
| 97 |
-
this.
|
|
|
|
| 98 |
this.notifyState();
|
| 99 |
}
|
| 100 |
|
|
@@ -113,41 +169,37 @@ class TTSService {
|
|
| 113 |
|
| 114 |
next() {
|
| 115 |
if (!this.hasActiveSession) return;
|
| 116 |
-
if (this.
|
| 117 |
-
this.audio.
|
| 118 |
-
this.
|
|
|
|
|
|
|
|
|
|
| 119 |
}
|
| 120 |
}
|
| 121 |
|
| 122 |
prev() {
|
| 123 |
if (!this.hasActiveSession) return;
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
this.notifyState();
|
| 132 |
-
}, 250);
|
| 133 |
-
}
|
| 134 |
-
|
| 135 |
-
stopProgressTracking() {
|
| 136 |
-
if (this.progressInterval) {
|
| 137 |
-
clearInterval(this.progressInterval);
|
| 138 |
-
this.progressInterval = null;
|
| 139 |
}
|
| 140 |
}
|
| 141 |
|
| 142 |
notifyState() {
|
| 143 |
if (this.onStateChange) {
|
| 144 |
let progress = 0;
|
| 145 |
-
if (this.
|
| 146 |
-
progress = (this.
|
| 147 |
}
|
| 148 |
|
| 149 |
this.onStateChange({
|
| 150 |
isPlaying: this.isPlaying,
|
|
|
|
| 151 |
rate: this.rate,
|
| 152 |
progress: progress,
|
| 153 |
hasActiveSession: this.hasActiveSession
|
|
|
|
| 2 |
constructor() {
|
| 3 |
this.audio = new Audio();
|
| 4 |
this.isPlaying = false;
|
| 5 |
+
this.isLoading = false;
|
| 6 |
this.rate = 1.0;
|
| 7 |
this.onStateChange = null;
|
|
|
|
| 8 |
this.hasActiveSession = false;
|
| 9 |
this.currentText = '';
|
| 10 |
+
|
| 11 |
+
this.chunks = [];
|
| 12 |
+
this.currentChunkIndex = 0;
|
| 13 |
|
| 14 |
this.audio.addEventListener('ended', () => {
|
| 15 |
+
if (this.hasActiveSession && this.isPlaying) {
|
| 16 |
+
this.currentChunkIndex++;
|
| 17 |
+
this.playChunk();
|
| 18 |
+
}
|
| 19 |
});
|
| 20 |
|
| 21 |
this.audio.addEventListener('error', (e) => {
|
| 22 |
console.error('Audio playback error:', e);
|
| 23 |
+
this.isLoading = false;
|
| 24 |
+
if (this.hasActiveSession && this.isPlaying) {
|
| 25 |
+
this.currentChunkIndex++;
|
| 26 |
+
this.playChunk();
|
| 27 |
+
}
|
| 28 |
});
|
| 29 |
}
|
| 30 |
|
|
|
|
| 49 |
return text.trim();
|
| 50 |
}
|
| 51 |
|
| 52 |
+
splitIntoChunks(text) {
|
| 53 |
+
let chunks = [];
|
| 54 |
+
const words = text.split(/\s+/);
|
| 55 |
+
let currentChunk = '';
|
| 56 |
+
|
| 57 |
+
for (const word of words) {
|
| 58 |
+
if (!word) continue;
|
| 59 |
+
if ((currentChunk + ' ' + word).length > 70 && currentChunk.length > 0) {
|
| 60 |
+
chunks.push(currentChunk.trim());
|
| 61 |
+
currentChunk = word;
|
| 62 |
+
} else {
|
| 63 |
+
currentChunk += (currentChunk.length === 0 ? '' : ' ') + word;
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
if (currentChunk.trim().length > 0) {
|
| 68 |
+
chunks.push(currentChunk.trim());
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
return chunks;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
play(text) {
|
| 75 |
this.stop();
|
| 76 |
this.currentText = this.cleanText(text);
|
| 77 |
if (!this.currentText) return;
|
| 78 |
|
| 79 |
+
this.chunks = this.splitIntoChunks(this.currentText);
|
| 80 |
+
this.currentChunkIndex = 0;
|
| 81 |
this.hasActiveSession = true;
|
| 82 |
|
| 83 |
+
if (this.chunks.length > 0) {
|
| 84 |
+
this.isPlaying = true;
|
| 85 |
+
this.playChunk();
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
playChunk() {
|
| 90 |
+
if (this.currentChunkIndex >= this.chunks.length) {
|
| 91 |
+
this.stop();
|
| 92 |
+
return;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
this.isLoading = true;
|
| 96 |
+
this.notifyState();
|
| 97 |
+
|
| 98 |
+
const text = this.chunks[this.currentChunkIndex];
|
| 99 |
+
const baseUrl = window.location.origin.includes('localhost') ? 'http://localhost:3000' : '';
|
| 100 |
+
const url = `${baseUrl}/api/tts?text=${encodeURIComponent(text)}&rate=${this.rate}`;
|
| 101 |
|
| 102 |
this.audio.src = url;
|
| 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 |
+
this.isLoading = false;
|
| 112 |
+
// Skip chunk on error
|
| 113 |
+
if (this.hasActiveSession && this.isPlaying) {
|
| 114 |
+
this.currentChunkIndex++;
|
| 115 |
+
this.playChunk();
|
| 116 |
+
}
|
| 117 |
+
});
|
| 118 |
}
|
| 119 |
|
| 120 |
pause() {
|
| 121 |
this.audio.pause();
|
| 122 |
this.isPlaying = false;
|
|
|
|
| 123 |
this.notifyState();
|
| 124 |
}
|
| 125 |
|
|
|
|
| 128 |
|
| 129 |
this.audio.play().then(() => {
|
| 130 |
this.isPlaying = true;
|
|
|
|
| 131 |
this.notifyState();
|
| 132 |
}).catch(err => {
|
| 133 |
console.error("Playback failed", err);
|
|
|
|
| 144 |
|
| 145 |
stop() {
|
| 146 |
this.audio.pause();
|
|
|
|
| 147 |
this.audio.src = '';
|
| 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 |
}
|
| 156 |
|
|
|
|
| 169 |
|
| 170 |
next() {
|
| 171 |
if (!this.hasActiveSession) return;
|
| 172 |
+
if (this.currentChunkIndex < this.chunks.length - 1) {
|
| 173 |
+
this.audio.pause();
|
| 174 |
+
this.currentChunkIndex++;
|
| 175 |
+
this.playChunk();
|
| 176 |
+
} else {
|
| 177 |
+
this.stop();
|
| 178 |
}
|
| 179 |
}
|
| 180 |
|
| 181 |
prev() {
|
| 182 |
if (!this.hasActiveSession) return;
|
| 183 |
+
if (this.currentChunkIndex > 0) {
|
| 184 |
+
this.audio.pause();
|
| 185 |
+
this.currentChunkIndex--;
|
| 186 |
+
this.playChunk();
|
| 187 |
+
} else {
|
| 188 |
+
this.audio.pause();
|
| 189 |
+
this.playChunk(); // Restart
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
}
|
| 191 |
}
|
| 192 |
|
| 193 |
notifyState() {
|
| 194 |
if (this.onStateChange) {
|
| 195 |
let progress = 0;
|
| 196 |
+
if (this.chunks.length > 0) {
|
| 197 |
+
progress = (this.currentChunkIndex / this.chunks.length) * 100;
|
| 198 |
}
|
| 199 |
|
| 200 |
this.onStateChange({
|
| 201 |
isPlaying: this.isPlaying,
|
| 202 |
+
isLoading: this.isLoading,
|
| 203 |
rate: this.rate,
|
| 204 |
progress: progress,
|
| 205 |
hasActiveSession: this.hasActiveSession
|