!7 sS keyphrase = "computer"; // bigger number means more tolerance // 1e-40 works pretty well with few false positives // 1e-30 gets no hits // 1e-35 doesn't trigger reliably // Using 1e-38 for now sS threshold = "1e-38"; // seconds to listen after hot word // TODO: keep listening when user said something static int recognitionTimeout = 10; sO onKeyPhrase; sbool exited; sS python = trim([[ #!/usr/bin/python from subprocess import Popen, PIPE, STDOUT import pty import os import sys cmd = 'pocketsphinx_continuous -inmic yes -keyphrase #KEYPHRASE# -kws_threshold #THRESHOLD#' master, slave = pty.openpty() p = Popen(cmd, shell=True, stdin=PIPE, stdout=slave, stderr=slave, close_fds=True) stdout = os.fdopen(master) while True: sys.stdout.write(stdout.readline()) sys.stdout.flush() ]]); static File fPython; static JButton btnStartStop; sbool paused; p-subst { makeBot("Hot Word Detection."); unix_killAll("pocketsphinx"); print("Key phrase is: " + keyphrase); onKeyPhrase = voidfunc(S s) { S language = loadTextFilePossiblyGZipped(getProgramFile(#1009555, "language.text")); fS response = startsWith(language, "de") ? "Ja!" : "Yep!"; infoBox(response); thread { sendOpt("Chrome Speech.", "start recognition timeout " + recognitionTimeout); } thread { sendOpt("Mouth.", response); } }; swing { showControls(jcenteredline(btnStartStop = jbutton("Stop recognition", f startStop))); } fPython = getProgramFile("pocketsphinx.py"); makeExecutable(saveTextFile(fPython, python.replace("#THRESHOLD#", threshold).replace("#KEYPHRASE#", bashQuote(keyphrase))); titleStatus_setBaseTitle(consoleFrame(), "Hot Word Detection"); infoBox("Say " + quote(keyphrase) + " to activate me!"); start(); } svoid start { backtickToConsole_lineBuf(f2s(fPython), voidfunc(S line) { print("Line read: " + quote(line)); if (eq(line, keyphrase)) pcallF(onKeyPhrase, line); else if (startsWith(line, "READY.")) consoleStatus("Ready"); else if (startsWith(line, "Listening.")) consoleStatus("Listening"); }); paused = true; setText(btnStartStop, "Start recognition"); consoleStatus("Stopped"); } svoid cleanMeUp { if (!exited) { exited = true; unix_killAll("pocketsphinx"); } } svoid startStop { if (paused) { paused = false; setText(btnStartStop, "Stop recognition"); thread { main.start(); } } else { paused = true; setText(btnStartStop, "Start recognition"); unix_killAll("pocketsphinx"); } }