MixtureServerFileWriter.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_MixtureServerFileWriter_cpp)
00056 #define ALIZE_MixtureServerFileWriter_cpp
00057 
00058 #include "MixtureServerFileWriter.h"
00059 #include "MixtureGD.h"
00060 #include "DistribGD.h"
00061 #include "Exception.h"
00062 #include "MixtureServer.h"
00063 #include "Config.h"
00064 
00065 using namespace alize;
00066 typedef MixtureServerFileWriter W;
00067 
00068 //-------------------------------------------------------------------------
00069 W::MixtureServerFileWriter(const FileName& f, const Config& c)
00070 :FileWriter(getFullFileName(c, f))
00071 {
00072   if (_fileName.endsWith(".xml"))
00073     _format = MixtureServerFileWriterFormat_XML;
00074   else
00075   {
00076     if (c.getParam_saveMixtureServerFileFormat() == MixtureServerFileWriterFormat_RAW)
00077       _format = MixtureServerFileWriterFormat_RAW;
00078     else if (c.getParam_saveMixtureServerFileFormat() == MixtureServerFileWriterFormat_XML)
00079       _format = MixtureServerFileWriterFormat_XML; // TODO : gerer des param dans la config
00080   }
00081 }
00082 //-------------------------------------------------------------------------
00083 String W::getFullFileName(const Config& c, const FileName& f) const
00084 {  // protected
00085   if (f.beginsWith("/") || f.beginsWith("./"))
00086     return f;
00087   return c.getParam_mixtureFilesPath() + f
00088        + c.getParam_saveMixtureFileExtension();
00089 }
00090 //-------------------------------------------------------------------------
00091 void W::writeMixtureServer(const MixtureServer& ms)
00092 {
00093   open(); //can throw IOException
00094   if (_format == MixtureServerFileWriterFormat_XML)
00095     writeMixtureServerXml(ms);
00096   else
00097     writeMixtureServerRaw(ms);
00098   close();
00099 }
00100 //-------------------------------------------------------------------------
00101 void W::writeMixtureServerXml(const MixtureServer& ms)
00102 {
00103   unsigned long i, c;
00104   writeString("<MixtureServer");
00105   writeAttribute("name", ms.getServerName());
00106   try { writeAttribute("vectSize", ms.getVectSize()); } catch (Exception&){}
00107   writeAttribute("mixtureCount", ms.getMixtureCount());
00108   writeAttribute("distribCount", ms.getDistribCount());
00109   writeString(">");
00110   //writeString("\n\t<DistribDict>");
00111   for (i=0; i<ms.getDistribCount(); i++)
00112   {
00113     const DistribGD* p = dynamic_cast<const DistribGD*>(&ms.getDistrib(i));
00114     if (p != NULL)
00115     {
00116       writeString("\n\t\t<DistribGD");
00117       writeAttribute("i", i);
00118       writeString(">");
00119       for (c=0; c<p->getVectSize(); c++)
00120       {
00121           writeString("\n\t\t\t<covInv");
00122           writeAttribute("i", c);
00123           writeString(">"
00124             + String::valueOf(p->getCovInv(c)) + "</covInv>");
00125       }
00126       for (c=0; c<p->getVectSize(); c++)
00127       {
00128           writeString("\n\t\t\t<mean");
00129           writeAttribute("i", c);
00130           writeString(">"
00131             + String::valueOf(p->getMean(c)) + "</mean>");
00132       }
00133       writeString("\n\t\t</DistribGD>");
00134     }
00135     else
00136       throw Exception("I don't know how to save a "
00137                + ms.getDistrib(i).getClassName()
00138                + " object", __FILE__, __LINE__);
00139   }
00140   //writeString("\n\t</DistribDict>");
00141   for (i=0; i<ms.getMixtureCount(); i++)
00142   {
00143     const MixtureGD* p = dynamic_cast<const MixtureGD*>(&ms.getMixture(i));
00144     if (p != NULL)
00145       writeMixtureGDXml(*p);
00146     else
00147       throw Exception("I don't know how to save a "
00148                + ms.getDistrib(i).getClassName()
00149                + " object", __FILE__, __LINE__);
00150   }
00151 
00152   writeString("\n</MixtureServer>");
00153 }
00154 //-------------------------------------------------------------------------
00155 void W::writeMixtureGDXml(const MixtureGD& m)
00156 {
00157   unsigned long i;
00158   writeString("\n\t<MixtureGD");
00159   writeAttribute("id", m.getId());
00160   writeAttribute("distribCount", m.getDistribCount());
00161   writeString(">");
00162 
00163   for (i=0; i< m.getDistribCount(); i++)
00164   {
00165     DistribGD& d = m.getDistrib(i);
00166     writeString("\n\t\t<DistribGD");
00167     writeAttribute("i", i);
00168     writeAttribute("dictIdx", d.dictIndex(K::k));
00169     writeAttribute("weight", m.weight(i));
00170     writeString("/>");
00171   }
00172   writeString("\n\t</MixtureGD>");
00173 }
00174 //-------------------------------------------------------------------------
00175 void W::writeMixtureServerRaw(const MixtureServer& ms)
00176 {
00177   unsigned long i, c;
00178   writeString("MixtureServer");
00179   writeUInt4(ms.getServerName().length());
00180   writeString(ms.getServerName());
00181   try
00182   {
00183     writeUInt4(ms.getVectSize());
00184   }
00185   catch (Exception&)
00186   {
00187     writeUInt4(0);
00188   }
00189   writeUInt4(ms.getMixtureCount());
00190   writeUInt4(ms.getDistribCount());
00191   for (i=0; i<ms.getDistribCount(); i++)
00192   {
00193     const DistribGD* p = dynamic_cast<const DistribGD*>(&ms.getDistrib(i));
00194     if (p != NULL)
00195     {
00196       writeString("GD");
00197       writeString("f"); // unused
00198       for (c=0; c<p->getVectSize(); c++)
00199        writeDouble(p->getCovInv(c));
00200 
00201       for (c=0; c<p->getVectSize(); c++)
00202         writeDouble(p->getMean(c));
00203     }
00204     else
00205       throw Exception("I don't know how to save a "
00206                + ms.getDistrib(i).getClassName()
00207                + " object", __FILE__, __LINE__);
00208   }
00209   for (i=0; i<ms.getMixtureCount(); i++)
00210   {
00211     const MixtureGD* p = dynamic_cast<const MixtureGD*>(&ms.getMixture(i));
00212     if (p != NULL)
00213       writeMixtureGDRaw(*p);
00214     else
00215       throw Exception("I don't know how to save a "
00216                + ms.getDistrib(i).getClassName()
00217                + " object", __FILE__, __LINE__);
00218   }
00219 }
00220 //-------------------------------------------------------------------------
00221 void W::writeMixtureGDRaw(const MixtureGD& m)
00222 {
00223   unsigned long i;
00224   writeString("GD");
00225   writeUInt4(m.getId().length());
00226   writeString(m.getId());
00227   writeUInt4(m.getDistribCount());
00228 
00229   for (i=0; i< m.getDistribCount(); i++)
00230   {
00231     DistribGD& d = m.getDistrib(i);
00232 
00233     writeUInt4(d.dictIndex(K::k));
00234     writeDouble(m.weight(i));
00235   }
00236 }
00237 //-------------------------------------------------------------------------
00238 String W::getClassName() const { return "MixtureServerFileWriter"; }
00239 //-------------------------------------------------------------------------
00240 W::~MixtureServerFileWriter() {}
00241 //-------------------------------------------------------------------------
00242 
00243 #endif // !defined(ALIZE_MixtureServerFileWriter_cpp)
00244