Skip to content

Commit c80c603

Browse files
committed
Project 20 complete
1 parent ca99b4e commit c80c603

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

20 - Speech Detection/index-START.html

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Speech Detection</title>
67
</head>
8+
79
<body>
810

911
<div class="words" contenteditable>
1012
</div>
1113

12-
<script>
13-
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
14-
15-
16-
</script>
14+
<script src="typescripts.js"></script>
1715

1816

1917
<style>
@@ -33,14 +31,14 @@
3331
margin: 50px auto;
3432
background: white;
3533
border-radius: 5px;
36-
box-shadow: 10px 10px 0 rgba(0,0,0,0.1);
34+
box-shadow: 10px 10px 0 rgba(0, 0, 0, 0.1);
3735
padding: 1rem 2rem 1rem 5rem;
3836
background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px;
3937
background-size: 100% 3rem;
4038
position: relative;
4139
line-height: 3rem;
4240
}
43-
41+
4442
p {
4543
margin: 0 0 3rem;
4644
}
@@ -58,4 +56,5 @@
5856
</style>
5957

6058
</body>
61-
</html>
59+
60+
</html>

20 - Speech Detection/typescripts.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
2+
const recognition = new SpeechRecognition();
3+
recognition.interimResults = true;
4+
let p = document.createElement('p');
5+
const words = document.querySelector('.words');
6+
words.appendChild(p);
7+
recognition.addEventListener('result', e => {
8+
const transcript = Array.from(e.results)
9+
.map(result => result[0])
10+
.map(result => result.transcript)
11+
.join('');
12+
p.textContent = transcript;
13+
if (e.results[0].isFinal) {
14+
p = document.createElement('p');
15+
words.appendChild(p);
16+
if (transcript.includes('unicorn')) {
17+
console.log('🦄🦄🦄🦄🦄🦄🦄');
18+
}
19+
}
20+
});
21+
recognition.addEventListener('end', recognition.start);
22+
recognition.start();

20 - Speech Detection/typescripts.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* JavaScript30 by Wes Bos, https://javascript30.com/
3+
* TypeScript implementation by Will Wager
4+
* Project: Speech Detection
5+
* Concepts: Speech Recognition API
6+
* Key takeaways: Chrome and Edge have native speech recognition;
7+
* The transcript can be used to trigger any other actions.
8+
* Sidenotes: es6 needed for Array.from; needed to extend window interface to add prefixed API.
9+
* Compilation command:
10+
* tsc --removeComments --strictNullChecks --noImplicitAny --target es6 typescripts.ts
11+
*/
12+
13+
interface Window {
14+
webkitSpeechRecognition: any;
15+
}
16+
17+
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
18+
19+
const recognition = new SpeechRecognition();
20+
recognition.interimResults = true;
21+
22+
let p = document.createElement('p');
23+
const words = document.querySelector('.words')! as HTMLDivElement;
24+
words.appendChild(p);
25+
26+
recognition.addEventListener('result', e => {
27+
const transcript = Array.from(e.results)
28+
.map(result => result[0])
29+
.map(result => result.transcript)
30+
.join('');
31+
32+
p.textContent = transcript;
33+
if (e.results[0].isFinal) {
34+
p = document.createElement('p');
35+
words.appendChild(p);
36+
if (transcript.includes('unicorn')) {
37+
console.log('🦄🦄🦄🦄🦄🦄🦄');
38+
}
39+
}
40+
});
41+
42+
recognition.addEventListener('end', recognition.start);
43+
44+
recognition.start();

0 commit comments

Comments
 (0)