Skip to content

Commit c2f7b6b

Browse files
committed
Ex20 - Speech detection
1 parent 4f85d0c commit c2f7b6b

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

20 - Speech Detection/index.html

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Speech Detection</title>
6+
</head>
7+
<body>
8+
9+
<div class="words" contenteditable>
10+
</div>
11+
12+
<script src="script.js" charset="utf-8"></script>
13+
14+
<style>
15+
html {
16+
font-size: 10px;
17+
}
18+
19+
body {
20+
background:#ffc600;
21+
font-family: 'helvetica neue';
22+
font-weight: 200;
23+
font-size: 20px;
24+
}
25+
26+
.words {
27+
max-width:500px;
28+
margin:50px auto;
29+
background:white;
30+
border-radius:5px;
31+
box-shadow:10px 10px 0 rgba(0,0,0,0.1);
32+
padding:1rem 2rem 1rem 5rem;
33+
background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px;
34+
background-size: 100% 3rem;
35+
position: relative;
36+
line-height:3rem;
37+
}
38+
p {
39+
margin: 0 0 3rem 0;
40+
}
41+
42+
.words:before {
43+
content: '';
44+
position: absolute;
45+
width: 4px;
46+
top: 0;
47+
left: 30px;
48+
bottom: 0;
49+
border: 1px solid;
50+
border-color: transparent #efe4e4;
51+
}
52+
</style>
53+
54+
</body>
55+
</html>

20 - Speech Detection/loustic.jpg

134 KB
Loading

20 - Speech Detection/script.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
// webkitSpeechRecognition in Chrome
4+
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
5+
6+
const recognition = new window.SpeechRecognition();
7+
recognition.interimResults = true; //on veut les résultats intérmédiaires, on ne veut pas avoir à attendre d'arreter de parler pour avoir les resultats
8+
9+
// on insere le texte dans un paragraphe
10+
const words = document.querySelector('.words');
11+
let p = document.createElement('p');
12+
words.appendChild(p);
13+
14+
recognition.addEventListener('result', event => {
15+
//e.results is a list, not an array
16+
const transcript = Array.from(event.results)
17+
.map(result => result[0])
18+
.map(result => result.transcript)
19+
.join('');
20+
21+
p.textContent = transcript;
22+
23+
// si le resultat est final, on remplace p par un nouveau p
24+
if (event.results[0].isFinal) {
25+
analyse(transcript);
26+
let p = document.createElement('p');
27+
words.appendChild(p);
28+
}
29+
30+
});
31+
32+
function analyse(transcript) {
33+
if (transcript.includes('the most beautiful dog')) {
34+
const image = document.createElement('img');
35+
image.src = 'loustic.jpg';
36+
image.style ='opacity: 0 ; transition: opacity 3s; width: 415px; height:550px';
37+
image.onload = function() {
38+
this.style.opacity='1';
39+
};
40+
// image.onload = (e) => e.target.style.opacity='1';
41+
document.querySelector('body').appendChild(image);
42+
}
43+
}
44+
45+
recognition.addEventListener('end', recognition.start);
46+
47+
recognition.start();

0 commit comments

Comments
 (0)