-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_recognition.html
177 lines (164 loc) · 6.31 KB
/
speech_recognition.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Speech Recognition</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://kit.fontawesome.com/69d1734429.js"></script>
<style>
* {
font-family: "Roboto", "HelveticaNeue-Light""IBM Plex Sans", sans-serif;
font-weight: 300;
/* letter-spacing: */
}
</style>
</head>
<body class="w-100 h-100 p-0 m-0 bg-light">
<div class="border shadow-sm container-fluid px-0 py-3 mx-0 bg-white">
<header class="container">
<h4>HTML5 Speech Recognition Example</h4>
</header>
</div>
<div class="container-fluid text-center py-5 mt-5">
<div class="row">
<div class="container">
<h3 id="record-transcription-area"></h3>
</div>
</div>
</div>
<div class="container-fluid text-center py-0 mt-0">
<div class="row">
<div id="sr-alternatives-container" class="container">
<small class="text-muted mt-2">Alternatives will show here:</small><br>
<span class="badge badge-primary px-3 py-2">have a nice day</span>
<span class="badge badge-primary px-3 py-2">I have a nice day</span>
<span class="badge badge-primary px-3 py-2">having a nice day</span>
<span class="badge badge-primary px-3 py-2">have a nice.day</span>
</div>
</div>
</div>
<div class="container-fluid text-center py-5">
<div class="row">
<div class="border rounded shadow-sm bg-white container p-4">
<div class="form-group text-left">
<label for="exampleInputEmail1">Say something amazing:</label>
<div class="input-group">
<input type="text" class="form-control" placeholder="Have a nice day!" aria-label="Recipiente para nickname"
aria-describedby="basic-addon2">
<div class="input-group-append">
<span id="record-toggle" class="input-group-text bg-light text-dark">
<i id="record-toggle-icon" class="fas fa-microphone-alt"></i>
</span>
</div>
</div>
<small id="emailHelp" class="form-text text-muted">
Press the microphone button to the right, and belt out!
</small>
</div>
</div>
</div>
</div>
<div class="text-center fixed-bottom border shadow-sm container-fluid px-0 py-3 mx-0 bg-white">
<footer class="container">
<small class="text-muted">
Source on <a href="https://github.com/mtslucasmartins/html5_speech_recognition">GitHub</a>.
</small>
</footer>
</div>
<script>
let isRecording = false;
function setupSpeechRecognition(lang = "pt-BR") {
let SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.interimResults = true;
recognition.lang = "en" || lang;
recognition.continuous = true;
recognition.maxAlternatives = 5;
return recognition;
}
window.onload = function () {
const that = this;
const recordToggleButton = document.querySelector('#record-toggle');
const recordToggleButtonIcon = document.querySelector('#record-toggle-icon');
const recordTranscriptionArea = document.querySelector('#record-transcription-area');
const recognition = this.setupSpeechRecognition();
recognition.onstart = function () {
that.isRecording = true;
that.toggleCssClasses(recordToggleButton, 'bg-success', 'bg-light');
that.toggleCssClasses(recordToggleButton, 'text-light', 'text-dark');
};
recognition.onend = function () {
that.isRecording = false;
that.toggleCssClasses(recordToggleButton, 'bg-success', 'bg-light');
that.toggleCssClasses(recordToggleButton, 'text-light', 'text-dark');
};
recognition.onresult = function (event) {
that.showAlternatives(event);
transcription = "";
for (let result of event.results) {
transcription += result[0].transcript;
}
recordTranscriptionArea.innerHTML = transcription;
};
recognition.onnomatch = function () {
transcription = "Oh sorry, I couldn't hear you!";
recordTranscriptionArea.innerHTML = transcription;
};
recordToggleButton.addEventListener('click', function (e) {
if (that.isRecording) {
recognition.stop();
return;
}
recognition.start();
}, false);
};
createElement = (name, classes = "", innerHTML = "") => {
let element = document.createElement(name);
element.className = classes;
element.innerHTML = innerHTML;
return element;
}
showAlternatives = (event) => {
const srAlternativesContainer = document.querySelector('#sr-alternatives-container');
srAlternativesContainer.innerHTML = '';
let counter = 0;
for (let result of event.results) {
srAlternativesContainer.appendChild(createElement(
'small', "text-muted mt-2", `Result ${++counter}:`
));
srAlternativesContainer.appendChild(document.createElement('br'));
for (let alternative of result) {
srAlternativesContainer.appendChild(createElement(
'span', "badge badge-primary px-3 py-2 m-2", alternative.transcript
));
}
srAlternativesContainer.appendChild(document.createElement('br'));
}
};
// CSS Utilities
function toggleCssClasses(el, c1, c2) {
if (containsCssClass(el, c1)) {
removeCssClass(el, c1);
appendCssClass(el, c2);
} else {
removeCssClass(el, c2);
appendCssClass(el, c1);
}
}
containsCssClass = (el, c) => el.className.split(" ").indexOf(c) >= 0;
removeCssClass = (el, c) => {
let classes = el.className.split(" ");
classes.splice(classes.indexOf(c), 1);
el.className = classes.join(" ");
};
function appendCssClass(el, c) {
let classes = el.className.split(" ");
classes.push(c);
el.className = classes.join(" ");
}
</script>
</body>
</html>