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

// isip include files
//
#include "SignalDetector.h"
#include <Sof.h>
#include <AudioFile.h>

//------------------------------------------------------------------
// these methods have to be in the same file so they can use the same
// static parser pointer
//------------------------------------------------------------------

// declare a static sof parser pointer
//
static SofParser* parser_d = (SofParser*)NULL;

// method: setParser
//
// arguments:
//  SofParser* parser: (input) sof file object
//
// return: a boolean value indicating status
//
// this method sets the parser from the parent object to be used in
// the next readData method
//
boolean SignalDetector::setParser(SofParser* parser_a) {
  
  // set the parser
  //
  parser_d = parser_a;
  
  // exit gracefully
  //
  return true;
}

// method: read
//
// 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 read itself from an Sof file according
// to the specified name and tag
//
boolean SignalDetector::read(Sof& sof_a, long tag_a, const String& name_a) {
  
  // get the instance of the object from the Sof file
  //
  if (!sof_a.find(name_a, tag_a)) {
    return false;
  }
  
  // read the actual data from the sof file
  //
  return readData(sof_a);
}

// method: readData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//  long size: (input) size in bytes of object (or FULL_OBJECT)
//  boolean param: (input) is the parameter name in the file?
//  boolean nested: (input) are we nested?
//
// return: a boolean value indicating status
//
// this method has the object read itself from an Sof file. it assumes
// that the Sof file is already positioned correctly
//
boolean SignalDetector::readData(Sof& sof_a, const String& pname_a,
				 long size_a, boolean param_a,
				 boolean nested_a) {
  
  // implicit parameter is impossible with composite objects, but
  // nesting can be used
  //
  if (parser_d == (SofParser*)NULL) {
    
    parser_d = new SofParser;
    
    if (nested_a) {
      parser_d->setNest();
    }
    
    // load the parse
    //
    if (!parser_d->load(sof_a, size_a)) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }           
  }
  
  // first clear the object to get rid of all components
  //
  clear();
  
  // get the algorithm name
  //
  if (parser_d->isPresent(sof_a, PARAM_ALGORITHM)) {
    if (!ALGO_MAP.readElementData((long&)algorithm_d, sof_a,
				  PARAM_ALGORITHM,
				  parser_d->getEntry(sof_a, PARAM_ALGORITHM))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    algorithm_d = DEF_ALGORITHM;
  }
  
  // get the implementation
  //
  if (parser_d->isPresent(sof_a, PARAM_IMPLEMENTATION)) {
    if (!IMPL_MAP.readElementData((long&)implementation_d, sof_a,
				  PARAM_IMPLEMENTATION,
				  parser_d->getEntry(sof_a,
						     PARAM_IMPLEMENTATION))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }      
  }
  else {
    implementation_d = DEF_IMPLEMENTATION;
  }
  
  // get the number of channels
  //
  Long num_chan;
  if (parser_d->isPresent(sof_a, PARAM_NUM_CHANNELS)) {
    if (!num_chan.readData(sof_a, PARAM_NUM_CHANNELS,
			   parser_d->getEntry(sof_a, PARAM_NUM_CHANNELS))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    num_chan.assign(DEF_NUM_CHANNELS);
  }
  
  // set the number of channels
  //
  setNumChannels(num_chan);
  
  Long num;
  if (parser_d->isPresent(sof_a, PARAM_SAMPLE_NUM_BYTES)) {
    if (!num.readData(sof_a, PARAM_SAMPLE_NUM_BYTES,
		      parser_d->getEntry(sof_a, PARAM_SAMPLE_NUM_BYTES))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	
  }
  else {
    num.assign(DEF_SAMPLE_NUM_BYTES);
  }
  setSampleNumBytes(num);
  
  // get the number of byte per samples
  //  
  if (parser_d->isPresent(sof_a, PARAM_CHANNEL_TO_BE_PROCESSED)) {
    if (!channel_to_be_processed_d.readData(sof_a, PARAM_CHANNEL_TO_BE_PROCESSED,
					    parser_d->getEntry(sof_a,
							       PARAM_CHANNEL_TO_BE_PROCESSED))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    channel_to_be_processed_d.assign(DEF_CHANNEL_TO_BE_PROCESSED);
  }
  
  if (parser_d->isPresent(sof_a, PARAM_SAMPLE_FREQUENCY)) {
    if (!sample_frequency_d.readData(sof_a, PARAM_SAMPLE_FREQUENCY,
				     parser_d->getEntry(sof_a,
							PARAM_SAMPLE_FREQUENCY))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    sample_frequency_d.assign(DEF_SAMPLE_FREQUENCY);
  }
  
  // read frame duration
  //
  if (parser_d->isPresent(sof_a, PARAM_FRAME_DURATION)) {
    if (!frame_duration_d.readData(sof_a,
				   PARAM_FRAME_DURATION,
				   parser_d->getEntry(sof_a,
						      PARAM_FRAME_DURATION))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }       
  }
  else {
    frame_duration_d.assign(DEF_FRAME_DURATION);
  }
  
  // get the window duration
  //
  if (parser_d->isPresent(sof_a, PARAM_WINDOW_DURATION)) {
    if (!window_duration_d.readData(sof_a, PARAM_WINDOW_DURATION,
				    parser_d->getEntry(sof_a, PARAM_WINDOW_DURATION),
				    false, false)) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }       
  }
  else {
    window_duration_d.assign(DEF_WINDOW_DURATION);
  }
  
  // preemphasis param put here
  //
  if (parser_d->isPresent(sof_a, PARAM_PREEMPHASIS)) {
    if (!preemphasis_d.readData(sof_a,
				PARAM_PREEMPHASIS,
				parser_d->getEntry(sof_a,
						   PARAM_PREEMPHASIS))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }       
  }
  else {
    preemphasis_d.assign(DEF_PREEMPHASIS);
  }
  
  
  // pad time
  //
  if (parser_d->isPresent(sof_a, PARAM_PAD_TIME)) {
    if (!pad_time_d.readData(sof_a, PARAM_PAD_TIME,
			     parser_d->getEntry(sof_a,
						PARAM_PAD_TIME))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    pad_time_d.assign(DEF_PAD_TIME);
  }
  
  
  // check signal-level related parameters
  //
  
  // nominal signal level
  //
  if (parser_d->isPresent(sof_a, PARAM_SIGNAL_NOMINAL_LEVEL)) {
    if (!signal_nominal_level_d.readData(sof_a, PARAM_SIGNAL_NOMINAL_LEVEL,
					 parser_d->getEntry(sof_a,
							    PARAM_SIGNAL_NOMINAL_LEVEL))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    signal_nominal_level_d.assign(DEF_SIGNAL_NOMINAL_LEVEL);
  }
  
  
  // signal adaptation constant
  //
  if (parser_d->isPresent(sof_a, PARAM_SIGNAL_ADAPTATION_CONSTANT)) {
    if (!signal_adaptation_constant_d.readData(sof_a, PARAM_SIGNAL_ADAPTATION_CONSTANT,
					       parser_d->getEntry(sof_a,
								  PARAM_SIGNAL_ADAPTATION_CONSTANT))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    signal_adaptation_constant_d.assign(DEF_SIGNAL_ADAPTATION_CONSTANT);
  }
  
  
  
  // signal adaptation delta
  //
  if (parser_d->isPresent(sof_a, PARAM_SIGNAL_ADAPTATION_DELTA)) {
    if (!signal_adaptation_delta_d.readData(sof_a, PARAM_SIGNAL_ADAPTATION_DELTA,
					    parser_d->getEntry(sof_a,
							       PARAM_SIGNAL_ADAPTATION_DELTA))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    signal_adaptation_delta_d.assign(DEF_SIGNAL_ADAPTATION_DELTA);
  }
  
  
  // check noise-level related parameters
  //
  
  // nominal noise level
  //
  if (parser_d->isPresent(sof_a, PARAM_NOISE_NOMINAL_LEVEL)) {
    if (!noise_nominal_level_d.readData(sof_a, PARAM_NOISE_NOMINAL_LEVEL,
					parser_d->getEntry(sof_a,
							   PARAM_NOISE_NOMINAL_LEVEL))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    noise_nominal_level_d.assign(DEF_NOISE_NOMINAL_LEVEL);
  }
  
  
  // noise adaptation constant
  //
  if (parser_d->isPresent(sof_a, PARAM_NOISE_ADAPTATION_CONSTANT)) {
    if (!noise_adaptation_constant_d.readData(sof_a, PARAM_NOISE_ADAPTATION_CONSTANT,
					      parser_d->getEntry(sof_a,
								 PARAM_NOISE_ADAPTATION_CONSTANT))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    noise_adaptation_constant_d.assign(DEF_NOISE_ADAPTATION_CONSTANT);
  }
  
  // noise adaptation delta
  //
  if (parser_d->isPresent(sof_a, PARAM_NOISE_ADAPTATION_DELTA)) {
    if (!noise_adaptation_delta_d.readData(sof_a, PARAM_NOISE_ADAPTATION_DELTA,
					   parser_d->getEntry(sof_a,
							      PARAM_NOISE_ADAPTATION_DELTA))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    noise_adaptation_delta_d.assign(DEF_NOISE_ADAPTATION_DELTA);
  }
  
  // noise floor 
  //
  if (parser_d->isPresent(sof_a, PARAM_NOISE_FLOOR)) {
    if (!noise_floor_d.readData(sof_a, PARAM_NOISE_FLOOR,
				parser_d->getEntry(sof_a,
						   PARAM_NOISE_FLOOR))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    noise_floor_d.assign(DEF_NOISE_FLOOR);
  }
  
  // check utterance duration parameters
  //
  
  
  // minimum utterance duration
  //
  if (parser_d->isPresent(sof_a, PARAM_UTTERANCE_MINIMUM_DURATION)) {
    if (!utterance_minimum_duration_d.readData(sof_a, PARAM_UTTERANCE_MINIMUM_DURATION,
					       parser_d->getEntry(sof_a,
								  PARAM_UTTERANCE_MINIMUM_DURATION))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    utterance_minimum_duration_d.assign(DEF_UTTERANCE_MINIMUM_DURATION);
  }
  
  // maximum utterance separation
  //
  if (parser_d->isPresent(sof_a, PARAM_UTTERANCE_MAXIMUM_DURATION)) {
    if (!utterance_maximum_duration_d.readData(sof_a, PARAM_UTTERANCE_MAXIMUM_DURATION,
					       parser_d->getEntry(sof_a,
								  PARAM_UTTERANCE_MAXIMUM_DURATION))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    utterance_maximum_duration_d.assign(DEF_UTTERANCE_MAXIMUM_DURATION);
  }
  
  // minimum utterance separation 
  //
  if (parser_d->isPresent(sof_a, PARAM_UTTERANCE_MINIMUM_SEPARATION)) {
    if (!utterance_minimum_separation_d.readData(sof_a, PARAM_UTTERANCE_MINIMUM_SEPARATION,
						 parser_d->getEntry(sof_a,
								    PARAM_UTTERANCE_MINIMUM_SEPARATION))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    utterance_minimum_separation_d.assign(DEF_UTTERANCE_MINIMUM_SEPARATION);
  }
  
  // utterance delta
  //
  if (parser_d->isPresent(sof_a, PARAM_UTTERANCE_DELTA)) {
    if (!utterance_delta_d.readData(sof_a, PARAM_UTTERANCE_DELTA,
				    parser_d->getEntry(sof_a,
						       PARAM_UTTERANCE_DELTA))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    utterance_delta_d.assign(DEF_UTTERANCE_DELTA);
  }
  
  
  // check for zc related parameters
  //
  
  // zc utt thresh
  //
  if (parser_d->isPresent(sof_a, PARAM_ZC_UTTERANCE_THRESH)) {
    if (!zc_utterance_thresh_d.readData(sof_a, PARAM_ZC_UTTERANCE_THRESH,
					parser_d->getEntry(sof_a,
							   PARAM_ZC_UTTERANCE_THRESH))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    zc_utterance_thresh_d.assign(DEF_ZC_UTTERANCE_THRESH);
  }
  
  // pzc thresh
  //
  if (parser_d->isPresent(sof_a, PARAM_ZC_POSITIVE_THRESH)) {
    if (!zc_positive_thresh_d.readData(sof_a, PARAM_ZC_POSITIVE_THRESH,
				       parser_d->getEntry(sof_a,
							  PARAM_ZC_POSITIVE_THRESH))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    zc_positive_thresh_d.assign(DEF_ZC_POSITIVE_THRESH);
  }
  
  
  // nzc thresh
  //
  if (parser_d->isPresent(sof_a, PARAM_ZC_NEGATIVE_THRESH)) {
    if (!zc_negative_thresh_d.readData(sof_a, PARAM_ZC_NEGATIVE_THRESH,
				       parser_d->getEntry(sof_a,
							  PARAM_ZC_NEGATIVE_THRESH))) {
      return Error::handle(name(), L"readData", Error::READ,
			   __FILE__, __LINE__, Error::WARNING);
    }	      
  }
  else {
    zc_negative_thresh_d.assign(DEF_ZC_NEGATIVE_THRESH);
  }
  
  
  // get the debug level
  //
  if (parser_d->isPresent(sof_a, PARAM_DBGL)) {
    if (!debug_level_d.readData(sof_a, PARAM_DBGL,
				parser_d->getEntry(sof_a, PARAM_DBGL))) {
      
      // delete the parser
      //
      delete parser_d;
      parser_d = (SofParser*)NULL;
      
      // return a warning message
      //
      return Error::handle(name(), L"readData", Error::IO,
			   __FILE__, __LINE__, Error::WARNING);
    }
  }
  else {
    debug_level_d = Integral::DEF_DEBUG;
  }
  
  // exit gracefully
  //
  return true;
}
