FileReader.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_FileReader_cpp)
00056 #define ALIZE_FileReader_cpp
00057 
00058 #if defined(_WIN32)
00059 #define _CRT_SECURE_NO_WARNINGS
00060 #endif
00061 
00062 #include <new>
00063 #include "FileReader.h"
00064 #include "Exception.h"
00065 #include "RealVector.h"
00066 //#include <iostream>
00067 //using namespace std;
00068 using namespace alize;
00069 typedef FileReader R;
00070 
00071 //-------------------------------------------------------------------------
00072 R::FileReader(const FileName& f, const String& path,
00073               const String& extension, bool swap)
00074 :Object(), _fullFileName(path + f + extension), _pFileStruct(NULL),
00075  _fileName(f), _path(path), _extension(extension), 
00076  _fileLengthDefined(false), _swap(swap) {}
00077 //-------------------------------------------------------------------------
00078 R& R::create(const FileName& f, const String& path, const String& ext,
00079              bool swap)
00080 {
00081   R* p = new (std::nothrow) R(f, path, ext, swap);
00082   assertMemoryIsAllocated(p, __FILE__, __LINE__);
00083   return *p;
00084 }
00085 //-------------------------------------------------------------------------
00086 bool R::isClosed() const { return _pFileStruct == NULL; }
00087 //-------------------------------------------------------------------------
00088 bool R::isOpen() const { return _pFileStruct != NULL; }
00089 //-------------------------------------------------------------------------
00090 void R::reset()
00091 {
00092   if (isOpen())
00093     seek(0);
00094 }
00095 //-------------------------------------------------------------------------
00096 void R::open()
00097 {
00098   close();
00099   _pFileStruct = ::fopen(_fullFileName.c_str(),"rb");
00100   if (_pFileStruct == NULL)
00101     throw FileNotFoundException("", __FILE__, __LINE__, _fullFileName);
00102 }
00103 //-------------------------------------------------------------------------
00104 void R::close()
00105 {
00106   if (isOpen())
00107     if (::fclose(_pFileStruct) == EOF)
00108       throw IOException("Cannot close file", __FILE__, __LINE__,
00109                  _fullFileName);
00110   _pFileStruct = NULL;
00111 }
00112 //-------------------------------------------------------------------------
00113 const FileName& R::getFullFileName() const { return _fullFileName; }
00114 //-------------------------------------------------------------------------
00115 const FileName& R::getFileName() const { return _fileName; }
00116 //-------------------------------------------------------------------------
00117 unsigned long R::getFileLength()
00118 {
00119   if (!_fileLengthDefined)
00120   {
00121     bool wasOpened = isOpen();
00122     unsigned long pos;
00123     long l;
00124 
00125     if (wasOpened)
00126     {
00127       if ( (pos = ::ftell(_pFileStruct)) < 0)
00128         throw IOException("ftell", __FILE__, __LINE__, _fullFileName);
00129     }
00130     else
00131     {
00132       open();
00133       pos = 0;
00134     }
00135     if (::fseek(_pFileStruct, 0, SEEK_END) != 0)
00136       throw IOException("fseek", __FILE__, __LINE__, _fullFileName);
00137     if ( (l = ::ftell(_pFileStruct)) < 0)
00138       throw IOException("ftell", __FILE__, __LINE__, _fullFileName);
00139     _fileLengthDefined = true;
00140     _fileLength = l;
00141     if (::fseek(_pFileStruct, pos, SEEK_SET) != 0)
00142       throw IOException("fseek", __FILE__, __LINE__, _fullFileName);
00143   }
00144   return _fileLength;
00145 }
00146 //-------------------------------------------------------------------------
00147 void R::seek(unsigned long pos) // protected
00148 {
00149   if (!isOpen())
00150     open();
00151   if (::fseek(_pFileStruct, pos, SEEK_SET) != 0 )
00152     throw IOException("seek out of bounds",
00153           __FILE__, __LINE__, _fullFileName);
00154 }
00155 //-------------------------------------------------------------------------
00156 void R::read(void* buffer, unsigned long length) // private
00157 {
00158   assert(buffer != NULL); // TODO : if public method, throw an Exception ?
00159   if (isClosed())
00160     open(); // can throw Exception if file name = ""
00161   if (::fread(buffer, 1, length, _pFileStruct) == length)
00162     return;
00163 
00164   // if end of file
00165   if (feof(_pFileStruct))
00166     throw EOFException("", __FILE__, __LINE__, _fullFileName);
00167 
00168   // other cases
00169   throw IOException("Cannot read file", __FILE__, __LINE__, _fullFileName);
00170 }
00171 //-------------------------------------------------------------------------
00172 char R::readChar()
00173 {
00174   char byte;
00175   read(&byte, 1); // can throw IOException, EOFException
00176   return byte;
00177 }
00178 //-------------------------------------------------------------------------
00179 int R::readInt2()
00180 {
00181   if (sizeof(short) == 2)
00182   {
00183     short s;
00184     read(&s, 2); // can throw IOException, EOFException
00185     if (_swap)
00186       swap2Bytes(&s, &s);
00187     return s;
00188   }
00189   else
00190     return 0; // pas terrible comme sortie...
00191 }
00192 //-------------------------------------------------------------------------
00193 long R::readInt4()
00194 {
00195   // Modified june 4th 2004 for IA 64 sizeof(long) = 8
00196 
00197   if (sizeof(long) == 4)
00198   {
00199     long s;
00200     read(&s, 4); // can throw IOException, EOFException
00201     if (_swap)
00202       swap4Bytes(&s);
00203     return s;
00204   }
00205   else if (sizeof(int) == 4)
00206   {
00207     int s;
00208     read(&s, 4); // can throw IOException, EOFException
00209     if (_swap)
00210       swap4Bytes(&s);
00211     return (long)s;
00212   }
00213   else
00214     return 0; // TODO : sortie a ameliorer
00215 }
00216 //-------------------------------------------------------------------------
00217 unsigned long R::readUInt4()
00218 {
00219   // Modified june 4th 2004 for IA 64 sizeof(unsigned long) = 8
00220 
00221   if (sizeof(unsigned long) == 4)
00222   {
00223     unsigned long s;
00224     read(&s, 4); // can throw IOException, EOFException
00225     if (_swap)
00226       swap4Bytes(&s);
00227     return s;
00228   }
00229   if (sizeof(unsigned int) == 4)
00230   {
00231     unsigned int s;
00232     read(&s, 4); // can throw IOException, EOFException
00233     if (_swap)
00234       swap4Bytes(&s);
00235     return (unsigned long)s;
00236   }
00237   return 0;
00238 }
00239 //-------------------------------------------------------------------------
00240 double R::readDouble()
00241 {
00242   double s;
00243   read(&s, 8); // can throw IOException, EOFException
00244   if (_swap)
00245     swap8Bytes(&s, &s);
00246   return s;
00247 }
00248 //-------------------------------------------------------------------------
00249 FloatVector& R::readFloats(FloatVector& v)
00250 {
00251   if (_swap)
00252   {
00253     float* array = v.getArray();
00254     unsigned long n = v.size();
00255     read(array, n*4);
00256     for (unsigned long i=0; i<n; i++)
00257       swap4Bytes(&array[i]);
00258   }
00259   else
00260     read(v.getArray(), v.size()*4);
00261   return v;
00262 }
00263 //-------------------------------------------------------------------------
00264 unsigned long R::readSomeFloats(FloatVector& v)
00265 {
00266   //static unsigned long f = 0;
00267   //f++;
00268   //if (f%10 == 0)
00269   //  cout << f << endl;
00270   if (isClosed())
00271     open(); // can throw Exception if file name = ""
00272   float* array = v.getArray();
00273   unsigned long n = (unsigned long)(::fread(array, 4, v.size(), _pFileStruct));
00274   if (_swap)
00275   {
00276     char* p = (char*)array;
00277     char t;
00278     for (unsigned long i=0; i<n; i++)
00279     {
00280       t = p[3]; p[3] = p[0]; p[0] = t;
00281       t = p[2]; p[2] = p[1]; p[1] = t;
00282       p += 4;
00283     }
00284   }
00285   return n;
00286 }
00287 //-------------------------------------------------------------------------
00288 float R::readFloat()
00289 {
00290   float s;
00291   read(&s, 4); // can throw IOException, EOFException
00292   if (_swap)
00293     swap4Bytes(&s);
00294   return s;
00295 }
00296 //-------------------------------------------------------------------------
00297 const String& R::readString(unsigned long length)
00298 {
00299   _string.reset();
00300   if (length != 0)
00301   {
00302     char* str = new (std::nothrow) char[length+1];
00303     assertMemoryIsAllocated(str, __FILE__, __LINE__);
00304     try { read(str, length); } // can throw IOException, EOFException
00305     catch (Exception&)
00306     {
00307       delete [] str;
00308       throw; // do not use 'throw e'
00309     }
00310     str[length] = 0;
00311     _string = str;
00312     delete [] str;
00313   }
00314   return _string;
00315 }
00316 //-------------------------------------------------------------------------
00317 const String& R::readLine()
00318 {
00319 
00320   char t[2];
00321   t[1] = 0;
00322   _string.reset();
00323 
00324   while (true)
00325   {
00326     try { read(&t, 1); } // can throw IOException, EOFException
00327     catch (EOFException&)
00328     {
00329       if (_string.length() != 0)
00330         return _string;
00331       throw; // do not use 'throw e'
00332     }
00333 
00334     if (t[0] == '\n')
00335       return _string;
00336     if (t[0] == '\r')
00337     {
00338       try { read(&t, 1); } // can throw IOException, EOFException
00339       catch (EOFException&)
00340       {
00341         if (_string.length() != 0)
00342           return _string;
00343         throw; // do not use 'throw e'
00344       }
00345       if (t[0] == '\n')
00346         return _string;
00347     }
00348     _string += t;
00349   }
00350 }
00351 //-------------------------------------------------------------------------
00352 void R::rewind() {seek(0);}
00353 //-------------------------------------------------------------------------
00354 long R::tell()
00355 {
00356   if (isClosed())
00357     open();
00358   return ::ftell(_pFileStruct);
00359 }
00360 //-------------------------------------------------------------------------
00361 bool& R::swap() { return _swap; }
00362 //-------------------------------------------------------------------------
00363 void R::swap2Bytes(void *src, void *dest)
00364 {
00365   char *p1,*p2;
00366   char tmp;
00367  
00368   p1 = (char*)src;
00369   p2 = (char*)dest;
00370   tmp = p1[1]; /* pour pouvoir utiliser src == dest */
00371   p2[1] = p1[0];
00372   p2[0] = tmp;
00373 }
00374 //-------------------------------------------------------------------------
00375 void R::swap4Bytes(void *src, void *dest)
00376 {
00377   char *p1,*p2;
00378   char tmp;
00379  
00380   p1 = (char*)src;
00381   p2 = (char*)dest;
00382   tmp = p1[3]; /* pour pouvoir utiliser src == dest */
00383   p2[3] = p1[0];
00384   p2[0] = tmp;
00385   tmp = p1[2]; /* pour pouvoir utiliser src == dest */
00386   p2[2] = p1[1];
00387   p2[1] = tmp;
00388 }
00389 //-------------------------------------------------------------------------
00390 void R::swap4Bytes(void *src)
00391 {
00392   char* p = (char*)src;
00393   char t;
00394   t = p[3]; p[3] = p[0]; p[0] = t;
00395   t = p[2]; p[2] = p[1]; p[1] = t;
00396 }
00397 //-------------------------------------------------------------------------
00398 void R::swap8Bytes(void *src, void *dest)
00399 {
00400   char *p1,*p2;
00401   char tmp;
00402   int i;
00403  
00404   p1 = (char*)src;
00405   p2 = (char*)dest;
00406   for (i=0; i<4; i++)
00407   {
00408     tmp = p1[i]; /* pour pouvoir utiliser src == dest */
00409     p1[i] = p2[7-i];
00410     p2[7-i] = tmp;
00411   }
00412 }
00413 //-------------------------------------------------------------------------
00414 String R::toString() const
00415 {
00416   return Object::toString()
00417     + "\n  full file name = '" + getFullFileName();
00418 }
00419 //-------------------------------------------------------------------------
00420 String R::getClassName() const { return "FileReader"; }
00421 //-------------------------------------------------------------------------
00422 R::~FileReader() { close(); }
00423 //-------------------------------------------------------------------------
00424 
00425 #endif // !defined(ALIZE_FileReader_cpp)
00426