-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
264 lines (231 loc) · 9.09 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!-- App style -->
<style>
.interim {
color: gray;
}
.final {
color: black;
padding-right: 3px;
}
</style>
<title>Yes Sir Bot</title>
</head>
<body>
<div class='container'>
<h1>Yes Sir Bot</h1>
<button type="button" id='btn-transcribe' class="btn btn-primary btn-lg my-3">Speak</button>
<span id='info'></span>
<blockquote class="blockquote">
<div id="results">
<span class="final" id="final_span"></span>
<span class="interim" id="interim_span"></span>
</div>
</blockquote>
<hr>
<ul>
<li><a href='./stt'>Speech-to-Text</a></li>
<li><a href='./tts'>Text-to-Speech</a></li>
<ul>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script>
var synth = window.speechSynthesis;
var final_transcript = '';
var recognizing = false;
var ignore_onend;
var start_timestamp;
var lastDebounceTranscript;
// utility tools
var two_line = /\n\n/g;
var one_line = /\n/g;
function linebreak(s) {
return s.replace(two_line, '<p></p>').replace(one_line, '<br>');
}
var first_char = /\S/;
function capitalize(s) {
return s.replace(first_char, function(m) { return m.toUpperCase(); });
}
function showInfo(msg){
// $('#info').html(msg);
}
function upgrade(){
alert('Browser is not support');
}
function startButton(event) {
if (recognizing) {
recognition.stop();
return;
}
final_transcript = '';
recognition.lang = 'th-TH';
recognition.start();
ignore_onend = false;
final_span.innerHTML = '';
interim_span.innerHTML = '';
showInfo('info_allow');
start_timestamp = event.timeStamp;
}
function speak(msg, options){
var utterThis = new SpeechSynthesisUtterance(msg);
var options = options || {};
var lang = options.lang || 'th-TH';
var pitch = options.pitch || 1;
var rate = options.rate || 1;
utterThis.lang = lang;
utterThis.pitch = pitch;
utterThis.rate = rate;
synth.speak(utterThis);
}
function agree_bot(msg){
console.log(msg);
if(!msg){
return;
}
if(msg.indexOf("สวัสดี") > -1){
speak('สวัสดีครับ');
}
else if(msg.indexOf("สิ่งนั้น") > -1){
speak('สิ่งนั้นคือสิ่งไหนครับ ?');
}
else if(msg.toLowerCase().indexOf("the toys") > -1){
speak('เพียงแค่ฝนตกลงที่หน้าต่าง ในบางครา');
speak('เพียงแค่ฝนตกลงในวันหยุด ในตอนเช้า');
speak('มันก็อาจจะมี');
speak('นิดเดียว', { pitch: 5, rate: 0.1 });
speak('ที่คิดถึงเธอ');
speak('มันก็อาจจะเป็น');
speak('ครั้งเดียว', { pitch: 5, rate: 0.1 });
speak('ที่บอกรักเธอ');
speak('เพียงแค่');
speak('ฉันเองก็ไม่รู้ว่า');
speak('ในทุกๆวันที่มีแต่เธอยังคงละเมอและเพ้อทุกครั้งที่มองขึ้นไป', { pitch: 2, rate: 5 });
speak('บนท้องฟ้า หยดน้ำค้าง ตกกระทบ ในตาฉัน');
speak('แล้วในวันนี้พรุ่งนี้จะเป็นยังไงจะเดินจะกินจะนอนจนวันนึงให้วันเวลาเดินไปเท่าไร', { pitch: 2, rate: 5 });
speak('แต่สุดท้าย แดดยามเช้า ก็ไม่สดใส');
}
else {
var rr = [
'เห็นด้วยครับ',
'คิดอย่างนั้นเหมือนกันคับ',
'ประมาณนั้นคับ',
'ใช่คับ',
'ครับ',
'เราใจตรงกันครับ',
'ถูกต้องที่สุด',
'คนส่วนใหญ่น่าจะคิดเหมือนกันครับ',
'ใช่เลยครับ',
`เห็นด้วย ${Math.floor(Math.random() * 51) + 50}% ครับ`,
];
var reply = rr[ Math.floor(Math.random() * rr.length) ];
speak(reply);
}
}
// initialize recognition
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition
var recognition = new SpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function() {
recognizing = true;
showInfo('info_speak_now');
};
recognition.onerror = function(event) {
if (event.error == 'no-speech') {
showInfo('info_no_speech');
ignore_onend = true;
}
if (event.error == 'audio-capture') {
showInfo('info_no_microphone');
ignore_onend = true;
}
if (event.error == 'not-allowed') {
if (event.timeStamp - start_timestamp < 100) {
showInfo('info_blocked');
} else {
showInfo('info_denied');
}
ignore_onend = true;
}
};
recognition.onend = function() {
recognizing = false;
if (ignore_onend) {
return;
}
if (!final_transcript) {
showInfo('info_start');
return;
}
showInfo('');
// speak agree
var msg = $('#final_span').html();
agree_bot(msg);
};
recognition.onresult = function(event) {
var interim_transcript = '';
if (typeof(event.results) == 'undefined') {
recognition.onend = null;
recognition.stop();
upgrade();
return;
}
for (var i = event.resultIndex; i < event.results.length; ++i) {
var transcript = event.results[i][0].transcript;
var confidence = event.results[i][0].confidence;
var isFinal = event.results[i].isFinal && (confidence > 0);
if (isFinal) {
// check duplicate result on android
if(transcript+confidence == lastDebounceTranscript) { return; }
lastDebounceTranscript = transcript+confidence;
final_transcript += transcript;
} else {
interim_transcript += transcript;
}
}
final_transcript = capitalize(final_transcript);
final_span.innerHTML = linebreak(final_transcript);
interim_span.innerHTML = linebreak(interim_transcript);
// stop recording when final word found
if (final_transcript && recognizing){
$('#btn-transcribe').click();
}
};
// handle transcribe button
$('#btn-transcribe').click((evt) => {
let thiz = evt.target;
let ready_text = 'Speak';
let working_text = 'Working...';
let working = $(thiz).html() == working_text;
if(!working){ // press to start
// ui
$(thiz).html(working_text);
$(thiz).removeClass('btn-primary');
$(thiz).addClass('btn-danger');
// start
startButton(evt);
}
else { // press to stop
// ui
$(thiz).html(ready_text);
$(thiz).removeClass('btn-danger');
$(thiz).addClass('btn-primary');
// stop
recognizing = false;
recognition.stop();
}
});
</script>
</body>
</html>