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

// import the neccessary files
//

// this is provided by Sun and has the neccessary interface for the
// jni stuff
//
#include <jni.h>

// this is the header file which is machine generated using the javah
// command
//
#include "isip_play.h"
#include "AudioFile.h"
#include <stdio.h>


// native method that reads audio files and returns back a flaot array
//
JNIEXPORT jobjectArray JNICALL Java_isip_1play_openAudio(JNIEnv *env,
							jobject obj) {

  // declare AudioFile objects for read
  //
  AudioFile src;

  // need to get it from commandline
  //
  src.setFileFormat(AudioFile::RAW);  // set file format
  src.setFileType(AudioFile::BINARY);  // set file type
  
  // open file, default is read only
  //
  src.open(L"/cavs/hse/ies/d002/isip/users/stanley/tools/doc/examples/data/audio/bg_119oo39a.raw");

  long N = 80; // defined the sample number for one process
  Long len;  // define local variable
  
  len = src.getNumSamples(); // get number of samples in this file
  len = (long)len;      // get how many frames we need to process
      
  // define local variable to hold data
  //
  Vector<VectorFloat> data(1);
    Long len_1 = (long)len - 1;

    // get sample data
    //
    src.getData(data(0), 0, 0, len);
    int len_a = data(0).length();

    // You are strongly adviced to look into the java tutorial on jni
    // in the Sun website to understand how the below code works
    // before nay modifications are done
    //
    
    // java float array
    //
    jfloatArray ba;
    float *p;

    // initialise the float array
    //
    ba =  (env)->NewFloatArray(len_a);

    // get the array as an native array pointer.
    //
    p  = (float *) (env)->GetFloatArrayElements(ba, NULL);

    // java float array
    //
    jfloatArray ba_2;
    float *p_2;
    
    // initialise the float array
    //
    ba_2 =  (env)->NewFloatArray(len_a);

    // get the array as an native array pointer.
    //
    p_2  = (float *) (env)->GetFloatArrayElements(ba_2, NULL); 

    
    for(int i = 0; i < len_a; i++) {
     
     *(p + i) = (float) data(0)(i);
     *(p_2 + i) = (float) data(0)(i);
    }

    // this command making sure the changes done t the native array
    // pointer are reflected in the corresponding java arrays
    //
    (env)->ReleaseFloatArrayElements(ba, (jfloat *) p, 0);
    (env)->ReleaseFloatArrayElements(ba_2, (jfloat *) p_2, 0);

    // put the java arrays in a java "Object" array
    //
    jobjectArray ret=(jobjectArray)env->NewObjectArray(2, env->GetObjectClass(ba), 0);
   env->SetObjectArrayElement(ret,0,ba);
   env->SetObjectArrayElement(ret,1,ba_2);

       
    // close file
    //
    src.close();

    // return the array
    //
    return ret;
}


// the native method that parses the commandline
//
JNIEXPORT jobjectArray JNICALL Java_isip_1play_commandlineParse(JNIEnv *env,
							 jobject obj) {
  // setup the command line
  //
  Sdb sdb;
  CommandLine cmdl(sdb);
  cmdl.setUsage(
#include "usage_message.text"
                );
  cmdl.setHelp(
#include "help_message.text"
               );
  
  // add a command line option for start time
  //
  Float start_time;
  cmdl.addOptionParam(start_time, OPTION_START_TIME, (Filename)EMPTY);

  // add a command line option for page time
  //
  Float page_time;
  cmdl.addOptionParam(page_time, OPTION_PAGE_TIME, (Filename)EMPTY);
  
  // add a command line option for amplitude
  //
  Float amplitude;
  cmdl.addOptionParam(amplitude, OPTION_AMPLITUDE, (Filename)EMPTY);
  
  // add a command line option for number of channels
  //
  Long channels;
  cmdl.addOptionParam(channels,OPTION_CHANNELS, EMPTY);

  // add a command line option for sampling frequency
  //
  Float sampling_frequency;
  cmdl.addOptionParam(sampling_frequency,OPTION_SAMPLING_FREQUENCY, EMPTY);

  // parse the command line
  //
  if (!cmdl.parse(argc, argv)) {
    cmdl.printUsage();
  };

}
