Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_FeatureFileWriterFormat_cpp)
00056 #define ALIZE_FeatureFileWriterFormat_cpp
00057
00058 #include <new>
00059 #include "FeatureFileWriter.h"
00060 #include "Feature.h"
00061 #include "Exception.h"
00062 #include "Config.h"
00063
00064 using namespace alize;
00065 typedef FeatureFileWriter W;
00066
00067
00068 W::FeatureFileWriter(const FileName& f, const Config& c)
00069 :FileWriter(getFullFileName(c, f)),
00070 _format(c.getParam_saveFeatureFileFormat()), _vectSizeDefined(false),
00071 _headerWritten(false), _config(c) {}
00072
00073 W& W::create(const FileName& f, const Config& c)
00074 {
00075 W* p = new (std::nothrow) W(f, c);
00076 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00077 return *p;
00078 }
00079
00080 String W::getFullFileName(const Config& c, const FileName& f) const
00081 {
00082 if (f.beginsWith("/") || f.beginsWith("./"))
00083 return f;
00084 return c.getParam_featureFilesPath() + f
00085 + c.getParam_saveFeatureFileExtension();
00086 }
00087
00088 void W::writeFeature(const Feature& f)
00089 {
00090 if (!_vectSizeDefined)
00091 {
00092 _vectSize = f.getVectSize();
00093 _vectSizeDefined = true;
00094 }
00095 else
00096 if (f.getVectSize() != _vectSize)
00097 throw Exception("Incompatible vectSize", __FILE__, __LINE__);
00098
00099 if (isClosed())
00100 open();
00101
00102 if (_format == FeatureFileWriterFormat_RAW)
00103 {
00104 for (unsigned long i=0; i<_vectSize; i++)
00105 {
00106 writeFloat((float)f[i]);
00107 }
00108 }
00109 else if (_format == FeatureFileWriterFormat_SPRO3)
00110 {
00111 if (!_headerWritten)
00112 {
00113 const FeatureFlags flags = _config.getParam_featureFlags();
00114 unsigned long dim = 0;
00115 const String& s = flags.getString();
00116 if (s == "100000")
00117 dim = _vectSize;
00118 else if (s == "110000")
00119 dim = _vectSize-1;
00120 else if (s == "101000")
00121 dim = _vectSize/2;
00122 else if (s == "111000" || s == "101100")
00123 dim = (_vectSize-1)/2;
00124 else if (s == "111100")
00125 dim = (_vectSize-2)/2;
00126 else if (s == "100010")
00127 dim = _vectSize/2;
00128 else if (s == "110010")
00129 dim = (_vectSize-1)/2;
00130 else if (s == "101010")
00131 dim = _vectSize/3;
00132 else if (s == "111010" || s == "101110")
00133 dim = (_vectSize-1)/3;
00134 else if (s == "111110")
00135 dim = (_vectSize-2)/3;
00136 else if (s == "100011")
00137 dim = (_vectSize-1)/2;
00138 else if (s == "110011")
00139 dim = (_vectSize-2)/2;
00140 else if (s == "101011")
00141 dim = (_vectSize-1)/3;
00142 else if (s == "111011" || s == "101111")
00143 dim = (_vectSize-2)/3;
00144 else if (s == "111111")
00145 dim = (_vectSize-3)/3;
00146 else
00147 throw Exception("Wrong featureFlag : " + s,
00148 __FILE__, __LINE__);
00149 writeUInt4(_config.getParam_saveFeatureFileSPro3DataKind());
00150 writeUInt4(dim);
00151 writeUInt4(0);
00152 writeUInt4(flags.toSPro3());
00153 _headerWritten = true;
00154 _featureCount = 0;
00155 }
00156 for (unsigned long i=0; i<_vectSize; i++)
00157 { writeFloat((float)f[i]); }
00158 _featureCount++;
00159 }
00160 else if (_format == FeatureFileWriterFormat_SPRO4)
00161 {
00162 if (!_headerWritten)
00163 {
00164 writeString("<header>\n");
00165 writeString("</header>\n");
00166 writeShort((short)_vectSize);
00167 writeUInt4(_config.getParam_featureFlags().toSPro4());
00168 writeFloat((float)_config.getParam_sampleRate());
00169 _headerWritten = true;
00170 }
00171 for (unsigned long i=0; i<_vectSize; i++)
00172 { writeFloat((float)f[i]); }
00173 }
00174 else
00175 ;
00176 }
00177
00178 void W::close()
00179 {
00180 if (_format == FeatureFileWriterFormat_SPRO3 && isOpen() && _headerWritten)
00181 {
00182
00183 if (::fseek(_pFileStruct, 4+4, SEEK_SET) != 0)
00184 throw IOException("", __FILE__, __LINE__, _fileName);
00185 writeUInt4(_featureCount);
00186 }
00187 FileWriter::close();
00188 }
00189
00190 String W::getClassName() const {return "FeatureFileWriter";}
00191
00192 W::~FeatureFileWriter() { close(); }
00193
00194 #endif // !defined(ALIZE_FeatureFileWriterFormat_cpp)
00195