-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTTS-v1.5.html
More file actions
1061 lines (921 loc) · 35.7 KB
/
STTS-v1.5.html
File metadata and controls
1061 lines (921 loc) · 35.7 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Simple Text To Speech (STTS) (v1.5)</title>
<style>
:root{
--primary:#2563eb; --bg:#f8fafc; --card:#ffffff; --ink:#1e293b; --muted:#64748b;
--soft:#f1f5f9; --ok:#10b981; --bad:#ef4444; --warn:#f59e0b;
}
*{box-sizing:border-box}
body{font-family:system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,"Helvetica Neue",Arial;background:var(--bg);
padding:1.5rem;color:var(--ink);margin:0}
.container{max-width:1000px;margin:0 auto;background:var(--card);padding:1.75rem;border-radius:20px;
box-shadow:0 10px 25px rgba(0,0,0,0.05)}
h2{margin:0 0 1rem 0;color:var(--primary);display:flex;align-items:center;gap:10px;flex-wrap:wrap}
.badge{font-size:.7rem;background:#dcfce7;color:#166534;padding:4px 8px;border-radius:10px;text-transform:uppercase}
.topbar{display:flex;gap:12px;align-items:center;justify-content:space-between;flex-wrap:wrap;margin-bottom:1rem}
.filebox{display:flex;gap:10px;align-items:center;flex-wrap:wrap}
input[type="file"]{font-size:.85rem}
.mini{font-size:.82rem;color:var(--muted)}
textarea{width:100%;height:320px;padding:1rem;border:2px solid #e2e8f0;border-radius:12px;font-size:1rem;
resize:vertical;margin:0;line-height:1.35}
textarea:focus{border-color:var(--primary);outline:none}
.row{display:grid;grid-template-columns:1fr;gap:1rem;margin-top:1rem}
@media(min-width:980px){ .row{grid-template-columns: 1fr 320px;} }
.controls{display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:1rem;margin-top:1rem}
.box{background:var(--soft);padding:1rem;border-radius:12px}
label{font-size:.7rem;font-weight:800;text-transform:uppercase;color:var(--muted);margin-bottom:8px;display:block}
select,input[type="range"],input[type="number"]{width:100%;cursor:pointer}
.playback-bar{display:flex;flex-wrap:wrap;gap:10px;margin-top:1rem}
button{padding:12px 14px;border:none;border-radius:10px;font-weight:800;cursor:pointer;transition:.15s;flex:1;min-width:140px}
button:active{transform:translateY(1px)}
.play-btn{background:var(--primary);color:white}
.stop-btn{background:var(--bad);color:white}
.ghost-btn{background:#e2e8f0;color:#0f172a}
.warn-btn{background:var(--warn);color:#0f172a}
.stack{display:grid;gap:.75rem}
.pill{display:flex;gap:10px;align-items:center;flex-wrap:wrap}
.pill > *{flex:1}
.status{font-size:.9rem;margin-top:1rem;color:var(--muted);font-style:italic}
.small{font-size:.78rem;color:var(--muted);line-height:1.25}
.kpi{display:flex;justify-content:space-between;gap:10px;flex-wrap:wrap;margin-top:.5rem}
.kpi span{font-size:.8rem;color:var(--muted)}
.mono{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace}
details{background:var(--soft);padding:.75rem 1rem;border-radius:12px}
summary{cursor:pointer;font-weight:800;color:#0f172a}
table{width:100%;border-collapse:collapse;margin-top:.5rem}
th,td{border-bottom:1px solid #dbeafe;padding:.4rem .25rem;font-size:.85rem;text-align:left}
th{color:#0f172a}
.muted{color:var(--muted)}
</style>
</head>
<body>
<div class="container">
<div class="topbar">
<h2>Simple Text To Speech (STTS) (v1.5)<span class="badge">Offline / Local</span><span class="mini">License: GNU GPL v3. Copyright (C) [2026] [Prof. Jyotirmay Kirtania]</span></h2>
<div class="filebox">
<input type="file" id="fileInput" accept=".txt,.md,.html,.htm,.pdf">
<button class="ghost-btn" id="clearBtn" type="button">Clear</button>
</div>
</div>
<div class="kpi">
<span class="mono" id="browserInfo">Browser: detecting…</span>
<span class="mono" id="charCount">0 / 1,000,000</span>
</div>
<textarea id="textInput" maxlength="1000000" placeholder="Paste up to 1,000,000 characters here… (TXT / MD / HTML only). For PDF, use file upload (or paste/drop the PDF file here)."></textarea>
<div class="row" style="margin-top:1rem">
<div class="box">
<label>Paste behavior</label>
<div class="pill" style="gap:12px;align-items:center">
<div style="display:flex;align-items:center;gap:10px;flex:1;min-width:260px">
<input type="checkbox" id="llmCleanToggle" checked style="width:18px;height:18px;cursor:pointer" />
<div>
<div style="font-weight:900">Clean LLM output on paste</div>
<div class="small">Default ON. Removes emoji/markdown noise, preserves clinical symbols + punctuation.</div>
</div>
</div>
<button class="ghost-btn" id="previewBtn" type="button" style="flex:0;min-width:170px">Preview cleaned</button>
</div>
<details id="previewDetails" style="margin-top:.75rem">
<summary>Preview — cleaned text (read-only)</summary>
<textarea id="cleanPreview" readonly style="height:220px;margin-top:.75rem;background:#fff;border:2px solid #dbeafe"></textarea>
<div class="small muted">Preview uses the same cleaner used on paste. It does not change your text unless you paste with the toggle ON (or use “Apply to text” in the dictionary panel).</div>
</details>
</div>
<div class="box">
<label>Symbol dictionary (editable)</label>
<textarea id="symbolDict" class="mono" style="height:220px" spellcheck="false"></textarea>
<div class="small">Format: one mapping per line: <span class="mono">token = spoken</span>. Empty lines and lines starting with <span class="mono">#</span> are ignored.</div>
<div class="pill" style="margin-top:.6rem">
<button class="ghost-btn" id="resetDictBtn" type="button">Reset dictionary</button>
<button class="ghost-btn" id="applyDictBtn" type="button">Apply to text</button>
</div>
</div>
</div>
<div class="controls">
<div class="box">
<label>Native Browser Voices (Auto-detected)</label>
<select id="voiceSelect"></select>
<div class="small" id="voiceMeta">…</div>
</div>
<div class="box">
<label>Read mode (fidelity)</label>
<select id="readMode">
<option value="plain">Plain (no stripping)</option>
<option value="clean" selected>Clean (remove markup)</option>
<option value="aggressive">Aggressive clean (extra symbols removed)</option>
</select>
<div class="small">Tip: use <b>Plain</b> for maximum fidelity (esp. medical text).</div>
</div>
<div class="box">
<label>Speed: <span id="vRate">1.0</span>x</label>
<input type="range" id="rate" min="0.5" max="2.5" step="0.1" value="1">
</div>
<div class="box">
<label>Pitch: <span id="vPitch">1.0</span></label>
<input type="range" id="pitch" min="0.1" max="2" step="0.1" value="1">
</div>
<div class="box">
<label>Jump sections</label>
<select id="sectionSelect"></select>
<div class="pill" style="margin-top:.6rem">
<button class="ghost-btn" id="prevSectionBtn" type="button">Prev section</button>
<button class="ghost-btn" id="nextSectionBtn" type="button">Next section</button>
</div>
<div class="small">Auto-detected from headings / blank-line blocks. Works for TXT/MD/HTML-ish content.</div>
</div>
<div class="box">
<label>Loop</label>
<div class="pill">
<select id="loopTarget">
<option value="off" selected>Off</option>
<option value="selection">Loop selection</option>
<option value="section">Loop section</option>
<option value="all">Loop all</option>
</select>
<input type="number" id="loopCount" min="1" step="1" value="2" title="Number of loops">
</div>
<div class="small">Loop count applies after reaching end. Stop cancels looping.</div>
</div>
</div>
<div class="playback-bar">
<button class="play-btn" id="playAllBtn" type="button">Play all</button>
<button class="warn-btn" id="playSectionBtn" type="button">Play section</button>
<button class="ghost-btn" id="playSelectionBtn" type="button">Play selection</button>
<button class="ghost-btn" id="pauseBtn" type="button">Pause</button>
<button class="ghost-btn" id="rewBtn" type="button">⟲ Rewind</button>
<button class="ghost-btn" id="ffBtn" type="button">Fast-forward ⟳</button>
<button class="stop-btn" id="stopBtn" type="button">Stop</button>
</div>
<details style="margin-top:1rem">
<summary>Detected TTS engines (voices) — browser-native</summary>
<div class="small muted">This lists the voices returned by <span class="mono">speechSynthesis.getVoices()</span> on this device/browser profile.</div>
<div id="voicesTableWrap"></div>
</details>
<div class="status" id="status">Ready (detecting voices…)</div>
</div>
<script>
(() => {
"use strict";
// Only allow these file types (upload / drop / paste-file)
const ALLOWED_FILE_EXTS = new Set(["txt","md","html","htm","pdf"]);
// ---------------------------
// Core references
// ---------------------------
const synth = window.speechSynthesis;
const textInput = document.getElementById('textInput');
const voiceSelect = document.getElementById('voiceSelect');
const status = document.getElementById('status');
const sectionSelect = document.getElementById('sectionSelect');
const voicesTableWrap = document.getElementById('voicesTableWrap');
const browserInfo = document.getElementById('browserInfo');
const charCount = document.getElementById('charCount');
const readMode = document.getElementById('readMode');
const loopTarget = document.getElementById('loopTarget');
const loopCountEl = document.getElementById('loopCount');
const voiceMeta = document.getElementById('voiceMeta');
// LLM-clean + preview + symbol dictionary
const llmCleanToggle = document.getElementById('llmCleanToggle');
const previewBtn = document.getElementById('previewBtn');
const cleanPreview = document.getElementById('cleanPreview');
const previewDetails = document.getElementById('previewDetails');
const symbolDict = document.getElementById('symbolDict');
const resetDictBtn = document.getElementById('resetDictBtn');
const applyDictBtn = document.getElementById('applyDictBtn');
// ---------------------------
// Symbol dictionary (clinical-safe defaults)
// ---------------------------
const DEFAULT_SYMBOL_DICT = `
# Core comparators / arrows
≥ = greater than or equal to
≤ = less than or equal to
≈ = approximately
≃ = approximately
≅ = approximately
≠ = not equal to
→ = leads to
← = from
↔ = two-way
⇒ = therefore
± = plus minus
× = times
÷ = divided by
% = percent
# Units / common clinical
mmHg = millimeters of mercury
bpm = beats per minute
SpO₂ = SpO2
FiO₂ = FiO2
PaO₂ = PaO2
PaCO₂ = PaCO2
HCO₃ = HCO3
Na⁺ = sodium
K⁺ = potassium
Ca²⁺ = calcium
Mg²⁺ = magnesium
Cl⁻ = chloride
µg = micrograms
µL = microliters
µm = micrometers
° = degrees
# Make common decorative symbols silent
• =
✅ =
☑ =
☐ =
◻ =
▪ =
# Normalize dashes
— = -
– = -
`.trim();
function loadDictFromStorage(){
const saved = localStorage.getItem("stts_symbol_dict_v1");
symbolDict.value = saved ? saved : DEFAULT_SYMBOL_DICT;
}
function saveDictToStorage(){
localStorage.setItem("stts_symbol_dict_v1", symbolDict.value || "");
}
function parseSymbolDict(text){
const map = new Map();
(text || "").split(/\n/).forEach(line => {
const raw = line.trim();
if (!raw || raw.startsWith("#")) return;
const idx = raw.indexOf("=");
if (idx === -1) return;
const key = raw.slice(0, idx).trim();
const val = raw.slice(idx+1).trim();
if (!key) return;
map.set(key, val);
});
return map;
}
function applySymbolMap(input, map){
let out = input || "";
if (!map || !map.size) return out;
const keys = Array.from(map.keys()).sort((a,b)=> b.length - a.length);
for (const k of keys){
const v = map.get(k);
const escaped = k.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const re = new RegExp(escaped, "g");
out = out.replace(re, (v ? v : " "));
}
return out;
}
// ---------------------------
// LLM adaptive cleaner (paste + preview)
// ---------------------------
function stripEmoji(text){
return (text || "")
.replace(/[\u{1F300}-\u{1FAFF}]/gu, "")
.replace(/[\u{2600}-\u{27BF}]/gu, "")
.replace(/[\uFE0E\uFE0F]/g, "")
.replace(/\u200D/g, "");
}
function cleanLLMText(raw){
let t = (raw || "").replace(/\r\n/g, "\n");
// Remove fenced code blocks; keep inline code content
t = t.replace(/```[\s\S]*?```/g, " ");
t = t.replace(/`([^`]+)`/g, "$1");
// Remove markdown links target, keep visible text
t = t.replace(/\[([^\]]+)\]\(([^\)]+)\)/g, "$1");
// Remove HTML tags but keep content
t = t.replace(/<script[\s\S]*?<\/script>/gi, " ");
t = t.replace(/<style[\s\S]*?<\/style>/gi, " ");
t = t.replace(/<[^>]*>/g, " ");
// Line-wise cleanup: headings, bullets, quotes, checkboxes
t = t.split("\n").map(line => {
let s = line;
s = s.replace(/^\s{0,3}(#{1,6})\s+/, ""); // headings
s = s.replace(/^\s{0,3}>\s?/, ""); // blockquote
s = s.replace(/^\s{0,3}[-*•]+\s+/, ""); // bullets
s = s.replace(/^\s{0,3}\[(x|X| )\]\s+/, ""); // checkboxes
// emphasis markers
s = s.replace(/\*\*([^*]+)\*\*/g, "$1");
s = s.replace(/__([^_]+)__/g, "$1");
s = s.replace(/~~([^~]+)~~/g, "$1");
return s;
}).join("\n");
// Remove emoji + common ASCII emoticons
t = stripEmoji(t);
t = t.replace(/(^|\s)(:-\)|:\)|:-\(|:\(|;\)|;\-\)|:D|:-D|XD|xD|:\/|:-\/|:\\|:-\\)(?=\s|$)/g, " ");
// Normalize punctuation spacing (keep punctuation)
t = t.replace(/\s+([,.;:!?])/g, "$1");
t = t.replace(/([,.;:!?])(\S)/g, "$1 $2");
// Convert decorative dividers to paragraph break
t = t.replace(/\n\s*([\-_=*]{3,})\s*\n/g, "\n\n");
// Apply dictionary
const map = parseSymbolDict(symbolDict.value || DEFAULT_SYMBOL_DICT);
t = applySymbolMap(t, map);
// Collapse whitespace while preserving paragraph breaks
t = t.replace(/[ \t]{2,}/g, " ");
t = t.replace(/\n{3,}/g, "\n\n");
t = t.split("\n").map(x => x.trimEnd()).join("\n").trim();
// If the text looks like a list (many short lines), treat single newlines as sentence breaks.
const lines = t.split("\n");
const nonEmpty = lines.filter(x => x.trim()).length;
const shortLines = lines.filter(x => x.trim() && x.trim().length <= 80).length;
const looksLikeList = (nonEmpty >= 8) && (shortLines / Math.max(1, nonEmpty) >= 0.6);
if (looksLikeList){
t = t.replace(/\n(?!\n)/g, ".\n"); // force a pause
t = t.replace(/\.\n\n/g, "\n\n"); // don't double punctuate before paragraph break
}
// Ensure paragraphs end with punctuation when they are long without it (light touch)
t = t.replace(/([A-Za-z0-9\)\]\}])(\n\n)/g, "$1.$2");
return t;
}
// Chunked playback state (enables fast-forward / rewind)
let chunks = [];
let chunkIndex = 0;
let activeUtterance = null;
let lastSpokenContext = { kind: "all", text: "" }; // all | section | selection
let loopsDone = 0;
// Sections state
let sections = []; // [{title, text}]
let currentSectionIndex = 0;
// ---------------------------
// Helpers
// ---------------------------
function setStatus(msg){ status.textContent = msg; }
function clamp(n, a, b){ return Math.max(a, Math.min(b, n)); }
function getSelectedVoice(){
const voices = synth.getVoices();
return voices.find(v => v.name === voiceSelect.value) || voices[0] || null;
}
function detectBrowser(){
const ua = navigator.userAgent || "";
let name = "Browser";
if (/Edg\//.test(ua)) name = "Microsoft Edge";
else if (/Chrome\//.test(ua) && !/Edg\//.test(ua)) name = "Google Chrome";
else if (/Firefox\//.test(ua)) name = "Mozilla Firefox";
else if (/Safari\//.test(ua) && !/Chrome\//.test(ua)) name = "Apple Safari";
browserInfo.textContent = `${name} | UA: ${ua.slice(0, 90)}${ua.length>90?'…':''}`;
}
function updateCharCount(){
const len = (textInput.value || "").length;
charCount.textContent = `${len.toLocaleString()} / 1,000,000`;
}
// Fidelity-preserving text cleanup modes
function sanitize(text){
const mode = readMode.value;
if (mode === "plain") {
// Minimal changes: normalize whitespace only.
return (text || "").replace(/\r\n/g, "\n").trim();
}
if (mode === "clean") {
// Similar to your previous behavior, but keep more punctuation.
return (text || "")
.replace(/\r\n/g, "\n")
.replace(/\[([^\]]+)\]\([^\)]+\)/g, '$1') // keep link text
.replace(/<script[\s\S]*?<\/script>/gi, ' ') // drop scripts
.replace(/<style[\s\S]*?<\/style>/gi, ' ') // drop styles
.replace(/<[^>]*>/g, ' ') // drop HTML tags
.replace(/[ \t]+\n/g, "\n")
.replace(/\n{3,}/g, "\n\n")
.replace(/[ \t]{2,}/g, " ")
.trim();
}
// aggressive
return (text || "")
.replace(/\r\n/g, "\n")
.replace(/[#*|>_~`]/g, ' ')
.replace(/--+/g, ' ')
.replace(/\[([^\]]+)\]\([^\)]+\)/g, '$1')
.replace(/<script[\s\S]*?<\/script>/gi, ' ')
.replace(/<style[\s\S]*?<\/style>/gi, ' ')
.replace(/<[^>]*>/g, ' ')
.replace(/\s+/g, ' ')
.trim();
}
// Chunking: split to manageable utterances; keeps sentence-ish boundaries.
function buildChunks(text, maxLen=520){
const t = (text || "").trim();
if (!t) return [];
const normalized = t.replace(/\r\n/g, "\n").replace(/[ \t]+\n/g, "\n");
// Paragraphs
const paras = normalized.replace(/\n{3,}/g, "\n\n").split(/\n\n/g);
const candidates = [];
for (const para of paras){
const p = para.trim();
if (!p) continue;
// Single newline inside paragraph becomes a comma pause
const p2 = p.replace(/\n(?!\n)/g, " , ");
// Sentence boundary: punctuation + common danda.
const sent = p2.split(/(?<=[\.!?;:।。!?])\s+/g);
for (const s of sent){
const x = s.replace(/\s+/g, " ").trim();
if (x) candidates.push(x);
}
}
// Assemble chunks
const out = [];
let buf = "";
for (const c of candidates){
if (!buf){
buf = c;
continue;
}
if ((buf.length + 1 + c.length) <= maxLen){
buf = buf + " " + c;
} else {
out.push(buf);
buf = c;
}
}
if (buf) out.push(buf);
// Hard-wrap any oversize
const finalOut = [];
for (const ch of out){
if (ch.length <= maxLen){
finalOut.push(ch);
} else {
for (let i=0; i<ch.length; i+=maxLen){
finalOut.push(ch.slice(i, i+maxLen));
}
}
}
return finalOut;
}
// Section detection from MD headings / HTML headings / blank-line blocks.
function detectSections(rawText){
const original = (rawText || "");
const lines = original.replace(/\r\n/g, "\n").split("\n");
// 1) Markdown headings
const mdHeadIdx = [];
for (let i=0;i<lines.length;i++){
const m = lines[i].match(/^\s{0,3}(#{1,6})\s+(.+?)\s*$/);
if (m) mdHeadIdx.push({i, title:m[2].trim()});
}
if (mdHeadIdx.length >= 2){
const secs = [];
for (let k=0;k<mdHeadIdx.length;k++){
const start = mdHeadIdx[k].i;
const end = (k+1<mdHeadIdx.length) ? mdHeadIdx[k+1].i : lines.length;
const title = mdHeadIdx[k].title || `Section ${k+1}`;
const body = lines.slice(start, end).join("\n");
secs.push({title, text: body});
}
return secs;
}
// 2) HTML headings (best-effort) if present in text
const htmlHeads = [];
const html = original;
const re = /<(h[1-6])[^>]*>([\s\S]*?)<\/\1>/gi;
let m;
while ((m = re.exec(html)) !== null){
const title = (m[2] || "").replace(/<[^>]*>/g, " ").replace(/\s+/g," ").trim();
if (title) htmlHeads.push(title);
if (htmlHeads.length > 2) break;
}
if (htmlHeads.length >= 2){
// fallback: split by headings text occurrences
const secs = [];
let cursor = 0;
for (let k=0;k<htmlHeads.length;k++){
const t = htmlHeads[k];
const idx = html.indexOf(t, cursor);
const nextIdx = (k+1<htmlHeads.length) ? html.indexOf(htmlHeads[k+1], idx+ t.length) : html.length;
const slice = html.slice(idx, nextIdx);
secs.push({title:t, text:slice});
cursor = nextIdx;
}
return secs;
}
// 3) Blank-line blocks (paragraph-style sections)
const clean = original.replace(/\r\n/g, "\n").trim();
if (!clean) return [];
const blocks = clean.split(/\n{2,}/g).map(s => s.trim()).filter(Boolean);
const secs = [];
for (let i=0;i<blocks.length;i++){
const firstLine = blocks[i].split("\n")[0].trim();
const title = (firstLine.length<=60) ? firstLine : `Block ${i+1}`;
secs.push({title, text: blocks[i]});
}
return secs;
}
function refreshSections(){
sections = detectSections(textInput.value);
if (!sections.length){
sections = [{title:"(All text)", text: textInput.value || ""}];
}
sectionSelect.innerHTML = sections.map((s, i) => {
const t = (s.title || `Section ${i+1}`).replace(/</g,"<").replace(/>/g,">");
return `<option value="${i}">${String(i+1).padStart(2,'0')}. ${t}</option>`;
}).join("");
currentSectionIndex = 0;
sectionSelect.value = "0";
}
function getTextFor(kind){
if (kind === "selection"){
const start = textInput.selectionStart ?? 0;
const end = textInput.selectionEnd ?? 0;
if (end > start) return textInput.value.slice(start, end);
return "";
}
if (kind === "section"){
const idx = parseInt(sectionSelect.value || "0", 10);
const s = sections[idx] || sections[0];
return s ? s.text : "";
}
return textInput.value || "";
}
function resetPlayback(){
chunks = [];
chunkIndex = 0;
activeUtterance = null;
loopsDone = 0;
}
// ---------------------------
// Voices: load + list engines
// ---------------------------
function renderVoicesTable(voices){
if (!voices || !voices.length){
voicesTableWrap.innerHTML = "<div class='small muted'>No voices returned by this browser profile.</div>";
return;
}
const rows = voices
.slice()
.sort((a,b) => (a.lang||"").localeCompare(b.lang||"") || (a.name||"").localeCompare(b.name||""))
.map(v => {
const ls = (typeof v.localService === "boolean") ? (v.localService ? "Local" : "Remote") : "—";
const dv = v.default ? "Yes" : "";
return `<tr>
<td>${escapeHtml(v.name||"")}</td>
<td>${escapeHtml(v.lang||"")}</td>
<td>${escapeHtml(ls)}</td>
<td>${escapeHtml(dv)}</td>
</tr>`;
}).join("");
voicesTableWrap.innerHTML = `
<table>
<thead><tr><th>Voice</th><th>Lang</th><th>Service</th><th>Default</th></tr></thead>
<tbody>${rows}</tbody>
</table>`;
}
function escapeHtml(s){
return String(s)
.replace(/&/g,"&")
.replace(/</g,"<")
.replace(/>/g,">")
.replace(/"/g,""")
.replace(/'/g,"'");
}
function loadVoices(){
const voices = synth.getVoices();
if (!voices || !voices.length){
voiceSelect.innerHTML = `<option value="">(No voices found)</option>`;
voiceMeta.textContent = "No voices detected. Try another browser profile, or enable system voices.";
renderVoicesTable([]);
setStatus("Ready (no voices returned).");
return;
}
voiceSelect.innerHTML = voices
.slice()
.sort((a,b)=> (a.name||"").localeCompare(b.name||""))
.map(v => `<option value="${escapeHtml(v.name)}">${escapeHtml(v.name)} (${escapeHtml(v.lang||"")})</option>`)
.join("");
// Auto-select a "natural" voice when possible
const natural = voices.find(v => /Natural|Neural|Online/i.test(v.name));
if (natural) voiceSelect.value = natural.name;
const selected = getSelectedVoice();
voiceMeta.textContent = `${voices.length} voices detected. Selected: ${selected ? selected.name : "—"}`;
renderVoicesTable(voices);
setStatus("Ready (system voices detected).");
}
// Chrome/Edge/Safari compatibility for voice loading
if (speechSynthesis.onvoiceschanged !== undefined) {
speechSynthesis.onvoiceschanged = loadVoices;
}
loadVoices();
// ---------------------------
// Speaking: sequential chunks
// ---------------------------
function speakFrom(index){
handleStop(); // stop current speech
const voice = getSelectedVoice();
if (!voice){
alert("No TTS voice detected in this browser profile.");
return;
}
if (!chunks.length){
setStatus("Nothing to speak.");
return;
}
chunkIndex = clamp(index, 0, chunks.length-1);
const utterance = new SpeechSynthesisUtterance(chunks[chunkIndex]);
activeUtterance = utterance;
utterance.voice = voice;
utterance.rate = parseFloat(document.getElementById('rate').value);
utterance.pitch = parseFloat(document.getElementById('pitch').value);
utterance.onstart = () => {
setStatus(`Speaking: ${voice.name} | chunk ${chunkIndex+1}/${chunks.length}`);
updatePauseBtn();
};
utterance.onerror = (e) => {
setStatus(`TTS error: ${e.error || "unknown"} (try another voice/browser)`);
};
utterance.onend = () => {
// advance
if (chunkIndex < chunks.length - 1){
chunkIndex++;
speakFrom(chunkIndex);
return;
}
// finished this run
const loopMode = loopTarget.value;
const wantLoop = (loopMode !== "off");
const maxLoops = clamp(parseInt(loopCountEl.value || "1", 10), 1, 9999);
if (wantLoop){
loopsDone++;
if (loopsDone < maxLoops){
setStatus(`Loop ${loopsDone}/${maxLoops} complete. Restarting…`);
speakFrom(0);
return;
}
}
setStatus("Finished.");
updatePauseBtn();
};
synth.speak(utterance);
updatePauseBtn();
}
function prepareAndPlay(kind){
const raw = getTextFor(kind);
if (!raw || !raw.trim()){
if (kind === "selection") return alert("No text selected.");
return alert("Text area is empty.");
}
const processed = sanitize(raw);
if (!processed.trim()){
alert("After cleaning, nothing remained to read. Try Read mode = Plain.");
return;
}
resetPlayback();
lastSpokenContext = {kind, text: raw};
chunks = buildChunks(processed, 900);
if (!chunks.length){
alert("Unable to chunk the text. Try simpler content.");
return;
}
speakFrom(0);
}
function handleStop(){
synth.cancel();
activeUtterance = null;
setStatus("Stopped.");
updatePauseBtn();
}
function fastForward(){
if (!chunks.length) return;
// Jump ahead ~3 chunks (approx "fast-forward")
const step = 3;
const next = clamp(chunkIndex + step, 0, chunks.length - 1);
if (next === chunkIndex) return;
speakFrom(next);
}
function rewind(){
if (!chunks.length) return;
const step = 3;
const prev = clamp(chunkIndex - step, 0, chunks.length - 1);
speakFrom(prev);
}
// ---------------------------
// Pause / Resume (native)
// ---------------------------
function updatePauseBtn(){
const btn = document.getElementById('pauseBtn');
if (!btn) return;
if (!synth.speaking){
btn.textContent = "Pause / Resume";
btn.disabled = true;
return;
}
btn.disabled = false;
if (synth.paused){
btn.textContent = "Pause / Resume";
} else {
btn.textContent = "Pause / Resume";
}
}
function togglePauseResume(){
if (!synth.speaking){
setStatus("Nothing is playing.");
updatePauseBtn();
return;
}
if (synth.paused){
synth.resume();
setStatus("Playing");
} else {
synth.pause();
setStatus("Paused");
}
updatePauseBtn();
}
// ---------------------------
// File loading: high-fidelity paths + optional parsers
// ---------------------------
async function loadFile(file){
if (!file) return;
const name = (file.name || "").toLowerCase();
const ext = name.includes(".") ? name.split(".").pop() : "";
if (!ALLOWED_FILE_EXTS.has(ext)){
setStatus(`Unsupported file type: .${ext || "?"}. Allowed: .txt, .md, .html/.htm, .pdf (clean text).`);
return;
}
// Text-like formats
const textExts = new Set(["txt","md","html","htm"]);
if (textExts.has(ext)){
const reader = new FileReader();
reader.onload = (ev) => {
textInput.value = ev.target.result || "";
updateCharCount();
refreshSections();
setStatus(`Loaded: ${file.name} (as text)`);
};
reader.readAsText(file);
return;
}
// PDF (optional PDF.js)
if (ext === "pdf"){
if (!window.pdfjsLib){
setStatus("PDF detected. This app supports clean text PDFs only. For offline extraction, include a local PDF.js build (pdfjsLib). Otherwise, export PDF → TXT and paste/load the text.");
return;
}
try{
setStatus("PDF detected. Extracting text (PDF.js)…");
const buf = await file.arrayBuffer();
const pdf = await window.pdfjsLib.getDocument({data: buf}).promise;
let fullText = "";
for (let p=1; p<=pdf.numPages; p++){
const page = await pdf.getPage(p);
const content = await page.getTextContent();
const strings = content.items.map(it => it.str);
fullText += strings.join(" ") + "\n\n";
}
textInput.value = fullText.trim();
updateCharCount();
refreshSections();
setStatus(`Loaded: ${file.name} (PDF.js text extract).`);
} catch(e){
setStatus("PDF extraction failed. This app supports clean text PDFs only. If your PDF is scanned, export/convert it to text first (OCR not included).");
}
return;
}
setStatus(`Unsupported file type: ${file.name}. Allowed: TXT, MD, HTML (.htm), or clean text PDF.`);
}
// ---------------------------
// UI wiring
// ---------------------------
document.getElementById('fileInput').addEventListener('change', (e) => {
const file = e.target.files && e.target.files[0];
loadFile(file);
e.target.value = ""; // allow reloading same file
});
// Allow paste/drop of allowed files directly into the input box
// (Some browsers let you paste a file object; drag-drop is common.)
function fileFromDataTransfer(dt){
if (!dt) return null;
if (dt.files && dt.files.length) return dt.files[0];
return null;
}
function extOfFile(f){
const n = (f && f.name ? f.name : "").toLowerCase();
return n.includes(".") ? n.split(".").pop() : "";
}
function tryLoadDroppedOrPastedFile(file, sourceLabel){
if (!file) return false;
const ext = extOfFile(file);
if (!ALLOWED_FILE_EXTS.has(ext)){
setStatus(`Unsupported ${sourceLabel} file type: .${ext || "?"}. Allowed: .txt, .md, .html/.htm, .pdf (clean text).`);
return true; // handled (rejected)
}
loadFile(file);
return true;
}
textInput.addEventListener('paste', (e) => {
const file = fileFromDataTransfer(e.clipboardData);
if (file){
e.preventDefault();
tryLoadDroppedOrPastedFile(file, "pasted");
return;
}
const clip = (e.clipboardData && e.clipboardData.getData) ? e.clipboardData.getData("text/plain") : "";
if (!clip) return;
if (llmCleanToggle && llmCleanToggle.checked){
e.preventDefault();
const cleaned = cleanLLMText(clip);
const start = textInput.selectionStart ?? textInput.value.length;
const end = textInput.selectionEnd ?? textInput.value.length;
const before = textInput.value.slice(0, start);
const after = textInput.value.slice(end);
textInput.value = before + cleaned + after;
const pos = (before + cleaned).length;
textInput.selectionStart = textInput.selectionEnd = pos;
updateCharCount();
scheduleSectionsRefresh();
setStatus("Pasted (LLM-clean ON).");
}
});
textInput.addEventListener('dragover', (e) => {
e.preventDefault();
});
textInput.addEventListener('drop', (e) => {
const file = fileFromDataTransfer(e.dataTransfer);
if (file){
e.preventDefault();
tryLoadDroppedOrPastedFile(file, "dropped");
}
});
// Dictionary + preview wiring
loadDictFromStorage();
symbolDict.addEventListener('input', () => {
saveDictToStorage();
});
resetDictBtn.addEventListener('click', () => {
if (!confirm("Reset symbol dictionary to defaults?")) return;
symbolDict.value = DEFAULT_SYMBOL_DICT.trim();
saveDictToStorage();
setStatus("Symbol dictionary reset.");
});
applyDictBtn.addEventListener('click', () => {
const raw = textInput.value || "";
if (!raw.trim()) return;
const map = parseSymbolDict(symbolDict.value || DEFAULT_SYMBOL_DICT);
textInput.value = applySymbolMap(raw, map);
updateCharCount();
refreshSections();
setStatus("Applied dictionary to text.");
});
previewBtn.addEventListener('click', () => {
const raw = textInput.value || "";
cleanPreview.value = raw ? cleanLLMText(raw) : "";
previewDetails.open = true;
setStatus("Preview updated.");
});
document.getElementById('clearBtn').addEventListener('click', () => {
handleStop();
textInput.value = "";
updateCharCount();
refreshSections();
setStatus("Cleared.");
});
document.getElementById('rate').addEventListener('input', (e) => document.getElementById('vRate').textContent = e.target.value);
document.getElementById('pitch').addEventListener('input', (e) => document.getElementById('vPitch').textContent = e.target.value);
document.getElementById('playAllBtn').addEventListener('click', () => {
loopTarget.value = (loopTarget.value === "selection" || loopTarget.value === "section") ? "all" : loopTarget.value; // keep user intent