Skip to content
Snippets Groups Projects
Commit b6672708 authored by v4nkor's avatar v4nkor
Browse files

WIP voice recognition

parent 5dc93e8d
No related branches found
No related tags found
No related merge requests found
var voiceBtn = document.getElementById("voiceBtn");
var voiceDiv = document.getElementById("voiceDiv");
var voiceSpan = document.getElementById("voiceSpan");
var heardSpan = document.getElementById("heardSpan");
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
console.log(voiceBtn);
voiceBtn.addEventListener("click", startListening);
function startListening(){
voiceBtn.disabled = true;
voiceSpan.innerText = 'Dites "Cristo !" pour commencer a donner votre requete';
voiceSpan.style.display = "";
heardSpan.innerText = "";
heardSpan.style.display = "none";
voiceBtn.innerText = "Ecoute en cours";
listenForCristo();
}
function listenForCristo(){
var cristo = [
'Cristo',
'cristo',
'crist',
'risto'
];
var grammar = '#JSGF V1.0; grammar phrase; public <phrase> = cristaux;';
var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.lang = 'fr-FR';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
recognition.start();
console.log('Started listening');
recognition.onresult = function(event) {
console.log('Listened');
/* if(heard.toLowerCase().includes("cristo")){
} */
var speechResult = event.results[0][0].transcript.toLowerCase();
console.log(speechResult);
heardSpan.innerText += " A entendu = " + speechResult;
heardSpan.style.display = "";
if(speechResult.toLowerCase().includes("cristaux")){
console.log('Heard Cristo ! (or Cristo)');
} else {
console.log('Heard nothing');
}
voiceSpan.style.display = "none";
console.log('Confidence: ' + event.results[0][0].confidence);
}
recognition.onspeechend = function() {
recognition.stop();
voiceBtn.disabled = false;
voiceBtn.textContent = 'Ecouter a nouveau';
voiceSpan.style.display = "none";
}
recognition.onerror = function(event) {
voiceBtn.disabled = false;
voiceBtn.textContent = 'Ecouter a nouveau';
diagnosticPara.textContent = 'Error occurred in recognition: ' + event.error;
voiceSpan.style.display = "none";
heardSpan.innerText += " Erreur ";
heardSpan.style.display = "";
}
recognition.onaudiostart = function(event) {
//Fired when the user agent has started to capture audio.
console.log('SpeechRecognition.onaudiostart');
}
recognition.onaudioend = function(event) {
//Fired when the user agent has finished capturing audio.
console.log('SpeechRecognition.onaudioend');
}
recognition.onend = function(event) {
//Fired when the speech recognition service has disconnected.
console.log('SpeechRecognition.onend');
console.log("A entendu = " + event.results);
voiceSpan.style.display = "none";
}
recognition.onnomatch = function(event) {
//Fired when the speech recognition service returns a final result with no significant recognition. This may involve some degree of recognition, which doesn't meet or exceed the confidence threshold.
console.log('SpeechRecognition.onnomatch');
}
recognition.onsoundstart = function(event) {
//Fired when any sound — recognisable speech or not — has been detected.
console.log('SpeechRecognition.onsoundstart');
}
recognition.onsoundend = function(event) {
//Fired when any sound — recognisable speech or not — has stopped being detected.
console.log('SpeechRecognition.onsoundend');
}
recognition.onspeechstart = function (event) {
//Fired when sound that is recognised by the speech recognition service as speech has been detected.
console.log('SpeechRecognition.onspeechstart');
}
recognition.onstart = function(event) {
//Fired when the speech recognition service has begun listening to incoming audio with intent to recognize grammars associated with the current SpeechRecognition.
console.log('SpeechRecognition.onstart');
}
}
\ No newline at end of file
<i class="fa-solid fa-microphone"></i>
<button id="voiceBtn">Commencer l'ecoute du micro</button>
\ No newline at end of file
<div id="voiceDiv">
<i class="fa-solid fa-microphone"></i><button id="voiceBtn">Commencer l'ecoute du micro</button><span id="voiceSpan" style="display:none"></span>
<br>
<span id="heardSpan" style="display:none"></span>
</div>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment