FeatureFileReaderSPro4.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_FeatureFileReaderSPro4_cpp)
00056 #define ALIZE_FeatureFileReaderSPro4_cpp
00057 
00058 #include <new>
00059 #include "FeatureFileReaderSPro4.h"
00060 #include "FileReader.h"
00061 #include "Feature.h"
00062 #include "Exception.h"
00063 #include "LabelServer.h"
00064 #include "Label.h"
00065 #include "Config.h"
00066 
00067 using namespace alize;
00068 typedef FeatureFileReaderSPro4 R;
00069 
00070 //-------------------------------------------------------------------------
00071 R::FeatureFileReaderSPro4(const FileName& f, const Config& c,
00072       LabelServer* l, BigEndian be, BufferUsage b, unsigned long bufferSize,
00073       HistoricUsage h, unsigned long historicSize)
00074 :FeatureFileReaderSingle(&FileReader::create(f, getPath(f, c),
00075  getExt(f, c), getBigEndian(c, be)), NULL, c, l, b, bufferSize, h, historicSize),
00076  _paramDefined(false) {}
00077 //-------------------------------------------------------------------------
00078 R& R::create(const FileName& f, const Config& c, LabelServer* l,
00079              BigEndian be, BufferUsage b, unsigned long bufferSize,
00080              HistoricUsage h, unsigned long historicSize)
00081 {
00082   R* p = new (std::nothrow)
00083          FeatureFileReaderSPro4(f, c, l, be, b, bufferSize, h, historicSize);
00084   assertMemoryIsAllocated(p, __FILE__, __LINE__);
00085   return *p;
00086 }
00087 //-------------------------------------------------------------------------
00088 void R::readParams() // private virtual
00089 {
00090   assert(_pReader != NULL);
00091   _pReader->open(); // can throw FileNotFoundException
00092 
00093   if (!readHeader())
00094   {
00095     _pReader->close();
00096     throw InvalidDataException("Wrong header", __FILE__, __LINE__,
00097                   _pReader->getFullFileName());
00098   }
00099 }
00100 //-------------------------------------------------------------------------
00101 unsigned long R::getFeatureCount()
00102 {
00103   if (!_paramDefined)
00104     readParams();  // can throw FileNotFoundException
00105   return _featureCount;
00106 }
00107 //-------------------------------------------------------------------------
00108 unsigned long R::getVectSize()
00109 {
00110   if (!_paramDefined)
00111     readParams(); // can throw FileNotFoundException
00112   return _vectSize;
00113 }
00114 //-------------------------------------------------------------------------
00115 const FeatureFlags& R::getFeatureFlags()
00116 {
00117   if (!_paramDefined)
00118     readParams(); // can throw FileNotFoundException
00119   return _flags;
00120 }
00121 //-------------------------------------------------------------------------
00122 real_t R::getSampleRate()
00123 {
00124   if (!_paramDefined)
00125     readParams(); // can throw FileNotFoundException
00126   return _sampleRate;
00127 }
00128 //-------------------------------------------------------------------------
00129 unsigned long R::getHeaderLength()
00130 {
00131   if (!_paramDefined)
00132     readParams(); // can throw FileNotFoundException
00133   return _headerLength;
00134 }
00135 //-------------------------------------------------------------------------
00136 String R::getClassName() const { return "FeatureFileReaderSPro4";}
00137 //-------------------------------------------------------------------------
00138 bool R::readHeader()
00139 {
00140   assert(_pReader != NULL);
00141   const int WITHE = 0x01; /* Data is with log energy         */
00142   //const int WITHZ = 0x02; /* Data as mean suppressed
00143   //                           (const only)*/
00144   //const int WITHN = 0x04; /* Absolute log energy suppressed    */
00145   const int WITHD = 0x08; /* Data as delta coefficients      */
00146   const int WITHA = 0xa0; /* Data contains delta-delta coeffs    */
00147   const int WITHNODE = 0x10; /* */
00148 
00149   if (_pReader->readString(9) == "<header>\n")
00150     while(true)
00151     {
00152       String line = _pReader->readLine();
00153       unsigned long l = line.length();
00154       if (l == 0)
00155         continue;
00156       if (line.getToken(0) == "</header>")
00157         break;
00158       const char* s = line.c_str();
00159       String name, value;
00160       unsigned long i;
00161       for (i=0; i<l&&s[i]!=';'&&s[i]!='='&&(s[i]==' '||s[i]=='\t'); i++)
00162         ;
00163       for (; i<l&&s[i]!=';'&&s[i]!='='&&s[i]!=' '&&s[i]!='\t'; i++)
00164         name += line[i];
00165       for (; i<l&&s[i]!=';'&&s[i]!='='; i++)
00166         ;
00167       for (; i<l&&s[i]!=';'&&(s[i]==' '||s[i]=='\t'); i++)
00168         ;
00169       for (; i<l&&s[i]!=';'&&s[i]!=' '&&s[i]!='\t'; i++)
00170       {
00171         value += line[i];
00172       }
00173       if (name.isEmpty() && value.isEmpty())
00174         return false;
00175       // ... use name & value
00176     }
00177   else
00178     _pReader->rewind();
00179 
00180   _vectSize = _pReader->readInt2();
00181   unsigned long flag = _pReader->readUInt4();
00182   _sampleRate = (real_t)_pReader->readFloat();
00183   _headerLength = _pReader->tell();
00184   //_vectSizeStatic = _vectSize;
00185   _flags.useS = true;
00186   
00187   /* version Sylvain */
00188   if ((flag & WITHE) != 0)
00189   {
00190     _flags.useE = true;
00191     //_vectSize++;
00192     if ((flag & WITHD) != 0)
00193     {
00194       _flags.useD = true;
00195       //_vectSize += _vectSizeStatic;
00196       if ((flag & WITHNODE) == 0)
00197       {
00198         _flags.useDE = true;
00199         //_vectSize++;
00200       }
00201       if ((flag & WITHA) != 0)
00202       {
00203         _flags.useDD = true;
00204         _flags.useDDE = true;
00205         //_vectSize += _vectSizeStatic;
00206         //_vectSize++;
00207       }
00208     }
00209     else
00210     {
00211       if ((flag & WITHA) != 0)
00212       {
00213         _flags.useD = true;
00214         _flags.useDE = true;
00215         //_vectSize += _vectSizeStatic;
00216         //_vectSize++;
00217       }
00218     }
00219   }
00220   else
00221   {
00222     if ((flag & WITHD) != 0)
00223     {
00224       _flags.useD = true;
00225       //_vectSize += _vectSizeStatic;
00226       if ((flag & WITHA) != 0)
00227       {
00228         _flags.useDD = true;
00229         //_vectSize += _vectSizeStatic;
00230       }
00231     }
00232     else
00233       if ((flag & WITHA) != 0)
00234       {
00235         _flags.useDD = true;
00236         //_vectSize += _vectSizeStatic;
00237       }
00238   }
00239 
00240   if (_vectSize == 0)
00241     return false;
00242   _featureCount = (_pReader->getFileLength()-_headerLength)/(_vectSize*sizeof(float));
00243   _paramDefined = true;
00244 
00245   return true;
00246 }
00247 //-------------------------------------------------------------------------
00248 R::~FeatureFileReaderSPro4() {}
00249 //-------------------------------------------------------------------------
00250 /*
00251 Format en-tete fichier SPRO 4.0
00252 L'en-tete peut ne pas exister (pas de 1er caractere "<")
00253 * = 0, 1 or n blanks or tab
00254 
00255 <header>\n
00256 *name*=*value;optional comment\n
00257 *name*=*value;optional comment\n
00258 *name*=*value;optional comment\n
00259 ...
00260 </header>\n
00261 
00262 dimension  (short)
00263 flag     (long)
00264 frame rate (float)
00265 
00266 */
00267 
00268 #endif // !defined(ALIZE_FeatureFileReaderSPro4_cpp)
00269