// $Id: ViaVoice.java,v 1.2 2004/02/25 17:29:18 cadlerun Exp $
// ViaVoice.java -- 
// Author: Aaron Adler <cadlerun@mystic.ai.mit.edu> 
// Copyright: Copyright (C) 2002 by Aaron Adler 
// Created: <Mon Oct 20 14:50:48 2003> 


package rec.multi;

import rec.speech.TimedResult;
import rec.speech.RecListen;

import java.rmi.Naming;
import java.rmi.RemoteException;

import org.apache.log4j.Logger;
import edu.mit.tools.watch.Logging;

/**
 * A simple class that connects to ViaVoice. This runs in a seperate
 * process and communicates with the rest of the system using RMI.
 **/
public class ViaVoice implements SpeechInput, Runnable {

  private boolean m_done = false; // are we done listening for speech input

  public ViaVoice() {
  }

  public void run() {
    while (!m_done) {
      System.out.println("in ViaVoice run loop...");
	
      TimedResult tr = RecListen.getListen().getTimedDictation();
      System.out.println("returned TimedResult");
      
	// don't do anything for an empty speech phrase.
      if ((tr != null) && (tr.getSpeech() != "")) {
	System.out.println("Speech is: "+tr.getSpeech()+" ("+tr.getStartTime()+" - "+tr.getEndTime()+")");
	
	try {
	  ViaVoiceInterface vvi = (ViaVoiceInterface) Naming.lookup("ViaVoiceServer");
	  vvi.incomingSpeech(tr.getSpeech(), tr.getStartTime(), tr.getEndTime());
	} catch (Exception e) {
	  System.out.println("ViaVoice error "+e.getMessage());
	  e.printStackTrace();
	}
      }
    }
  }
  
  /**
   * Abort the listening.
   */
  public void abort() {
    System.out.println("aborting the connection attempt");
    
    m_done = true;
  }

  public static void main(String args[]) {
    ViaVoice vv = new ViaVoice();
    new Thread(vv).start();
  }

}

