//  file: $isip/util/isip_play/isip_play.java
// version: $Id: isip_play.java 10349 2006-01-06 14:27:22Z stanley $
//

// import necessary java libraries
//
import java.io.*;
import javax.sound.sampled.*;

/**
 * Presents a simple interface to read and write from a line.
 */
public class isip_play {
    
    // empty constructor
    //
    public isip_play() {
    }
    
    // the native method that is used to open audio files. The native
    // method uses IFC's AudioFile class to read a audio file and
    // returns a float array
    //
    public native float[][] openAudio();

    // the native method that uses the commandline object of the IFCs
    // to parse the commandline parameters passed through the java
    // driver program
    //
    public native void commandlineParse(String args_a[]);

    // as the libraries are dynamic, the read_audio library that has
    // the compiled code for the "openAudio" method is loaded in
    // memory
    //
    static {
        System.loadLibrary("read_audio");
    }
        
    //-------------------------------------------------------------------------
    //
    // main diagnostic method for this class
    //
    //-------------------------------------------------------------------------
    
    /**
     * Diagnostic method for this class
     *
     * @param args arguments from the command line
     */
    public static void main (String args[]) {

	 // create an AudioInterface object
	 //
	 AudioInterface audioInterface = new AudioInterface();
	 
	 // a default format that can be used by the test program
	 //
	 AudioFormat SPEECH = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
					      16000.0F, 
					     16, 1, 2, 16000.0F, false);
	 
	 // create a isip_play object to call the native method
	 //
	 isip_play play_object = new isip_play();
	 float[][] float_array = play_object.openAudio();
	 
	 // testing.. 
	 // creating an one dimensioanl array
	 //
	 float[][] onedim_array = new float [1] [float_array[0].length];
	 for (int i = 0; i < float_array[0].length; i++) {
	     
	     onedim_array [0] [i] = float_array[0][i];
	 }
	 
	 // create an AudioSignal object
	 // 
	 AudioSignal signal = new AudioSignal();
	 
	 // set the audio format to the Signal object
	 //
	 signal.setFormat(SPEECH);
	 
	 // set the audio data which in our case is an one dimensional
	 // object
	 //
	 signal.setData(onedim_array);
	 
	 // switch to write mode
	 //
	 audioInterface.mode_d = audioInterface.WRITE;
	 
	 // set the audio format to the line
	 //
	 if(!(audioInterface.setAudioFormat(SPEECH))) {
	     System.out.println("format not supported");
	 }
	 
	 // create an SourceDataLine object and open the line
	 //
	 audioInterface.open();
	 
	 // start the line
	 //
	 audioInterface.start();
	 
	 // write the signal object to the line
	 //
	 audioInterface.startPlayBack(signal);
	 
	 // sleep for sometime so that playback takes place
	 // 
	 try {
	     Thread.sleep(5000);
	 }
	 catch (Exception e) { }
	 
	 // stop the line
	 //
	 audioInterface.stop();
	 
	 // close the line
	 //
	 audioInterface.close();
     }

}


//COMMANDLINE TO RUN THE JNI STUFF:
//NEEDS TO GO INTO THE GNU-MAKE PROCESS

/*
  export LD_LIBRARY_PATH="/usr/local/gnu/gcc-3.2/lib:/usr/local/groupwise/lib:/usr/local/groupwise/jre/lib/i386:/usr/local/groupwise/jre/lib/i386/client:/usr/openwin/lib:/usr/local/lib:/cavs/hse/ies/d002/isip/users/stanley/research/jni/:-L/cavs/hse/ies/d002/isip/users/stanley/tools/lib/i686-pc-linux-gnu:/cavs/hse/ies/d001/isip/common/local/audiofile/lib -laudiofile -L/cavs/hse/ies/d001/isip/common/local/sphere/lib:/cavs/hse/ies/d001/isip/common/local/communicator/lib/x86-linux:/cavs/hse/ies/d002/isip/users/stanley/tools/lib/i686-pc-linux-gnu"
  
  javac -classpath /cavs/hse/ies/d002/isip/users/stanley/tools/lib/java/libjava.jar:. *.java
  
  javah -jni isip_play
  
  g++ -fPIC -D_REENTRANT -I ../jdk1.5.0_06/include/ -I ../jdk1.5.0_06/include/linux/ -I/cavs/hse/ies/d002/isip/users/stanley/tools/include -I./ -I/cavs/hse/ies/d001/isip/common/local/audiofile/include -I/cavs/hse/ies/d001/isip/common/local/sphere/include -I/cavs/hse/ies/d001/isip/common/local/communicator/include -I/usr/local/mysql/include -I -Wall -ftemplate-depth-50 -DHAVE_AUDIOFILE -DHAVE_SPHERE -DHAVE_COMMUNICATOR -DHAVE_EXPAT -c read_audio.cc 
  
  g++ -shared read_audio.o -o libread_audio.so -L/cavs/hse/ies/d002/isip/users/stanley/tools/lib/i686-pc-linux-gnu -l_asr -l_pr -l_search -l_sp -l_algo -l_stat -l_mmedia -l_shell -l_numeric -l_math_matrix -l_math_vector -l_math_scalar -l_io -l_system -lm -L/cavs/hse/ies/d001/isip/common/local/audiofile/lib -laudiofile -L/cavs/hse/ies/d001/isip/common/local/sphere/lib -lsp -lutil -L/cavs/hse/ies/d001/isip/common/local/communicator/lib/x86-linux -lGalaxy -L -lexpat -L/cavs/hse/ies/d002/isip/users/stanley/tools/lib/i686-pc-linux-gnu -l_dialog_parse -l_dialog_dm -l_dialog_widget -L/usr/local/mysql/lib -lmysqlclient -lz -lcrypt -lm
  
  java -classpath /cavs/hse/ies/d002/isip/users/stanley/tools/lib/java/libjava.jar:. isip_play
  
*/
