// file: $isip/class/mmedia/FeatureSelect/FeatureSelect.h
// version: $Id: FeatureSelect.h 9407 2004-02-25 01:53:46Z parihar $
//

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

// isip include files
//
#ifndef ISIP_VECTOR_FLOAT
#include <VectorFloat.h>
#endif

#ifndef ISIP_SDB
#include <Sdb.h>
#endif

#ifndef ISIP_FEATURE_FILE
#include <FeatureFile.h>
#endif

#ifndef ISIP_AUDIO_DATABASE
#include <AudioDatabase.h>
#endif

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

// FeatureSelect: a class that selects the feature
// vectors. currently, only one algorithm is supported:
// CLASS_BASED - selects out of class data
// Only one implementation supported for NONE is:
// NONE.
//
class FeatureSelect {

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

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // define the algorithm choices
  //
  enum ALGORITHM { CLASS_BASED = 0, DEF_ALGORITHM = CLASS_BASED };

  // define the implementation choices
  //
  enum IMPLEMENTATION { NONE = 0, DEF_IMPLEMENTATION = NONE };

  // define the static NameMap objects
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;

  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------

  static const String DEF_PARAM;
  static const String PARAM_ALGORITHM;
  static const String PARAM_IMPLEMENTATION;

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

  static const unichar DEF_DELIM = L':';
  static const long DEF_DIMENSION = 118;
  static const String DEF_RATIO_CLASS;
  static const String DEF_RATIO_SIMILAR;
  static const String DEF_OUTPUT_SUFFIX;
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 50900;
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:
  
  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation name
  //
  IMPLEMENTATION implementation_d;

  // variables associated to feature selection
  //
  VectorLong ratio_class_d;
  VectorLong ratio_similar_d;
  
  // variables associated with input feature file
  //
  Long dim_d;
  FeatureFile::FILE_TYPE input_type_d;
  FeatureFile::FILE_FORMAT input_format_d;
  
  // variables associated with output feature file
  //
  FeatureFile::FILE_TYPE output_type_d;
  FeatureFile::FILE_FORMAT output_format_d;
  String output_directory_d;
  String output_extension_d;
  Long output_preserve_d;
  String output_suffix_d;
  
  // variables associated with the databases
  //
  Filename audiodb_file_d;
  AudioDatabase audiodb_d;

  // declare a static debug level for all class instantiations
  //
  static Integral::DEBUG debug_level_d;

  // declare a static debug level for all class instantiations
  //
  static Integral::DEBUG verbosity_d;
  
  // static memory manager
  //
  static MemoryManager mgr_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
  //
  static boolean setDebug(Integral::DEBUG arg) {
    debug_level_d = arg;
    return true;
  }
  
  // other debug methods
  //
  boolean debug(const unichar* msg) const;

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

  // other constructor
  //
  FeatureSelect(ALGORITHM algorithm = DEF_ALGORITHM,
		IMPLEMENTATION implementation = DEF_IMPLEMENTATION);
  FeatureSelect(const FeatureSelect& arg) {
    assign(arg);
  }
  
  // assign methods
  //
  boolean assign(const FeatureSelect& arg);

  // method: operator=
  //
  FeatureSelect& operator= (const FeatureSelect& 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 FeatureSelect& 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;
    return true;
  }

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

  // method: setAudioDatabaseFile
  //
  boolean setAudioDatabaseFile(const Filename& audiodb_file) {
    audiodb_file_d = audiodb_file;
    return true;
  }

  // method: setDimension
  //
  boolean setDimension(long dim) {
    dim_d = dim;
    return true;
  }

  // method: setRatioClass
  //
  boolean setRatioClass(const String& ratio_class) {
    ratio_class_d.assign(ratio_class, DEF_DELIM);
    return true;
  }

  // method: setRatioSimilar
  //
  boolean setRatioSimilar(const String& ratio_similar) {
    ratio_similar_d.assign(ratio_similar, DEF_DELIM);
    return true;
  }
  
  // method: setInputType
  //
  boolean setInputType(const FeatureFile::FILE_TYPE input_type =
		       FeatureFile::DEF_FILE_TYPE) {
    input_type_d = input_type;
    return true;
  }

  // method: setInputFormat
  //
  boolean setInputFormat(const FeatureFile::FILE_FORMAT
			 input_format =
			 FeatureFile::DEF_FILE_FORMAT) {
    input_format_d = input_format;
    return true;
  }
  
  // method: setOutputType
  //
  boolean setOutputType(const FeatureFile::FILE_TYPE output_type =
			FeatureFile::DEF_FILE_TYPE) {
    output_type_d = output_type;
    return true;
  }

  // method: setOutputFormat
  //
  boolean setOutputFormat(const FeatureFile::FILE_FORMAT
			  output_format =
			  FeatureFile::DEF_FILE_FORMAT) {
    output_format_d = output_format;
    return true;
  }
  
  // method: setOutputDirectory
  //
  boolean setOutputDirectory(const String& new_dir) {
    return output_directory_d.assign(new_dir);
  }
  
  // method: setOutputExtension
  //
  boolean setOutputExtension(const String& extension) {
    output_extension_d = extension;
    return true;
  }
  
  // method: setOutputPreserve
  //
  boolean setOutputPreserve(const long new_preserve_level) {
    return output_preserve_d.assign(new_preserve_level);
  }
  
  // method: setOutputSuffix
  //
  boolean setOutputSuffix(const String& suffix) {
    output_suffix_d = suffix;
    return true;
  }
  
  // method: setVerbosity
  //
  boolean setVerbosity(const Integral::DEBUG verbosity) {
    verbosity_d = verbosity;
    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: getAudioDatabaseFile
  //
  Filename getAudioDatabaseFile() const {
    return audiodb_file_d;
  }
  
  // method: getDimension
  //
  long getDimension() const {
    return dim_d;
  }

  // method: getRatioClass
  //
  VectorLong getRatioClass() const {
    return ratio_class_d;
  }

  // method: getRatioSimilar
  //
  VectorLong getRatioSimilar() const {
    return ratio_similar_d;
  }

  // method: getInputType
  //
  FeatureFile::FILE_TYPE getInputType() const {
    return input_type_d;
  }

  // method: getInputFormat
  //
  FeatureFile::FILE_FORMAT getInputFormat() const {
    return input_format_d;
  }
  
  // method: getOutputType
  //
  FeatureFile::FILE_TYPE getOutputType() const {
    return output_type_d;
  }

  // method: getOutputFormat
  //
  FeatureFile::FILE_FORMAT getOutputFormat() const {
    return output_format_d;
  }
  
  // method: getOutputDirectory
  //
  String getOutputDirectory() const {
    return output_directory_d;
  }
  
  // method: getOutputExtension
  //
  String getOutputExtension() const {
    return output_extension_d;
  }
  
  // method: getOutputPreserve
  //
  long getOutputPreserve() const {
    return (long)output_preserve_d;
  }
  
  // method: getOutputSuffix
  //
  String getOutputSuffix() const {
    return output_suffix_d;
  }
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computational methods
  //
  //---------------------------------------------------------------------------
  
  // compute method
  //
  boolean compute(String& symbol, Sdb& symbols_similar, Sdb& symbols_all);
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

  // methods to open audio database
  //
  boolean openAudioDatabase();
  
  // methods to close audio database
  //
  boolean closeAudioDatabase();

  // method to retrieve feature file from audio database
  //
  boolean retrieveFtrFile(const String& identifier, Filename& ftr_file);
  
  // method to read features from feature file
  //
  boolean readFeatures(const Filename& ftr_file,
		       Vector<VectorFloat>& features);
  
  // method to write features to feature file
  //
  boolean writeFeatures(const Filename& ftr_file,
			const Vector<VectorFloat>& features);

  // method to select features on a class basis
  //
  boolean computeClassBased(const String& symbol, Sdb& symbols_similar,
			    Sdb& symbols_all);
  
  // method to get features corresponding to symbols
  //
  boolean getAllFeatures(Sdb& symbols, long num_feat_vectors,
			 Vector<VectorFloat>& features);
  
  // method to get features corresponding to a symbol
  //
  boolean getFeatures(String& symbol, long num_feat_vectors,
		      Vector<VectorFloat>& features);  
};

// end of include file
// 
#endif
