cleaned all redundant comments

d7cc70256aaec88fd6a0f0499656aebf31756661

YunZLu <130174259+YunZLu@users.noreply.github.com>

Signed
1 files changed, +6 -33Ignore whitespace
public/scripts/extensions/tts/system.js+6 -33
@@ -172,83 +172,66 @@ class SystemTtsProvider {
172 //#################//172 //#################//
173 fetchTtsVoiceObjects() {173 fetchTtsVoiceObjects() {
174 if (!('speechSynthesis' in window)) {174 if (!('speechSynthesis' in window)) {
175 // Browser doesn't support speech synthesis
176 return Promise.resolve([]);175 return Promise.resolve([]);
177 }176 }
178177
179 return new Promise((resolve) => {178 return new Promise((resolve) => {
180 // Use a minimal timeout to allow the voice list to potentially populate
181 setTimeout(() => {179 setTimeout(() => {
182 let voices = speechSynthesis.getVoices();180 let voices = speechSynthesis.getVoices();
183181
184 if (voices.length === 0) {182 if (voices.length === 0) {
185 // If no voices returned (e.g., Edge on first load), provide a default option183 // Edge compat: Provide default when voices empty
186 console.warn('SystemTTS: getVoices() returned empty list. Providing browser default option.');184 console.warn('SystemTTS: getVoices() returned empty list. Providing browser default option.');
187 const defaultVoice = {185 const defaultVoice = {
188 name: SystemTtsProvider.BROWSER_DEFAULT_VOICE_NAME,186 name: SystemTtsProvider.BROWSER_DEFAULT_VOICE_NAME,
189 voice_id: SystemTtsProvider.BROWSER_DEFAULT_VOICE_ID,187 voice_id: SystemTtsProvider.BROWSER_DEFAULT_VOICE_ID,
190 preview_url: false,188 preview_url: false,
191 // Try to guess the browser's default language
192 lang: navigator.language || 'en-US',189 lang: navigator.language || 'en-US',
193 };190 };
194 resolve([defaultVoice]);191 resolve([defaultVoice]);
195 } else {192 } else {
196 // If voices are available, map them as before
197 const mappedVoices = voices193 const mappedVoices = voices
198 .sort((a, b) => a.lang.localeCompare(b.lang) || a.name.localeCompare(b.name))194 .sort((a, b) => a.lang.localeCompare(b.lang) || a.name.localeCompare(b.name))
199 .map(x => ({ name: x.name, voice_id: x.voiceURI, preview_url: false, lang: x.lang }));195 .map(x => ({ name: x.name, voice_id: x.voiceURI, preview_url: false, lang: x.lang }));
200 resolve(mappedVoices);196 resolve(mappedVoices);
201 }197 }
202 }, 50); // Increased timeout slightly just in case it helps voice population on some browsers198 }, 50);
203 });199 });
204 }200 }
205201
206
207 previewTtsVoice(voiceId) {202 previewTtsVoice(voiceId) {
208 if (!('speechSynthesis' in window)) {203 if (!('speechSynthesis' in window)) {
209 throw new Error('Speech synthesis API is not supported'); // Keep Error type for consistency204 throw new Error('Speech synthesis API is not supported');
210 }205 }
211206
212 let voice = null;207 let voice = null;
213 // Check if the requested voice is NOT the browser default
214 if (voiceId !== SystemTtsProvider.BROWSER_DEFAULT_VOICE_ID) {208 if (voiceId !== SystemTtsProvider.BROWSER_DEFAULT_VOICE_ID) {
215 const voices = speechSynthesis.getVoices();209 const voices = speechSynthesis.getVoices();
216 // Try to find the actual voice
217 voice = voices.find(x => x.voiceURI === voiceId);210 voice = voices.find(x => x.voiceURI === voiceId);
218211
219 if (!voice && voices.length > 0) {212 if (!voice && voices.length > 0) {
220 // If voices are loaded but the specific ID wasn't found, log a warning
221 console.warn(`SystemTTS Preview: Voice ID "${voiceId}" not found among available voices. Using browser default.`);213 console.warn(`SystemTTS Preview: Voice ID "${voiceId}" not found among available voices. Using browser default.`);
222 // Fallback to default (voice remains null)
223 } else if (!voice && voices.length === 0) {214 } else if (!voice && voices.length === 0) {
224 // If no voices are loaded at all, we expect to use default
225 console.warn('SystemTTS Preview: Voice list is empty. Using browser default.');215 console.warn('SystemTTS Preview: Voice list is empty. Using browser default.');
226 // Fallback to default (voice remains null)
227 }216 }
228 } else {217 } else {
229 console.log('SystemTTS Preview: Using browser default voice as requested.');218 console.log('SystemTTS Preview: Using browser default voice as requested.');
230 // Use default (voice remains null)
231 }219 }
232220
233 speechSynthesis.cancel(); // Stop any previous speech221 speechSynthesis.cancel();
234 // Use the language from the found voice if available, otherwise default to 'en-US' or browser lang for the preview text
235 const langForPreview = voice ? voice.lang : (navigator.language || 'en-US');222 const langForPreview = voice ? voice.lang : (navigator.language || 'en-US');
236 const text = getPreviewString(langForPreview);223 const text = getPreviewString(langForPreview);
237 const utterance = new SpeechSynthesisUtterance(text);224 const utterance = new SpeechSynthesisUtterance(text);
238225
239 // Only set the voice if we found a specific one and it wasn't the default request
240 if (voice) {226 if (voice) {
241 utterance.voice = voice;227 utterance.voice = voice;
242 }228 }
243 // Otherwise, utterance.voice remains null/undefined, causing the browser to use its default
244229
245 utterance.rate = this.settings.rate || 1;230 utterance.rate = this.settings.rate || 1;
246 utterance.pitch = this.settings.pitch || 1;231 utterance.pitch = this.settings.pitch || 1;
247232
248 // Add error handling for the speech itself
249 utterance.onerror = (event) => {233 utterance.onerror = (event) => {
250 console.error(`SystemTTS Preview Error: ${event.error}`, event);234 console.error(`SystemTTS Preview Error: ${event.error}`, event);
251 // Potentially notify the user here
252 };235 };
253236
254 speechSynthesis.speak(utterance);237 speechSynthesis.speak(utterance);
@@ -256,11 +239,9 @@ class SystemTtsProvider {
256239
257 async getVoice(voiceName) {240 async getVoice(voiceName) {
258 if (!('speechSynthesis' in window)) {241 if (!('speechSynthesis' in window)) {
259 // Return a predictable null-like structure if API not supported
260 return { voice_id: null, name: 'API Not Supported' };242 return { voice_id: null, name: 'API Not Supported' };
261 }243 }
262244
263 // Check if the requested name is the browser default placeholder
264 if (voiceName === SystemTtsProvider.BROWSER_DEFAULT_VOICE_NAME) {245 if (voiceName === SystemTtsProvider.BROWSER_DEFAULT_VOICE_NAME) {
265 return {246 return {
266 voice_id: SystemTtsProvider.BROWSER_DEFAULT_VOICE_ID,247 voice_id: SystemTtsProvider.BROWSER_DEFAULT_VOICE_ID,
@@ -268,16 +249,10 @@ class SystemTtsProvider {
268 };249 };
269 }250 }
270251
271 // Attempt to get voices, might be async
272 // Note: This relies on voices potentially being populated by now.
273 // A more robust approach might involve re-calling fetchTtsVoiceObjects if needed,
274 // but sticking to minimal changes based on original code structure.
275 const voices = speechSynthesis.getVoices();252 const voices = speechSynthesis.getVoices();
276253
277 if (voices.length === 0) {254 if (voices.length === 0) {
278 // If voices are still empty, we can't find any specific name255 console.warn('SystemTTS: Empty voice list, using default fallback');
279 console.warn(`SystemTTS getVoice: Voice list empty, cannot find "${voiceName}". Falling back to browser default ID.`);
280 // Return the default placeholder as a fallback in this edge case
281 return {256 return {
282 voice_id: SystemTtsProvider.BROWSER_DEFAULT_VOICE_ID,257 voice_id: SystemTtsProvider.BROWSER_DEFAULT_VOICE_ID,
283 name: SystemTtsProvider.BROWSER_DEFAULT_VOICE_NAME,258 name: SystemTtsProvider.BROWSER_DEFAULT_VOICE_NAME,
@@ -287,7 +262,6 @@ class SystemTtsProvider {
287 const match = voices.find(x => x.name == voiceName);262 const match = voices.find(x => x.name == voiceName);
288263
289 if (!match) {264 if (!match) {
290 // If voices are loaded but name not found, throw error as before
291 throw new Error(`SystemTTS getVoice: TTS Voice name "${voiceName}" not found`);265 throw new Error(`SystemTTS getVoice: TTS Voice name "${voiceName}" not found`);
292 }266 }
293267
@@ -313,10 +287,9 @@ class SystemTtsProvider {
313 speechUtteranceChunker(utterance, {287 speechUtteranceChunker(utterance, {
314 chunkLength: 200,288 chunkLength: 200,
315 }, function () {289 }, function () {
316 //some code to execute when done
317 resolve(silence);290 resolve(silence);
318 console.log('System TTS done');291 console.log('System TTS done');
319 });292 });
320 });293 });
321 }294 }
322}295}