import processing.serial.*; Sphinx listener; Scripter script; String s = ""; // variable for sphinx results Serial myPort; // port for receipt printer final int LF = 10; final boolean doprint = false; final boolean dodraw = true; color bc = color(0, 0, 64); ///color bc = color(0, 0, 0); int lastevent = 0; void setup() { size(1024, 768); background(bc); smooth(); // setup printer if necessary if(doprint) setupPrinter(); // setup speech recognition listener = new Sphinx(this,"bochner.config.xml"); lastevent = 0; // for intra-utterance timing // set up scripter script = new Scripter(this, "/Users/rtwomey/processing/sphinxGrammarScripter/data/bochner.txt"); //script.showText(); for(int i=0;i<27;i++) script.advance(); 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() { background(bc); if(dodraw) script.draw(); // check how long it has been silent // doquit(); } void advanceAndCut() { // fast feed paper myPort.write(ESC); myPort.write('d'); myPort.write(8); // feed this many lines // cut paper myPort.write(ESC); myPort.write('i'); } void doQuit() { // advance paper and cut if(doprint) advanceAndCut(); // 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; String[] tokens = split(s,' '); for(int j=0; j=0) { System.out.println("matched: "+tokens[j].substring(i)); // send to printer if necessary if(doprint) { myPort.write(script.getNext()); myPort.write(LF); } script.advance(); nextword=script.getNext().toLowerCase();//.replaceAll("\\W",""); println("looking for: "+nextword+" "+nextword.length()); //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(); } } void keyPressed() { if (key == CODED) if (keyCode == RIGHT) script.advance(); if (key==' ') advanceAndCut(); }