-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathspeech.php
More file actions
439 lines (393 loc) · 13.3 KB
/
Copy pathspeech.php
File metadata and controls
439 lines (393 loc) · 13.3 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!DOCTYPE HTML>
<html>
<head>
<title>
CHATSCRIPT SERVER
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
#topPanel {
min-width: 200px;
min-height: 200px;
width: 80vw;
height: 50vh;
overflow: auto;
margin: 0px auto;
}
#responseHolder {
min-width: 400px;
min-height: 250px;
width: 90%;
height: 250px;
overflow: auto;
margin: 10px auto;
background-color: lightgrey;
border-radius: 12px;
padding: 10px;
font-family: Arial;
font-size: 10pt;
font-style: normal;
font-weight: normal;
}
#avatarHolder {
height: 200px;
width: 200px;
margin: 0px auto;
background-color: darkgrey;
border-radius: 12px;
padding: 0px;
float: right;
font-family: Arial;
font-size: 12pt;
font-style: normal;
font-weight: normal;
}
#formpanel {
min-width: 400px;
min-height: 200px;
width: 80%;
overflow: auto;
margin: 0px auto;
font-family: Arial;
font-size: 10pt;
font-style: normal;
font-weight: normal;
}
#form_table {
width: 100%;
overflow: auto;
margin: 0px auto;
}
#txtUser {
padding: 2px;
}
#txtMessage {
width: 95%;
right-margin: 5px;
padding: 2px;
}
#speechcontainer {
margin: 10px auto;
border-style: solid;
border-width: 1px;
border-radius: 12px;
border-color: darkgrey;
width: 110%;
height: 80px;
padding: 10px;
font: Arial;
font-size: 10pt;
font-style: normal;
font-weight: normal;
color: black;
}
#button_panel {
width: 80%;
}
#btnMicrophone {
margin: 10px;
float: left;
}
#results {
font-family: cursive,Arial;
font-size: 14pt;
font-style: italic;
font-weight: normal;
color: darkgrey;
margin: 10px;
}
</style>
<script type="text/javascript">
var cbAutoSend = 'checked';
var cbAutoSpeech = 'checked';
</script>
</head>
<body>
<div id="responseHolder"></div>
<div id="avatarHolder">
<img src="rose.gif" alt="rose image" height="200px" width="200px" style="border-radius: 12px;" >
</div>
<div id="formpanel">
<div>
<input type="checkbox" name="autosend" value="checked" checked onclick="if (this.checked) {cbAutoSend = this.value} else { cbAutoSend = '' }"/> Continuous Speech Input
<input type="checkbox" name="speech" value="checked" checked onclick="if (this.checked) {cbAutoSpeech = this.value} else { cbAutoSpeech = '' }"/> Speech Output
</div>
<form id="frmChat" action="#" >
<p>
Hi. My name is Rose. I care about security, so while I'm happy to chat with you, it will have to be through this untrackable interface.
</p>
<table id="form_table" >
<tr>
<td>Name:</td>
<td>
<input type="text" id="txtUser" name="user" size="20" value="User" />
<input type="hidden" name="send" />
</td>
</tr>
<tr>
<td>Message:</td>
<td><input type="text" name="message" id="txtMessage" size="80" /></td>
<td colspan="2"><input type="submit" name="send" value="Send Input" /></td>
</tr>
<!-----Added this row for Speech Recognition-------------------------------------------------------->
<tr>
<td colspan="2">
<div id="speechcontainer" >
<div id="info">Or click on the microphone icon and begin speaking.</div>
<div id="button_panel">
<button id="btnMicrophone" type="button" value="microphone" onclick="microphoneClick()">
<img id="start_img" src="mic.gif" alt="Start"></button>
</div>
<div id="results">
<span id="final_span" class="final"></span>
<span id="interim_span" class="interim"></span><p>
</div>
</div>
</td>
</tr>
<!-------------------------------------------------------------------------------------------------->
</table>
</form>
</div>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
var botName = 'Rose'; // change this to your bot name
// declare timer variables
var alarm = null;
var callback = null;
var loopback = null;
$(function(){
$('#frmChat').submit(function(e){
// this function overrides the form's submit() method, allowing us to use AJAX calls to communicate with the ChatScript server
e.preventDefault(); // Prevent the default submit() method
var name = $('#txtUser').val();
if (name == '') {
alert('Please provide your name.');
document.getElementById('txtUser').focus();
}
var chatLog = $('#responseHolder').html();
var youSaid = '<strong>' + name + ':</strong> ' + $('#txtMessage').val() + "<br>\n";
update(youSaid);
var data = $(this).serialize();
sendMessage(data);
$('#txtMessage').val('').focus();
});
// any user typing cancels loopback or callback for this round
$('#txtMessage').keypress(function(){
window.clearInterval(loopback);
window.clearTimeout(callback);
});
});
function sendMessage(data){ //Sends inputs to the ChatScript server, and returns the response- data - a JSON string of input information
$.ajax({
url: 'ui.php',
dataType: 'text',
data: data,
type: 'post',
success: function(response){
processResponse(parseCommands(response));
},
error: function(xhr, status, error){
alert('oops? Status = ' + status + ', error message = ' + error + "\nResponse = " + xhr.responseText);
}
});
}
function parseCommands(response){ // Response is data from CS server. This processes OOB commands sent from the CS server returning the remaining response w/o oob commands
var len = response.length;
var i = -1;
while (++i < len )
{
if (response.charAt(i) == ' ' || response.charAt(i) == '\t') continue; // starting whitespace
if (response.charAt(i) == '[') break; // we have an oob starter
return response; // there is no oob data
}
if ( i == len) return response; // no starter found
var user = $('#txtUser').val();
// walk string to find oob data and when ended return rest of string
var start = 0;
while (++i < len )
{
if (response.charAt(i) == ' ' || response.charAt(i) == ']') // separation
{
if (start != 0) // new oob chunk
{
var blob = response.slice(start,i);
start = 0;
var commandArr = blob.split('=');
if (commandArr.length == 1) continue; // failed to split left=right
var command = commandArr[0]; // left side is command
var interval = (commandArr.length > 1) ? commandArr[1].trim() : -1; // right side is millisecond count
if (interval == 0) /* abort timeout item */
{
switch (command){
case 'alarm':
window.clearTimeout(alarm);
alarm = null;
break;
case 'callback':
window.clearTimeout(callback);
callback = null;
break;
case 'loopback':
window.clearInterval(loopback);
loopback = null;
break;
}
}
else if (interval == -1) interval = -1; // do nothing
else
{
var timeoutmsg = {user: user, send: true, message: '[' + command + ' ]'}; // send naked command if timer goes off
switch (command) {
case 'alarm':
alarm = setTimeout(function(){sendMessage(timeoutmsg );}, interval);
break;
case 'callback':
callback = setTimeout(function(){sendMessage(timeoutmsg );}, interval);
break;
case 'loopback':
loopback = setInterval(function(){sendMessage(timeoutmsg );}, interval);
break;
}
}
} // end new oob chunk
if (response.charAt(i) == ']') return response.slice(i + 2); // return rest of string, skipping over space after ]
} // end if
else if (start == 0) start = i; // begin new text blob
} // end while
return response; // should never get here
}
function update(text){ // text is HTML code to append to the 'chat log' div. This appends the input text to the response div
var chatLog = $('#responseHolder').html();
$('#responseHolder').html(chatLog + text);
var rhd = $('#responseHolder');
var h = rhd.get(0).scrollHeight;
rhd.scrollTop(h);
}
// TTS code taken and modified from here:
// http://stephenwalther.com/archive/2015/01/05/using-html5-speech-recognition-and-text-to-speech
//---------------------------------------------------------------------------------------------------
// say a message
function speak(text, callback) {
var u = new SpeechSynthesisUtterance();
u.text = text;
u.lang = 'en-US';
//u.lang = 'en-GB';
u.voice = 4; // 3 = american female 0 worse female 1 = not best femal
u.rate = 1.0; // .85
u.pitch = .9;
u.volume = 1.0; // .5
u.onend = function () {
if (callback) {
callback();
}
};
u.onerror = function (e) {
if (callback) {
callback(e);
}
};
speechSynthesis.speak(u);
}
//-----End of TTS Code Block-----------------------------------------------------------------------------
function processResponse(response) { // given the final CS text, converts the parsed response from the CS server into HTML code for adding to the response holder div
var botSaid = '<strong>' + botName + ':</strong> ' + response + "<br>\n";
update(botSaid);
if (cbAutoSpeech == 'checked') { speak(response); }
}
// Continuous Speech recognition code taken and modified from here:
// https://github.com/GoogleChrome/webplatform-samples/tree/master/webspeechdemo
//----------------------------------------------------------------------------------------------------
var final_transcript = '';
var recognizing = false;
var ignore_onend;
var start_timestamp;
if (!('webkitSpeechRecognition' in window)) {
info.innerHTML = "Speech only works if you use Chrome or Safari. Works best with headphones and mic for separation of signals";
} else {
btnMicrophone.style.display = 'inline-block';
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList
var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent
var recognition = new SpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.lang = 'en-US';
recognition.onstart = function() {
recognizing = true;
info.innerHTML = " Speak now.";
start_img.src = 'mic-animate.gif';
};
recognition.onerror = function(event) {
if (event.error == 'no-speech') {
start_img.src = 'mic.gif';
info.innerHTML = "You did not say anything.";
ignore_onend = true;
}
if (event.error == 'audio-capture') {
start_img.src = 'mic.gif';
info.innerHTML = "You need a microphone.";
ignore_onend = true;
}
if (event.error == 'not-allowed') {
// if (event.timeStamp - start_timestamp < 100) {
//Added more detailed message to unblock access to microphone.
info.innerHTML = " I am blocked. In Chrome go to settings. Click Advanced Settings at the bottom. Under Privacy click the Content Settings button. Under Media click Manage Exceptions Button. Remove this site from the blocked sites list. ";
// } else {
// info.innerHTML = "You did not click the allow button."
// }
ignore_onend = true;
}
};
recognition.onend = function() {
recognizing = false;
if (ignore_onend) {
return;
}
start_img.src = 'mic.gif';
if (!final_transcript) {
info.innerHTML = "Click on the microphone icon and begin speaking.";
return;
}
info.innerHTML = "";
};
recognition.onresult = function(event) {
var interim_transcript = '';
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
//----Added this section to integrate with Chatscript submit functionality-----
txtMessage.value = final_transcript;
final_transcript ='';
final_span.innerHTML = '';
interim_span.innerHTML = '';
if (cbAutoSend == 'checked') { $('#frmChat').submit(); }
//-----------------------------------------------------------------------------
} else {
interim_transcript += event.results[i][0].transcript;
}
}
final_span.innerHTML = final_transcript;
interim_span.innerHTML = interim_transcript;
};
}
function microphoneClick(event) {
if (recognizing) {
recognition.stop();
return;
}
final_transcript = '';
txtMessage.value = '';
recognition.start();
ignore_onend = false;
final_span.innerHTML = '';
interim_span.innerHTML = '';
start_img.src = 'mic-slash.gif';
info.innerHTML = " Click the Allow button above to enable your microphone.";
start_timestamp = event.timeStamp;
}
//End of Continuous Speech Recognition Block
//----------------------------------------------------------------------------------------------------
</script>
</body>
</html>