/* Copyright (c) 2008 Charles Rich and Worcester Polytechnic Institute.
 * All Rights Reserved.  Use is subject to license terms.  See the file
 * "license.terms" for information on usage and redistribution of this
 * file and for a DISCLAIMER OF ALL WARRANTIES.
 * 
 * Refactored out UI to SpeechUI by willie
 */
package edu.wpi.cetask.guide;

import java.io.IOException;

import edu.wpi.cetask.TaskEngine;
import edu.wpi.cetask.TaskModel;
import edu.wpi.cetask.Utils;

public class SpeechGuide extends Guide {

	final protected SpeechUI speech;
	
	   public static void main (String[] args) {
		      VERSION += " - Speech";
		      SpeechGuide guide = new SpeechGuide(args.length > 0 ? args[0] : null);
		      //if ( args.length > 0 ) guide.mute = true;
		      guide.loop();
		   }

	   public SpeechGuide(String log) {
		   super(log);
		   speech = new SpeechUI(new Sphinx4(), new FreeTTS());
		   uiList.add(speech);
	   }
	   	   
	   // two new commands

	   public void ptt (String ignore) throws Throwable { 
	      String utterance = speech.recognizer.recognize();
	      if ( utterance == null || utterance.length() == 0 ) respond("@repeat");
	      else {
	         super.println("say "+utterance);
	         say(utterance);
	      }
	   }
	   
	   public void say (String utterance) throws Throwable {
	      // for this guide, semantics is simply a command string
	      String command = (String) speech.recognizer.interpret(utterance);
	      if ( command == null ) respond("@rephrase");
	      else {
	         if ( TaskEngine.DEBUG ) println("# "+command);
	         processCommand(command);
	      }
	   }
	   
	   @Override
	   protected void help () {
	      super.help();
	      System.out.println("    ptt                 - push to talk [control key]");
	      System.out.println("    say <utterance>     - natural language text input");
	   }
}
