Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //exemple très basique qui écoute et relance l'écoute en boucle.
- const SRConstructor = window.SpeechRecognition || window.webkitSpeechRecognition;
- let recognition = new SRConstructor ();
- recognition.continuous = true;
- recognition.interimResults = true;
- recognition.lang = "fr-FR";
- recognition.onresult = async (event) => {
- let finalText= "";//Contient le texte corrigé ~1s après avoir fini de parler
- let tempText = "";//Contient le flow de texte
- for (let i = event.resultIndex; i < event.results.length; i++) {
- if(event.results[i].isFinal) {
- finalText = event.results[i][0].transcript;
- }else{
- tempText += event.results[i][0].transcript;
- }
- }
- if(finalText.length > 0) {
- //Fais ce que tu veux là avec le texte corrigé !
- //Exemple de TTS natif à chrome
- var speech = new SpeechSynthesisUtterance();
- speech.lang = "fr";
- speech.text = finalText;
- window.speechSynthesis.speak(speech);
- }
- };
- recognition.onend = (e) => {
- // console.log("ON END");
- recognition.start();//Relance l'écoute pour écouter en permanence
- };
- recognition.onspeechend = () => {
- // console.log("SPEECH END");
- };
- recognition.onerror = (e) => {
- // console.log("ON ERROR", e);
- }
- recognition.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement