// file: extract_feature/extf_gp_0.cc
//
// a simple command line parser
//

// system include files
//
#include <string.h>

// local include files
//
#include "extract_feature.h"
#include "extract_feature_constants.h"

// function: get_parameters_cc
//
// arguments:
//
//  int argc: (input) argc from the main program
//  char** argv: (input) argv from the main program
//
// return value: a logical_1 indicating status
//
// this function parses command line arguments
//
logical_1 Extract_feature::get_parameter_cc(int_4 argc, char_1** argv) {

  // local variable
  //
  logical_1 if_set_fbanks = ISIP_FALSE;
  
  // loop over all of the arguments and set the appropriate variables
  //
  for (int_4 index = 1; index < argc; index++) {

    if (strcmp((char*)EXTF_OPT_WINDOW_TYPE, (char*)argv[index]) == (int)0) {

      // set the flag to true
      //
      strcpy((char*)window_type_d, (char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_PRE_EMPH_COEFF, (char*)argv[index]) == (int)0) {

      // set the flag to true
      //
      use_pre_emph_d = ISIP_TRUE;
      pre_emph_coeff_d = (float_8)atof((char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_LIFTER_COEFF, (char*)argv[index]) == (int)0) {

      // set the flag to true
      //
      use_liftering_d = ISIP_TRUE;
      lifter_coeff_d = (float_8)atof((char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_FRAME_DUR, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      frame_dur_d = (float_8)atof((char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_WINDOW_DUR, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      window_dur_d = (float_8)atof((char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_SF, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      sample_freq_d = (float_8)atof((char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_NUM_CHANS, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      num_chans_d = (int_4)atoi((char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_CHAN_NUM, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      channel_d = (int_4)atoi((char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_SWAP, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      swap_byte_d = ISIP_TRUE;
    }

    else if (strcmp((char*)EXTF_OPT_NORMALIZE, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      cepstral_mean_subtract_d = ISIP_TRUE;
    }

    else if (strcmp((char*)EXTF_OPT_OUT_MODE, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      index++;
      if (strcmp((char*)EXTF_OPT_OUT_MODE_ASCII, (char*)argv[index]) == (int)0) {
	output_mode_d = (int_4)EXTF_OUT_MODE_ASCII;
      }
      else if (strcmp((char*)EXTF_OPT_OUT_MODE_BINA, (char*)argv[index]) == (int)0) {
	output_mode_d = (int_4)EXTF_OUT_MODE_BINA;
      }
    }

    else if (strcmp((char*)EXTF_OPT_DELTA_WIN, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      delta_win_d = (int_4)atoi((char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_NUM_FBANKS, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      num_fbanks_d = (int_4)atoi((char*)argv[++index]);
      if_set_fbanks = ISIP_TRUE;
    }

    else if (strcmp((char*)EXTF_OPT_MEL_WIDTH, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      mel_width_d = (float_8)atof((char*)argv[++index]);
    }
    
    else if (strcmp((char*)EXTF_OPT_FILE_IN, (char*)argv[index]) == (int)0) {

      // copy the input filename
      //
      strcpy((char*)input_file_d, (char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_FILE_OUT, (char*)argv[index]) == (int)0) {

      // copy the name of the output files list
      //
      strcpy((char*)output_file_d, (char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_ALGO_MEAN, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      num_coeffs_d[EXTF_ALGO_MEAN]++;
    }

    else if (strcmp((char*)EXTF_OPT_ALGO_FBA, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      num_coeffs_d[EXTF_ALGO_FBA] = (int_4)atoi((char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_ALGO_FFT_MFCC, (char*)argv[index]) == (int)0) {
      
      // copy the output filename
      //
      num_coeffs_d[EXTF_ALGO_FFT_MFCC] = (int_4)atoi((char*)argv[++index]);
    }

    else if (strcmp((char*)EXTF_OPT_C0, (char*)argv[index]) == (int)0) {

      // set the c0 flag
      //
      c0_d = ISIP_TRUE;
    }

    else if (strcmp((char*)EXTF_OPT_DELTA, (char*)argv[index]) == (int)0) {

      // set the delta flag
      //
      delta_d = ISIP_TRUE;
    }

    else if (strcmp((char*)EXTF_OPT_DELTA_DELTA, (char*)argv[index]) == (int)0) {

      // set the flag true
      //
      delta_delta_d = ISIP_TRUE;

      // delta is needed for delta_delta
      //
      delta_d = ISIP_TRUE;
    }

    else if (strcmp((char*)EXTF_OPT_ZSM, (char*)argv[index]) == 0) {

      zero_mean_d = ISIP_TRUE;
    }

    else if (strcmp((char*)EXTF_OPT_EN, (char*)argv[index]) == 0) {

      energy_norm_d = ISIP_TRUE;
    }

    // check for help: display the help screen
    //
    else if (strcmp((char*)EXTF_OPT_HELP, (char*)argv[index]) == 0) {

      // allocate memory for the command string
      //
      char_1 command_string[ISIP_MAX_STRING_LENGTH];

      // create the command line to display the help file
      //
      sprintf((char*)command_string, (char*)ISIP_HELP_FORMAT,
	      (char*)EXTF_HELP_FILE);

      // display the help file
      //
      system((char*)command_string);
      
      // exit gracefully
      //
      return ISIP_FALSE;
    }
    
    // check if we need do VTLN
    //
    else if (strcmp((char*)EXTF_OPT_VTLN, (char*)argv[index]) == 0) {

      vtln_d = ISIP_TRUE;
    }

    else if (strcmp((char*)EXTF_OPT_NON_VTLN, (char*)argv[index]) == 0) {

      vtln_d = ISIP_FALSE;
    }

    // check for normalization factor
    else if (strcmp((char*)EXTF_OPT_VTLN_FACTOR, (char*)argv[index]) == 0) {

      vtln_factor_d = (float_8)atof((char*)argv[++index]);
    }

    // check for VTLN algorithm
    else if (strcmp((char*)EXTF_OPT_BILINEAR, (char*)argv[index]) == 0) {

      vtln_algo_d = EXTF_BILINEAR;
    }
    
    else {

      // return with failure
      //
      return ISIP_FALSE;
    }
  }

  // check num_fbanks and mel_width sets
  //  if user set num_fbanks, we should ignore mel_width
  //
  if (if_set_fbanks) {
    mel_width_d = (float_8)0;
  }

  // if user specifies mel_width, we need to compute the number of filterbanks
  //
  if (mel_width_d != (float_8)0) {
    float_8 max_mel = 2595 * log10(1 + sample_freq_d/(2*700.0));
    max_mel = max_mel / mel_width_d;
    num_fbanks_d = (int_4) floor(max_mel);
    if ((max_mel - (float_8)num_fbanks_d) < 0.00001) {
      num_fbanks_d--;
    }
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
