-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.js
211 lines (170 loc) · 5.44 KB
/
speech.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
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
let recognized = false;
let continousListenOn = false;
function httpGetAsyncJsonFromBahn(theUrl, callback) {
const xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
const jsonData = JSON.parse(xmlHttp.responseText);
callback(jsonData);
}
};
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.setRequestHeader('Authorization', 'Bearer 676d050c501071f9e11a0c177da4d7da');
xmlHttp.setRequestHeader('Accept', 'application/json');
xmlHttp.send();
}
function httpGetAsyncJson(theUrl, callback) {
const xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
const jsonData = JSON.parse(xmlHttp.responseText);
callback(jsonData);
}
};
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send();
}
function httpGetAsyncText(theUrl, callback) {
const xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
callback(xmlHttp.responseText);
}
};
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send();
}
class Command {
constructor(name, doFn, hitFn) {
this.name = name;
this.hitFn = hitFn;
this.doFn = doFn;
}
hit(text) {
if (typeof this.hitFn === 'function') {
return this.hitFn(text);
}
return text === this.name;
}
do() {
this.doFn();
}
}
window.commandList = [];
const output = document.getElementById('output');
const ask = document.getElementById('ask');
const start = document.getElementById('start');
const input = document.getElementById('input');
const read = document.getElementById('read');
const messages = [];
console.log('test');
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList
var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
const say = (text, lang = 'de') => {
console.debug(`say: ${text}`);
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = lang; //Sprache auf Deutsch festlegen
speechSynthesis.speak(utterance);
};
//say('Hallo Welt');
const sayRoomTemperature = (roomName) => {
// chrome CORS plugin needed
httpGetAsyncJson(`http://openhab-test.synyx.coffee:8080/rest/items/${roomName}_Temperature_Current`, (event) => {
console.log(event.state);
const temperature = event.state;
const room = event.name.split('_')[0];
say(`Es sind ${temperature} Grad`);
});
};
const recognize = () => {
console.log('start recognition');
const recognition = new SpeechRecognition();
recognition.lang = 'de'; //Sprache auf Deutsch festlegen
recognition.onresult = function (event) {
if (event.results.length > 0) {
const msg = event.results[0][0].transcript;
messages.push(msg);
output.innerText = JSON.stringify(messages);
console.log('finished: ', msg);
console.log(msg); //erstes Ergebnis ausgeben
const cmdRecognized = window.commandList.some(cmd => {
if (cmd.hit(msg)) {
cmd.do();
return true;
}
return false;
});
if (!cmdRecognized) {
const cmd = window.commandList.filter(cmd => cmd.name === 'UPS');
if (cmd) {
cmd[0].do();
}
}
}
};
recognition.onaudiostart = function(event) {
console.log('onaudiostart ', event);
};
recognition.onaudioend = function(event) {
console.log('onaudioend ', event);
setTimeout(() => {
if (continousListenOn) {
recognized = false;
continousListen();
}
}, 1000);
};
recognition.start();
};
ask.addEventListener('click', () => {
continousListenOn = false;
recognize();
});
const continousListen = () => {
const grammar = '#JSGF V1.0; grammar keyword; public <keyword> = hallo | test ;';
const recognition = new SpeechRecognition();
const speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.lang = 'de'; //Sprache auf Deutsch festlegen
recognition.interimResults = false;
recognition.continuous = false;
recognition.onaudiostart = () => console.log('onaudiostart');
recognition.onaudioend = () => console.log('onaudio');
recognition.onspeechstart = () => console.log('onspeechstart');
recognition.onspeechend = () => console.log('onspeechend');
recognition.onsoundstart = () => console.log('onsoundstart');
recognition.onsoundend = () => console.log('onsoundend');
recognition.onstart = () => console.log('onstart');
recognition.onend = () => {
console.log('onend');
if (!recognized) {
setTimeout(continousListen, 1);
}
};
recognition.onerror = (e) => console.log('onerror ', e);
recognition.onresult = (e) => {
const result = e.results[0][0];
console.log('onresult ', result);
if (result.transcript === 'Hallo Computer') {
recognized = true;
say('Ja?');
setTimeout(recognize, 1000);
}
};
recognition.start();
};
recognized = false;
start.addEventListener('click', () => {
continousListenOn = true;
recognized = false;
continousListen();
});
read.addEventListener('click', () => {
const texts = input.value.split('. ');
console.log(texts);
texts.forEach(text => {
say(`${text}.`);
});
});