Advertisement
durss

Speech to text template

Jun 24th, 2021 (edited)
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //exemple très basique qui écoute et relance l'écoute en boucle.
  2. const SRConstructor = window.SpeechRecognition || window.webkitSpeechRecognition;
  3. let recognition = new SRConstructor ();
  4. recognition.continuous = true;
  5. recognition.interimResults = true;
  6. recognition.lang = "fr-FR";
  7. recognition.onresult = async (event) => {
  8.     let finalText= "";//Contient le texte corrigé ~1s après avoir fini de parler
  9.     let tempText = "";//Contient le flow de texte
  10.     for (let i = event.resultIndex; i < event.results.length; i++) {
  11.         if(event.results[i].isFinal) {
  12.             finalText = event.results[i][0].transcript;
  13.         }else{
  14.             tempText += event.results[i][0].transcript;
  15.         }
  16.     }
  17.     if(finalText.length > 0) {
  18.         //Fais ce que tu veux là avec le texte corrigé !
  19.  
  20.         //Exemple de TTS natif à chrome
  21.         var speech = new SpeechSynthesisUtterance();
  22.         speech.lang = "fr";
  23.         speech.text = finalText;
  24.         window.speechSynthesis.speak(speech);
  25.     }
  26. };
  27.  
  28. recognition.onend = (e) => {
  29.     // console.log("ON END");
  30.     recognition.start();//Relance l'écoute pour écouter en permanence
  31. };
  32.  
  33. recognition.onspeechend = () => {
  34.     // console.log("SPEECH END");
  35. };
  36.  
  37. recognition.onerror = (e) => {
  38.     // console.log("ON ERROR", e);
  39. }
  40.  
  41. recognition.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement