-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (58 loc) · 2.09 KB
/
index.js
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
const startBtn = document.getElementById('start-btn');
const stopBtn = document.getElementById('stop-btn');
const output = document.getElementById('output');
// Initialize SpeechRecognition API
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
let novaActive = false;
// When speech recognition starts
recognition.onstart = function() {
output.textContent = "Nova is listening...";
};
// When speech recognition gets a result
recognition.onresult = function(event) {
const speechResult = event.results[0][0].transcript.toLowerCase();
output.textContent = "You said: " + speechResult;
if (novaActive) {
processCommand(speechResult);
}
// Check for activation or deactivation of Nova
if (speechResult.includes("activate nova")) {
novaActive = true;
output.textContent = "Nova is now active!";
} else if (speechResult.includes("stop nova")) {
novaActive = false;
output.textContent = "Nova has been stopped.";
}
};
// Start recognition when the button is clicked
startBtn.addEventListener('click', () => {
recognition.start();
});
// Stop recognition when Stop Nova is clicked
stopBtn.addEventListener('click', () => {
recognition.stop();
output.textContent = "Nova is no longer listening.";
});
// Function to process voice commands
function processCommand(command) {
if (command.includes("go to section 1")) {
window.location.href = "#section1";
} else if (command.includes("go to section 2")) {
window.location.href = "#section2";
} else if (command.includes("open section 1")) {
document.getElementById('section1').scrollIntoView();
} else if (command.includes("open section 2")) {
document.getElementById('section2').scrollIntoView();
}
}
function speakResponse(responseText) {
const speech = new SpeechSynthesisUtterance(responseText);
speech.lang = 'en-US';
window.speechSynthesis.speak(speech);
}
// Example: speak when Nova activates
if (speechResult.includes("activate nova")) {
novaActive = true;
speakResponse("Nova is now active!");
}