// file: $isip/class/mmedia/FeatureFile/ftrf_09.cc
// version: $Id: ftrf_09.cc 10636 2007-01-26 22:18:09Z tm334 $
//

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

// method: readSofData
//
// arguments:
//  Vector<AlgorithmData>& data: (output) the audio data
//  long start_pos: (input) the start index of the features data
//  long num_elem: (input) the number of features data
//  
// return: number of features read
//
// this method gets data from an Sof file and returns each
// channel as a VectorFloat.
//
long FeatureFile::readSofData(Vector<AlgorithmData>& data_a,
			      long start_pos_a, long num_elem_a) {
  
  // read from sof file
  //
  if (file_format_d != SOF) {
    Error::handle(name(), L"readSofData", ERR, __FILE__, __LINE__);
    return -1;
  }

  // read the data
  //
  readPartialData(start_pos_a, num_elem_a);

  // copy from partially read internal data
  //
  data_a = v_d;

  /*
  // loop over all elements
  //
  long len = v_d.length();
  data_a.setLength(len);

  for (long i = 0; i < len; i++) {
    
    // transfer the data
    //
    data_a(i).makeVectorFloat().setLength(v_d(i).getVectorFloat().length());
   
    for (long j = 0; j < v_d(i).getVectorFloat().length(); j++) {
      data_a(i).getVectorFloat()(j) = v_d(i).getVectorFloat()(j);
    }
    }
  */
  
  
  // return the number of samples read
  //
  return v_d.length();
}

// method: writeSofData
//
// arguments:
//  Vector<AlgorithmData>& data: (input) the audio data to write
//  long ctag: (input) channel tag
//  
// return: the number of elements written
//
// this method writes audio data to an Sof file.
//
long FeatureFile::writeSofData(Vector<AlgorithmData>& data_a,
			       long ctag_a) {
  
  // check the format
  //
  if (file_format_d != SOF) {
    return Error::handle(name(), L"writeSofData", ERR, __FILE__, __LINE__);
  }

  v_d = data_a;

  /*
  // loop over all elements
  //
  long len = data_a.length();
  v_d.setLength(len);

  for (long i = 0; i < len; i++) {

    // transfer the data
    //
    v_d(i).makeVectorFloat().setLength(data_a(i).getVectorFloat().length());
   
    for (long j = 0; j < data_a(i).getVectorFloat().length(); j++) {
      v_d(i).getVectorFloat()(j) = data_a(i).getVectorFloat()(j);
    }
  }
  */
  
  // write the data
  //
  long len = data_a.length();
  writePartialData(0, len);
  
  // exit gracefully
  //
  return len;
}

/*
// method: readSofData
//
// arguments:
//  Vector<VectorFloat>& data: (output) the audio data
//  long ctag: (input) channel index
//  long start_pos: (input) the start index of the features data
//  long num_elem: (input) the number of features data
//  
// return: number of features read
//
// this method gets data from an Sof file and returns each
// channel as a VectorFloat.
//
long FeatureFile::readSofData(Vector<VectorFloat>& data_a, long ctag_a,
			      long start_pos_a, long num_elem_a) {
  
  // read from sof file
  //
  if (file_format_d != SOF) {
    Error::handle(name(), L"readSofData", ERR, __FILE__, __LINE__);
    return -1;
  }

  // read the data
  //
  readPartialData(start_pos_a, num_elem_a);

  // loop over all elements
  //
  long len = v_d.length();
  data_a.setLength(len);

  for (long i = 0; i < len; i++) {
    
    // transfer the data
    //
    data_a(i).setLength(v_d(i).getVectorFloat().length());
   
    for (long j = 0; j < v_d(i).getVectorFloat().length(); j++) {
      data_a(i)(j) = v_d(i).getVectorFloat()(j);
    }
  }
  
  // return the number of samples read
  //
  return len;
}

// method: writeSofData
//
// arguments:
//  Vector<VectorFloat>& data: (input) the audio data to write
//  long ctag: (input) channel tag
//  
// return: the number of elements written
//
// this method writes the audio data to an Sof file.
//
long FeatureFile::writeSofData(Vector<VectorFloat>& data_a,
			       long ctag_a) {
  
  // check the format
  //
  if (file_format_d != SOF) {
    return Error::handle(name(), L"writeSofData", ERR, __FILE__, __LINE__);
  }

  // loop over all the elements
  //
  long len = data_a.length();
  v_d.setLength(len);

  for (long i = 0; i < len; i++) {

    // transfer the data
    //
    v_d(i).makeVectorFloat().setLength(data_a(i).length());
   
    for (long j = 0; j < data_a(i).length(); j++) {
      v_d(i).getVectorFloat()(j) = data_a(i)(j);
    }
  }
  
  // write the data
  //
  writePartialData(0, len);
  
  // exit gracefully
  //
  return len;
}
*/

// method: readSofStart
//
// arguments: none
//  
// return: a boolean value indicating status
//
// this method initializes reading of data from an Sof file.
//
boolean FeatureFile::readSofStart() {
  
  if (!read(in_sof_d, tag_d)) {
    return Error::handle(name(), L"readSofStart",
			 Error::READ, __FILE__, __LINE__, Error::WARNING);
  }

  if (file_format_d != SOF) {
    return Error::handle(name(), L"readSofStart",
			 Error::READ, __FILE__, __LINE__, Error::WARNING);
  }
  
  if (no_data_d) {
    return Error::handle(name(), L"readSofStart",
			 Error::READ, __FILE__, __LINE__);
  }
  
  // save the object size from the temporary placeholder
  //
  long object_size = sof_length_pos_d;

  // now set the sof_length_pos_d flag to the start of the object
  //
  sof_length_pos_d = in_sof_d.tell();
  
  if (data_type_d == ALGORITHM_DATA) {

    // start the read
    //
    Vector <AlgorithmData> vec;
    
    if (!vec.readStart(in_sof_d, PARAM_DATA, object_size, false, false)) {
      return Error::handle(name(), L"readSofStart", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }

  else if (data_type_d == VECTOR_FLOAT) {
    
    // start the read
    //
    Vector<VectorFloat> vec;

    if (!vec.readStart(in_sof_d, PARAM_DATA, object_size, false, false)) {
      return Error::handle(name(), L"readSofStart", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }  

  // exit gracefully
  //
  return true;
}

// method: writeSofStart
//
// arguments: none
//  
// return: a boolean value indicating status
//
// this method initializes writing of data to an Sof file.
//
boolean FeatureFile::writeSofStart() {
  
  // write the object configuration to the Sof file.
  //
  if (!write(in_sof_d, tag_d)) {
    return Error::handle(name(), L"writeSofStart", Error::WRITE,
			 __FILE__, __LINE__);
  }

  // initialize sof pointers
  //
  boolean status = false;
  
  if (data_type_d == ALGORITHM_DATA) {

    // now write a zero-length vector to the file
    //
    Vector<AlgorithmData> vec(1);
    status = vec.writeStart(in_sof_d, PARAM_DATA);
  }

  else if (data_type_d == VECTOR_FLOAT) {

    // now write a zero-length vector to the file
    //
    Vector<VectorFloat> vec(1);
    status = vec.writeStart(in_sof_d, PARAM_DATA);
  }

  // set the sof pointers
  //
  sof_length_pos_d = in_sof_d.getStartPos();

  // exit gracefully
  //
  return status;
}

