-
Notifications
You must be signed in to change notification settings - Fork 0
/
webspeech.html
142 lines (129 loc) · 3.65 KB
/
webspeech.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web Speech Gutted</title>
<style>
.transcipt{
border-width: 1px;
border-style: solid;
border-color: black;
}
</style>
</head>
<body>
<h1> Gutting Google Speech API</h1>
<button type="button" id="start-transcript" onclick="startButton(event)">Start</button>
<button type="button" id="end-transcript" onclick="endButton(event)">End</button>
<div class="transcipt">
<span id="final_span"></span>
<span id="interim_span"></span>
</div>
<span id="total_time"></span>
<span id="total_words"></span>
<span id="wpm"></span>
<script>
//Google Speak API: Web speech demo
var final_transcript = '';
var recognizing = false; // is speech recognition on or not
var ignore_onend;
var start_timestamp;
var recognition; // var for webkit
var start_time, end_time;
if (!('webkitSpeechRecognition' in window)) {
console.log("didn't initialize");
}
else{
console.log("Initializing webkit");
recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.lang = "en-US";
recognition.onstart = function(){
console.log("started recognition");
if(!recognizing){
start_time = new Date().getTime();
}
recognizing = true;
};
recognition.onerror = function(event){
console.log("onerror", event);
if(recognizing){
recognition.start();
console.log("restarting recognition after error");
}
if(event.error == 'audio-capture'){
console.log("no microphone");
recognizing = false;
}
if(event.error == 'not-allowed'){
console.log("permission denied");
recognizing = false;
}
};
recognition.onend = function(){
if(recognizing){
recognition.start();
console.log("restarting recognition");
}
else{
end_time = new Date().getTime();
var total_time= (end_time - start_time)/1000;
var total_words = get_word_count(final_transcript);
total_time.innerHTML = "total speaking time is "+ (end_time - start_time)/100 +"seconds";
total_words.innerHTML = "total word count is "+ get_word_count(final_transcript);
wpm.innerHTML = "WPM = "+ total_words/ (total_time/60);
console.log("ended recognition");
}
}
recognition.onresult = function(event){
console.log("speech recognition output: event.results", event.results);
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;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
final_transcript = capitalize(final_transcript);
final_span.innerHTML = linebreak(final_transcript);
interim_span.innerHTML = linebreak(interim_transcript);
//console.log("final_transcript",final_transcript,"interim_transcript",interim_transcript);
}
}
function get_word_count(str) {
if (str.length == 0) {
return 0;
}
else {
return str.match(/\S+/g).length;
}
}
var first_char = /\S/;
function capitalize(s) {
return s.replace(first_char, function(m) { return m.toUpperCase(); });
}
function startButton(event){
if(recognizing){
return;
}
final_transcript = '';
recognition.start();
start_timestamp = event.timeStamp;
}
function endButton(event){
if(recognizing){
recognizing = false;
recognition.stop();
return;
}
}
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>');
}
</script>
</body>
</html>