Skip to content
Snippets Groups Projects
Select Git revision
  • 991eec42e926d0f3a6b7ed7c83d67a8ae08916ec
  • main default protected
2 results

voice.js

Blame
  • voice.js 10.36 KiB
    var voiceBtn = document.getElementById("voiceBtn");
    var voiceDiv = document.getElementById("voiceDiv");
    var voiceSpan = document.getElementById("voiceSpan");
    var heardSpan = document.getElementById("heardSpan");
    var commandSpan = document.getElementById("commandSpan");
    
    const voiceSelect = document.getElementById("selectVoice");
    
    let voices = [];
    
    const synth = window.speechSynthesis;
    
    var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
    var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
    var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
    
    function populateVoiceList() {
      voices = synth.getVoices().sort(function (a, b) {
        const aname = a.name.toUpperCase();
        const bname = b.name.toUpperCase();
    
        if (aname < bname) {
          return -1;
        } else if (aname == bname) {
          return 0;
        } else {
          return +1;
        }
      });
    
      const selectedIndex = voiceSelect.selectedIndex < 0 ? 0 : voiceSelect.selectedIndex;
      
      for (let i = 0; i < voices.length; i++) {
        const option = document.createElement("option");
        option.textContent = `${voices[i].name} (${voices[i].lang})`;
    
        if (voices[i].default) {
          option.textContent += " -- DEFAULT";
        }
    
        option.setAttribute("data-lang", voices[i].lang);
        option.setAttribute("data-name", voices[i].name);
        voiceSelect.appendChild(option);
      }
      voiceSelect.selectedIndex = selectedIndex;
    }
    
    voiceBtn.addEventListener("click", startListening);
    
    if ('speechSynthesis' in window) {
        
    }else{
        // Speech Synthesis Not Supported
        alert("Sorry, your browser doesn't support text to speech!");
    }
    
    function startListening(){
        voiceBtn.disabled = true;
        voiceSpan.innerText = 'Dites "Cristo !" pour commencer a donner votre requete';
        voiceSpan.style.display = "";
        heardSpan.innerText = "";
        heardSpan.style.display = "none";
        commandSpan.innerText = "";
        commandSpan.style.display = "none";
        voiceBtn.innerText = "Ecoute en cours";
        listenForCristo();
    }
    
    function listenForCristo(){
        var cristo = [