-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-speech.html
128 lines (110 loc) · 4.01 KB
/
web-speech.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Web Speech API</title>
<style>
body
{
max-width: 500px;
margin: 2em auto;
font-size: 20px;
}
h1
{
text-align: center;
}
.buttons-wrapper
{
text-align: center;
}
.hidden
{
display: none;
}
#transcription,
#log
{
display: block;
width: 100%;
height: 5em;
overflow-y: scroll;
border: 1px solid #333333;
line-height: 1.3em;
}
.button-demo
{
padding: 0.5em;
display: inline-block;
margin: 1em auto;
}
</style>
</head>
<body>
<h1>Web Speech API</h1>
<h2>Transcription</h2>
<textarea id="transcription" readonly="readonly"></textarea>
<span>Results:</span>
<label><input type="radio" name="recognition-type" value="final" checked="checked" /> Final only</label>
<label><input type="radio" name="recognition-type" value="interim" /> Interim</label>
<h3>Log</h3>
<div id="log"></div>
<div class="buttons-wrapper">
<button id="button-play-ws" class="button-demo">Play demo</button>
<button id="button-stop-ws" class="button-demo">Stop demo</button>
<button id="clear-all" class="button-demo">Clear all</button>
</div>
<span id="ws-unsupported" class="hidden">API not supported</span>
<script>
// Test browser support
window.SpeechRecognition = window.SpeechRecognition ||
window.webkitSpeechRecognition ||
null;
if (window.SpeechRecognition === null) {
document.getElementById('ws-unsupported').classList.remove('hidden');
document.getElementById('button-play-ws').setAttribute('disabled', 'disabled');
document.getElementById('button-stop-ws').setAttribute('disabled', 'disabled');
} else {
var recognizer = new window.SpeechRecognition();
var transcription = document.getElementById('transcription');
var log = document.getElementById('log');
// Recogniser doesn't stop listening even if the user pauses
recognizer.continuous = true;
// Start recognising
recognizer.onresult = function(event) {
transcription.textContent = '';
for (var i = event.resultIndex; i < event.results.length; i++) {
if (event.results[i].isFinal) {
transcription.textContent = event.results[i][0].transcript + ' (Confidence: ' + event.results[i][0].confidence + ')';
} else {
transcription.textContent += event.results[i][0].transcript;
}
}
};
// Listen for errors
recognizer.onerror = function(event) {
log.innerHTML = 'Recognition error: ' + event.message + '<br />' + log.innerHTML;
};
document.getElementById('button-play-ws').addEventListener('click', function() {
// Set if we need interim results
recognizer.interimResults = document.querySelector('input[name="recognition-type"][value="interim"]').checked;
try {
recognizer.start();
log.innerHTML = 'Recognition started' + '<br />' + log.innerHTML;
} catch(ex) {
log.innerHTML = 'Recognition error: ' + ex.message + '<br />' + log.innerHTML;
}
});
document.getElementById('button-stop-ws').addEventListener('click', function() {
recognizer.stop();
log.innerHTML = 'Recognition stopped' + '<br />' + log.innerHTML;
});
document.getElementById('clear-all').addEventListener('click', function() {
transcription.textContent = '';
log.textContent = '';
});
}
</script>
</body>
</html>