FileWriter.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_FileWriter_cpp)
00056 #define ALIZE_FileWriter_cpp
00057 
00058 #if defined(_WIN32)
00059 #define _CRT_SECURE_NO_WARNINGS
00060 #endif
00061 
00062 #include "FileWriter.h"
00063 #include "Exception.h"
00064 
00065 using namespace alize;
00066 
00067 //-------------------------------------------------------------------------
00068 FileWriter::FileWriter(const FileName& f)
00069 :Object(), _pFileStruct(NULL) , _fileName(f), _swap(false){}
00070 //-------------------------------------------------------------------------
00071 bool FileWriter::isClosed() const { return _pFileStruct == NULL; }
00072 //-------------------------------------------------------------------------
00073 bool FileWriter::isOpen() const { return _pFileStruct != NULL; }
00074 //-------------------------------------------------------------------------
00075 void FileWriter::open()
00076 {
00077   if (isOpen())
00078     close();
00079   if (_fileName.isEmpty())
00080     throw Exception("empty file name", __FILE__, __LINE__);
00081   _pFileStruct = ::fopen(_fileName.c_str(),"wb");
00082   if (_pFileStruct == NULL)
00083     throw IOException("Cannot create new file", __FILE__, __LINE__,
00084                _fileName);
00085 }
00086 //-------------------------------------------------------------------------
00087 void FileWriter::close()
00088 {
00089   if (isOpen())
00090     if (::fclose(_pFileStruct) == EOF)
00091       throw IOException("Cannot close file", __FILE__, __LINE__,
00092                  _fileName);
00093   _pFileStruct = NULL;
00094 }
00095 //-------------------------------------------------------------------------
00096 void FileWriter::writeUInt4(unsigned long v)
00097 {
00098   assert(_pFileStruct != NULL);
00099   if (sizeof(unsigned int) == 4)
00100   {
00101     if (::fwrite(&v, sizeof(unsigned int), 1, _pFileStruct) != 1)
00102       throw IOException("Cannot write in file", __FILE__, __LINE__,
00103                  _fileName);
00104   }
00105   else if (sizeof(unsigned long) == 4)
00106   {
00107     if (::fwrite(&v, sizeof(v), 1, _pFileStruct) != 1)
00108       throw IOException("Cannot write in file", __FILE__, __LINE__,
00109                 _fileName);
00110   }
00111   else
00112     return; // TODO : what to do ?
00113 }
00114 //-------------------------------------------------------------------------
00115 void FileWriter::writeDouble(double v)
00116 {
00117   assert(_pFileStruct != NULL);
00118   if (::fwrite(&v, sizeof(v), 1, _pFileStruct) != 1)
00119     throw IOException("Cannot write in file", __FILE__, __LINE__,
00120                _fileName);
00121 }
00122 //-------------------------------------------------------------------------
00123 void FileWriter::writeFloat(float v)
00124 {
00125   assert(_pFileStruct != NULL);
00126   if (::fwrite(&v, sizeof(v), 1, _pFileStruct) != 1)
00127     throw IOException("Cannot write in file", __FILE__, __LINE__,
00128                _fileName);
00129 }
00130 //-------------------------------------------------------------------------
00131 void FileWriter::writeShort(short v)
00132 {
00133   assert(_pFileStruct != NULL);
00134   if (::fwrite(&v, sizeof(v), 1, _pFileStruct) != 1)
00135     throw IOException("Cannot write in file", __FILE__, __LINE__,
00136                _fileName);
00137 }
00138 //-------------------------------------------------------------------------
00139 void FileWriter::writeChar(char v)
00140 {
00141   assert(_pFileStruct != NULL);
00142   if (::fwrite(&v, sizeof(v), 1, _pFileStruct) != 1)
00143     throw IOException("Cannot write in file", __FILE__, __LINE__,
00144                _fileName);
00145 }
00146 //-------------------------------------------------------------------------
00147 void FileWriter::writeString(const String& string)
00148 {
00149   if (string.isEmpty())
00150     return;
00151   assert(_pFileStruct != NULL);
00152   if (::fwrite(string.c_str(), string.length(), 1, _pFileStruct) != 1)
00153     throw IOException("Cannot write in file", __FILE__, __LINE__,
00154                _fileName);
00155 }
00156 //-------------------------------------------------------------------------
00157 void FileWriter::writeAttribute(const String& name, const String& value)
00158 {
00159   //assert(false); // transformer les < > &... idem pour FileReader
00160   writeString(" " + name + "=\"" + value + "\"");
00161 }
00162 //-------------------------------------------------------------------------
00163 void FileWriter::writeAttribute(const String& name, unsigned long value)
00164 { writeString(" " + name + "=\"" + String::valueOf(value) + "\""); }
00165 //-------------------------------------------------------------------------
00166 void FileWriter::writeAttribute(const String& name, double value)
00167 { writeString(" " + name + "=\"" + String::valueOf(value) + "\""); }
00168 //-------------------------------------------------------------------------
00169 String FileWriter::toString() const
00170 {
00171   return Object::toString()
00172     + "\n  file name = '" + _fileName;
00173 }
00174 //-------------------------------------------------------------------------
00175 void FileWriter::swap2Bytes(void *src, void *dest)
00176 {
00177   char *p1,*p2;
00178   char tmp;
00179  
00180   p1 = (char*)src;
00181   p2 = (char*)dest;
00182   tmp = p1[1]; /* pour pouvoir utiliser src == dest */
00183   p2[1] = p1[0];
00184   p2[0] = tmp;
00185 }
00186 //-------------------------------------------------------------------------
00187 void FileWriter::swap4Bytes(void *src, void *dest)
00188 {
00189   char *p1,*p2;
00190   char tmp;
00191  
00192   p1 = (char*)src;
00193   p2 = (char*)dest;
00194   tmp = p1[3]; /* pour pouvoir utiliser src == dest */
00195   p2[3] = p1[0];
00196   p2[0] = tmp;
00197   tmp = p1[2]; /* pour pouvoir utiliser src == dest */
00198   p2[2] = p1[1];
00199   p2[1] = tmp;
00200 }
00201 //-------------------------------------------------------------------------
00202 void FileWriter::swap8Bytes(void *src, void *dest)
00203 {
00204   char *p1,*p2;
00205   char tmp;
00206   int i;
00207  
00208   p1 = (char*)src;
00209   p2 = (char*)dest;
00210   for (i=0; i<4; i++)
00211   {
00212     tmp = p1[i]; /* pour pouvoir utiliser src == dest */
00213     p1[i] = p2[7-i];
00214     p2[7-i] = tmp;
00215   }
00216 }
00217 //-------------------------------------------------------------------------
00218 String FileWriter::getClassName() const { return "FileWriter"; }
00219 //-------------------------------------------------------------------------
00220 FileWriter::~FileWriter() { close(); }
00221 //-------------------------------------------------------------------------
00222 
00223 #endif // !defined(ALIZE_FileWriter_cpp)
00224