// file: $isip/class/algo/Window/Window.h
// version: $Id: Window.h 8165 2002-05-31 21:57:33Z picone $
//

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

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

// Window: a class used to design various types of windows such as:
// rectangular, hamming, hanning, blackman, and bartlett.  the windows
// implemented here represent the most common windows found in the
// signal processing literature. the user can select different types
// of windows and apply them to a vector of data. alternatively, they
// can define their own window in custom mode and apply that window on
// input data.
// 
class Window : public AlgorithmBase {

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

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // define algorithm choices:
  //  rectangular is chosen as the default for obvious reasons. the remaining
  //  windows are listed in alphabetical order. custom is listed last
  //  because it is a special case.
  //
  enum ALGORITHM { RECTANGULAR = 0, BLACKMAN, BARTLETT, DOLPH_CHEBYSHEV,
		   GAUSSIAN, HAMMING, HANNING, KAISER, LIFTER, CUSTOM,
		   DEF_ALGORITHM = RECTANGULAR };

  // define implementation choices:
  // multiplication is the only implementation for window since it
  // multiplies the two vectors.
  //
  enum IMPLEMENTATION { MULTIPLICATION = 0,
			DEF_IMPLEMENTATION = MULTIPLICATION };

  // define alignment choices:
  //  center means that the center of the window is aligned with
  //  the center of the frame. left means that the first sample in
  //  the window corresponds to the first sample in the current frame
  //  (and the remainder of the window extends into the future). right
  //  means that the last sample in the window corresponds to the last
  //  sample in the frame (and the remainder of the window extends
  //  into the past.
  //  
  enum ALIGNMENT { CENTER = 0, LEFT, RIGHT, DEF_ALIGNMENT = CENTER };

  // define normalization choices:
  //  unit_energy means that the energy of the window function is always
  //  normalized to be unity.
  //
  enum NORMALIZATION { NONE = 0, UNIT_ENERGY, DEF_NORMALIZATION = NONE };
    
  // define static NameMap objects for the enumerated values
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;
  static const NameMap ALGN_MAP;
  static const NameMap NORM_MAP;

  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------  
  
  static const String DEF_PARAM;
  static const String PARAM_ALGORITHM;
  static const String PARAM_IMPLEMENTATION;
  static const String PARAM_ALIGNMENT;
  static const String PARAM_NORMALIZATION;
  static const String PARAM_DURATION;
  static const String PARAM_CONSTANTS;
  static const String PARAM_DATA;
  static const String PARAM_CMODE;  

  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define the default value(s) of the class data
  //
  static const float DEF_DURATION = 0.025;
  
  // define default argument(s)
  //
  static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE = AlgorithmData::SIGNAL;
  
  // default constants for each window algorithm
  //
  static const VectorFloat DEF_RECT_CONSTANTS;
  static const VectorFloat DEF_BLCK_CONSTANTS;
  static const VectorFloat DEF_BART_CONSTANTS;
  static const VectorFloat DEF_DCHB_CONSTANTS;
  static const VectorFloat DEF_GAUS_CONSTANTS;
  static const VectorFloat DEF_HAMM_CONSTANTS;
  static const VectorFloat DEF_HANN_CONSTANTS;
  static const VectorFloat DEF_KAIS_CONSTANTS;
  static const VectorFloat DEF_LIFT_CONSTANTS;

  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 72000;
  static const long ERR_PRM = 72001;

  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:
  
  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation name
  //
  IMPLEMENTATION implementation_d;

  // alignment name
  //
  ALIGNMENT alignment_d;
  
  // normalization name
  //
  NORMALIZATION normalization_d;

  // window duration (in seconds)
  //
  Float duration_d;

  // static memory manager
  //
  static MemoryManager mgr_d;

  // this section contains data for a specific algorithm
  //

  // algorithm: non-CUSTOM
  // mode: all
  // implementation: MULTIPLICATION
  //
  // parameters of the window function:
  //  since different algorithms require different numbers of parameters,
  //  by making this a vector we are able to collapse all algorithms into
  //  one set of function calls.
  //
  VectorFloat constants_d;

  // algorithm: CUSTOM
  // mode: all
  // implementation: MULTIPLICATION
  //
  // a user-defined window function
  //
  VectorFloat data_d;
  
  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:
    
  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }
  
  // other static methods
  //
  static boolean diagnose(Integral::DEBUG debug_level);
  
  // debug methods:
  //  setDebug is inherited from the base class  
  //
  boolean debug(const unichar* msg) const;

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

  // method: default constructor
  //
  Window(ALGORITHM algorithm = DEF_ALGORITHM,
	 IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
	 ALIGNMENT alignment = DEF_ALIGNMENT,
	 NORMALIZATION normalization = DEF_NORMALIZATION,
	 float duration = DEF_DURATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    alignment_d = alignment;
    normalization_d = normalization;
    duration_d = duration;
    is_valid_d = false;
  }
    
  // method: copy constructor
  //
  Window(const Window& arg) {
    assign(arg);
  }

  // assign methods
  //
  boolean assign(const Window& arg);
  
  // method: operator=
  //
  Window& operator= (const Window& 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& pname = DEF_PARAM) const;
  
  // equality methods
  //
  boolean eq(const Window& 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;
    constants_d.clear(Integral::RESET);
    is_valid_d = false;
    return true;  
  }  

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

  // method: setAlignment
  //
  boolean setAlignment(ALIGNMENT alignment) {
    alignment_d = alignment;
    is_valid_d = false;
    return true;
  }
  
  // method: setNormalization
  //
  boolean setNormalization(NORMALIZATION normalization) {
    normalization_d = normalization;
    is_valid_d = false;
    return true; 
  }
  
  // method: setDuration
  //
  boolean setDuration(float duration) {
    duration_d = duration;
    is_valid_d = false;
    return true;
  }
  
  // method: setSize
  //
  boolean setSize(long num_points) {
    if (data_d.length() != num_points) {
      data_d.setLength(num_points);
      is_valid_d = false;
    }
    return true; 
  }

  // method: setData
  //
  boolean setData(const VectorFloat& data) {
    if (algorithm_d == CUSTOM) {
      return data_d.assign(data);
    }
    else {
      return Error::handle(name(), L"setData", ERR, __FILE__, __LINE__);
    }
  }

  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION,
	      ALIGNMENT alignment = DEF_ALIGNMENT,
	      NORMALIZATION normalization = DEF_NORMALIZATION,
	      float duration = DEF_DURATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    alignment_d = alignment;
    normalization_d = normalization;
    duration_d = duration;
    is_valid_d = false;
    return true;
  }

  // other set methods
  //
  boolean setConstants(const VectorFloat& constants);
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods
  //  get methods
  //
  //---------------------------------------------------------------------------

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

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

  // method: getAlignment
  //
  ALIGNMENT getAlignment() const {
    return alignment_d;
  }
  
  // method: getNormalization
  //
  NORMALIZATION getNormalization() const {
    return normalization_d;
  }

  // method: getDuration
  //
  float getDuration() const {
    return duration_d;
  }

  // method: getSize
  //
  long getSize() const {
    return data_d.length();
  }
  
  // method: getData
  //
  const VectorFloat& getData() const {
    return data_d;
  }

  // method: get
  //
  boolean get(ALGORITHM& algorithm,
	      IMPLEMENTATION& implementation,
	      ALIGNMENT& alignment,
	      NORMALIZATION& normalization,
	      float& duration) {
    algorithm = algorithm_d;
    implementation = implementation_d;
    alignment = alignment_d;
    normalization = normalization_d;
    duration = duration_d;
    return true;
  }

  // method: getConstants
  //
  const VectorFloat& getConstants() const {
    return constants_d;
  }

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

  boolean compute(VectorFloat& output, const VectorFloat& input,
		  AlgorithmData::COEF_TYPE coef_type = DEF_COEF_TYPE,
		  long index = DEF_CHANNEL_INDEX);

  boolean compute(VectorComplexFloat& output, const VectorComplexFloat& input,
		  AlgorithmData::COEF_TYPE coef_type = DEF_COEF_TYPE,
		  long index = DEF_CHANNEL_INDEX);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  public methods required by the AlgorithmBase interface contract
  //
  //---------------------------------------------------------------------------

  // 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);
  
  // pad time methods:
  //  windows typically extend beyond the current frame. the leading and
  //  trailing pads return values in frames since the algorithm classes
  //  are designed to be frame based.
  //
  long getLeadingPad() const;
  long getTrailingPad() const;
  
  // method to set the parser
  //
  boolean setParser(SofParser* parser);
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

  // common i/o methods
  //
  boolean readDataCommon(Sof& sof, const String& pname,
                         long size = SofParser::FULL_OBJECT,
                         boolean param = true, boolean nested = false);
  boolean writeDataCommon(Sof& sof, const String& pname) const;

  // common compute methods
  //
  boolean computeCommon(VectorFloat& vector_out,
			const VectorFloat& vector_in);

  boolean computeCommon(VectorComplexFloat& vector_out,
			const VectorComplexFloat& vector_in);

  // window generation methods:
  //  these methods generate common window functions
  //
  boolean generateRectangular(VectorFloat& output,
			      const VectorFloat& params,
			      long num_points) const;

  boolean generateBartlett(VectorFloat& output,
			   const VectorFloat& params,
			   long num_points) const;

  boolean generateBlackman(VectorFloat& output,
			   const VectorFloat& params,
			   long num_points) const;
  
  boolean generateDolphChebyshev(VectorFloat& output,
				 const VectorFloat& params,
				 long num_points) const;
  
  boolean generateGaussian(VectorFloat& output,
			   const VectorFloat& params,
			   long num_points) const;
  
  boolean generateGeneralizedHanning(VectorFloat& output,
				     const VectorFloat& params,
				     long num_points) const;
  
  boolean generateKaiser(VectorFloat& output,
			 const VectorFloat& params,
			 long num_points) const;
  
  boolean generateLifter(VectorFloat& output,
			 const VectorFloat& params,
			 long num_points) const;

  boolean compute(VectorFloat& output_a,
		  const CircularBuffer<AlgorithmData>& input_a,
		  AlgorithmData::COEF_TYPE coef_type_a = DEF_COEF_TYPE,
		  long channel_index_a = DEF_CHANNEL_INDEX);

};

// end of include file
// 
#endif
