-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
165 lines (140 loc) · 6.14 KB
/
main.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
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
//------------------------------------------ SPEECH RECOGNITION ---------------------------------------------------------------
const SpeechRecognition = SpeechRecognition || webkitSpeechRecognition
const SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList
const SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent
// Language mode, 0 is Polish, 1 is English (US)
let lang = 0;
// Commands program recognizes and answers with matching indexes
const commands = [ "date", "name", "creator", "time", "help", "joke"];
const answerArray = [dateInfo, nameInfo, creatorInfo, timeInfo, helpInfo, jokesInfo];
const grammar = "#JSGF v1.0; grammar commands; public <command> = " + commands.join(" | ") + " ;";
// Initialazing recognision and grammar list
const recognition = new SpeechRecognition();
const speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
// Setting up recognition options, default language (0) is Polish
recognition.grammars = speechRecognitionList;
recognition.lang = 'en-US';
// Showing spoken words on the fly
recognition.interimResults = true;
recognition.maxAlternatives = 1;
// DOM elements
const answerField = document.getElementById("answer");
const record = document.getElementById("start");
// Array with the words from recognized sentence
let sentence = [];
// When record button clicked starting new recognition
record.onclick = function() {
recognition.start();
console.log('Ready to receive a command.');
}
// Once we get the result it prints out the result word/sentence
recognition.onresult = function(event) {
const last = event.results.length - 1;
answerField.innerHTML = event.results[last][0].transcript;
sentence = (event.results[last][0].transcript.split(" "));
console.log(sentence);
}
// Once we end speaking, the recognition is stopped
recognition.onspeechend = function() {
recognition.stop();
// Starting answer() function, line 117 (of main.js)
answer();
}
// ----------------------------------- CHANGE LANGUAGE (PL/EN) ----------------------------------------------------
const langBtn = document.getElementById("langMode");
langBtn.onclick = function() {
if (recognition.lang === 'PL') {
recognition.lang = 'en-US';
langBtn.innerHTML = "en-US";
console.log("ENG");
} else if (recognition.lang === 'en-US') {
recognition.lang = 'PL';
langBtn.innerHTML = "PL";
console.log("PL");
}
}
// ----------------------------------- SPEECH SYNTHESIS ----------------------------------------------------
// setting up speech synthesis and picking DOM elements
const synth = window.speechSynthesis;
const voiceSelect = document.querySelector('select');
// Text we want to read, basically program answer, determined later on
let textToRead = "Speak English. Mate.";
// ----------------------------------------------------------- COMMAND CHECKING FUNCTION ------------------------------------------------------------
function checkForCommands() {
// Before execution textToRead is set to default
textToRead = "Speak English. Mate.";
for (let i = 0; i < sentence.length; i++) {
// Checking for human phrases
for (let greeting of humanPhrases.phrasesGreetings.length) {
if (sentence.includes(greeting)) {
textToRead = humanPhrases.answersGreetings[Math.round(Math.random()*4)];
}
}
// New human phrases loop example, you can use this as your template i, j, k, l, m, n... etc.
for (let l = 0; l < humanPhrases.phrasesHowAreYou.length; l++) {
if (sentence.join(" ").includes(humanPhrases.phrasesHowAreYou[l])) {
textToRead = humanPhrases.answersHowAreYou[l];
}
}
for (let m = 0; m < humanPhrases.phrasesComplains.length; m++) {
if (sentence.join(" ").includes(humanPhrases.phrasesComplains[m])) {
textToRead = humanPhrases.answersComplains[m];
}
}
for (let n = 0; n < humanPhrases.phrasesCommonQuestions.length; n++) {
if (sentence.join(" ").includes(humanPhrases.phrasesCommonQuestions[n])) {
textToRead = humanPhrases.answersCommonQuestions[n];
}
}
// Checking for commands
for (let j = 0; j < commands.length; j++) {
if (sentence[i] == commands[j]) {
textToRead = answerArray[j];
console.log(textToRead);
console.log(answerArray);
}
}
}
}
// available voices to use
let voices = [];
// creating a list of available voices
function populateVoiceList() {
voices = synth.getVoices();
for(let voice of voices) {
const option = document.createElement("option");
option.textContent = voice.name + " (" + voice.lang + ")";
if (voice.default) {
option.textContent += " - DEFAULT";
}
option.setAttribute("data-lang", voice.lang);
option.setAttribute("data-name", voice.name);
voiceSelect.appendChild(option);
}
}
populateVoiceList();
// Making it work in some browsers where it won't work other way (Firefox if I remember correctly)
if (speechSynthesis.onvoiceschanged !== undefined) {
speechSynthesis.onvoiceschanged = populateVoiceList;
}
// Speaking!
function answer() {
checkForCommands();
// Program answer, exactly the parameter in the SpeechSynthesisUtterance brackets
const utterThis = new SpeechSynthesisUtterance(textToRead);
// Selecting a voice
const selectedVoice = voiceSelect.selectedOptions[0].getAttribute("data-name");
for (let voice of voices) {
if (voice.name === selectedVoice) {
utterThis.voice = voice;
}
}
synth.speak(utterThis);
}
// This solves the problem of too long sentences. Now it's not breaking the program.
function resumeInfinity() {
window.speechSynthesis.resume();
timeoutResumeInfinity = setTimeout(resumeInfinity, 1000);
}
resumeInfinity();