FileInfo.cpp

Go to the documentation of this file.
00001 /*
00002 This file is part of LIA_RAL which is a set of software based on ALIZE
00003 toolkit for speaker recognition. ALIZE toolkit is required to use LIA_RAL.
00004 
00005 LIA_RAL project is a development project was initiated by the computer
00006 science laboratory of Avignon / France (Laboratoire Informatique d'Avignon -
00007 LIA) [http://lia.univ-avignon.fr <http://lia.univ-avignon.fr/>]. Then it
00008 was supported by two national projects of the French Research Ministry:
00009         - TECHNOLANGUE program [http://www.technolangue.net]
00010         - MISTRAL program [http://mistral.univ-avignon.fr]
00011 
00012 LIA_RAL is free software: you can redistribute it and/or modify
00013 it under the terms of the GNU Lesser General Public License as
00014 published by the Free Software Foundation, either version 3 of
00015 the License, or any later version.
00016 
00017 LIA_RAL is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00020 GNU Lesser General Public License for more details.
00021 
00022 You should have received a copy of the GNU Lesser General Public
00023 License along with LIA_RAL.
00024 If not, see [http://www.gnu.org/licenses/].
00025 
00026 The LIA team as well as the LIA_RAL project team wants to highlight the
00027 limits of voice authentication in a forensic context.
00028 The "Person Authentification by Voice: A Need of Caution" paper
00029 proposes a good overview of this point (cf. "Person
00030 Authentification by Voice: A Need of Caution", Bonastre J.F.,
00031 Bimbot F., Boe L.J., Campbell J.P., Douglas D.A., Magrin-
00032 chagnolleau I., Eurospeech 2003, Genova].
00033 The conclusion of the paper of the paper is proposed bellow:
00034 [Currently, it is not possible to completely determine whether the
00035 similarity between two recordings is due to the speaker or to other
00036 factors, especially when: (a) the speaker does not cooperate, (b) there
00037 is no control over recording equipment, (c) recording conditions are not
00038 known, (d) one does not know whether the voice was disguised and, to a
00039 lesser extent, (e) the linguistic content of the message is not
00040 controlled. Caution and judgment must be exercised when applying speaker
00041 recognition techniques, whether human or automatic, to account for these
00042 uncontrolled factors. Under more constrained or calibrated situations,
00043 or as an aid for investigative purposes, judicious application of these
00044 techniques may be suitable, provided they are not considered as infallible.
00045 At the present time, there is no scientific process that enables one to
00046 uniquely characterize a persones voice or to identify with absolute
00047 certainty an individual from his or her voice.]
00048 
00049 Copyright (C) 2004-2010
00050 Laboratoire d'informatique d'Avignon [http://lia.univ-avignon.fr]
00051 LIA_RAL admin [alize@univ-avignon.fr]
00052 Jean-Francois Bonastre [jean-francois.bonastre@univ-avignon.fr]
00053 */
00054 
00055 #if !defined(ALIZE_FileInfo_cpp)
00056 #define ALIZE_FileInfo_cpp
00057 
00058 #if defined(_WIN32)
00059 #pragma warning( disable : 4127 4702)
00060 #endif
00061 
00062 #include "FileInfo.h"
00063 #include "Exception.h"
00064 
00065 using namespace alize;
00066 
00067 //-------------------------------------------------------------------------
00068 FileInfo::FileInfo(const FileName & f):Object(), _pFileStruct(NULL),
00069 _fileName(f), _swap(false)
00070 {
00071 }
00072 
00073 //-------------------------------------------------------------------------
00074 bool FileInfo::isClosed() const 
00075 {
00076   return _pFileStruct == NULL;
00077 }
00078 
00079 //-------------------------------------------------------------------------
00080 bool FileInfo::isOpen() const 
00081 {
00082   return _pFileStruct != NULL;
00083 }
00084 
00085 //-------------------------------------------------------------------------
00086 void FileInfo::open(int mode)
00087 {
00088   if (isOpen())
00089     close();
00090   if (_fileName.isEmpty())
00091     throw Exception("empty file name", __FILE__, __LINE__);
00092   if (mode == 1)
00093     _pFileStruct =::fopen(_fileName.c_str(), "wb");
00094   else if (mode == 2)
00095     _pFileStruct =::fopen(_fileName.c_str(), "rb");
00096   if (_pFileStruct == NULL)
00097     throw IOException("Cannot create new file", __FILE__, __LINE__,
00098     _fileName);
00099 }
00100 
00101 //-------------------------------------------------------------------------
00102 void FileInfo::close()
00103 {
00104   if (isOpen())
00105     if (::fclose(_pFileStruct) == EOF)
00106       throw IOException("Cannot close file", __FILE__, __LINE__, _fileName);
00107   _pFileStruct = NULL;
00108 }
00109 
00110 //-------------------------------------------------------------------------
00111 void FileInfo::writeTopInfo(const LKVector & v, Config & c)
00112 {
00113   if (isClosed())
00114     open(1);                    // can throw Exception if file name = ""
00115   assert(_pFileStruct != NULL);
00116   for (int i = 0; i < c.getParam("topDistribsCount").toLong(); i++)
00117     if (::fwrite(&(v.getArray()[i].idx), sizeof(v.getArray()[i].idx), 1,
00118         _pFileStruct) != 1)
00119       throw IOException("Cannot write in file", __FILE__, __LINE__,
00120     _fileName);
00121 
00122   if (::fwrite(&v.sumNonTopDistribLK, sizeof(v.sumNonTopDistribLK), 1,
00123       _pFileStruct) != 1)
00124     throw IOException("Cannot write in file", __FILE__, __LINE__, _fileName);
00125 
00126   if (::fwrite(&v.sumNonTopDistribWeights, sizeof(v.sumNonTopDistribWeights),
00127       1, _pFileStruct) != 1)
00128     throw IOException("Cannot write in file", __FILE__, __LINE__, _fileName);
00129 
00130 }
00131 
00132 //-------------------------------------------------------------------------
00133 void FileInfo::writeTopInfo(RealVector <real_t> & v, Config & c)
00134 {
00135   if (isClosed())
00136     open(1);                    // can throw Exception if file name = ""
00137   assert(_pFileStruct != NULL);
00138 
00139   for (unsigned long i = 0; i < v.size(); i++){
00140                 if (i<c.getParam("topDistribsCount").toULong()){                        
00141                         unsigned long tmp=(unsigned long)v[i];
00142                         if (::fwrite(&tmp, sizeof(tmp), 1,_pFileStruct) != 1)
00143                         throw IOException("Cannot write in file", __FILE__, __LINE__, _fileName);
00144                 }
00145                 else {
00146                          if (::fwrite(&v[i], sizeof(v[i]), 1,_pFileStruct) != 1)
00147                          throw IOException("Cannot write in file", __FILE__, __LINE__, _fileName);
00148                 }
00149                    
00150 }
00151 
00152 }
00153 
00154 //-------------------------------------------------------------------------
00155 void FileInfo::loadTopInfo(StatServer & ss, unsigned long &numLigne,
00156   Config & c)
00157 {
00158   ULongVector index;
00159   double sumNonSelectedWeights;
00160   double sumNonSelectedLLK;
00161   unsigned long s;
00162   if (isClosed())
00163     {
00164       open(2);                  // can throw Exception if file name = ""
00165     }
00166 
00167   assert(_pFileStruct != NULL);
00168   unsigned long pos =    numLigne * (c.getParam("topDistribsCount").toLong() *    sizeof(unsigned long) + 2 * sizeof(real_t));
00169 
00170 
00171   if (::fseek(_pFileStruct, pos, SEEK_SET) != 0)
00172     throw IOException("seek out of bounds", __FILE__, __LINE__, _fileName);
00173 
00174   for (int i = 0; i < c.getParam("topDistribsCount").toLong(); i++)
00175     {
00176       ::fread(&s, 1, sizeof(unsigned long), _pFileStruct);
00177       index.addValue(s);
00178     }
00179 
00180   ::fread(&sumNonSelectedLLK, 1, sizeof(double), _pFileStruct);
00181 
00182   ::fread(&sumNonSelectedWeights, 1, sizeof(double), _pFileStruct);
00183   ss.setTopDistribIndexVector(index, sumNonSelectedWeights, sumNonSelectedLLK); //SET the top Component
00184 
00185 
00186 
00187 }
00188 
00189 //-------------------------------------------------------------------------
00190 String FileInfo::toString() const 
00191 {
00192   return Object::toString() + "\n  file name = '" + _fileName;
00193 }
00194 
00195 //-------------------------------------------------------------------------
00196 String FileInfo::getClassName() const 
00197 {
00198   return "FileInfo";
00199 }
00200 
00201 //-------------------------------------------------------------------------
00202 FileInfo::~FileInfo()
00203 {
00204   close();
00205 }
00206 
00207 //-------------------------------------------------------------------------
00208 
00209 #endif // !defined(ALIZE_FileInfo_cpp)