Skip to content

Instantly share code, notes, and snippets.

@andreescocard
Created March 10, 2023 18:42
Show Gist options
  • Save andreescocard/b31e173211b1d301652af7e958d152ce to your computer and use it in GitHub Desktop.
Save andreescocard/b31e173211b1d301652af7e958d152ce to your computer and use it in GitHub Desktop.
speech2text
<!DOCTYPE html>
<html>
<head>
<title>Simple Command Voice</title>
</head>
<body>
<p id="output"></p>
<button id="start">Click and say something!</button>
<script>
(() => {
const startBtn = document.querySelector('#start');
const output = document.querySelector('#output');
function start() {
const recognition = new webkitSpeechRecognition();
recognition.interimResults = true;
recognition.lang = "pt-BR";
recognition.continuous = true;
recognition.start();
// This event happens when you talk in the microphone
recognition.onresult = function(event) {
for (let i = event.resultIndex; i < event.results.length; i++) {
if (event.results[i].isFinal) {
// Here you can get the string of what you told
const content = event.results[i][0].transcript.trim();
output.textContent = content;
}
}
};
};
startBtn.addEventListener('click', () => start());
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment