Skip to content

Instantly share code, notes, and snippets.

@denisso
Last active August 2, 2025 19:28
Show Gist options
  • Select an option

  • Save denisso/03f387e32a210e4ec5cfd14c4dec506e to your computer and use it in GitHub Desktop.

Select an option

Save denisso/03f387e32a210e4ec5cfd14c4dec506e to your computer and use it in GitHub Desktop.
SpeechRecognition
<!DOCTYPE html>
<html>
<head>
<title>Speech to Text Demo</title>
</head>
<body>
<h1>Speech to Text</h1>
<button id="start-btn">Start</button>
<button id="stop-btn">Stop</button>
<p>Output:</p>
<textarea id="output" rows="10" cols="50"></textarea>
<script>
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!SpeechRecognition) {
alert("SpeechRecognition not supported");
} else {
const recognition = new SpeechRecognition();
recognition.lang = "ru-RU";
recognition.interimResults = true;
recognition.continuous = true;
const startBtn = document.getElementById("start-btn");
const stopBtn = document.getElementById("stop-btn");
const output = document.getElementById("output");
let finalTranscript = '';
recognition.onresult = (event) => {
let interim = '';
for (let i = event.resultIndex; i < event.results.length; i++) {
const result = event.results[i];
if (result.isFinal) {
finalTranscript += result[0].transcript;
} else {
interim += result[0].transcript;
}
}
output.value = finalTranscript + interim;
};
recognition.onerror = (e) => {
console.error('Speech recognition error:', e.error);
};
recognition.onend = () => {
console.log('Speech recognition stopped.');
};
startBtn.onclick = () => {
finalTranscript = '';
output.value = '';
recognition.start();
};
stopBtn.onclick = () => {
recognition.stop();
};
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment