// file: $isip/class/sp/AudioFrontEnd/afnd_04.cc
// version: $Id: afnd_04.cc 8856 2002-12-03 02:54:04Z parihar $
//

// isip include files
//
#include "AudioFrontEnd.h"
#include <Sof.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 AudioFrontEnd::sofSize() const {

  // start with the space required for the audiofiles
  //
  long bytes = FrontEndBase::DATA_TYPE_MAP.elementSofSize();
  bytes += input_flag_d.sofSize();
  bytes += audio_input_d.sofSize();
  bytes += feature_input_d.sofSize();
  
  // add the space required for the signal processing parameters
  //
  bytes += frame_duration_d.sofSize();
  bytes += signal_duration_d.sofSize();
  bytes += number_of_features_d.sofSize();
  bytes += start_time_d.sofSize();
  bytes += end_time_d.sofSize();
  bytes += context_window_d.sofSize();
  bytes += channel_index_d.sofSize();
    
  // add the space required for the component list
  //
  bytes += component_list_d.sofSize();
  
  // add the space required for the coefficient names
  //
  bytes += coef_name_d.sofSize();

  // add the space required for the data mode name
  //
  bytes += DATA_MODE_MAP.elementSofSize();
  
  // 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 AudioFrontEnd::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 be dynamic
    //
    obj_size = Sof::ANY_SIZE;
  }
  else {

    // set the size to be the size of the object written to the Sof file
    //
    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 writes the object to the Sof file. it assumes that the
// Sof file is already positioned correctly
//
boolean AudioFrontEnd::writeData(Sof& sof_a, const String& pname_a) const {
  
  // write a start string if necessary
  //
  sof_a.writeLabelPrefix(pname_a);

  // write parameters related to the type of input
  //
  FrontEndBase::DATA_TYPE_MAP.writeElementData(sof_a, PARAM_INPUT_DATA_TYPE,
					       (long)input_data_type_d);
  input_flag_d.writeData(sof_a, PARAM_INPUT_FLAG);
  audio_input_d.writeData(sof_a, PARAM_AUDIO_INPUT);
  feature_input_d.writeData(sof_a, PARAM_FEATURE_INPUT);

  // write the signal processing parameters
  //
  frame_duration_d.writeData(sof_a, PARAM_FRAME_DURATION);
  signal_duration_d.writeData(sof_a, PARAM_SIGNAL_DURATION);
  number_of_features_d.writeData(sof_a, PARAM_NUMBER_OF_FEATURES);
  start_time_d.writeData(sof_a, PARAM_START_TIME);
  end_time_d.writeData(sof_a, PARAM_END_TIME);
  context_window_d.writeData(sof_a, PARAM_CONTEXT_WINDOW);
  channel_index_d.writeData(sof_a, PARAM_CHANNEL_INDEX);
    
  // write the component list
  //
  component_list_d.writeData(sof_a, PARAM_COMPONENT_LIST);

  // note that the coefficient name (target) has __output_ prepended
  // to it when the object is read in, so we need to undo this when we
  // write it back out
  //
  String write_coef_name;
  if (coef_name_d.firstStr(Recipe::OUTPUT_PREFIX) != 0) {
    return Error::handle(name(), L"writeData", ERR, __FILE__, __LINE__);
  }
  coef_name_d.substr(write_coef_name, Recipe::OUTPUT_PREFIX.length());
  write_coef_name.writeData(sof_a, PARAM_COEF_NAME);

  // write the data processing mode
  //
  AlgorithmBase::DMODE_MAP.writeElementData(sof_a, PARAM_DATA_MODE,
					    (long)data_mode_d);
  
  // put an end string if necessary
  //
  sof_a.writeLabelSuffix(pname_a);
  
  // exit gracefully
  //
  return true;
}
