// Robert Twomey rtwomey@u.washington.edu import processing.serial.*; // voice recognition variables Sphinx listener; String s = ""; // receipt printer variables Serial myPort; static int LF = 10; static boolean doprint = true; // behavior parameters static int silenttime = 60000; int lastevent = 0; boolean waiting; PImage scriptImg; PFont font; void setup() { size(screen.width, screen.height-100); //size(800, 600); background(0); font = loadFont("Serif-48.vlw"); textFont(font); scriptImg = loadImage("solipsist text.png"); // setup printer if necessary if(doprint) setupPrinter(); // setup speech recognition // NOTE: you need to set the appropriate audio input in microphone section of the config file // do this with a statement with appropriate value. // read more here: http://cmusphinx.sourceforge.net/sphinx4/doc/Sphinx4-faq.html#microphone_selection listener = new Sphinx(this,"bochner.config.xml"); // set-up intra-utterance timing lastevent = 0; waiting = true; } 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() { listener.dispose(); } void draw() { background(0); image(scriptImg, 0, 0, width*0.9, height*0.9); text(s, 0, height*0.95);//scriptImg.height+100); // check how long it has been silent // if(!waiting && ((millis()-lastevent) > silenttime)) { // // advance and cut, set state to waiting for new input // advanceAndCut(); // waiting = true; // } } 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(); waiting = false; s = _l.readString(); // returns the recognized string // echo to screen // System.out.print("["+now+"] sphinx heard: "+s); // intra-utterance timing if(s.length() > 7) { // send to printer if necessary if(doprint) { myPort.write(s); myPort.write(LF); } } else { System.out.print(" TOO SHORT!"); s+=" TOO SHORT!"; } System.out.print("["+now+"] "+s); System.out.println(" ("+(now-lastevent)+" since last utterance)"); lastevent=now; // check for stop command if((s.indexOf("quit") >= 0) || (s.indexOf("exit") >= 0) || ((s.indexOf("stop") >= 0) && (s.indexOf("dystopian") < 0))) { doQuit(); } if(s.lastIndexOf("cut")>=0) advanceAndCut(); } void keyPressed() { if(key==' ') advanceAndCut(); if(key=='q') doQuit(); }