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_FeatureInputStreamModifier_cpp)
00056 #define ALIZE_FeatureInputStreamModifier_cpp
00057
00058 #include <new>
00059 #include "FeatureInputStreamModifier.h"
00060 #include "Exception.h"
00061 #include "LabelServer.h"
00062 #include "Label.h"
00063 #include "FeatureFlags.h"
00064 #include "XLine.h"
00065 #include "Config.h"
00066
00067 using namespace alize;
00068 typedef FeatureInputStreamModifier M;
00069
00070
00071 M::FeatureInputStreamModifier(FeatureInputStream& is, const String& m,
00072 bool ownStream)
00073 :FeatureInputStream(is.getConfig()), _pInput(&is), _useMask(false),
00074 _ownStream(ownStream) { setMask(m); }
00075
00076 M& M::create(FeatureInputStream& is, const String& m, bool ownStream)
00077 {
00078 M* p = new (std::nothrow) M(is, m, ownStream);
00079 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00080 return *p;
00081 }
00082
00083 void M::setMask(const String& m)
00084 {
00085 if (m == "NO_MASK")
00086 {
00087 _useMask = false;
00088 return;
00089 }
00090 _tmpMask.reset();
00091 unsigned long i = 0;
00092 String begin, end;
00093
00094 if (m.isEmpty())
00095 goto xend;
00096 while (true)
00097 {
00098 if (m[i] < "0" || m[i] > "9")
00099 throw Exception("Invalid feature mask", __FILE__, __LINE__);
00100 begin += m[i++];
00101 end = begin;
00102 if (i == m.length())
00103 {
00104 updateMask(begin, end);
00105 goto xend;
00106 }
00107 if (m[i] == "-")
00108 {
00109 i++;
00110 if (i == m.length())
00111 throw Exception("Invalid feature mask", __FILE__, __LINE__);
00112 end.reset();
00113 do
00114 {
00115 if (m[i] < "0" || m[i] > "9")
00116 throw Exception("Invalid feature mask", __FILE__, __LINE__);
00117 end += m[i++];
00118 if (i == m.length())
00119 {
00120 updateMask(begin, end);
00121 goto xend;
00122 }
00123 }
00124 while (m[i] != ",");
00125 }
00126 if (m[i] == ",")
00127 {
00128 updateMask(begin, end);
00129 i++;
00130 if (i == m.length())
00131 throw Exception("Invalid feature mask", __FILE__, __LINE__);
00132 begin.reset();
00133 end.reset();
00134 }
00135 }
00136 xend:
00137 _useMask = true;
00138 _mask = m;
00139 _selection.clear();
00140 char* p = const_cast<char*>(_tmpMask.c_str());
00141 for (i=0; i<_tmpMask.length(); i++)
00142 if (p[i] == '1')
00143 _selection.addValue(i);
00144 _selectionSize = _selection.size();
00145 }
00146
00147 void M::updateMask(const String& b, const String& e)
00148 {
00149 unsigned long ee = e.toULong();
00150 unsigned long bb = b.toULong();
00151 if (ee < bb)
00152 throw Exception("Invalid feature mask", __FILE__, __LINE__);
00153 for (unsigned long i=bb; i<=ee; i++)
00154 {
00155 while (i >= _tmpMask.length())
00156 _tmpMask += "0";
00157 const_cast<char*>(_tmpMask.c_str())[i] = '1';
00158 }
00159 }
00160
00161 bool M::readFeature(Feature& f, unsigned long step)
00162 {
00163 bool ok;
00164 if (!_useMask)
00165 ok = _pInput->readFeature(f, step);
00166 else if ( (ok = _pInput->readFeature(_feature, step)) )
00167 {
00168 f.setVectSize(K::k, _selectionSize);
00169 f.copySelectedData(_feature, _selection);
00170 }
00171 _error = _pInput->getError();
00172 return ok;
00173 }
00174
00175 bool M::writeFeature(const Feature& f, unsigned long step)
00176 {
00177 bool ok;
00178 if (!_useMask)
00179 ok = _pInput->writeFeature(f, step);
00180 else
00181 {
00182 if (_selectionSize != f.getVectSize())
00183 throw Exception("Invalid feature mask : " + _mask,
00184 __FILE__, __LINE__);
00185 _feature.setVectSize(K::k, _pInput->getVectSize());
00186 Feature::data_t* outputVect = _feature.getDataVector();
00187 Feature::data_t* inputVect = f.getDataVector();
00188 for (unsigned long i=0; i<_selectionSize; i++)
00189 outputVect[_selection[i]] = inputVect[i];
00190 ok = _pInput->writeFeature(_feature, step);
00191 }
00192 _error = _pInput->getError();
00193 return ok;
00194 }
00195
00196 unsigned long M::getFeatureCount() { return _pInput->getFeatureCount(); }
00197
00198 unsigned long M::getVectSize()
00199 {
00200 if (_useMask)
00201 return _selectionSize;
00202 return _pInput->getVectSize();
00203 }
00204
00205 const FeatureFlags& M::getFeatureFlags()
00206 { return _pInput->getFeatureFlags(); }
00207
00208
00209 void M::seekFeature(unsigned long i, const String& s)
00210 { _pInput->seekFeature(i, s); }
00211
00212 real_t M::getSampleRate() { return _pInput->getSampleRate(); }
00213
00214 void M::reset() { _pInput->reset(); }
00215
00216 void M::close() { _pInput->close(); }
00217
00218 unsigned long M::getSourceCount() {return _pInput->getSourceCount();}
00219
00220 unsigned long M::getFeatureCountOfASource(unsigned long srcIdx)
00221 { return _pInput->getFeatureCountOfASource(srcIdx); }
00222
00223 unsigned long M::getFeatureCountOfASource(const String& f)
00224 { return _pInput->getFeatureCountOfASource(f); }
00225
00226 unsigned long M::getFirstFeatureIndexOfASource(unsigned long srcIdx)
00227 { return _pInput->getFirstFeatureIndexOfASource(srcIdx); }
00228
00229 unsigned long M::getFirstFeatureIndexOfASource(const String& srcName)
00230 { return _pInput->getFirstFeatureIndexOfASource(srcName); }
00231
00232 const String& M::getNameOfASource(unsigned long srcIdx)
00233 { return _pInput->getNameOfASource(srcIdx); }
00234
00235 String M::getClassName() const { return "FeatureInputStreamModifier"; }
00236
00237 String M::toString() const
00238 {
00239 String s = FeatureInputStream::toString()
00240 + "\n input stream = " + _pInput->getClassName()
00241 + "[" + getAddress() + "]";
00242 if (_useMask)
00243 s += "\n mask = '" + _mask + "'";
00244 else
00245 s += "\n mask = no mask";
00246 return s;
00247 }
00248
00249 M::~FeatureInputStreamModifier()
00250 {
00251 if (_ownStream)
00252 delete _pInput;
00253 }
00254
00255
00256 #endif // !defined(ALIZE_FeatureInputStreamModifier_cpp)
00257