FeatureServer.cpp

Go to the documentation of this file.
00001 /*
00002         This file is part of ALIZE which is an open-source tool for 
00003         speaker recognition.
00004 
00005     ALIZE is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU Lesser General Public License as 
00007     published by the Free Software Foundation, either version 3 of 
00008     the License, or any later version.
00009 
00010     ALIZE is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public 
00016     License along with ALIZE.
00017     If not, see <http://www.gnu.org/licenses/>.
00018         
00019         ALIZE is a development project initiated by the ELISA consortium
00020         [alize.univ-avignon.fr/] and funded by the French Research 
00021         Ministry in the framework of the TECHNOLANGUE program 
00022         [www.technolangue.net]
00023 
00024         The ALIZE project team wants to highlight the limits of voice
00025         authentication in a forensic context.
00026         The "Person  Authentification by Voice: A Need of Caution" paper 
00027         proposes a good overview of this point (cf. "Person  
00028         Authentification by Voice: A Need of Caution", Bonastre J.F., 
00029         Bimbot F., Boe L.J., Campbell J.P., Douglas D.A., Magrin-
00030         chagnolleau I., Eurospeech 2003, Genova].
00031         The conclusion of the paper of the paper is proposed bellow:
00032         [Currently, it is not possible to completely determine whether the 
00033         similarity between two recordings is due to the speaker or to other 
00034         factors, especially when: (a) the speaker does not cooperate, (b) there 
00035         is no control over recording equipment, (c) recording conditions are not 
00036         known, (d) one does not know whether the voice was disguised and, to a 
00037         lesser extent, (e) the linguistic content of the message is not 
00038         controlled. Caution and judgment must be exercised when applying speaker 
00039         recognition techniques, whether human or automatic, to account for these 
00040         uncontrolled factors. Under more constrained or calibrated situations, 
00041         or as an aid for investigative purposes, judicious application of these 
00042         techniques may be suitable, provided they are not considered as infallible.
00043         At the present time, there is no scientific process that enables one to 
00044         uniquely characterize a person=92s voice or to identify with absolute 
00045         certainty an individual from his or her voice.]
00046         Contact Jean-Francois Bonastre for more information about the licence or
00047         the use of ALIZE
00048 
00049         Copyright (C) 2003-2010
00050         Laboratoire d'informatique d'Avignon [lia.univ-avignon.fr]
00051         ALIZE admin [alize@univ-avignon.fr]
00052         Jean-Francois Bonastre [jean-francois.bonastre@univ-avignon.fr]
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() // private
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   /*try
00180   {
00181     const_cast<Config&>(config).setParam("vectSize", getVectSize());
00182   }
00183   catch (Exception&) {} // thrown if getVectSize() fail*/
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; // size not used
00205   const String& s = getConfig().getParam_featureServerBufferSize();
00206   if (s == "ALL_FEATURES")
00207     return 0; // size not used
00208   return s.toULong();
00209 }
00210 //-------------------------------------------------------------------------
00211 BufferUsage S::defineBufferUsage() const // private
00212 {
00213   if (getConfig().existsParam_featureServerMemAlloc)
00214     return BUFFER_USERDEFINE;
00215   return BUFFER_AUTO;
00216 }
00217 //-------------------------------------------------------------------------
00218 unsigned long S::defineBufferSize() const // private
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