// file: $isip_ifc/class/sp/SignalDetector/sigd_04.cc
// version: $Id: sigd_04.cc 10210 2005-08-12 20:24:51Z stanley $
//

// isip include files
//
#include "SignalDetector.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 SignalDetector::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();
  
  // add the space required for number of channels
  //
  bytes += num_channels_d.sofSize();
  
  // add the space required for the sample frequency
  //
  bytes += sample_frequency_d.sofSize();
  
  // add the space required for number of bytes per samples
  //
  bytes += sample_num_bytes_d.sofSize();
  
  // add the space required for the channel to be processed
  //
  bytes += channel_to_be_processed_d.sofSize();
  
  // add the space required for the frame duration
  //
  bytes += frame_duration_d.sofSize();
  
  // add the space required for the window duration
  //
  bytes += window_duration_d.sofSize();
  
  // add the space required for the preemphasis
  //
  bytes += preemphasis_d.sofSize();
  
  // add the space required for the pad time
  //
  bytes += pad_time_d.sofSize();
  
  // add the space required for the nominal signal level
  //
  bytes += signal_nominal_level_d.sofSize();
  
  // add the space required for the signal adaptation delta
  //
  bytes += signal_adaptation_delta_d.sofSize();
  
  // add the space required for the signal adaptation constant
  //
  bytes += signal_adaptation_constant_d.sofSize();
  
  // add the space required for the nominal noise level
  //
  bytes += noise_nominal_level_d.sofSize();
  
  // add the space required for the noise adaptation delta
  //
  bytes += noise_adaptation_delta_d.sofSize();
  
  // add the space required for the noise adaptation constant
  //
  bytes += noise_adaptation_constant_d.sofSize();
  
  // add the space required for the noise floor
  //
  bytes += noise_floor_d.sofSize();
  
  // add the space required for the  minimum utterance duration
  //
  bytes += utterance_minimum_duration_d.sofSize();
  
  // add the space required for the minimum utterance separation 
  //
  bytes += utterance_maximum_duration_d.sofSize();
  
  // add the space required for maximum utterance separation
  //
  bytes += utterance_minimum_separation_d.sofSize();  
  
  // add the space required for the utterance delta 
  //
  bytes += utterance_delta_d.sofSize();
  
  // add the space required for the zc utt thresh
  //
  bytes += zc_utterance_thresh_d.sofSize();
  
  // add the space required for the pzc thresh
  //
  bytes += zc_negative_thresh_d.sofSize();
  
  // add the space required for nzc thresh
  //
  bytes += zc_positive_thresh_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 SignalDetector::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 SignalDetector::writeData(Sof& sof_a, const String& pname_a) const {
  
  // 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);
  
  // write parameters related to the number of channels
  // 
  num_channels_d.writeData(sof_a, PARAM_NUM_CHANNELS);
  
  // write parameters related to the sample frequency
  // 
  sample_frequency_d.writeData(sof_a, PARAM_SAMPLE_FREQUENCY);
  
  // write parameters related to the number of bytes per samples
  // 
  sample_num_bytes_d.writeData(sof_a, PARAM_SAMPLE_NUM_BYTES);
  
  // write parameters related to the channel to be processed
  // 
  channel_to_be_processed_d.writeData(sof_a,  PARAM_CHANNEL_TO_BE_PROCESSED);
  
  // write parameters related to the frame duration
  // 
  frame_duration_d.writeData(sof_a, PARAM_FRAME_DURATION);
  
  // write parameters related to the window duration
  // 
  window_duration_d.writeData(sof_a, PARAM_WINDOW_DURATION);
  
  // write parameters related to the preemphasis
  // 
  preemphasis_d.writeData(sof_a, PARAM_PREEMPHASIS);
  
  // write parameters related to the pad time
  // 
  pad_time_d.writeData(sof_a, PARAM_PAD_TIME);
  
  // write parameters related to nominal signal level
  // 
  signal_nominal_level_d.writeData(sof_a, PARAM_SIGNAL_NOMINAL_LEVEL);
  
  // write parameters related to the tsignal adaptation delta
  // 
  signal_adaptation_delta_d.writeData(sof_a, PARAM_SIGNAL_ADAPTATION_DELTA);
  
  // write parameters related to the signal adaptation constant
  // 
  signal_adaptation_constant_d.writeData(sof_a, PARAM_SIGNAL_ADAPTATION_CONSTANT);
  
  // write parameters related to the nominal noise level
  // 
  noise_nominal_level_d.writeData(sof_a, PARAM_NOISE_NOMINAL_LEVEL);
  
  // write parameters related to the noise adaptation delta
  // 
  noise_adaptation_delta_d.writeData(sof_a, PARAM_NOISE_ADAPTATION_DELTA);
  
  // write parameters related to the noise adaptation constant
  // 
  noise_adaptation_constant_d.writeData(sof_a, PARAM_NOISE_ADAPTATION_CONSTANT);
  
  // write parameters related to the noise floor
  // 
  noise_floor_d.writeData(sof_a, PARAM_NOISE_FLOOR);
  
  // write parameters related to the minimum utterance duration
  // 
  utterance_minimum_duration_d.writeData(sof_a, PARAM_UTTERANCE_MINIMUM_DURATION);
  
  // write parameters related to the maximum utterance separation
  // 
  utterance_maximum_duration_d.writeData(sof_a, PARAM_UTTERANCE_MAXIMUM_DURATION);
  
  // write parameters related to the minimum utterance separation 
  // 
  utterance_minimum_separation_d.writeData(sof_a, PARAM_UTTERANCE_MINIMUM_SEPARATION);
  
  // write parameters related to the utterance delta 
  // 
  utterance_delta_d.writeData(sof_a, PARAM_UTTERANCE_DELTA);
  
  // write parameters related to the zc utt thresh
  // 
  zc_utterance_thresh_d.writeData(sof_a, PARAM_ZC_UTTERANCE_THRESH);
  
  // write parameters related to the pzc thresh
  // 
  zc_negative_thresh_d.writeData(sof_a, PARAM_ZC_NEGATIVE_THRESH);
  
  // write parameters related to the nzc thresh
  // 
  zc_positive_thresh_d.writeData(sof_a, PARAM_ZC_POSITIVE_THRESH);
  
  // write the debug level 
  //
  debug_level_d.writeData(sof_a, PARAM_DBGL);
  
  // put an end string if necessary
  //
  sof_a.writeLabelSuffix(pname_a);
  
  // exit gracefully
  //
  return true;
}
