Speech recognition

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Speech recognition

Post by jak64 »

Hello Infratec,
Can we reduce the time between the word being spoken and the moment it appears?

For example, I would like to say "GO" and have it appear almost instantly.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Speech recognition

Post by infratec »

You can try to set

Code: Select all

recognition.continuous = false;
But I have to read the docu like you.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Speech recognition

Post by jak64 »

Hello Infratec, I made the modification but it doesn't change anything, there is still a delay of almost 1 second between the word spoken and the display of this word
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 693
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Speech recognition

Post by Kurzer »

You must not forget that speech recognition does not take place in real time on your computer. In the case of WebView2, the Microsoft Azure Speech Service (cloud-based) is probably used.

This means that your spoken recording is first sent as an audio file to Microsoft's server, where it is converted into text and then sent back to your program.

A delay of 1 second is already a good value here, which you probably cannot reduce.
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Speech recognition

Post by jak64 »

Thanks, Kurzer.
Indeed, I want to use it to voice-command a game (like saying "GO" or "STOP") and have a 1-second wait, which is already very good.
dige
Addict
Addict
Posts: 1416
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Speech recognition

Post by dige »

Adding a SpeechGrammarList with your keywords (“Go”, “Alexa”, “Purebasic”, “HeySiri”) narrows the search space. The recognizer stabilizes sooner when those terms are spoken, reducing latency for likely matches.

Code: Select all

<!DOCTYPE html>
<html lang="de">
  <head><meta charset="utf-8"><title>Voice To Text – Grammar Bias</title></head>
  <body>
    <div id="output"></div>

    <script>
      const outputDiv = document.getElementById('output');

      const SR  = window.SpeechRecognition || window.webkitSpeechRecognition || window.mozSpeechRecognition || window.msSpeechRecognition;
      const SGL = window.SpeechGrammarList || window.webkitSpeechGrammarList;

      const recognition = new SR();

      // ✅ GrammarList hinzufügen (bias auf Keywords)
      if (SGL) {
        // JSGF-Grammatik mit deinen Keywords
        const grammar = '#JSGF V1.0; grammar keywords; public <keyword> = Go | Alexa | Purebasic | HeySiri | "Hey Siri" ;';
        const list = new SGL();
        list.addFromString(grammar, 1); // Gewicht 1.0 (max)
        recognition.grammars = list;
      }

      // Low-Latency-Settings
      recognition.lang = 'de-DE';            // ggf. auf 'en-US' ändern, wenn du englisch sprichst
      recognition.interimResults = true;     // sofortige Zwischenstände
      recognition.continuous = true;
      recognition.maxAlternatives = 1;

      let finalText = '';

      recognition.onresult = (event) => {
        let interim = '';
        for (let i = event.resultIndex; i < event.results.length; i++) {
          const res = event.results[i];
          if (res.isFinal) finalText += res[0].transcript;
          else interim += res[0].transcript;
        }
        const text = (finalText + interim).trim();
        outputDiv.textContent = text;
        if (window.output) window.output(text);
      };

      recognition.onspeechend = () => { try { recognition.stop(); } catch(_) {} };
      recognition.onend      = () => { try { recognition.start(); } catch(_) {} };

      document.addEventListener('DOMContentLoaded', () => {
        try { recognition.start(); } catch(_) {}
      });
    </script>
  </body>
</html>

"Daddy, I'll run faster, then it is not so far..."
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Speech recognition

Post by jak64 »

Hello Dige,

I replaced the HTML with this one (yours), but it's not working!
Can you help me?

Html$ = ~"<!DOCTYPE html>\n" +
~"<html lang=fr>\n" +
~"<head><meta charset=utf-8><title>Voice To Text – Grammar Bias</title></head>\n" +
~"<body>\n" +
~"<div id=output></div>\n" +

~"<script>\n" +
~"const outputDiv = document.getElementById('output');\n" +

~"const SR = window.SpeechRecognition || window.webkitSpeechRecognition || window.mozSpeechRecognition || window.msSpeechRecognition;\n" +
~"const SGL = window.SpeechGrammarList || window.webkitSpeechGrammarList;\n" +

~"const recognition = new SR();\n" +

~"// ✅ GrammarList hinzufügen (bias auf Keywords)\n" +
~"If (SGL) {\n" +
~"// JSGF-Grammatik mit deinen Keywords\n" +
~"const grammar = '#JSGF V1.0; grammar keywords; public <keyword> = Go | Alexa | Purebasic | HeySiri | Hey Siri ;';\n" +
~"const List = new SGL();\n" +
~"List.addFromString(grammar, 1); // Gewicht 1.0 (max)\n" +
~"recognition.grammars = List;\n" +
~"}\n" +

~"// Low-Latency-Settings\n" +
~"recognition.lang = 'fr-FR';\n" +
~"recognition.interimResults = true;\n" +
~"recognition.continuous = true;\n" +
~"recognition.maxAlternatives = 1;\n" +

~"let finalText = '';\n" +

~"recognition.onresult = (event) => {\n" +
~"let interim = '';\n" +
~"For (let i = event.resultIndex; i < event.results.length; i++) {\n" +
~"const res = event.results;\n" +
~"If (res.isFinal) finalText += res[0].transcript;\n" +
~"Else interim += res[0].transcript;\n" +
~"}\n" +
~"const text = (finalText + interim).trim();\n" +
~"outputDiv.textContent = text;\n" +
~"If (window.output) window.output(text);\n" +
~"};\n" +

~"recognition.onspeechend = () => { try { recognition.stop(); } catch(_) {} };\n" +
~"recognition.onend = () => { try { recognition.start(); } catch(_) {} };\n" +

~"document.addEventListener('DOMContentLoaded', () => {\n" +
~"try { recognition.start(); } catch(_) {}\n" +
~"});\n" +
~"</script>\n" +
~"</body>\n" +
~"</html>\n"
dige
Addict
Addict
Posts: 1416
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Speech recognition

Post by dige »

If/For/Else → if/for/else (JS ist case-sensitive).

Unfortunately, testing with iOS/Safari did not yield any better results.

Code: Select all


Html$ = ~"<!DOCTYPE html>\n" +
        ~"<html lang=fr>\n" +
        ~"<head><meta charset=utf-8><title>Voice To Text – Grammar Bias</title></head>\n" +
        ~"<body>\n" +
        ~"<button id=start>Start</button> <button id=stop>Stop</button>\n" +
        ~"<div id=output></div>\n" +
        ~"<script>\n" +
        ~"const outputDiv = document.getElementById('output');\n" +
        ~"const startBtn = document.getElementById('start');\n" +
        ~"const stopBtn  = document.getElementById('stop');\n" +
        ~"const SR  = window.SpeechRecognition || window.webkitSpeechRecognition;\n" +
        ~"const SGL = window.SpeechGrammarList || window.webkitSpeechGrammarList;\n" +
        ~"if (!SR) {\n" +
        ~"  outputDiv.textContent = 'SpeechRecognition wird von diesem Browser/Setup nicht unterstützt.';\n" +
        ~"} else {\n" +
        ~"  const recognition = new SR();\n" +
        ~"  // ✅ GrammarList (wird von vielen Browsern ignoriert, schadet aber nicht)\n" +
        ~"  if (SGL) {\n" +
        ~"    const grammar = '#JSGF V1.0; grammar keywords; public <keyword> = Go | Alexa | Purebasic | \"Hey Siri\" | HeySiri ;';\n" +
        ~"    const list = new SGL();\n" +
        ~"    list.addFromString(grammar, 1);\n" +
        ~"    recognition.grammars = list;\n" +
        ~"  }\n" +
        ~"  // Low-Latency-Settings\n" +
        ~"  recognition.lang = 'fr-FR';\n" +
        ~"  recognition.interimResults = true;\n" +
        ~"  recognition.continuous = true;\n" +
        ~"  recognition.maxAlternatives = 1;\n" +
        ~"  let finalText = '';\n" +
        ~"  let listening = false;\n" +
        ~"  recognition.onresult = (event) => {\n" +
        ~"    const r = event.results[event.results.length - 1];\n" +
        ~"    const t = r[0].transcript;\n" +
        ~"    outputDiv.textContent = t;        // zeigt sofort interim oder final an\n" +
        ~"    if (window.output) window.output(t);\n" +
        ~"  };\n" +
        ~"  recognition.onerror = (e) => {\n" +
        ~"    console.error(e);\n" +
        ~"    outputDiv.textContent = 'Fehler: ' + e.error;\n" +
        ~"  };\n" +
        ~"  recognition.onend = () => {\n" +
        ~"    if (listening) { try { recognition.start(); } catch(_) {} }\n" +
        ~"  };\n" + 
        ~"  startBtn.addEventListener('click', () => {\n" +
        ~"    if (listening) return;\n" +
        ~"    finalText = '';\n" +
        ~"    listening = true;\n" +
        ~"    try { recognition.start(); } catch(_) {}\n" +
        ~"  });\n" +
        ~"  stopBtn.addEventListener('click', () => {\n" +
        ~"    listening = false;\n" +
        ~"    try { recognition.stop(); } catch(_) {}\n" +
        ~"  });\n" +
        ~"}\n" +
        ~"</script>\n" +
        ~"</body>\n" +
        ~"</html>\n"

"Daddy, I'll run faster, then it is not so far..."
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Speech recognition

Post by jak64 »

Hello Dige
I replaced the HTML with yours, but it still doesn't work. I say "GO" and nothing appears (in my program), even though it worked with my initial program!
Post Reply