// file: extf_write_0.cc
//
// this function prints out the feature data in either ascii or binary
//
// the order is c1 c2 .. cn c0 log-energy
//

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

// isip include files
//
#include "extract_feature.h"
#include "extract_feature_constants.h"
#include <signal_constants.h>

logical_1 Extract_feature::write_cc(float_8** vectors_a, int_4 num_frames_a) {

  int mfcc_start_pos;
  mfcc_start_pos = include_energy_d ? 1 : 0;

  int_4 size = 0;
  if (c0_d)
    size = vector_first_d + 1;
  else
    size = vector_first_d;
  
      
  // output the full vectors
  //
  if (output_mode_d == EXTF_OUT_MODE_ASCII) {
    FILE* fp = fopen((char*)output_file_d, "w");
    for (int_4 i=0; i < num_frames_a; i++) {

      // loop over all features
      //
      if (include_energy_d == ISIP_TRUE) {
	// start at 1: skip energy
	//
	for (int_4 j=1; j < size; j++) {
	    fprintf(fp,"%5.3f  ", vectors_a[i][j]);
        }
	// write energy
	//
        fprintf(fp,"%5.3f  ", vectors_a[i][0]);
      }
      else {
	for (int_4 j=0; j < size; j++) {
	    fprintf(fp,"%5.3f  ", vectors_a[i][j]);
	}
      }
      
      // write the delta features if desired
      //
      if (delta_d == ISIP_TRUE) {
	
	fprintf(fp, "     ");
	for (int_4 j = size + mfcc_start_pos; j < 2 * size;
	     j++) {
	  fprintf(fp,"%5.3f  ", vectors_a[i][j]);
	}

	// write energy's delta
	//
	if (include_energy_d == ISIP_TRUE) {	
	  fprintf(fp,"%5.3f  ", vectors_a[i][size]);
	}
      }

      // write acceleration coefficients if desired
      //
      if (delta_delta_d == ISIP_TRUE) {
	
	fprintf(fp, "     ");
	for (int_4 j = 2 * size + mfcc_start_pos; j < 3 * size;
	     j++) {
	  fprintf(fp,"%5.3f  ", vectors_a[i][j]);
	}

	// write energy's delta-delta
	//
	if (include_energy_d == ISIP_TRUE) {	
	  fprintf(fp,"%5.3f", vectors_a[i][2 * size]);
	}
      }

      fprintf(fp, "\n");
    }
    // clean up
    //
    fclose(fp);
  }
  else if (output_mode_d == EXTF_OUT_MODE_BINA) {
    
    FILE* fp = fopen((char*)output_file_d, "wb");
    for (int_4 i=0; i < num_frames_a; i++) {
      //   fwrite(&i, sizeof(int_4), 1, fp);
      
      // loop over all features
      //
      if (include_energy_d == ISIP_TRUE) {
	// start at 1: skip energy
	//
	for (int_4 j=1; j < size; j++) {
	     fwrite(&vectors_a[i][j], sizeof(float_8), 1, fp);
         }
	 // write energy
	 //
         fwrite(&vectors_a[i][0], sizeof(float_8), 1, fp);
      }
      else {
	for (int_4 j=0; j < size; j++) {
	     fwrite(&vectors_a[i][j], sizeof(float_8), 1, fp);
        }
      }
      
      // write the delta features if desired
      //
      if (delta_d == ISIP_TRUE) {
	
	for (int_4 j = size + mfcc_start_pos; j < 2 * size;
	     j++) {
	  fwrite(&vectors_a[i][j], sizeof(float_8), 1, fp);
	}

	// write energy's delta
	//
	if (include_energy_d == ISIP_TRUE) {
	  fwrite(&vectors_a[i][size], sizeof(float_8), 1, fp);
	}
      }
      
      // write acceleration coefficients if desired
      //
      if (delta_delta_d == ISIP_TRUE) {
	
	for (int_4 j = 2 * size + mfcc_start_pos; j < 3 * size;
	     j++) {
	  fwrite(&vectors_a[i][j], sizeof(float_8), 1, fp);
	}

	// write energy's delta-delta
	//
	if (include_energy_d == ISIP_TRUE) {
	  fwrite(&vectors_a[i][2 * size], sizeof(float_8), 1, fp);
	}
      }
    }

    // clean up
    //
    fclose(fp);
  }
  
  // return without error
  //
  return ISIP_TRUE;
}
