Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 16, 2023 01:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save code-boxx/d7b7f5c718ff7c35553018b96f3618a2 to your computer and use it in GitHub Desktop.
Save code-boxx/d7b7f5c718ff7c35553018b96f3618a2 to your computer and use it in GitHub Desktop.
Javascript Voice Commands

JAVASCRIPT VOICE COMMANDS

https://code-boxx.com/voice-commands-javascript-speech-recognition/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
#vwrap {
box-sizing: border-box;
width: 100%;
height: 100px;
padding: 10px;
transition: all 0.3s;
}
<!DOCTYPE html>
<html>
<head>
<title>Simple Voice Command Demo</title>
<link rel="stylesheet" href="voice-command.css">
<script src="voice-command.js"></script>
</head>
<body>
<!-- (A) DEMO WRAPPER -->
<div id="vwrap"></div>
<!-- (B) DEMO BUTTON -->
<input type="button" id="vbtn" value="Loading" disabled>
<div>Try "power on", "power off", or "say hello".</div>
</body>
</html>
var voice = {
// (A) INIT VOICE COMMAND
wrap : null, // html demo <div> wrapper
btn : null, // html demo button
recog : null, // speech recognition object
init : () => {
// (A1) GET HTML ELEMENTS
voice.wrap = document.getElementById("vwrap");
voice.btn = document.getElementById("vbtn");
// (A2) GET MIC ACCESS PERMISSION
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
// (A3) SPEECH RECOGNITION OBJECT & SETTINGS
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
voice.recog = new SpeechRecognition();
voice.recog.lang = "en-US";
voice.recog.continuous = false;
voice.recog.interimResults = false;
// (A4) ON SPEECH RECOGNITION - RUN CORRESPONDING COMMAND
voice.recog.onresult = evt => {
let said = evt.results[0][0].transcript.toLowerCase();
if (cmd[said]) { cmd[said](); }
else { said += " (command not found)"; }
voice.wrap.innerHTML = said;
voice.stop();
};
// (A5) ON SPEECH RECOGNITION ERROR
voice.recog.onerror = err => console.error(evt);
// (A6) READY!
voice.btn.disabled = false;
voice.stop();
})
.catch(err => {
console.error(err);
voice.wrap.innerHTML = "Please enable access and attach a microphone.";
});
},
// (B) START SPEECH RECOGNITION
start : () => {
voice.recog.start();
voice.btn.onclick = voice.stop;
voice.btn.value = "Speak Now Or Click Again To Cancel";
},
// (C) STOP/CANCEL SPEECH RECOGNITION
stop : () => {
voice.recog.stop();
voice.btn.onclick = voice.start;
voice.btn.value = "Press To Speak";
}
};
window.addEventListener("DOMContentLoaded", voice.init);
// (D) COMMANDS LIST
var cmd = {
"power on" : () => {
voice.wrap.style.backgroundColor = "yellow";
voice.wrap.style.color = "black";
},
"power off" : () => {
voice.wrap.style.backgroundColor = "black";
voice.wrap.style.color = "white";
},
"say hello" : () => {
alert("Hello World!");
}
};
@anthony0000
Copy link

This is just perfect, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment