-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
734 lines (660 loc) · 23.7 KB
/
script.js
File metadata and controls
734 lines (660 loc) · 23.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
'use strict';
/* ============================================================
APP STATE & CONFIG
============================================================ */
const AppState = {
apiKey: '',
endpoint: '',
isConnected: false,
isRecording: false,
isSpeaking: false,
transcript: '',
audioBlob: null,
voices: [],
currentLang: 'en-US', // Default STT language
};
// Supported Languages for STT (Speech Recognition)
const SUPPORTED_LANGS = [
{ code: 'en-US', name: 'English (US)' },
{ code: 'hi-IN', name: 'Hindi (India)' },
{ code: 'es-ES', name: 'Spanish (Spain)' },
{ code: 'fr-FR', name: 'French (France)' },
{ code: 'de-DE', name: 'German (Germany)' },
{ code: 'ja-JP', name: 'Japanese (Japan)' },
{ code: 'zh-CN', name: 'Chinese (Mandarin)' },
{ code: 'ru-RU', name: 'Russian (Russia)' },
{ code: 'pt-BR', name: 'Portuguese (Brazil)' }
];
/* ============================================================
UTILITY — Toast Notifications
============================================================ */
const Toast = {
container: null,
init() { this.container = document.getElementById('toast-container'); },
show(message, type = 'info', duration = 3500) {
const toast = document.createElement('div');
toast.className = `toast ${type}`;
toast.innerHTML = `<span class="toast-dot"></span><span>${message}</span>`;
this.container.appendChild(toast);
setTimeout(() => {
toast.classList.add('hiding');
setTimeout(() => toast.remove(), 260);
}, duration);
},
success(msg) { this.show(msg, 'success'); },
error(msg) { this.show(msg, 'error', 4500); },
info(msg) { this.show(msg, 'info'); },
warning(msg) { this.show(msg, 'warning', 4000); },
};
/* ============================================================
UTILITY — DOM helpers
============================================================ */
const $ = (sel) => document.querySelector(sel);
const $id = (id) => document.getElementById(id);
/* ============================================================
UI CONTROLLER
============================================================ */
const UI = {
updateConnectionStatus(state) {
const chip = $id('connection-status');
if (!chip) return;
chip.className = 'status-chip';
if (state === 'connected') {
chip.classList.add('connected');
chip.querySelector('.status-label').textContent = 'Connected';
} else if (state === 'error') {
chip.classList.add('error');
chip.querySelector('.status-label').textContent = 'Error';
} else {
chip.querySelector('.status-label').textContent = 'Not Connected';
}
},
showFieldError(id, msg) {
const el = $id(id);
if (el) {
el.textContent = msg;
el.classList.remove('hidden');
const inputId = id.replace('-error', '');
const input = $id(inputId);
if (input) input.classList.add('error');
}
},
clearFieldError(id) {
const el = $id(id);
if (el) {
el.classList.add('hidden');
const inputId = id.replace('-error', '');
const input = $id(inputId);
if (input) input.classList.remove('error');
}
},
clearAllErrors() {
['key-error', 'endpoint-error'].forEach(id => this.clearFieldError(id));
},
setConnectBtnLoading(loading) {
const btn = $id('connect-btn');
if (!btn) return;
if (loading) {
btn.innerHTML = `<span class="spinner"></span> Connecting…`;
btn.disabled = true;
} else {
btn.innerHTML = `<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><path d="M5 12h14M12 5l7 7-7 7"/></svg> Connect Service`;
btn.disabled = false;
}
},
setRecorderStatus(status, text) {
const el = $id('recorder-status').querySelector('.status-text');
if (!el) return;
el.className = 'status-text';
if (status) el.classList.add(status);
el.textContent = text;
},
setWaveformActive(active) {
const bars = document.querySelectorAll('.waveform-bar');
bars.forEach(bar => {
if (active) bar.classList.add('active');
else {
bar.classList.remove('active');
bar.style.height = '8px';
}
});
},
animateWaveform() {
const bars = document.querySelectorAll('.waveform-bar');
bars.forEach(bar => {
const h = Math.floor(Math.random() * 38) + 8;
bar.style.height = `${h}px`;
});
},
setRecordBtn(recording) {
const btn = $id('record-btn');
const label = $id('record-label');
if (!btn || !label) return;
if (recording) {
btn.classList.add('recording');
label.textContent = 'Stop Recording';
} else {
btn.classList.remove('recording');
label.textContent = 'Start Recording';
}
},
setTranscript(text, interim = false) {
const el = $id('transcript-output');
if (!el) return;
if (!text) {
el.innerHTML = `<span class="placeholder-text">Your transcript will appear here…</span>`;
const copyBtn = $id('copy-transcript-btn');
if (copyBtn) copyBtn.disabled = true;
const meta = $id('transcript-meta');
if (meta) meta.textContent = '';
return;
}
el.textContent = text + (interim ? '…' : '');
const copyBtn = $id('copy-transcript-btn');
if (copyBtn) copyBtn.disabled = false;
if (!interim) {
const wc = text.trim().split(/\s+/).filter(Boolean).length;
const meta = $id('transcript-meta');
if (meta) meta.textContent = `${wc} word${wc !== 1 ? 's' : ''} · ${text.length} chars`;
}
},
setBadge(id, label) {
const el = $id(id);
if (el) el.textContent = label;
},
updateSpeakBtn(speaking) {
const btn = $id('speak-btn');
const stop = $id('stop-btn');
if (!btn || !stop) return;
if (speaking) {
btn.innerHTML = `<span class="spinner"></span> <span>Synthesizing…</span>`;
btn.disabled = true;
stop.disabled = false;
} else {
btn.innerHTML = `<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2"><polygon points="5 3 19 12 5 21 5 3"/></svg> <span>Convert to Speech</span>`;
const input = $id('tts-input');
btn.disabled = (input && input.value.trim().length === 0);
stop.disabled = true;
}
},
showAudioPlayer(show) {
const card = $id('audio-player-card');
if (card) {
if (show) card.classList.remove('hidden');
else card.classList.add('hidden');
}
},
setTTSProgress(pct, statusText) {
const fill = $id('tts-progress-fill');
const status = $id('tts-status');
if (fill) fill.style.width = `${pct}%`;
if (status && statusText) status.textContent = statusText;
},
};
/* ============================================================
VALIDATOR
============================================================ */
const Validator = {
validateCredentials(key, endpoint) {
let valid = true;
if (!key || key.trim().length < 4) {
UI.showFieldError('key-error', 'API Key is required and must be at least 4 characters.');
valid = false;
} else {
UI.clearFieldError('key-error');
}
if (!endpoint || !endpoint.trim()) {
UI.showFieldError('endpoint-error', 'Endpoint URL is required.');
valid = false;
} else if (!endpoint.trim().startsWith('https://')) {
UI.showFieldError('endpoint-error', 'Endpoint must start with https://');
valid = false;
} else {
UI.clearFieldError('endpoint-error');
}
return valid;
},
};
/* ============================================================
SPEECH ENGINE
============================================================ */
const SpeechEngine = {
recognition: null,
synthesis: window.speechSynthesis,
waveInterval: null,
// ----------- STT -----------
startRecognition(langCode, onResult, onError, onEnd) {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!SpeechRecognition) {
onError('Speech recognition not supported. Use Chrome/Edge.');
return;
}
const rec = new SpeechRecognition();
rec.continuous = true;
rec.interimResults = true;
rec.lang = langCode;
rec.maxAlternatives = 1;
this.recognition = rec;
rec.onstart = () => {
this.waveInterval = setInterval(() => UI.animateWaveform(), 80);
};
rec.onresult = (event) => {
let interim = '';
let final = '';
for (let i = event.resultIndex; i < event.results.length; i++) {
const t = event.results[i][0].transcript;
if (event.results[i].isFinal) final += t + ' ';
else interim += t;
}
onResult({ final: final.trim(), interim: interim.trim() });
};
rec.onerror = (event) => {
clearInterval(this.waveInterval);
let msg = 'Error: ' + event.error;
// Specific handling for common errors
if (event.error === 'not-allowed') {
msg = 'Microphone permission denied. Please allow access in browser settings.';
} else if (event.error === 'no-speech') {
msg = 'No speech detected. Try again.';
} else if (event.error === 'network') {
// This usually happens if running from file:// protocol
msg = 'Network error. Ensure you are running on localhost or HTTPS, not file://.';
}
onError(msg);
};
rec.onend = () => {
clearInterval(this.waveInterval);
onEnd();
};
try {
rec.start();
} catch (e) {
onError('Failed to start microphone. Check permissions.');
}
},
stopRecognition() {
clearInterval(this.waveInterval);
if (this.recognition) {
this.recognition.stop();
this.recognition = null;
}
},
// ----------- TTS -----------
loadVoices() {
return new Promise((resolve) => {
const populate = () => {
const voices = this.synthesis.getVoices();
if (voices.length) resolve(voices);
};
populate();
this.synthesis.onvoiceschanged = populate;
setTimeout(() => resolve(this.synthesis.getVoices()), 1000);
});
},
speak(text, voice, rate, pitch, onStart, onEnd, onError) {
if (this.synthesis.speaking) this.synthesis.cancel();
const utterance = new SpeechSynthesisUtterance(text);
if (voice) utterance.voice = voice;
utterance.rate = parseFloat(rate) || 1;
utterance.pitch = parseFloat(pitch) || 1;
utterance.onstart = onStart;
utterance.onend = onEnd;
utterance.onerror = (e) => onError('Speech synthesis error: ' + e.error);
this.synthesis.speak(utterance);
},
stop() {
if (this.synthesis.speaking) this.synthesis.cancel();
},
};
/* ============================================================
VOICE & LANGUAGE SELECTOR
============================================================ */
async function loadVoicesAndLanguages() {
const voices = await SpeechEngine.loadVoices();
AppState.voices = voices;
// 1. Populate STT Language Dropdown
const sttLangSelect = $id('stt-lang-select');
if (sttLangSelect) {
sttLangSelect.innerHTML = '';
SUPPORTED_LANGS.forEach(lang => {
const opt = document.createElement('option');
opt.value = lang.code;
opt.textContent = lang.name;
if (lang.code === AppState.currentLang) opt.selected = true;
sttLangSelect.appendChild(opt);
});
}
// 2. Populate TTS Voice Dropdown (Load ALL voices)
updateTTSVoiceDropdown();
}
function updateTTSVoiceDropdown() {
const ttsVoiceSelect = $id('voice-select');
if (!ttsVoiceSelect) return;
ttsVoiceSelect.innerHTML = '';
// Sort voices by language then name
const sortedVoices = AppState.voices.sort((a, b) => {
return a.lang.localeCompare(b.lang) || a.name.localeCompare(b.name);
});
if (sortedVoices.length === 0) {
const opt = document.createElement('option');
opt.textContent = 'No voices found';
ttsVoiceSelect.appendChild(opt);
return;
}
// Group by Language for cleaner UI
let currentLangGroup = '';
let currentOptGroup = null;
sortedVoices.forEach((v, i) => {
// Find original index in AppState.voices to keep reference correct
const originalIndex = AppState.voices.indexOf(v);
// Create OptGroup if language changes
if (v.lang !== currentLangGroup) {
currentLangGroup = v.lang;
currentOptGroup = document.createElement('optgroup');
currentOptGroup.label = v.lang; // e.g., "hi-IN"
ttsVoiceSelect.appendChild(currentOptGroup);
}
const opt = document.createElement('option');
opt.value = originalIndex;
// Gender hint
const nameLower = v.name.toLowerCase();
const genderHint = (nameLower.includes('female') || nameLower.includes('zira') ||
nameLower.includes('samantha') || nameLower.includes('victoria'))
? '♀' : '♂';
opt.textContent = `${genderHint} ${v.name}`;
currentOptGroup.appendChild(opt);
});
}
/* ============================================================
THEME TOGGLE
============================================================ */
function initTheme() {
const saved = localStorage.getItem('voiceos-theme') || 'light';
document.documentElement.setAttribute('data-theme', saved);
updateThemeLabel(saved);
}
function toggleTheme() {
const current = document.documentElement.getAttribute('data-theme');
const next = current === 'light' ? 'dark' : 'light';
document.documentElement.setAttribute('data-theme', next);
localStorage.setItem('voiceos-theme', next);
updateThemeLabel(next);
}
function updateThemeLabel(theme) {
const label = $id('theme-label');
if (label) label.textContent = theme === 'light' ? 'Dark Mode' : 'Light Mode';
}
/* ============================================================
TAB NAVIGATION
============================================================ */
function initTabs() {
document.querySelectorAll('.nav-item').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.nav-item').forEach(b => b.classList.remove('active'));
document.querySelectorAll('.tab-panel').forEach(p => p.classList.remove('active'));
btn.classList.add('active');
const tabId = `tab-${btn.dataset.tab}`;
const panel = $id(tabId);
if (panel) panel.classList.add('active');
});
});
}
/* ============================================================
CREDENTIALS
============================================================ */
function initCredentials() {
const apiKeyInput = $id('api-key');
const endpointInput = $id('endpoint');
const connectBtn = $id('connect-btn');
const clearBtn = $id('clear-btn');
const toggleKey = $id('toggle-key');
if (apiKeyInput) apiKeyInput.addEventListener('input', () => UI.clearFieldError('key-error'));
if (endpointInput) endpointInput.addEventListener('input', () => UI.clearFieldError('endpoint-error'));
if (toggleKey) {
toggleKey.addEventListener('click', () => {
const isPassword = apiKeyInput.type === 'password';
apiKeyInput.type = isPassword ? 'text' : 'password';
toggleKey.querySelector('.eye-open').style.display = isPassword ? 'none' : '';
toggleKey.querySelector('.eye-closed').style.display = isPassword ? '' : 'none';
});
}
if (connectBtn) {
connectBtn.addEventListener('click', async () => {
const key = apiKeyInput.value;
const endpoint = endpointInput.value;
UI.clearAllErrors();
if (!Validator.validateCredentials(key, endpoint)) return;
UI.setConnectBtnLoading(true);
await new Promise(r => setTimeout(r, 1200));
AppState.apiKey = key.trim();
AppState.endpoint = endpoint.trim();
AppState.isConnected = true;
UI.setConnectBtnLoading(false);
UI.updateConnectionStatus('connected');
UI.setBadge('stt-mode-badge', 'Azure + Web Speech');
UI.setBadge('tts-mode-badge', 'Azure + Web Speech');
Toast.success('Service connected successfully.');
});
}
if (clearBtn) {
clearBtn.addEventListener('click', () => {
if (apiKeyInput) apiKeyInput.value = '';
if (endpointInput) endpointInput.value = '';
AppState.apiKey = '';
AppState.endpoint = '';
AppState.isConnected = false;
UI.clearAllErrors();
UI.updateConnectionStatus('none');
UI.setBadge('stt-mode-badge', 'Web Speech API');
UI.setBadge('tts-mode-badge', 'Web Speech API');
Toast.info('Credentials cleared.');
});
}
}
/* ============================================================
SPEECH TO TEXT (STT)
============================================================ */
function initSTT() {
const recordBtn = $id('record-btn');
const copyTranscriptBtn = $id('copy-transcript-btn');
const clearTranscriptBtn = $id('clear-transcript-btn');
const sttLangSelect = $id('stt-lang-select');
let accumulatedTranscript = '';
// Handle Language Change
if (sttLangSelect) {
sttLangSelect.addEventListener('change', (e) => {
AppState.currentLang = e.target.value;
Toast.info(`STT Language changed to ${e.target.options[e.target.selectedIndex].text}`);
});
}
if (recordBtn) {
recordBtn.addEventListener('click', () => {
if (AppState.isRecording) {
// Stop
SpeechEngine.stopRecognition();
AppState.isRecording = false;
UI.setRecordBtn(false);
UI.setWaveformActive(false);
UI.setRecorderStatus('', 'Ready to record');
if (AppState.transcript) Toast.success('Transcript ready.');
} else {
// Start
// Request permission explicitly
navigator.mediaDevices.getUserMedia({ audio: true })
.then(() => {
AppState.isRecording = true;
UI.setRecordBtn(true);
UI.setWaveformActive(true);
UI.setRecorderStatus('listening', 'Listening…');
SpeechEngine.startRecognition(
AppState.currentLang,
// onResult
({ final, interim }) => {
if (final) {
accumulatedTranscript += (accumulatedTranscript ? ' ' : '') + final;
AppState.transcript = accumulatedTranscript;
UI.setTranscript(accumulatedTranscript);
UI.setRecorderStatus('listening', 'Listening…');
} else {
UI.setTranscript(accumulatedTranscript + (accumulatedTranscript ? ' ' : '') + interim, true);
UI.setRecorderStatus('processing', 'Processing…');
}
},
// onError
(msg) => {
AppState.isRecording = false;
UI.setRecordBtn(false);
UI.setWaveformActive(false);
UI.setRecorderStatus('', 'Ready to record');
Toast.error(msg);
},
// onEnd
() => {
AppState.isRecording = false;
UI.setRecordBtn(false);
UI.setWaveformActive(false);
UI.setRecorderStatus('', 'Ready to record');
}
);
})
.catch(err => {
Toast.error('Microphone access denied. Please enable permissions in your browser.');
console.error(err);
});
}
});
}
if (copyTranscriptBtn) {
copyTranscriptBtn.addEventListener('click', () => {
if (!AppState.transcript) return;
navigator.clipboard.writeText(AppState.transcript)
.then(() => Toast.success('Transcript copied to clipboard.'))
.catch(() => Toast.error('Could not copy. Please copy manually.'));
});
}
if (clearTranscriptBtn) {
clearTranscriptBtn.addEventListener('click', () => {
if (AppState.isRecording) {
SpeechEngine.stopRecognition();
AppState.isRecording = false;
UI.setRecordBtn(false);
UI.setWaveformActive(false);
}
accumulatedTranscript = '';
AppState.transcript = '';
UI.setTranscript('');
UI.setRecorderStatus('', 'Ready to record');
});
}
}
/* ============================================================
TEXT TO SPEECH (TTS)
============================================================ */
function initTTS() {
const ttsInput = $id('tts-input');
const speakBtn = $id('speak-btn');
const stopBtn = $id('stop-btn');
const charCount = $id('char-count');
const downloadBtn = $id('download-audio-btn');
const voiceSelect = $id('voice-select');
const rateSelect = $id('rate-select');
const pitchSelect = $id('pitch-select');
if (ttsInput) {
ttsInput.addEventListener('input', () => {
const len = ttsInput.value.length;
if (charCount) charCount.textContent = `${len} / 5000`;
if (speakBtn) speakBtn.disabled = len === 0;
if (charCount) charCount.style.color = len > 4500 ? 'var(--error)' : '';
});
}
if (speakBtn) {
speakBtn.addEventListener('click', () => {
const text = ttsInput.value.trim();
if (!text) { Toast.warning('Please enter some text first.'); return; }
const voiceIdx = voiceSelect.value;
// Ensure voice exists for the selected index
const voice = (voiceIdx !== '' && AppState.voices[parseInt(voiceIdx)]) ? AppState.voices[parseInt(voiceIdx)] : null;
const rate = rateSelect ? rateSelect.value : 1;
const pitch = pitchSelect ? pitchSelect.value : 1;
AppState.isSpeaking = true;
UI.updateSpeakBtn(true);
UI.showAudioPlayer(true);
UI.setTTSProgress(10, 'Synthesizing audio…');
let progress = 10;
const progressInterval = setInterval(() => {
progress = Math.min(progress + Math.random() * 15, 85);
UI.setTTSProgress(progress, 'Synthesizing audio…');
}, 300);
SpeechEngine.speak(
text, voice, rate, pitch,
// onStart
() => {
clearInterval(progressInterval);
UI.setTTSProgress(100, 'Playing…');
Toast.success('Audio synthesis complete.');
if (downloadBtn) downloadBtn.disabled = true;
},
// onEnd
() => {
clearInterval(progressInterval);
AppState.isSpeaking = false;
UI.updateSpeakBtn(false);
UI.setTTSProgress(100, 'Playback complete.');
const badge = $id('audio-playing-badge');
if (badge) badge.innerHTML = `<span style="color:var(--text-muted);font-weight:500;font-size:.75rem">Playback complete</span>`;
},
// onError
(msg) => {
clearInterval(progressInterval);
AppState.isSpeaking = false;
UI.updateSpeakBtn(false);
UI.showAudioPlayer(false);
Toast.error(msg);
}
);
});
}
if (stopBtn) {
stopBtn.addEventListener('click', () => {
SpeechEngine.stop();
AppState.isSpeaking = false;
UI.updateSpeakBtn(false);
UI.setTTSProgress(0, '');
UI.showAudioPlayer(false);
Toast.info('Playback stopped.');
});
}
if (downloadBtn) {
downloadBtn.addEventListener('click', () => {
Toast.info('Download is available only with Azure TTS. Web Speech API does not expose audio data.');
});
}
}
/* ============================================================
BOOT
============================================================ */
async function init() {
Toast.init();
initTheme();
initTabs();
initCredentials();
initSTT();
initTTS();
// Load voices and populate language dropdowns
await loadVoicesAndLanguages();
const themeToggle = $id('theme-toggle');
if (themeToggle) themeToggle.addEventListener('click', toggleTheme);
const hasSpeechRecog = !!(window.SpeechRecognition || window.webkitSpeechRecognition);
const hasSpeechSynth = !!window.speechSynthesis;
if (!hasSpeechRecog) {
Toast.warning('Speech recognition not supported. Use Chrome or Edge for STT.');
const recordBtn = $id('record-btn');
if (recordBtn) recordBtn.disabled = true;
}
if (!hasSpeechSynth) {
Toast.warning('Speech synthesis not supported in this browser.');
const speakBtn = $id('speak-btn');
if (speakBtn) speakBtn.disabled = true;
}
}
document.addEventListener('DOMContentLoaded', init);