// (c) MIT 2003.  All rights reserved.

// -*-Mode: Java-*- 
// $Id: AudioMonitorFrame.java,v 1.2 2003/03/06 00:40:40 moltmans Exp $
// AudioMonitorFrame.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Wed Mar  8 14:24:47 2000> 
// Time-stamp: <2000-03-09 23:01:40 moltmans> 
package rec.speech;

import java.awt.*;
import java.awt.event.*;

public class AudioMonitorFrame extends Frame {
  private double m_level = 0.0;
  private Timeout m_timeout;
  
  public AudioMonitorFrame( int width, int height) 
  {
    super( "Speech Monitor" );

    WindowListener wl = new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
	dispose();
      }
    };

    addWindowListener( wl );
    setSize( width, height );
    show();

//      MouseListener m = new MouseAdapter() {
//        public void mouseClicked( MouseEvent e ) {
//  	setLevel( 0.3);
//        }
//      };
//      addMouseListener( m );

  }

  public void setLevel( double level )
  {
    setLevelInternal( level );
    if ( isListening() ) {
      repaint();
      setTimer();
    }
  }
  
  public synchronized void setLevelInternal( double level ) 
  {
    m_level = level;
  }

  public synchronized boolean isListening() 
  {
    return m_level > 0.0;
  }

  public synchronized double getLevel() 
  {
    return m_level;
  }
  

  private void setTimer() 
  {
    if ( (m_timeout != null) && m_timeout.isAlive() ) {
      return;
    }
    else {
      m_timeout = new Timeout();
      m_timeout.start();
    }
    
  }

  public void paint( Graphics g ) 
  {
    //    System.out.println( "Paint: Active = " + isListening() );
    if ( isListening() ) {
      //      System.out.println( "       GREEN" );      
      g.setColor( Color.green );
    }
    else {
      //      System.out.println( "       RED" );
      g.setColor( Color.red );
    }
	
    g.fillRect(0, 0, getWidth(), getHeight() );
    g.setColor( Color.black );

    String text = "Level: " + getLevel();
    int index = text.indexOf( "." ) + 3;

    if ( index > text.length() )
      index = text.length();
    
    g.drawString( text.substring(0, index),
		  5,
		  getHeight() - g.getFontMetrics().getMaxDescent() - 5);
  }


  // The auto timeout thread class.
  class Timeout extends Thread {
    public void run() {
      try {
	sleep(1000);
      } 
      catch (InterruptedException ex) {
	ex.printStackTrace();
      }
      //      System.out.println( "awake active = " + isListening());
      setLevelInternal( 0.0 );
      repaint();
      //      update( getGraphics() );
      //      System.out.println( "awake active = " + isListening());
      //      repaint();
    }
  }


  public static void main( String[] argv ) 
  {
    AudioMonitorFrame f = new AudioMonitorFrame(150,45);

    //    f.setSize( 150, 45 );
    //    f.show();
    
    WindowListener wl = new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
	System.exit(0);
      }
    };

    f.addWindowListener( wl );
    
    while ( true ) {
      try {
	Thread.sleep(500);
      } 
      catch (InterruptedException ex) {
	ex.printStackTrace();
      }
      //      System.out.println( "SET" );
      f.setLevel( 0.1 );
    }
    
  }
  

}


