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
|
class VoiceModule {
constructor() {
this.commands = {};
this.terminal = null;
this.recognition = null;
this.listening = false;
this._lastError = '';
}
requirements() {
if (typeof SpeechRecognition !== 'undefined' || typeof webkitSpeechRecognition !== 'undefined') {
return { met: true, missing: [] };
}
return { met: false, missing: ['SpeechRecognition not available in this browser'] };
}
getHelpInfo() {
return { 'voice': 'voice [stop] — Start voice recognition, or type "voice stop" to cancel' };
}
registerCommands() {
this.commands['voice'] = (args) => this.handle(args);
}
handle(args) {
const sub = args[0] && args[0].toLowerCase();
if (sub === 'stop') return this.stopListening();
if (this.listening) return this.terminal.print('Already listening. Say something or type "voice stop"', 'warning');
this.startListening();
}
_updateButton(listening, error) {
const btn = document.getElementById('voice-toggle-btn');
if (!btn) return;
if (listening) {
btn.style.borderColor = 'var(--success-color)';
btn.style.color = 'var(--success-color)';
btn.style.animation = 'pulse 1.5s ease-in-out infinite';
btn.textContent = '🔴 Listening';
} else {
btn.style.borderColor = '';
btn.style.color = '';
btn.style.animation = '';
if (error) {
btn.style.borderColor = 'var(--error-color)';
btn.style.color = 'var(--error-color)';
} else {
btn.style.borderColor = '';
btn.style.color = '';
}
btn.textContent = '🎤 Mic';
}
}
startListening() {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!SpeechRecognition) {
this._updateButton(false, true);
return this.terminal.print('Speech recognition not supported in this browser', 'error');
}
this.recognition = new SpeechRecognition();
this.recognition.lang = 'en-US';
this.recognition.interimResults = false;
this.recognition.maxAlternatives = 1;
this.recognition.continuous = false;
this.listening = true;
this._updateButton(true);
this.terminal.print('🎤 Listening... (say a command, or tap mic button to cancel)', 'info');
this.recognition.onresult = (event) => {
const transcript = event.results[0][0].transcript;
this.terminal.print(`🎤 Recognized: "${transcript}"`, 'success');
const input = this.terminal.inputField;
input.value = transcript;
input.focus();
this.listening = false;
this._updateButton(false);
};
this.recognition.onerror = (event) => {
this.terminal.print(`🎤 Error: ${event.error}`, 'error');
this.listening = false;
this._updateButton(false, true);
};
this.recognition.onend = () => {
if (this.listening) {
this.listening = false;
this._updateButton(false);
this.terminal.print('🎤 Voice input ended', 'info');
}
};
try { this.recognition.start(); }
catch (e) {
this.terminal.print(`🎤 Failed to start: ${e.message}`, 'error');
this.listening = false;
this._updateButton(false, true);
}
}
stopListening() {
if (this.recognition && this.listening) {
this.recognition.stop();
this.listening = false;
this._updateButton(false);
this.terminal.print('🎤 Listening stopped', 'info');
}
}
}
if (typeof window.webx11 !== 'undefined' && window.webx11) {
webx11.moduleManager.registerModule(new VoiceModule(), 'https://webx11.duckdns.org/spaces/newos4/modules/ext/voice.js');
}
|