// file: comm_frontend_0.cc
//

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

// isip include files
//
#include "extract_feature.h"

// Communicator API
//
#include "verification.h"

Extract_feature vv_frontend1;
Extract_feature vv_frontend2;

// method: init_frontend
//
// arguments: none
//
// return: a logical flag to indicate status
//
// this method initilizes the frontend from parameter file
//
int vv_init_frontend(char* params1_a, char* params2_a) {

  // read the parameter files
  //
  if ((vv_frontend1.read_parameter_cc((char_1*)params1_a) == ISIP_FALSE) ||
      vv_frontend2.read_parameter_cc((char_1*)params2_a) == ISIP_FALSE) {
    
    // output error message and exit
    //
    fprintf(stderr, "Extract_feature : Error reading command line parameters\n");
    
    exit(ISIP_PROTO_ERROR);
  }

  // configure the frontend
  //
  vv_frontend1.config_frontend_cc();
  vv_frontend2.config_frontend_cc();
	
  // return gracefully
  //
  return ISIP_TRUE;
}

// method: uttproc_rawdata
//
// arguments:
//  char* data: (input) feature input buffer
//  int* num_samples: (input) number of feature data
//  int block: (input) pending blocks (currently unused)
//
// return: bool indicating status
//
// this method decodes an utterance specified by the input mfc data.
//
int vv_uttproc_rawdata(short* in_data, int num_samples, int block) {

  // local variables
  //
  int_4 samples1 = num_samples;
  int_4 samples2 = num_samples;

  short *buf_data = new short[num_samples];
  memset(buf_data, 0, num_samples * sizeof(short));
  memcpy(buf_data, &in_data, num_samples * sizeof(short));
  
  // compute the feature data from audio raw data
  //
  float_8* mfc_data1 = vv_frontend1.compute_cc((int_2*&)in_data, samples1,
					       (int_4)block);
  float_8* mfc_data2 = vv_frontend2.compute_cc((int_2*&)buf_data, samples2,
					       (int_4)block);

  //Added by Tang to fix the memory leakage bug
  if (buf_data) delete []buf_data;
  
  // decoding procedure
  //
  if (mfc_data1 != NULL) {
    vv_uttproc_mfcdata((double*)mfc_data1, samples1);
    delete [] mfc_data1;
  }

  if (mfc_data2 != NULL) {
    vv_uttproc_mfcdata((double*)mfc_data2, samples2);
    delete [] mfc_data2;
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}

// method: clean_frontend
//
// arguments:
//  none
//
// return: bool indicating status
//
// this method clean up the frontend before receiving a new utterance
//
int vv_clean_frontend() {
  vv_frontend1.clean_cc();
  vv_frontend2.clean_cc();
  return true;
}
