// file: $isip/class/algo/Output/Output.h
// version: $Id: Output.h 9236 2003-06-23 18:12:22Z gao $
//

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

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

#ifndef ISIP_COMMAND_LINE
#include <CommandLine.h>
#endif

#ifndef ISIP_AUDIO_FILE
#include <AudioFile.h>
#endif

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

#ifndef ISIP_FILENAME
#include <Filename.h>
#endif

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

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

// Output: this is another class that is primarily used to facilitate
// recipe processing. this class is used to output data to a file.
// a recipe can have multiple outputs, and each output can write
// multichannel data in a variety of formats.
//
class Output : public AlgorithmBase {

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

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

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

  // 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;
  static const String PARAM_BASENAME;
  static const String PARAM_AUDIO_OUTPUT;
  static const String PARAM_FEATURE_OUTPUT;
  static const String PARAM_DMODE;   
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // the default filename
  //
  static const String DEF_FILENAME;

  // a default temporary filename
  //
  static const String DEF_TMP_FILE;

  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 71400;

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

  // implementation name
  //
  IMPLEMENTATION implementation_d;
  
  // declare variables to hold filename information
  //
  Filename input_filename_d;
  Filename output_filename_d;
  String output_basename_d;
  String output_directory_d;
  String output_extension_d;
  Long output_preserve_d;
  String output_suffix_d;
  
  // the audiofile or featurefile to write out
  //
  AudioFile audio_output_d;
  FeatureFile feature_output_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);
  
  // debug methods:
  //  setDebug is inherited from the base class
  //
  boolean debug(const unichar* msg) const;

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

  // method: default constructor
  //
  Output(ALGORITHM algorithm = DEF_ALGORITHM,
	   IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    output_extension_d.assign(Filename::DEF_EXTENSION);
    output_suffix_d.assign(Filename::DEF_SUFFIX);
    output_directory_d.clear();
    output_preserve_d.clear();
    is_valid_d = false;
  }

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

  // assign methods
  //
  boolean assign(const Output& arg);

  // method: operator=
  //
  Output& operator= (const Output& 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 Output& 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: setInputFilename
  //
  boolean setInputFilename(String& inputfilename) {
    return input_filename_d.assign(inputfilename);
  }
    
  // method: setOutputFilename
  //
  boolean setOutputFilename(String& filename) {
    output_filename_d.assign(filename);
    is_valid_d = false;
    return true; 
  }

  // method: setOutputDirectory
  //
  boolean setOutputDirectory(const String& new_dir) {
    output_directory_d.assign(new_dir);
    is_valid_d = false;
    return true;
  }

  // method: setOutputBasename
  //
  boolean setOutputBasename(String& basename) {
    output_basename_d.assign(basename);
    is_valid_d = false;
    return true; 
  }

  // method: setOutputExtension
  //
  boolean setOutputExtension(const String& extension) {
    output_extension_d = extension;
    is_valid_d = false;
    return true;
  }

  // method: setOutputPreserve
  //
  boolean setOutputPreserve(long new_preserve_level) {
    output_preserve_d.assign(new_preserve_level);
    is_valid_d = false;
    return true;
  }
  
  // method: setOutputSuffix
  //
  boolean setOutputSuffix(String& suffix) {
    output_suffix_d = suffix;
    is_valid_d = false;
    return true;
  }

  // method: setOutputType
  //
  boolean setOutputType(File::TYPE type) {
    if (type == File::TEXT) {
      feature_output_d.setFileType(FeatureFile::TEXT);
      audio_output_d.setFileType(AudioFile::TEXT);
    }
    else {
      feature_output_d.setFileType(FeatureFile::BINARY);
      audio_output_d.setFileType(AudioFile::BINARY);	
    }	
    return true;    
  }
  
  // method: setFileType
  //
  boolean setFileType(AudioFile::FILE_TYPE file_type) {
    audio_output_d.setFileType(file_type);
    return true;
  }
  
  // method: setFileType
  //
  boolean setFileType(FeatureFile::FILE_TYPE file_type) {
    feature_output_d.setFileType(file_type);
    return true;
  }

  // method: setFileFormat
  //
  boolean setFileFormat(AudioFile::FILE_FORMAT file_format) {
    audio_output_d.setFileFormat(file_format);
    return true;
  }

  // method: setFileFormat
  //
  boolean setFileFormat(FeatureFile::FILE_FORMAT file_format) {
    feature_output_d.setFileFormat(file_format);
    return true;
  }

  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    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: getInputFilename
  //
  boolean getInputFilename(Filename& inputfilename) const{
    return inputfilename.assign(input_filename_d);
  }
  
  // method: getOutputFilename
  //
  boolean getOutputFilename(String& filename) {
    init();
    filename = output_filename_d;
    return true; 
  }

  // method: getOutputDirectory
  //
  boolean getOutputDirectory(String& directory) const {
    directory = output_directory_d;
    return true;
  }

  // method: getOutputBasename
  //
  boolean getOutputBasename(String& basename) const{
    basename = output_basename_d;
    return true; 
  }

  // method: getOutputExtension
  //
  boolean getOutputExtension(String& extension) const{
    extension = output_extension_d;
    return true; 
  }  

  // method: getOutputPreserve
  //
  long getOutputPreserve() const {
    return (long)output_preserve_d;
  }
  
  // method: getOutputSuffix
  //
  boolean getOutputSuffix(String& suffix) const{
    suffix = output_suffix_d;
    return true; 
  }

  // method: get
  //
  boolean get(ALGORITHM& algorithm,
	      IMPLEMENTATION& implementation) const{
    algorithm = algorithm_d;
    implementation = implementation_d;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computational methods
  //
  //---------------------------------------------------------------------------
  
  //---------------------------------------------------------------------------
  //
  // 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);
  
  // method to set the parser
  //
  boolean setParser(SofParser* parser);

  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

  // algorithm-specific i/o methods: DATA
  //
  boolean readDataData(Sof& sof, const String& pname,
			 long size, boolean param, boolean nested);
  boolean writeDataData(Sof& sof, const String& pname) const;
  
  // implementation specific compute methods: FEATURES
  //
  boolean computeFeatures(Vector<AlgorithmData>& output,
			  const Vector< CircularBuffer<AlgorithmData> >&
			  input);

  // implementation specific compute methods: SAMPLED_DATA
  //
  boolean computeSampledData(Vector<AlgorithmData>& output,
			     const Vector< CircularBuffer<AlgorithmData> >&
			     input);
  // transformation methods 
  //
  boolean transformName();

};

// end of include file
// 
#endif







