Last active
August 2, 2025 19:28
-
-
Save denisso/03f387e32a210e4ec5cfd14c4dec506e to your computer and use it in GitHub Desktop.
SpeechRecognition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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