|
| 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