Skip to content

Commit ac101b9

Browse files
wesbos#20 - speech detection
1 parent 8966f8d commit ac101b9

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

20 - Speech Detection/index.html

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Speech Detection</title>
6+
<script type="text/javascript" src="https://www.cornify.com/js/cornify.js"></script>
7+
</head>
8+
<body>
9+
10+
<div class="words" contenteditable>
11+
</div>
12+
13+
<script>
14+
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
15+
16+
const recognition = new SpeechRecognition();
17+
recognition.interimResults = true;
18+
recognition.continuous = true;
19+
20+
let p = document.createElement('p');
21+
const words = document.querySelector('.words');
22+
words.appendChild(p);
23+
24+
recognition.addEventListener('result', e => {
25+
26+
const transcript = Array.from( e.results )
27+
.map(result => result[0])
28+
.map(result => result.transcript)
29+
.join('');
30+
31+
p.textContent = transcript;
32+
33+
if ( e.results[0].isFinal ) {
34+
p = document.createElement('p');
35+
words.appendChild( p );
36+
}
37+
38+
if ( transcript.includes( 'unicorn' ) ) cornify_add();
39+
})
40+
41+
recognition.addEventListener('end', recognition.start);
42+
43+
recognition.start();
44+
</script>
45+
46+
47+
<style>
48+
html {
49+
font-size: 10px;
50+
}
51+
52+
body {
53+
background: #ffc600;
54+
font-family: 'helvetica neue';
55+
font-weight: 200;
56+
font-size: 20px;
57+
}
58+
59+
.words {
60+
max-width: 500px;
61+
margin: 50px auto;
62+
background: white;
63+
border-radius: 5px;
64+
box-shadow: 10px 10px 0 rgba(0,0,0,0.1);
65+
padding: 1rem 2rem 1rem 5rem;
66+
background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px;
67+
background-size: 100% 3rem;
68+
position: relative;
69+
line-height: 3rem;
70+
}
71+
72+
p {
73+
margin: 0 0 3rem;
74+
}
75+
76+
.words:before {
77+
content: '';
78+
position: absolute;
79+
width: 4px;
80+
top: 0;
81+
left: 30px;
82+
bottom: 0;
83+
border: 1px solid;
84+
border-color: transparent #efe4e4;
85+
}
86+
</style>
87+
88+
</body>
89+
</html>

index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<style>
2+
body {
3+
display: flex;
4+
flex-direction: column;
5+
align-items: center;
6+
}
7+
8+
a {
9+
text-decoration: none;
10+
font-size: 40px;
11+
}
12+
</style>
13+
14+
<a href="/js30/20%20-%20Speech%20Detection/index.html">🎙️</a>

0 commit comments

Comments
 (0)