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.

Revisions

  1. andreescocard created this gist Mar 10, 2023.
    34 changes: 34 additions & 0 deletions speech2text.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    <!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>