-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlingo.js
More file actions
635 lines (553 loc) · 17.8 KB
/
lingo.js
File metadata and controls
635 lines (553 loc) · 17.8 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
const readBtn = document.getElementById("readBtn");
const stopBtn = document.getElementById("stopBtn");
const pauseBtn = document.getElementById("pauseBtn");
const resumeBtn = document.getElementById("resumeBtn");
const micBtn = document.getElementById("micBtn");
const copyBtn = document.getElementById("copyBtn");
const clearBtn = document.getElementById("clearBtn");
const voiceSelect = document.getElementById("voiceSelect");
const rateSelect = document.getElementById("rateSelect");
const textArea = document.getElementById("text");
const status = document.getElementById("status");
const STORAGE_VOICE_KEY = "tts_selected_voice_name_v1";
const STORAGE_RATE_KEY = "tts_selected_rate_v1";
const STORAGE_TEXT_KEY = "tts_text_buffer_v1";
let currentUtterance = null;
let isTTSSpeaking = false;
let isTTSPaused = false;
// Paragraph queue for chunked TTS reading
let paragraphQueue = [];
// Speech Recognition variables
let recognition = null;
let isListening = false;
let shouldKeepListening = false;
function supportsSpeech() {
return "speechSynthesis" in window && typeof SpeechSynthesisUtterance !== "undefined";
}
function supportsSpeechRecognition() {
return "webkitSpeechRecognition" in window;
}
function setStatus(text) {
status.textContent = text || "";
}
function updateReadButton() {
if (isTTSSpeaking) {
readBtn.style.display = "none";
stopBtn.style.display = "";
if (isTTSPaused) {
pauseBtn.style.display = "none";
resumeBtn.style.display = "";
} else {
pauseBtn.style.display = "";
resumeBtn.style.display = "none";
}
} else {
readBtn.style.display = "";
stopBtn.style.display = "none";
pauseBtn.style.display = "none";
resumeBtn.style.display = "none";
// Disable read button when speech recognition is active
readBtn.disabled = shouldKeepListening;
}
}
function updateMicButton() {
if (shouldKeepListening) {
micBtn.classList.add('listening');
micBtn.textContent = '⏹️ Stop';
micBtn.disabled = false;
} else {
micBtn.classList.remove('listening');
micBtn.textContent = '🎤 Mic';
// Disable mic button when TTS is speaking
micBtn.disabled = isTTSSpeaking;
}
}
function populateVoices() {
const voices = window.speechSynthesis.getVoices() || [];
// If no voices available yet, try again later
if (voices.length === 0) {
setTimeout(populateVoices, 100);
return;
}
voiceSelect.innerHTML = "";
voices.forEach((v, i) => {
const option = document.createElement("option");
option.value = v.name;
option.textContent = `${v.name} (${v.lang})${v.default ? " — default" : ""}`;
option.dataset.lang = v.lang;
voiceSelect.appendChild(option);
});
// restore saved voice if present
const saved = localStorage.getItem(STORAGE_VOICE_KEY);
if (saved) {
const found = Array.from(voiceSelect.options).find(o => o.value === saved);
if (found) {
voiceSelect.value = saved;
}
}
// If nothing selected, pick the default voice or first
if (!voiceSelect.value && voiceSelect.options.length) {
const defaultIndex = Array.from(voiceSelect.options).findIndex(o => o.textContent.includes("— default"));
voiceSelect.selectedIndex = defaultIndex >= 0 ? defaultIndex : 0;
}
// restore rate
const savedRate = localStorage.getItem(STORAGE_RATE_KEY);
if (savedRate) {
rateSelect.value = savedRate;
}
}
function savePreferences() {
try {
localStorage.setItem(STORAGE_VOICE_KEY, voiceSelect.value);
localStorage.setItem(STORAGE_RATE_KEY, rateSelect.value);
} catch (e) {
// ignore storage errors
}
}
function speakText(text) {
if (!text || !text.trim()) {
setStatus("Nothing to read");
return;
}
if (!supportsSpeech()) {
setStatus("Browser does not support the Web Speech API.");
return;
}
// Stop speech recognition if it's active
if (shouldKeepListening) {
shouldKeepListening = false;
if (recognition) {
try {
recognition.stop();
} catch (error) {
console.error('Error stopping speech recognition:', error);
}
}
// Update mic button state immediately
isListening = false;
micBtn.classList.remove('listening');
micBtn.textContent = '🎤 Mic';
textArea.classList.remove('listening-textarea');
updateMicButton();
}
// Cancel any current speech and clear the queue
window.speechSynthesis.cancel();
paragraphQueue = [];
// Split text into chunks that the speech API can handle
// First split on paragraph breaks (double newlines or more)
// Then split long paragraphs into sentences
const MAX_CHUNK_LENGTH = 200; // Characters - keep chunks small for reliability
const rawParagraphs = text.split(/\n\s*\n/).filter(p => p.trim().length > 0);
// If no paragraph breaks found, treat the whole text as one paragraph
const paragraphs = rawParagraphs.length > 0 ? rawParagraphs : [text];
// Further split long paragraphs into sentences
const chunks = [];
for (const para of paragraphs) {
const trimmed = para.trim().replace(/\n/g, ' '); // Replace single newlines with spaces
if (trimmed.length <= MAX_CHUNK_LENGTH) {
chunks.push(trimmed);
} else {
// Split on sentence boundaries (., !, ?)
// Keep the punctuation with the sentence
const sentences = trimmed.match(/[^.!?]+[.!?]+\s*/g) || [trimmed];
let currentChunk = '';
for (const sentence of sentences) {
const trimmedSentence = sentence.trim();
if (currentChunk.length + trimmedSentence.length <= MAX_CHUNK_LENGTH) {
currentChunk += (currentChunk ? ' ' : '') + trimmedSentence;
} else {
if (currentChunk) {
chunks.push(currentChunk);
}
// If a single sentence is too long, just add it anyway
currentChunk = trimmedSentence;
}
}
if (currentChunk) {
chunks.push(currentChunk);
}
}
}
if (chunks.length === 0) {
setStatus("Nothing to read");
return;
}
// Populate the queue
paragraphQueue = chunks;
// Wait a moment for cancel to complete, then start reading
setTimeout(() => {
speakNextParagraph();
}, 100);
}
function speakNextParagraph() {
// If queue is empty, we're done
if (paragraphQueue.length === 0) {
currentUtterance = null;
isTTSSpeaking = false;
isTTSPaused = false;
setStatus("Done");
updateReadButton();
updateMicButton();
return;
}
const voices = window.speechSynthesis.getVoices() || [];
// If no voices available, try to trigger voice loading
if (voices.length === 0) {
// Force voice loading by speaking empty text first
const tempUtter = new SpeechSynthesisUtterance("");
window.speechSynthesis.speak(tempUtter);
window.speechSynthesis.cancel();
// Retry after a moment
setTimeout(() => speakNextParagraph(), 200);
return;
}
// Get the first paragraph from the queue
const paragraphText = paragraphQueue[0].trim();
const utter = new SpeechSynthesisUtterance(paragraphText);
// prefer exact match by name; fallback to currently selected index
const chosenName = voiceSelect.value;
const chosenVoice = voices.find(v => v.name === chosenName) || voices[voiceSelect.selectedIndex] || null;
if (chosenVoice) {
utter.voice = chosenVoice;
}
utter.rate = parseFloat(rateSelect.value) || 1.0;
utter.pitch = 1.0;
utter.volume = 1.0;
utter.onstart = () => {
currentUtterance = utter;
isTTSSpeaking = true;
const remaining = paragraphQueue.length;
setStatus(`Speaking... (${remaining} paragraph${remaining > 1 ? 's' : ''} remaining)`);
updateReadButton();
updateMicButton();
};
utter.onend = () => {
// Remove the paragraph we just finished reading
paragraphQueue.shift();
// Continue with the next paragraph
speakNextParagraph();
};
utter.onerror = (evt) => {
console.error("Speech error:", evt);
// "interrupted" errors can happen during normal cancel operations
// or when Chrome's speech synthesis times out - try to continue
if (evt.error === 'interrupted' && paragraphQueue.length > 0) {
// Remove the current chunk and try the next one
paragraphQueue.shift();
if (paragraphQueue.length > 0) {
setStatus("Resuming after interruption...");
setTimeout(() => speakNextParagraph(), 100);
return;
}
}
// On other errors, clear everything
paragraphQueue = [];
currentUtterance = null;
isTTSSpeaking = false;
isTTSPaused = false;
setStatus("Error during speech: " + (evt.error || "Unknown error"));
updateReadButton();
updateMicButton();
};
try {
window.speechSynthesis.speak(utter);
} catch (e) {
setStatus("Error: " + e.message);
console.error("Speech synthesis error:", e);
paragraphQueue = [];
isTTSSpeaking = false;
updateReadButton();
updateMicButton();
}
}
// event handlers
readBtn.addEventListener("click", () => {
// Start TTS
const selStart = textArea.selectionStart ?? 0;
const selEnd = textArea.selectionEnd ?? 0;
let textToRead;
if (selEnd > selStart) {
// If there's a selection, read only the selected text
textToRead = textArea.value.substring(selStart, selEnd);
} else {
// Otherwise, read from cursor position to the end
textToRead = textArea.value.substring(selStart);
// If at the very end (nothing to read), start from the beginning
if (!textToRead.length) {
textToRead = textArea.value;
}
}
speakText(textToRead);
});
stopBtn.addEventListener("click", () => {
// Stop TTS and clear the queue
if (supportsSpeech()) {
window.speechSynthesis.cancel();
setStatus("Stopped");
}
paragraphQueue = [];
currentUtterance = null;
isTTSSpeaking = false;
isTTSPaused = false;
updateReadButton();
updateMicButton();
});
pauseBtn.addEventListener("click", () => {
if (supportsSpeech() && isTTSSpeaking && !isTTSPaused) {
window.speechSynthesis.pause();
isTTSPaused = true;
setStatus("Paused");
updateReadButton();
}
});
resumeBtn.addEventListener("click", () => {
if (supportsSpeech() && isTTSSpeaking && isTTSPaused) {
window.speechSynthesis.resume();
isTTSPaused = false;
setStatus("Speaking...");
updateReadButton();
}
});
voiceSelect.addEventListener("change", () => {
savePreferences();
});
rateSelect.addEventListener("change", () => {
savePreferences();
});
// Speech Recognition Functions
function initializeSpeechRecognition() {
if (!supportsSpeechRecognition()) {
console.warn('Speech recognition not supported in this browser');
micBtn.disabled = true;
micBtn.title = "Speech recognition not supported in this browser";
return;
}
recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = false;
recognition.lang = 'en-US';
recognition.onstart = () => {
isListening = true;
micBtn.classList.add('listening');
micBtn.textContent = '⏹️ Stop';
textArea.classList.add('listening-textarea');
setStatus("Listening...");
updateReadButton();
updateMicButton();
};
recognition.onerror = (event) => {
console.error('Speech recognition error:', event.error);
setStatus(`Speech error: ${event.error}`);
};
recognition.onend = () => {
isListening = false;
micBtn.classList.remove('listening');
micBtn.textContent = '🎤 Mic';
textArea.classList.remove('listening-textarea');
if (shouldKeepListening) {
// Auto-restart if we should keep listening
try {
recognition.start();
} catch (error) {
console.error('Error restarting speech recognition:', error);
shouldKeepListening = false;
setStatus("Speech recognition stopped");
updateReadButton();
updateMicButton();
}
} else {
setStatus("Speech recognition stopped");
updateReadButton();
updateMicButton();
}
};
recognition.onresult = (event) => {
for (let i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
const transcript = event.results[i][0].transcript.trim();
if (transcript) {
insertTextAtCursor(transcript + ' ');
}
}
}
};
}
function insertTextAtCursor(text) {
const cursorPosition = textArea.selectionStart;
const selectionEnd = textArea.selectionEnd;
const currentContent = textArea.value;
// Insert text at cursor position, replacing any selected text
const beforeCursor = currentContent.substring(0, cursorPosition);
const afterCursor = currentContent.substring(selectionEnd);
const newContent = beforeCursor + text + afterCursor;
textArea.value = newContent;
// Set cursor position after the inserted text
const newCursorPosition = cursorPosition + text.length;
textArea.setSelectionRange(newCursorPosition, newCursorPosition);
textArea.focus();
}
function toggleSpeechRecognition() {
if (!supportsSpeechRecognition()) {
setStatus("Speech recognition is not supported in this browser. Please use Google Chrome.");
return;
}
if (!recognition) {
initializeSpeechRecognition();
if (!recognition) return;
}
if (shouldKeepListening) {
// Stop listening
shouldKeepListening = false;
try {
recognition.stop();
// Save text immediately when stopping voice input
saveTextToStorage();
} catch (error) {
console.error('Error stopping speech recognition:', error);
}
} else {
// Stop TTS if it's active before starting speech recognition
if (isTTSSpeaking) {
if (supportsSpeech()) {
window.speechSynthesis.cancel();
}
currentUtterance = null;
isTTSSpeaking = false;
updateReadButton();
updateMicButton();
}
// Start listening
shouldKeepListening = true;
try {
recognition.start();
} catch (error) {
console.error('Error starting speech recognition:', error);
shouldKeepListening = false;
setStatus(`Error starting speech recognition: ${error.message}`);
}
}
}
// Speech Recognition button event
micBtn.addEventListener("click", toggleSpeechRecognition);
// Copy button event
copyBtn.addEventListener("click", async () => {
const text = textArea.value;
if (!text || !text.trim()) {
setStatus("Nothing to copy");
return;
}
try {
await navigator.clipboard.writeText(text);
setStatus("Copied to clipboard!");
setTimeout(() => setStatus("Ready"), 2000);
} catch (error) {
console.error('Copy failed:', error);
setStatus("Failed to copy to clipboard");
setTimeout(() => setStatus("Ready"), 2000);
}
});
// Function to save text to storage
function saveTextToStorage() {
try {
let textToSave = textArea.value;
// Add divider prefix if text exists and doesn't already start with one
if (textToSave.trim() && !textToSave.trim().startsWith('---')) {
textToSave = '\n---\n' + textToSave;
}
localStorage.setItem(STORAGE_TEXT_KEY, textToSave);
} catch (e) {
console.error('Error saving text buffer:', e);
}
}
// Clear button event
clearBtn.addEventListener("click", () => {
textArea.value = "";
textArea.focus();
setStatus("Text cleared");
setTimeout(() => setStatus("Ready"), 2000);
});
// Save text buffer when page is about to close
window.addEventListener("beforeunload", () => {
saveTextToStorage();
});
// keyboard shortcuts while textarea focused
textArea.addEventListener("keydown", (evt) => {
if ((evt.ctrlKey || evt.metaKey) && evt.key === "Enter") {
evt.preventDefault();
readBtn.click();
return;
}
if (evt.key === "Escape") {
evt.preventDefault();
if (shouldKeepListening) {
micBtn.click(); // Stop speech recognition first
} else if (isTTSSpeaking) {
stopBtn.click(); // Stop TTS
}
return;
}
if ((evt.ctrlKey || evt.metaKey) && evt.key.toLowerCase() === "m") {
evt.preventDefault();
micBtn.click();
return;
}
});
// initial setup
if (!supportsSpeech()) {
setStatus("Web Speech API not supported in this browser. Use Chrome/Chromium for best results.");
readBtn.disabled = true;
} else {
setStatus("Loading voices...");
// populate voices (some browsers require async wait)
populateVoices();
// Many browsers fire an event when voices change
window.speechSynthesis.onvoiceschanged = () => {
populateVoices();
setStatus("Ready");
};
// call again after a short delay in case voices arrive
setTimeout(() => {
populateVoices();
setStatus("Ready");
}, 250);
}
// Initialize speech recognition
initializeSpeechRecognition();
// Restore saved text buffer on page load
try {
const savedText = localStorage.getItem(STORAGE_TEXT_KEY);
if (savedText) {
textArea.value = savedText;
}
} catch (e) {
console.error('Error restoring text buffer:', e);
}
// After a short delay, ensure focus and cursor position again
// (in case browser stole focus during page load)
setTimeout(() => {
textArea.focus();
}, 1000);
// After a short delay, ensure focus and cursor position again
// (in case browser stole focus during page load)
setTimeout(() => {
textArea.setSelectionRange(0, 0);
}, 1200);
// Check URL parameter to auto-start mic dictation
// Use a delay to ensure speech recognition is fully initialized
function checkAutoMicStart() {
const urlParams = new URLSearchParams(window.location.search);
const micParam = urlParams.get('mic');
if (micParam && micParam.toLowerCase() === 'on') {
// Start mic dictation if not already listening and recognition is available
if (!shouldKeepListening && supportsSpeechRecognition()) {
toggleSpeechRecognition();
}
}
}
// Delay auto-mic start to ensure speech engine is initialized
setTimeout(checkAutoMicStart, 1000);
// Expose some helpers to console for debugging if needed (handy during development)
window.__tts = {
speakNow: (txt) => speakText(String(txt)),
cancel: () => { if (supportsSpeech()) window.speechSynthesis.cancel(); }
};