// file: $isip/class/algo/FilterBank/fb_04.cc
// version: $Id: fb_04.cc 8389 2002-07-16 02:02:14Z gao $
//

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

// method: sofSize
//
// arguments: none
//
// return: size of object
//
// this method returns the size of the object in the Sof file and is
// used for binary write
//
long FilterBank::sofSize() const {
  
  // start with the space required for the algorithm name
  //
  long bytes = ALGO_MAP.elementSofSize();

  // add the space required for the implementation name
  //
  bytes += IMPL_MAP.elementSofSize();

  // check known algorithms: TIME
  //
  if (algorithm_d == TIME) {
    // add the space required for the computation mode name
    //
    bytes += CMODE_MAP.elementSofSize();
    
    // add the space required for the filters parameter file
    //
    bytes += filters_pfile_d.sofSize();
  }

  // check know algorithm: FREQUENCY
  //
  else {
    
    // add the space required for the input_mode type
    //
    bytes += INPUT_MODE_MAP.elementSofSize();

    // add the space required for the mode name
    //
    bytes += DMODE_MAP.elementSofSize();
    
    // add the space required for the scale name
    //
    bytes += SCALE_MAP.elementSofSize();
    
    // add the space required for the frequency sampling method
    //
    bytes += FREQUENCY_SAMPLING_MAP.elementSofSize();
    
    // add the space required for the order
    //
    bytes += order_d.sofSize();
    
    // add the space required for the filter spacing bandwidth
    //
    bytes += bandwidth_d.sofSize();
  }
  
  // add the space required for the debug level
  //
  bytes += debug_level_d.sofSize();
  
  // return the size
  //
  return bytes;
}

// method: write
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: a boolean value indicating status
//
// this method has the object write itself to an Sof file
//
boolean FilterBank::write(Sof& sof_a, long tag_a, const String& name_a) const {

  // declare local variable
  //
  long obj_size;
  
  // write the instance of the object into the Sof file
  //
  if (sof_a.isText()) {
    
    // set the size to by dynamic
    //
    obj_size = Sof::ANY_SIZE;
  }
  
  else {

    // the size index and the size of each element
    //
    obj_size = sofSize();
  }
  
  // write the object into the sof file's index
  //
  if (!sof_a.put(name_a, tag_a, obj_size)) {
    return false;
  }
  
  // write data and exit gracefully
  //
  return writeData(sof_a);
}

// method: writeData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//
// return: a boolean value indicating status
//
// this method branches on the algorithm name
//
boolean FilterBank::writeData(Sof& sof_a, const String& pname_a) const {
  
  // declare local variables
  //
  boolean status = false;
  
  // write a start string if necessary
  //
  sof_a.writeLabelPrefix(pname_a);

  // write the algorithm name
  //
  ALGO_MAP.writeElementData(sof_a, PARAM_ALGORITHM, (long)algorithm_d);
    
  // write the implementation type
  //
  IMPL_MAP.writeElementData(sof_a, PARAM_IMPLEMENTATION,
			    (long)implementation_d);
  
  // check known algorithms: TIME
  //
  if (algorithm_d == TIME) {
    status = writeDataTime(sof_a, pname_a);
  }

  // check known algorithms: FREQUENCY
  //
  else if (algorithm_d == FREQUENCY) {
    status = writeDataFrequency(sof_a, pname_a);
  }

  // check unsupported algorithms
  //
  else {
    return Error::handle(name(), L"writeData",
			 ERR_UNKALG, __FILE__, __LINE__);
  }

  // write the debug level (from the base class)
  //
  debug_level_d.writeData(sof_a, PARAM_DBGL);

  // put an end string if necessary
  //
  sof_a.writeLabelSuffix(pname_a);
  
  // exit gracefully
  //
  return status;
}

// method: writeDataTime
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//
// return: a boolean value indicating status
//
// this method writes data to a file for algorithm choice of TIME.
// note this method will write the filters parameter file that
// contains the filter coefficients for each filter of the filter bank.
//
boolean FilterBank::writeDataTime(Sof& sof_a, const String& pname_a) const {

  // write the computation mode type
  //
  CMODE_MAP.writeElementData(sof_a, PARAM_CMODE, (long)cmode_d);
  
  // write the filters parameter file
  //
  filters_pfile_d.writeData(sof_a, PARAM_FILTERS_PFILE);

  // exit gracefully
  //
  return true;
}

// method: writeDataFrequency
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//
// return: a boolean value indicating status
//
// this method writes data to a file for the FREQUENCY algorithm
//
boolean FilterBank::writeDataFrequency(Sof& sof_a, const String& pname_a) const {

  // write the input_mode type
  //
  INPUT_MODE_MAP.writeElementData(sof_a, PARAM_INPUT_MODE,
				  (long)input_mode_d);
  
  // write the data mode type
  //
  DMODE_MAP.writeElementData(sof_a, PARAM_DMODE, (long)dmode_d);
  
  // write the scale type
  //
  SCALE_MAP.writeElementData(sof_a, PARAM_SCALE, scale_d);

  // write the frequency sampling method
  //
  FREQUENCY_SAMPLING_MAP.writeElementData(sof_a,
					  PARAM_FREQUENCY_SAMPLING, fsmp_d);

  // write the numbers of filter
  //
  order_d.writeData(sof_a, PARAM_ORDER);

  // write the filter spacing width
  //
  bandwidth_d.writeData(sof_a, PARAM_BANDWIDTH);

  // exit gracefully
  //
  return true;
}
