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_FeatureServer_cpp)
00056 #define ALIZE_FeatureServer_cpp
00057
00058 #include <new>
00059 #include "FeatureServer.h"
00060 #include "Feature.h"
00061 #include "alizeString.h"
00062 #include "FeatureFileReader.h"
00063 #include "FeatureInputStreamModifier.h"
00064 #include "Config.h"
00065 #include "XLine.h"
00066
00067 using namespace alize;
00068 typedef FeatureServer S;
00069
00070
00071 S::FeatureServer()
00072 :_pInputStream(NULL) {}
00073
00074 S::FeatureServer(const Config& c)
00075 :_pInputStream(NULL) { init(c); }
00076
00077 void S::init(const Config& c)
00078 {
00079 releaseAll();
00080 FeatureInputStream::init(c);
00081 _pInputStream = &FeatureFileReader::create(c);
00082 init();
00083 }
00084
00085 S::FeatureServer(const Config& c, FeatureInputStream& s)
00086 :_pInputStream(NULL) { init(c, s); }
00087
00088 void S::init(const Config& c, FeatureInputStream& s)
00089 {
00090 releaseAll();
00091 FeatureInputStream::init(c);
00092 _ownInputStream = false;
00093 _pInputStream = &s;
00094 init();
00095 }
00096
00097 S::FeatureServer(const Config& c, const FileName& f, LabelServer& ls)
00098 :_pInputStream(NULL) { init(c, f, ls); }
00099
00100 void S::init(const Config& c, const FileName& f, LabelServer& ls)
00101 {
00102 releaseAll();
00103 FeatureInputStream::init(c);
00104 _pLabelServer = &ls;
00105 _pInputStream = &FeatureFileReader::createStream(f, c, &ls,
00106 BIGENDIAN_AUTO, defineBufferUsage(), defineBufferSize(),
00107 defineHistoricUsage(), defineHistoricSize());
00108 init();
00109 }
00110
00111 S::FeatureServer(const Config& c, const FileName& f)
00112 :_pInputStream(NULL) { init(c, f); }
00113
00114 void S::init(const Config& c, const FileName& f)
00115 {
00116 releaseAll();
00117 FeatureInputStream::init(c);
00118 _pInputStream = &FeatureFileReader::createStream(f, c, NULL,
00119 BIGENDIAN_AUTO, defineBufferUsage(), defineBufferSize(),
00120 defineHistoricUsage(), defineHistoricSize());
00121 init();
00122 }
00123
00124 S::FeatureServer(const Config& c, const XLine& l, LabelServer& ls)
00125 :_pInputStream(NULL) { init(c, l, ls); }
00126
00127 void S::init(const Config& c, const XLine& l, LabelServer& ls)
00128 {
00129 releaseAll();
00130 FeatureInputStream::init(c);
00131 _pLabelServer = &ls;
00132 _pInputStream = &FeatureFileReader::createStream(l, c, &ls,
00133 BIGENDIAN_AUTO, defineBufferUsage(), defineBufferSize(),
00134 defineHistoricUsage(), defineHistoricSize());
00135 init();
00136 }
00137
00138 S::FeatureServer(const Config& c, const XLine& l)
00139 :_pInputStream(NULL) { init(c, l); }
00140
00141 void S::init(const Config& c, const XLine& l)
00142 {
00143 releaseAll();
00144 FeatureInputStream::init(c);
00145 _pInputStream = &FeatureFileReader::createStream(l, c, NULL,
00146 BIGENDIAN_AUTO, defineBufferUsage(), defineBufferSize(),
00147 defineHistoricUsage(), defineHistoricSize());
00148 init();
00149 }
00150
00151 void S::init()
00152 {
00153 const Config& config = this->getConfig();
00154 if (config.existsParam_featureServerMask)
00155 if (_pInputStream != NULL)
00156 {
00157 _pInputStream = &FeatureInputStreamModifier::create(inputStream(),
00158 config.getParam_featureServerMask(), _ownInputStream);
00159 _ownInputStream = true;
00160 }
00161 bool vectSizeDefined = false;
00162 unsigned long vectSize = 0;
00163 try
00164 {
00165 vectSize = getVectSize();
00166 vectSizeDefined = true;
00167 }
00168 catch (Exception&) {}
00169 if (vectSizeDefined)
00170 {
00171 if (!config.existsParam_vectSize)
00172 const_cast<Config&>(config).setParam("vectSize", String::valueOf(vectSize));
00173 else if (config.getParam_vectSize() != vectSize)
00174 throw Exception("vectSize from config ("
00175 + String::valueOf(config.getParam_vectSize())
00176 + ") is not equal to vectSize from file ("
00177 + String::valueOf(getVectSize()) + ")", __FILE__, __LINE__);
00178 }
00179
00180
00181
00182
00183
00184 reset();
00185 }
00186
00187 void S::reset()
00188 {
00189 if (_pInputStream != NULL)
00190 seekFeature(0);
00191 }
00192
00193 HistoricUsage S::defineHistoricUsage() const
00194 {
00195 if (!getConfig().existsParam_featureServerBufferSize ||
00196 getConfig().getParam_featureServerBufferSize() == "ALL_FEATURES")
00197 return ALL_FEATURES;
00198 return LIMITED;
00199 }
00200
00201 unsigned long S::defineHistoricSize() const
00202 {
00203 if (!getConfig().existsParam_featureServerBufferSize)
00204 return 0;
00205 const String& s = getConfig().getParam_featureServerBufferSize();
00206 if (s == "ALL_FEATURES")
00207 return 0;
00208 return s.toULong();
00209 }
00210
00211 BufferUsage S::defineBufferUsage() const
00212 {
00213 if (getConfig().existsParam_featureServerMemAlloc)
00214 return BUFFER_USERDEFINE;
00215 return BUFFER_AUTO;
00216 }
00217
00218 unsigned long S::defineBufferSize() const
00219 {
00220 if (getConfig().existsParam_featureServerMemAlloc)
00221 return getConfig().getParam_featureServerMemAlloc();
00222 return 0;
00223 }
00224
00225 void S::close()
00226 {
00227 if (_pInputStream != NULL)
00228 inputStream().close();
00229 }
00230
00231 void S::seekFeature(unsigned long featureNbr, const String& srcName)
00232 {
00233 if (_pInputStream != NULL)
00234 inputStream().seekFeature(featureNbr, srcName);
00235 }
00236
00237 bool S::writeFeature(const Feature& f, unsigned long step)
00238 {
00239 bool ok = inputStream().writeFeature(f, step);
00240 _error = inputStream().getError();
00241 return ok;
00242 }
00243
00244 bool S::readFeature(Feature& f, unsigned long step)
00245 {
00246 if (_pInputStream == NULL)
00247 return false;
00248 bool ok = inputStream().readFeature(f, step);
00249 _error = inputStream().getError();
00250 return ok;
00251 }
00252
00253 const String& S::getServerName() const { return _serverName; }
00254
00255 void S::setServerName(const String& s) { _serverName = s; }
00256
00257 unsigned long S::getFeatureCount()
00258 {
00259 if (_pInputStream != NULL)
00260 return inputStream().getFeatureCount();
00261 return 0;
00262 }
00263
00264 unsigned long S::getVectSize() { return inputStream().getVectSize(); }
00265
00266 const FeatureFlags& S::getFeatureFlags()
00267 { return inputStream().getFeatureFlags(); }
00268
00269 real_t S::getSampleRate() { return inputStream().getSampleRate(); }
00270
00271 unsigned long S::getSourceCount()
00272 {
00273 if (_pInputStream != NULL)
00274 return inputStream().getSourceCount();
00275 return 0;
00276 }
00277
00278 unsigned long S::getFeatureCountOfASource(unsigned long srcIdx)
00279 { return inputStream().getFeatureCountOfASource(srcIdx); }
00280
00281 unsigned long S::getFeatureCountOfASource(const String& f)
00282 { return inputStream().getFeatureCountOfASource(f); }
00283
00284 unsigned long S::getFirstFeatureIndexOfASource(unsigned long srcIdx)
00285 { return inputStream().getFirstFeatureIndexOfASource(srcIdx); }
00286
00287 unsigned long S::getFirstFeatureIndexOfASource(const String& srcName)
00288 { return inputStream().getFirstFeatureIndexOfASource(srcName); }
00289
00290 const String& S::getNameOfASource(unsigned long srcIdx)
00291 { return inputStream().getNameOfASource(srcIdx); }
00292
00293 FeatureInputStream& S::inputStream()
00294 {
00295 if (_pInputStream == NULL)
00296 throw Exception("No feature stream in this server", __FILE__, __LINE__);
00297 return *_pInputStream;
00298 }
00299
00300 String S::toString() const
00301 {
00302 String s = Object::toString()
00303 + "\n serverName = '" + _serverName + "'";
00304 if (_pInputStream != NULL)
00305 {
00306 s += "\n vectSize = " + String::valueOf(_pInputStream->getVectSize())
00307 + "\n flags = " + _pInputStream->getFeatureFlags().getString();
00308 }
00309 else
00310 s += "\n vectSize = UNKNOWN\n flags = UNKNOWN";
00311 return s;
00312 }
00313
00314 String S::getClassName() const { return "FeatureServer"; }
00315
00316 void S::releaseAll()
00317 {
00318 if (_pInputStream != NULL && _ownInputStream)
00319 delete _pInputStream;
00320 _pInputStream = NULL;
00321 _pLabelServer = NULL;
00322 _ownInputStream = true;
00323 }
00324
00325 S::~FeatureServer() { releaseAll(); }
00326
00327
00328 #endif // !defined(ALIZE_FeatureServer_cpp)
00329