import processing.serial.*; Sphinx listener; Scripter script; String s = ""; // variable for sphinx results Serial myPort; // port for receipt printer static int LF = 10; static boolean doprint = false; int lastevent = 0; void setup() { size(400, 400); background(0); // setup printer if necessary if(doprint) setupPrinter(); // setup speech recognition listener = new Sphinx(this,"sphinx.config.xml"); lastevent = 0; // for intra-utterance timing // set up scripter script = new Scripter(this, "/Users/rtwomey/script/script.txt"); //script.showText(); System.out.println("first token: "+script.getNext()); } void setupPrinter() { // serial init println(Serial.list()); // I know that the first port in the serial list on my mac // is always my Keyspan adapter, so I open Serial.list()[0]. // Open whatever port is the one you're using. myPort = new Serial(this, Serial.list()[0], 4800); //init receipt printer myPort.write(ESC); myPort.write("@"); // fast feed myPort.write(ESC); myPort.write('d'); myPort.write(8); // line feed myPort.write(LF); // cut paper myPort.write(ESC); myPort.write('i'); delay(1000); } void dispose() { // clean up listener threads listener.dispose(); } void draw() { stroke(255); // check how long it has been silent // doquit(); } void doQuit() { // advance print and cut paper if(doprint) { // fast feed myPort.write(ESC); myPort.write('d'); myPort.write(8); // feed this many lines // cut myPort.write(ESC); myPort.write('i'); } // close and dispose exit(); } void SphinxEvent(Sphinx _l) { int now = millis(); s = _l.readString(); // returns the recognized string // echo to screen // System.out.print("["+now+"] sphinx heard: "+s); System.out.print("["+now+"] "+s); // intra-utterance timing System.out.println(" ("+(now-lastevent)+" since last utterance)"); lastevent=now; // send to printer if necessary if(doprint) { myPort.write(s); myPort.write(LF); } int i = s.indexOf(script.getNext()); if(i>=0) { System.out.println("matched "+s.substring(i)); script.advance(); System.out.println("next: "+script.getNext()); } // check for stop command if((s.indexOf("quit") >= 0) || (s.indexOf("exit") >= 0) || ((s.indexOf("stop") >= 0) && (s.indexOf("dystopian") < 0))) { doQuit(); } }