// file: $isip/class/mmedia/FeatureFile/FeatureFile.h // version: $Id: FeatureFile.h 10597 2006-08-08 21:28:39Z ss754 $ // // make sure definitions are only made once // #ifndef ISIP_FEATURE_FILE #define ISIP_FEATURE_FILE // isip include files // #ifndef ISIP_DOUBLE #include #endif #ifndef ISIP_STRING #include #endif #ifndef ISIP_LONG #include #endif #ifndef ISIP_FLOAT #include #endif #ifndef ISIP_FILE #include #endif #ifndef ISIP_SOF #include #endif #ifndef ISIP_ALGORITHM_DATA #include #endif #ifndef ISIP_VECTOR #include #endif #ifndef ISIP_CIRCULAR_BUFFER #include #endif #ifndef ISIP_VECTOR_FLOAT #include #endif #ifndef ISIP_MATRIX_FLOAT #include #endif #ifndef ISIP_NAME_MAP #include #endif #ifndef ISIP_MEMORY_MANAGER #include #endif // forward class definitions // class Filename; // FeatureFile: this class is used to manipulate files containing feature // data. it is used in isip_transform. its function is similar to AudioFile. // class FeatureFile { //--------------------------------------------------------------------------- // // public constants // //--------------------------------------------------------------------------- public: // define the class name // static const String CLASS_NAME; //---------------------------------------- // // other important constants // //---------------------------------------- // define the supported file types, file format, data type // enum FILE_TYPE { TEXT = 0, BINARY, DEF_FILE_TYPE = TEXT }; enum FILE_FORMAT { SOF = 0, RAW, DEF_FILE_FORMAT = SOF }; // compression modes and channel tags // enum COMP_TYPE { LINEAR = 0, ULAW, ALAW, DEF_COMP_TYPE = LINEAR }; // enumeration for sample precision // enum SAMPLE_PRECISION { NONE = 0, USE_SIZE, EIGHT_BITS, TWELVE_BITS, SIXTEEN_BITS, TWENTY_BITS, TWENTY_FOUR_BITS, THIRTY_TWO_BITS, DEF_SAMPLE_PRECISION = NONE }; // enumeration for the type of data // enum DATA_TYPE { ALGORITHM_DATA = 0, VECTOR_FLOAT, DEF_DATA_TYPE = ALGORITHM_DATA }; // define name maps for each of the enumerated values // static NameMap FILE_TYPE_MAP; static NameMap FILE_FORMAT_MAP; static NameMap COMP_TYPE_MAP; static NameMap SAMPLE_PRECISION_MAP; static NameMap DATA_TYPE_MAP; //---------------------------------------- // // i/o related constants // //---------------------------------------- static const String PARAM_NAME; static const String PARAM_FILE_TYPE; static const String PARAM_FILE_FORMAT; static const String PARAM_COMP_TYPE; static const String PARAM_RANGE; static const String PARAM_NUM_CHANNELS; static const String PARAM_ID; static const String PARAM_NUM_FEATURES; static const String PARAM_DATA_TYPE; static const String PARAM_COEF_TYPE; static const String PARAM_TAG; static const String PARAM_BLOCK_SIZE; static const String PARAM_BUF_SIZE; static const String PARAM_FRAME_DURATION; static const String PARAM_SAMPLE_FREQUENCY; static const String PARAM_DATA; //---------------------------------------- // // default values and arguments // //---------------------------------------- // define the channel related constants // static const int32 CHANNEL_TAG_RIGHT = 1; static const int32 CHANNEL_TAG_LEFT = 0; static const int32 CHANNEL_TAG_ALL = -2; static const int32 DEF_CHANNEL_TAG = CHANNEL_TAG_ALL; // define some buffer management constants: // a buffer contains DEF_BUF_SIZE * DEF_BLOCK_SIZE elements // static const int32 DEF_BLOCK_SIZE = 8192; static const int32 DEF_BUF_SIZE = 4; static const int32 DEF_NUM_FEATURES = 1; static const int32 DEF_START_POS = 0; static const int32 DEF_NUM_ELEM = 4000; static const int32 DEF_TAG = 0; // define a default name for feature data // static const String DEF_FEATURE_NAME; // define signal related parameters // static const int32 DEF_NUM_CHANNELS = 1; static const int32 DEF_NUM_FRAMES = 0; static const float32 DEF_FRAME_DURATION = 0.01; static const float32 DEF_SAMPLE_FREQ = 8000.0; static const float32 DEF_AMPLITUDE_RANGE = 1.0; static const int32 DEF_SAMPLE_NUM_BYTES = 2; static const float64 DEF_START_TIME = 0.0; static const float64 DEF_CENTER_TIME = 0.25; static const float64 DEF_DURATION = 0.5; //---------------------------------------- // // error codes // //---------------------------------------- static const int32 ERR = 50400; //--------------------------------------------------------------------------- // // protected data // //--------------------------------------------------------------------------- protected: // define file format-related parameters // String name_d; FILE_TYPE file_type_d; FILE_FORMAT file_format_d; COMP_TYPE compression_type_d; // signal-related parameters // Long num_frames_d; Double amplitude_range_d; Long num_channels_d; String id_d; Long num_features_d; DATA_TYPE data_type_d; AlgorithmData::COEF_TYPE coef_type_d; // Sof file-related parameters // Long tag_d; Sof in_sof_d; // buffer-related parameters // Long block_size_d; Long buf_size_d; // signal processing-related parameters // Float frame_duration_d; Float sample_frequency_d; // define a variable to hold the data // Vector v_d; // raw feature file-related parameters // File raw_features_d; // Vector< CircularBuffer > buffers_d; Vector< CircularBuffer > buffers_d; Vector buf_end_ftr_d; // since the FeatureFile class can either hold configuration // information for auxiliary file types (RAW FEATURES) or // the actual features data, this flag lets the class keep track of whether // data is actually present in the file. // bool8 no_data_d; // have we reached the end of file? // bool8 end_of_file_d; // if we are writing to a binary Sof file we need to keep track of // where we write the FeatureFile object's size and the vector's length // int32 sof_length_pos_d; // declare a static debug level for all class instantiations // static Integral::DEBUG debug_level_d; // a static memory manager // static MemoryManager mgr_d; //--------------------------------------------------------------------------- // // required public methods // //--------------------------------------------------------------------------- public: // method: name // static const String& name() { return CLASS_NAME; } // other static methods // static bool8 diagnose(Integral::DEBUG debug_level); // method: setDebug // the setDebug method for this class is static because the debug_level // is shared across all objects of this class type // static bool8 setDebug(Integral::DEBUG arg) { debug_level_d = arg; return true; } // other debug methods // bool8 debug(const unichar* msg) const; // method: destructor // ~FeatureFile() {} // method: default constructor // FeatureFile(); // method: copy constructor // FeatureFile(const FeatureFile& arg) { num_frames_d = DEF_NUM_FRAMES; num_channels_d = DEF_NUM_CHANNELS; data_type_d = DEF_DATA_TYPE; coef_type_d = AlgorithmData::DEF_CTYPE; assign(arg); } // assign methods // bool8 assign(const FeatureFile& arg); // method: operator= // inline FeatureFile& operator=(const FeatureFile& arg) { if (!assign(arg)) { Error::handle(name(), L"operator=", Error::ARG, __FILE__, __LINE__); } return *this; } // i/o methods // int32 sofSize() const; bool8 read(Sof& sof, int32 tag, const String& name = CLASS_NAME); bool8 write(Sof& sof, int32 tag, const String& name = CLASS_NAME ) const; bool8 readData(Sof& sof, const String& pname = String::EMPTY, int32 size = SofParser::FULL_OBJECT, bool8 param = true, bool8 nested = false); bool8 writeData(Sof& sof, const String& name = String::EMPTY) const; bool8 read(int32 tag, const String& name = CLASS_NAME) { return read(in_sof_d, tag, name); } // equality methods: // bool8 eq(const FeatureFile& 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 bool8 setGrowSize(int32 grow_size) { return mgr_d.setGrow(grow_size); } // other memory-management methods // bool8 clear(Integral::CMODE ctype = Integral::DEF_CMODE); //--------------------------------------------------------------------------- // // class-specific public methods: // additional i/o methods // //--------------------------------------------------------------------------- // open and close methods: // these also handle clearing the internal data // bool8 open(const Filename& filename, File::MODE mode = File::READ_ONLY); bool8 open(const unichar* filename, File::MODE mode = File::READ_ONLY); bool8 close(); bool8 isOpen() const; // methods that get features from buffers or files // int32 getBufferedData(Vector& data, int32 start_pos = DEF_START_POS, int32 num_elem = DEF_NUM_ELEM); int32 getBufferedData(Vector& data, int32 start_pos = DEF_START_POS); int32 getBufferedData(VectorFloat& data, int32 channel_tag, int32 start_pos = DEF_START_POS, int32 num_elem = DEF_NUM_ELEM); int32 readFeatureData(Vector& data, int32 start_pos = DEF_START_POS, int32 num_elem = DEF_NUM_ELEM); int32 readFeatureData(Vector& data, int32 channel_tag = DEF_CHANNEL_TAG, int32 start_pos = DEF_START_POS, int32 num_elem = DEF_NUM_ELEM); // methods that write features to a file // int32 writeFeatureData(Vector& data, int32 channel_tag = DEF_CHANNEL_TAG); int32 writeFeatureData(Vector& data, int32 channel_tag = DEF_CHANNEL_TAG); //--------------------------------------------------------------------------- // // class-specific public methods: // indexing methods // //--------------------------------------------------------------------------- // method: operator() // AlgorithmData& operator()(int32 index) { return v_d(index); } // method: operator() // const AlgorithmData& operator()(int32 index) const { return v_d(index); } // method: operator() // AlgorithmData& operator()(int32 ctag, int32 frame) { return v_d(frame * num_channels_d + ctag); } // method: operator() // const AlgorithmData& operator()(int32 ctag, int32 frame) const { return v_d(frame * num_channels_d + ctag); } //--------------------------------------------------------------------------- // // class-specific public methods: // set methods // //--------------------------------------------------------------------------- // method: setName // bool8 setName(const String& arg) { return name_d.assign(arg); } // method: setFileType // bool8 setFileType(FILE_TYPE file_type) { file_type_d = file_type; return true; } // method: setFileFormat // bool8 setFileFormat(FILE_FORMAT file_format) { file_format_d = file_format; return true; } // method: setCompType // bool8 setCompType(COMP_TYPE comp_type) { if (comp_type != LINEAR) { return Error::handle(name(), L"setCompType", Error::NOT_IMPLEM, __FILE__, __LINE__); } compression_type_d = comp_type; return true; } // method: setAmplitudeRange // bool8 setAmplitudeRange(float64 amplitude_range) { amplitude_range_d = amplitude_range; return true; } // method: setNumChannels // bool8 setNumChannels(int32 arg) { return num_channels_d.assign(arg); } // method: setID // bool8 setID(const String& arg) { return id_d.assign(arg); } // method: setNumFeatures // bool8 setNumFeatures(int32 arg) { num_features_d = arg; return true; } // method: setDataType // bool8 setDataType(DATA_TYPE arg) { data_type_d = arg; return true; } // method: setCoefType // bool8 setCoefType(AlgorithmData::COEF_TYPE arg) { coef_type_d = arg; return true; } // method: setFrameDuration // bool8 setFrameDuration(float32 arg) { frame_duration_d = arg; return true; } // method: setSampleFrequency // bool8 setSampleFrequency(float32 arg) { sample_frequency_d = arg; return true; } // method: setLength // bool8 setLength(int32 arg) { v_d.setLength(arg); return true; } // other set methods // bool8 setBufferSize(int32 nblocks); //--------------------------------------------------------------------------- // // class-specific public methods: // get methods // //--------------------------------------------------------------------------- // method: getName // const String& getName() const { return name_d; } // method: getFileType // FILE_TYPE getFileType() const { return file_type_d; } // method: getFileFormat // FILE_FORMAT getFileFormat() const { return file_format_d; } // method: getCompType // COMP_TYPE getCompType() const { return compression_type_d; } // method: getAmplitudeRange // float64 getAmplitudeRange() const { return amplitude_range_d; } // method: getNumChannels // int32 getNumChannels() const { return num_channels_d; } // method: getID // const String& getID() const { return id_d; } // method: getNumFeatures // int32 getNumFeatures() const { return num_features_d; } // method: getDataType // DATA_TYPE getDataType() const { return data_type_d; } // method: getCoefType // AlgorithmData::COEF_TYPE getCoefType() const { return coef_type_d; } // method: getFrameDuration // float32 getFrameDuration() const { return frame_duration_d; } // method: getSampleFrequency // float32 getSampleFrequency() const { return sample_frequency_d; } // method: getNumFrames // int32 getNumFrames(const Sof& sof) const { return (sof.getVecSize() / num_channels_d); } // other get methods // int32 getNumFrames(); //--------------------------------------------------------------------------- // // class-specific public methods: // partial i/o methods // //--------------------------------------------------------------------------- // method: readStart // bool8 readStart(const String& pname = String::EMPTY, int32 size = SofParser::FULL_OBJECT, bool8 param = true, bool8 nested = true) { in_sof_d.find(name(), (int32) tag_d); return readStart(in_sof_d, pname, size, param, nested); } // method: readTerminate // bool8 readTerminate() { return in_sof_d.stopPartialRead(); } // other partial read methods // bool8 readPartialData(int32 start_pos, int32 num_elem); // method: writeStart // bool8 writeStart(const String& pname = String::EMPTY); // method: writeTerminate // bool8 writeTerminate(const String& pname = String::EMPTY) { writeTerminate(in_sof_d, pname); return true; } // other partial write methods // bool8 writePartialData(int32 start_pos, int32 num_elem); // almostEqual function // bool8 almostEqual(const FeatureFile& arg, float64 percent = Integral::DEF_PERCENTAGE, float64 bound = Integral::DEF_BOUND) const; //--------------------------------------------------------------------------- // // private methods // //--------------------------------------------------------------------------- private: // method: getStartFeature // int32 getStartFeature(int32 ctag = DEF_CHANNEL_TAG) const { int32 buf_duration = buffers_d(ctag).getNumElements(); int32 buf_end = buf_end_ftr_d(ctag); // calculate and return the start time // return (buf_end - buf_duration); } // method: getEndFeature // int32 getEndFeature(int32 ctag = DEF_CHANNEL_TAG) const { return buf_end_ftr_d(ctag); } // append methods // bool8 appendData(); // configuration i/o methods: // this method reads and writes only the configuration parameters. it is // called by both readData and readStart // bool8 readConfig(Sof& sof, SofParser& parser); bool8 writeConfig(Sof& sof) const; // feature file format i/o methods: // read data of a certain number from the feature file, in the // default case, read the whole file // int32 readRawData(Vector& data, int32 start_pos = DEF_START_POS, int32 num_samp = DEF_NUM_ELEM); // int32 readRawData(Vector& data_a, int32 ctag_a, // int32 start_samp_a, int32 num_samp_a); int32 writeRawData(Vector& data_a, int32 ctag_a); // int32 writeRawData(Vector& data_a, // int32 ctag_a); int32 readSofData(Vector& data, int32 start_pos = DEF_START_POS, int32 num_elem = DEF_NUM_ELEM); // int32 readSofData(Vector& data_a, int32 ctag_a, // int32 start_samp_a, int32 num_samp_a); int32 writeSofData(Vector& data, int32 channel_tag = DEF_CHANNEL_TAG); // int32 writeSofData(Vector& data, // int32 channel_tag = DEF_CHANNEL_TAG); bool8 readSofStart(); bool8 writeSofStart(); // partial i/o methods: // bool8 readStart(Sof& sof, const String& pname = String::EMPTY, int32 size = SofParser::FULL_OBJECT, bool8 param = true, bool8 nested = true); bool8 writeStart(Sof& sof, const String& pname = String::EMPTY) const; bool8 writeTerminate(Sof& sof, const String& pname = String::EMPTY) const; // reset methods: clear out buffer and pointers // bool8 resetBuffer(int32 channel_tag = CHANNEL_TAG_ALL); }; // end of include file // #endif