Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

133
LINES

< > BotCompany Repo | #1009578 // Linux: Hot Word Detection ["Computer!"] + "Eve" Icon - using PocketSphinx + Python wrapper

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 4079K of libraries. Click here for Pure Java version (7992L/59K/199K).

!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-39 for now
sS threshold = "1e-39";

// seconds to listen after hot word
// TODO: keep listening when user said something
static int recognitionTimeout = 10;

sS icon = /*#1009669*/ #1009667; //#1009662;

// milliseconds before starting recognition (time for saying "yep" or "ja")
static int delayBeforeRecognition = 300;

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 {
  if (!isLinux()) fail("Get Linux!");
  load('paused);
  
  //onLeftClick(installTrayIcon(icon, "Computer!"), f jump_blip);
  TrayIcon trayIcon = onLeftClick(
    installTrayIcon2(resizeImage(loadImage2(icon), 24), "Computer!",
      makePopupMenu(menuItem("Computer!", f jump_blip))),
      f jump_blip);
      
  trayIcon.displayMessage(
    "", "Click on EVE to activate the assistant!",
    TrayIcon.MessageType.INFO/*NONE*/);
  
  makeBot("Hot Word Detection.");
  unix_killAll("pocketsphinx");
  
  print("Key phrase is: " + keyphrase);
  
  onKeyPhrase = voidfunc(S s) { jump() };
  
  swing {
    showControls(jcenteredline(btnStartStop = jbutton("Listen for hot word", f startStop)));
  }
  
  fPython = getProgramFile("pocketsphinx.py");
  makeExecutable(saveTextFile(fPython, python.replace("#THRESHOLD#", threshold).replace("#KEYPHRASE#", bashQuote(keyphrase))));
  
  titleStatus_setBaseTitle(consoleFrame(), "Hot Word Detection");
  if (!paused) {
    infoBox("Say " + quote(keyphrase) + " to activate me!");
    start();
  }
  
  makeBot("Hot Word Detection.");
}

svoid start {
  setText(btnStartStop, "Don't listen for hot word");
  thread {
    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");
    });
    setAndSave(paused := true);
    setText(btnStartStop, "Listen for hot word");
    consoleStatus("Stopped");
  }
}

svoid cleanMeUp {
  if (!exited) { exited = true; unix_killAll("pocketsphinx"); }
}

svoid startStop {
  if (paused) {
    setAndSave(paused := false);
    start();
  } else {
    setAndSave(paused := true);
    setText(btnStartStop, "Listen for hot word");
    unix_killAll("pocketsphinx");
  }
}

svoid jump_blip {
  thread {
    infoBox(botAnswerWord());
    blipAndWait();
    sendOpt("Chrome Speech.", "start recognition timeout " + recognitionTimeout);
  }
}
  
svoid jump {
  fS response = botAnswerWord();
  infoBox(response);
  thread { sleep(delayBeforeRecognition); sendOpt("Chrome Speech.", "start recognition timeout " + recognitionTimeout); }
  thread { sendOpt("Mouth.", response); }
}

answer {
  if "listen to hot word" { if (paused) startStop(); ret "OK"; }
  if "don't listen to hot word" { if (!paused) startStop(); ret "OK"; }
}

Author comment

Began life as a copy of #1009577

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv

No comments. add comment

Snippet ID: #1009578
Snippet name: Linux: Hot Word Detection ["Computer!"] + "Eve" Icon - using PocketSphinx + Python wrapper
Eternal ID of this version: #1009578/83
Text MD5: df5bdc785b81a9b59c953a9172ea03bd
Transpilation MD5: 7d0b7081e9d435b7200d79b1df3cf617
Author: stefan
Category: javax / speech
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-19 19:21:54
Source code size: 3643 bytes / 133 lines
Pitched / IR pitched: No / No
Views / Downloads: 768 / 1820
Version history: 82 change(s)
Referenced in: [show references]