durss

Untitled

Nov 18th, 2021 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let recognition = new webkitSpeechRecognition();
  2. let noActivityTimeout = null;
  3. recognition.continuous = true;
  4. recognition.interimResults = true;
  5. recognition.lang = "fr-FR";
  6. recognition.onresult = (event) => {
  7.     let finalText= "";//Contient le texte corrigé ~1s après avoir fini de parler
  8.     let tempText = "";//Contient le flow de texte
  9.     for (let i = event.resultIndex; i < event.results.length; i++) {
  10.         if(event.results[i].isFinal) {
  11.             finalText = event.results[i][0].transcript;
  12.         }else{
  13.             tempText += event.results[i][0].transcript;
  14.         }
  15.     }
  16.    
  17.     clearTimeout(noActivityTimeout);
  18.     //Avoid staying blocked if talking a lot and S2T is lost
  19.     noActivityTimeout = setTimeout(_=> {
  20.         recognition.stop();
  21.     }, 2000);
  22.  
  23.     if(/et (compagnie|cie)/gi.test(tempText)) {
  24.         console.log("WESH ARRETE DE DIRE CA !!");
  25.     }
  26.  
  27.     if(finalText.length > 0) {
  28.         //Ici c'est le texte corrigé quand tu arrête de parler
  29.         //pendant ~1 seconde.
  30.  
  31.         //Exemple de TTS natif à chrome
  32.         //var speech = new SpeechSynthesisUtterance();
  33.         //speech.lang = "fr";
  34.         //speech.text = finalText;
  35.         //window.speechSynthesis.speak(speech);
  36.     }
  37. };
  38.  
  39. recognition.onend = (e) => {
  40.     // console.log("ON END");
  41.     recognition.start();//Relance l'écoute pour écouter en permanence
  42. };
  43.  
  44. recognition.onspeechend = () => {
  45.     // console.log("SPEECH END");
  46. };
  47.  
  48. recognition.onerror = (e) => {
  49.     // console.log("ON ERROR", e);
  50. }
  51.  
  52. recognition.start();
Add Comment
Please, Sign In to add comment