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

// isip include files
//
#include "SignalDetector.h"
#include "AudioFile.h" 

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//


boolean SignalDetector::diagnose(Integral::DEBUG level_a) {
  
  //---------------------------------------------------------------------
  //
  // 0. preliminaries
  //
  //---------------------------------------------------------------------
  
  // output the class name
  //
  if (level_a > Integral::NONE) {
    String output(L"diagnosing class ");
    output.concat(CLASS_NAME);
    output.concat(L": ");
    Console::put(output);
    Console::increaseIndention();
  }
  
  
  //--------------------------------------------------------------------
  //
  // 1. required public methods
  //
  //--------------------------------------------------------------------
  
  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing required public methods...\n");
    Console::increaseIndention();
  }
  
  Sof sof;
  String file;
  file.assign(L"./diagnose_param.sof");
  
  // open and try reading the sof file
  //
  if (!sof.open(file, File::READ_ONLY)) {
    Console::put(L"error opening the file.. \n");
  }
  
  // close the sof file
  //
  sof.close();
  
  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 2. class-specific public methods:
  //
  //--------------------------------------------------------------------------
  
  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing class-specific public methods...\n");
    Console::increaseIndention();
  }
  
  SignalDetector sig_det;
  
  file.assign(L"./diagnose_param.sof");
  
  // try opening the file
  //
  sof.open(file, File::READ_ONLY);
  
  // read the sof file into the signal detector object
  //
  sig_det.read(sof, (long)0);
  
  // initialise the signal detector object
  //
  sig_det.init();
  
  VectorFloat data;
  AudioFile src;
  
  // set the file format
  //
  src.setFileFormat(AudioFile::RAW);  
  
  // set the file type
  //
  src.setFileType(AudioFile::BINARY);  
  
  // set the sample frequency, number of bytes per sample and number
  // of channels
  //
  src.setSampleFrequency(sig_det.getSampleFrequency());
  src.setSampleNumBytes(sig_det.getSampleNumBytes());
  src.setNumChannels(sig_det.getNumChannels());
  
  // open file, default is read only
  //
  src.open(L"./diagnose.raw");
  
  // get number of samples in this file
  //
  long len = src.getNumSamples(); 
  
  
  Vector<VectorFloat> vector_data(1);
  src.getData(vector_data(0), 0, 0, len); 
  sig_det.apply(vector_data);
  
  //the sof file object
  //
  src.close();
  sof.close();
  
  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  // --------------------------------------------------------------------
  //
  // 3. print completion message
  //
  // --------------------------------------------------------------------
  
  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  if (level_a > Integral::NONE) {
    String output(L"diagnostics passed for class ");
    output.concat(name());
    output.concat(L"\n");
    Console::put(output);
  }
  
  // exit gracefully
  //
  return true;
}

