// file: extf_write_0.cc
//
// this function prints out the feature data in either ascii or binary
//

// 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*& out_a, float_8** vectors_a, int_4 num_frames_a) {

  int index = 0;
  
  // output the full vectors
  //
  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 < vector_first_d; j++) {
	out_a[index++] = vectors_a[i][j];
      }
      // write energy
      //
      out_a[index++] = vectors_a[i][0];
    }
    else {
      for (int_4 j=0; j < vector_first_d; j++) {
	out_a[index++] = vectors_a[i][j];
      }
    }
    
      // write the delta features if desired
    //
    if (delta_d == ISIP_TRUE) {
      
      for (int_4 j = vector_first_d + 1; j < 2*vector_first_d;
	   j++) {
	out_a[index++] = vectors_a[i][j];
      }
      out_a[index++] = vectors_a[i][vector_first_d];
    }
    
    // write acceleration coefficients if desired
    //
    if (delta_delta_d == ISIP_TRUE) {
      
      for (int_4 j = 2 * vector_first_d + 1; j < 3 * vector_first_d;
	   j++) {
	  out_a[index++] = vectors_a[i][j];
      }
      out_a[index++] = vectors_a[i][2*vector_first_d];
      }
  }

  // output feature data to a file if requested
  //
  if (output_mode_d == EXTF_OUT_MODE_ASCII) {
    FILE* fp = fopen((char*)output_file_d, "w");
    if (fp == NULL) return ISIP_TRUE;
    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 < vector_first_d; 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 < vector_first_d; 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 = vector_first_d + 1; j < 2 * vector_first_d;
	     j++) {
	  fprintf(fp,"%5.3f  ", vectors_a[i][j]);
	}
	fprintf(fp,"%5.3f  ", vectors_a[i][vector_first_d]);
      }

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

      fprintf(fp, "\n");
    }
    // clean up
    //
    fclose(fp);
  }
  else if (output_mode_d == EXTF_OUT_MODE_BINA) {
    
    FILE* fp = fopen((char*)output_file_d, "wb");
    if (fp == NULL) return ISIP_TRUE;
    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 < vector_first_d; 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 < vector_first_d; 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 = vector_first_d + 1; j < 2 * vector_first_d;
	     j++) {
	  fwrite(&vectors_a[i][j], sizeof(float_8), 1, fp);
	}
	fwrite(&vectors_a[i][vector_first_d], sizeof(float_8), 1, fp);
      }
      
      // write acceleration coefficients if desired
      //
      if (delta_delta_d == ISIP_TRUE) {
	
	for (int_4 j = 2 * vector_first_d + 1; j < 3 * vector_first_d;
	     j++) {
	  fwrite(&vectors_a[i][j], sizeof(float_8), 1, fp);
	}
	fwrite(&vectors_a[i][2 * vector_first_d], sizeof(float_8), 1, fp);
      }
    }

    // clean up
    //
    fclose(fp);
  }

  // return without error
  //
  return ISIP_TRUE;
}
