-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (109 loc) · 3.72 KB
/
Copy pathscript.js
File metadata and controls
129 lines (109 loc) · 3.72 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
const resultDiv = document.getElementById("result");
const statusDiv = document.getElementById("status");
const langSelect = document.getElementById("langSelect");
const startButton = document.getElementById("startButton");
const stopButton = document.getElementById("stopButton");
const clearButton = document.getElementById("clearButton");
let recognition;
let isListening = false;
// Initial status
document.addEventListener("DOMContentLoaded", () => {
statusDiv.textContent = "Ready.";
clearButton.disabled = true;
resultDiv.contentEditable = "false"
});
// Initialize the SpeechRecognition API
function initializeRecognition() {
const SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition;
if (!SpeechRecognition) {
statusDiv.textContent =
"Web Speech API is not supported in this browser.";
startButton.disabled = true;
return null;
}
const recog = new SpeechRecognition();
recog.lang = langSelect.value;
const langCode = langSelect.value.slice(0, 2);
// Set text direction and font based on language
if (langCode === "ar") {
resultDiv.dir = "rtl";
resultDiv.style.fontFamily = "'Cairo', 'Amiri', sans-serif";
resultDiv.style.textAlign = "right";
} else {
resultDiv.dir = "ltr";
resultDiv.style.fontFamily = "'Roboto', sans-serif";
resultDiv.style.textAlign = "left";
}
recog.maxAlternatives = 1;
recog.interimResults = false;
recog.continuous = true;
return recog;
}
function startRecognition() {
if (isListening) return;
recognition = initializeRecognition();
if (!recognition) return;
isListening = true;
resultDiv.contentEditable = "false";
// Update buttons state and animations
startButton.classList.add("listening");
startButton.querySelector(".button-text").textContent = "Listening...";
statusDiv.textContent = "Speak now – we're listening";
startButton.disabled = true;
stopButton.disabled = false;
clearButton.disabled = true;
recognition.onresult = (event) => {
const transcript =
event.results[event.results.length - 1][0].transcript;
resultDiv.textContent += transcript + " ";
resultDiv.scrollTop = resultDiv.scrollHeight;
};
recognition.onerror = (event) => {
statusDiv.textContent = "Error: " + event.error;
isListening = false;
resetButtonState();
};
recognition.onend = () => {
if (isListening) {
recognition.start();
} else {
statusDiv.textContent = "Ready.";
resetButtonState();
}
};
recognition.start();
}
function stopRecognition() {
if (!isListening) return;
isListening = false;
recognition.stop();
resultDiv.contentEditable = resultDiv.textContent ? "true" : "false";
resetButtonState();
}
function resetButtonState() {
startButton.classList.remove("listening");
startButton.querySelector(".button-text").textContent = "Speak";
startButton.disabled = false;
stopButton.disabled = true;
clearButton.disabled = resultDiv.textContent ? false : true;
}
function clearText() {
resultDiv.textContent = "";
clearButton.disabled = true;
resultDiv.contentEditable = "false";
}
// Event Listeners
langSelect.addEventListener("change", () => {
if (recognition && !isListening) {
initializeRecognition();
} else if (isListening) {
stopRecognition();
setTimeout(startRecognition, 100);
} else {
initializeRecognition();
}
});
startButton.addEventListener("click", startRecognition);
stopButton.addEventListener("click", stopRecognition);
clearButton.addEventListener("click", clearText);