// file: $isip/class/algo/FilterBank/FilterBank.h
// version: $Id: FilterBank.h 10636 2007-01-26 22:18:09Z tm334 $
//

// make sure definitions are only made once
//
#ifndef ISIP_FILTERBANK
#define ISIP_FILTERBANK

// isip include files
//
#ifndef ISIP_ALGORITHM_BASE
#include <AlgorithmBase.h>
#endif

#ifndef ISIP_FILTER
#include <Filter.h>
#endif

#ifndef ISIP_VECTOR
#include <Vector.h>
#endif

#ifndef ISIP_STRING
#include <String.h>
#endif

#ifndef ISIP_LONG
#include <Long.h>
#endif

#ifndef ISIP_FLOAT
#include <Float.h>
#endif

#ifndef ISIP_VECTOR_FLOAT
#include <VectorFloat.h>
#endif

#ifndef ISIP_MEMORY_MANAGER
#include <MemoryManager.h>
#endif

// FilterBank: a class that converts a signal to a multichannel output
// that represents a frequency domain analysis of the signal. several
// techniques for generating and interpreting the spectrum are supported.
//
class FilterBank : public AlgorithmBase {

  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // define algorithm choices
  //
  enum ALGORITHM { FREQUENCY = 0, TIME, DEF_ALGORITHM = FREQUENCY };
  
  // define the implementation choices
  //
  enum IMPLEMENTATION { UNIFORM = 0, TRIANGULAR, RAISED_COSINE, CCDE, DEF_IMPLEMENTATION = UNIFORM };

  // define the scale choices
  //
  enum SCALE { LINEAR = 0, MEL, BARK, DEF_SCALE = LINEAR };

  // define the frequency sampling method
  //
  enum FREQUENCY_SAMPLING { ORDER = 0, BANDWIDTH,
			    DEF_FREQUENCY_SAMPLING = ORDER };

  // define the input mode choices
  //
  enum INPUT_MODE { FULL = 0, SYMMETRIC, DEF_INPUT_MODE = FULL };
  
  // define the static NameMap objects
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;
  static const NameMap SCALE_MAP;
  static const NameMap FREQUENCY_SAMPLING_MAP;
  static const NameMap INPUT_MODE_MAP;
  
  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------

  static const String DEF_PARAM;
  static const String PARAM_ALGORITHM;
  static const String PARAM_IMPLEMENTATION;
  static const String PARAM_FILTERS_PFILE;
  static const String PARAM_SCALE;
  static const String PARAM_FREQUENCY_SAMPLING;
  static const String PARAM_INPUT_MODE;
  static const String PARAM_ORDER;
  static const String PARAM_BANDWIDTH;
  static const String PARAM_CMODE;
  static const String PARAM_DMODE;  
  
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  
  
  // default default value(s) of the class data
  //
  static const String DEF_FILTERS_PFILE;
  static const long DEF_ORDER = (long)24;
  static const float DEF_BANDWIDTH = 85.84258;
  static const long DEF_NUM_FILTERS = (long)1;

  // define default argument(s)
  //
  static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE = AlgorithmData::GENERIC;
  static const long DEF_CHANNEL_INDEX = 0;
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 70700;
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:
  
  // algorithm name
  //
  ALGORITHM algorithm_d;
  
  // implementation name
  //
  IMPLEMENTATION implementation_d;
  
  // static memory manager
  //
  static MemoryManager mgr_d;
  
  // this section contains data for a specific algorithm
  //

  // algorithm: TIME
  // implementation: CCDE
  //

  // define parameters used to describe a multichannel digital filter bank
  //
  Vector<Filter> filters_d;
  String filters_pfile_d;

  // algorithm: FREQUENCY
  // implementation: all
  //

  // define a parameter that describes the methods used for
  // frequency scaling and sampling
  //
  SCALE scale_d;
  FREQUENCY_SAMPLING fsmp_d;

  // define a parameter that describes how the spectrum is represented
  //
  INPUT_MODE input_mode_d;

  // the number of filter banks specified by the user
  //
  Long order_d;

  // the filter bank spacing specified by the user:
  //  note that the units of this are dependent on the technique selected
  //  (e.g, FREQUENCY/MEL means the units of this constant are mel)
  //
  Float bandwidth_d;

  // temporary buffers used to describe the filter bank
  //
  VectorFloat warp_freq_d;
  VectorFloat lin_freq_d;
  VectorFloat cen_freq_d;

  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:
    
  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }

  // other static methods
  // 
  static boolean diagnose(Integral::DEBUG debug_level);
  
  // method: setDebug
  //  these methods are inherited from the AlgorithmBase class

  // other debug methods:
  //
  boolean debug(const unichar* msg) const;

  // method: destructor
  //
  ~FilterBank() {}

  // method: default constructor
  //
  FilterBank() {
    algorithm_d = DEF_ALGORITHM;
    implementation_d = DEF_IMPLEMENTATION;
    filters_d.setLength(DEF_NUM_FILTERS);
    filters_pfile_d.assign(DEF_FILTERS_PFILE);
    scale_d = DEF_SCALE;
    fsmp_d = DEF_FREQUENCY_SAMPLING;
    input_mode_d = DEF_INPUT_MODE;
    order_d = DEF_ORDER;
    bandwidth_d = DEF_BANDWIDTH;
  }

  // method: copy constructor
  //  
  FilterBank(const FilterBank& arg) {
    assign(arg);
  }

  // method: assign
  //
  boolean assign(const FilterBank& arg);
  
  // method: operator=
  //
  FilterBank& operator= (const FilterBank& arg) {
    assign(arg);
    return *this;
  }

  // i/o methods
  //
  long sofSize() const;
  
  boolean read(Sof& sof, long tag, const String& name = CLASS_NAME);
  boolean write(Sof& sof, long tag, const String& name = CLASS_NAME) const;
  
  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
                   long size = SofParser::FULL_OBJECT,
                   boolean param = true,
                   boolean nested = false);
  boolean writeData(Sof& sof, const String& name = DEF_PARAM) const;

  // equality methods
  //
  boolean eq(const FilterBank& arg) const;
 
  // method: new
  //
  static void* operator new(size_t size) {
    return mgr_d.get();
  }

  // method: new[]
  //
  static void* operator new[](size_t size) {
    return mgr_d.getBlock(size);
  }

  // method: delete
  //
  static void operator delete(void* ptr) {
    mgr_d.release(ptr);
  }

  // method: delete[]
  //
  static void operator delete[](void* ptr) {
    mgr_d.releaseBlock(ptr);
  }

  // method: setGrowSize
  //
  static boolean setGrowSize(long grow_size) {
    return mgr_d.setGrow(grow_size);
  }

  // other memory management methods
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  set methods  
  //
  //---------------------------------------------------------------------------

  // method: setAlgorithm
  //
  boolean setAlgorithm(ALGORITHM algorithm) {
    algorithm_d = algorithm;
    is_valid_d = false;
    return true;
  }

  // method: setImplementation
  //
  boolean setImplementation(IMPLEMENTATION implementation) {
    implementation_d = implementation;
    is_valid_d = false;
    return true;  
  }

  // method: setScale
  //
  boolean setScale(SCALE scale) {
    scale_d = scale;
    is_valid_d = false;
    return true;
  }

  // method: setFrequencySampling
  //
  boolean setFrequencySampling(FREQUENCY_SAMPLING fsmp) {
    fsmp_d = fsmp;
    is_valid_d = false;
    return true;
  }

  // method: setInputMode
  //
  boolean setInputMode(INPUT_MODE input_mode) {
    input_mode_d = input_mode;
    is_valid_d = false;
    return true;  
  }
  
  // method: setOrder
  //
  boolean setOrder(long order) {
    fsmp_d = ORDER;
    order_d = order;
    is_valid_d = false;
    return true;
  }

  // method: setBandwidth
  //
  boolean setBandwidth(float bandwidth) {
    fsmp_d = BANDWIDTH;
    bandwidth_d = bandwidth;
    is_valid_d = false;
    return true;
  }

  // method: set
  //
  boolean set(ALGORITHM algo, IMPLEMENTATION impl,
	      INPUT_MODE input_mode, SCALE scale,
	      long order, float sample_freq) {
    algorithm_d = algo;
    implementation_d = impl;
    scale_d = scale;
    fsmp_d = ORDER;
    input_mode_d = input_mode;
    order_d = order;
    sample_freq_d = sample_freq;
    is_valid_d = false;
    return true;
  }

  // method: set
  //
  boolean set(ALGORITHM algo, IMPLEMENTATION impl,
	      INPUT_MODE input_mode, SCALE scale,
	      float bandwidth, float sample_freq) {
    algorithm_d = algo;
    implementation_d = impl;
    scale_d = scale;
    fsmp_d = BANDWIDTH;
    input_mode_d = input_mode;
    bandwidth_d = bandwidth;
    sample_freq_d = sample_freq;
    is_valid_d = false;
    return true;
  }

  // method: setFiltersParamFile
  //
  boolean setFiltersParamFile(String filters_pfile) {
    filters_pfile_d = filters_pfile;
    is_valid_d = false;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  get methods  
  //
  //---------------------------------------------------------------------------

  // method: getAlgorithm
  //
  ALGORITHM getAlgorithm() const {
    return algorithm_d;
  }

  // method: getImplementation
  //
  IMPLEMENTATION getImplementation() const {
    return implementation_d;
  }

  // method: getScale
  //
  SCALE getScale() const {
    return scale_d;
  }
  
  // method: getFrequencySampling
  //
  FREQUENCY_SAMPLING getFrequencySampling() {
    return fsmp_d;
  }

  // method: getInputMode
  //
  INPUT_MODE getInputMode() const {
    return input_mode_d;
  }

  // method: getOrder
  //
  long getOrder() const {
    return (long)order_d;
  }

  // method: getBandwidth
  //
  float getBandwidth() const {
    return (float)bandwidth_d;
  }

  // method: get
  //
  boolean get(ALGORITHM& algo, IMPLEMENTATION& impl,
	      INPUT_MODE& input_mode, SCALE& scale,
	      long& order, float& sample_freq) const {
    algo = algorithm_d;
    impl = implementation_d;
    scale = scale_d;
    input_mode = input_mode_d;
    order = order_d;
    sample_freq = sample_freq_d;
    return true;
  }

  // method: get
  //
  boolean get(ALGORITHM& algo, IMPLEMENTATION& impl,
	      INPUT_MODE& input_mode, SCALE& scale,
	      float& bandwidth, float& sample_freq) const {
    algo = algorithm_d;
    impl = implementation_d;
    scale = scale_d;
    input_mode = input_mode_d;
    bandwidth = bandwidth_d;
    sample_freq = sample_freq_d;
    return true;
  }

  // method: getFiltersParamFile
  //
  boolean getFiltersParamFile(String& filters_pfile) const {
    filters_pfile = filters_pfile_d;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computational methods
  //
  //---------------------------------------------------------------------------

  // compute methods: TIME algorithm (multichannel output)
  //
  boolean compute(Vector<VectorFloat>& output, const VectorFloat& input,
		  AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE,
		  long index = DEF_CHANNEL_INDEX);

  // compute methods: FREQUENCY algorithm (vector output)
  //
  boolean compute(VectorFloat& output, const VectorFloat& input,
		  AlgorithmData::COEF_TYPE input_coef_type = DEF_COEF_TYPE,
		  long index = DEF_CHANNEL_INDEX);
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:  
  //  AlgorithmBase interface contract methods
  //
  //---------------------------------------------------------------------------

  // assign method
  //
  boolean assign(const AlgorithmBase& arg);

  // equality method
  //
  boolean eq(const AlgorithmBase& arg) const;

  // method: className
  //
  const String& className() const {
    return CLASS_NAME;
  }

  // initialization method
  //
  boolean init();
  
  // apply method
  //
  boolean apply(Vector<AlgorithmData>& output,
		const Vector< CircularBuffer<AlgorithmData> >& input);

  // method to set the parser
  //
  boolean setParser(SofParser* parser);

  // getLeadingPad method
  //
  long getLeadingPad() const;

  // getTrailingPad method
  //
  long getTrailingPad() const;    

  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:
  
  // TIME algorithm i/o methods
  //
  boolean readDataTime(Sof& sof, const String& pname = DEF_PARAM,
		       long size = SofParser::FULL_OBJECT,
		       boolean param = true, boolean nested = false);
  boolean writeDataTime(Sof& sof, const String& pname = DEF_PARAM) const;
  
  // FREQUENCY algorithm i/o methods
  //
  boolean readDataFrequency(Sof& sof, const String& pname = DEF_PARAM,
			    long size = SofParser::FULL_OBJECT,
			    boolean param = true, boolean nested = false);
  boolean writeDataFrequency(Sof& sof, const String& pname = DEF_PARAM) const;
  
  // TIME algorithm compute methods
  //
  boolean computeTimeCommon(Vector<VectorFloat>& output,
			    const CircularBuffer<AlgorithmData>& input,
			    AlgorithmData::COEF_TYPE coef_type,
			    long channel_index);
  boolean computeTimeCcde(Vector<VectorFloat>& output,
			  const VectorFloat& input,
			  long index = DEF_CHANNEL_INDEX);
    
  // FREQUENCY algorithm compute methods
  //
  boolean computeFrequencyCommon(VectorFloat& output,
				 const VectorFloat& input);
  boolean computeFrequencyUniform(VectorFloat& output,
				  const VectorFloat& input);
  boolean computeFrequencyTriangular(VectorFloat& output,
				     const VectorFloat& input);
  boolean computeFrequencyRaisedcosine(VectorFloat& output,
				       const VectorFloat& input);
};

// end of include file
// 
#endif
