MixtureServer.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_MixtureServer_cpp)
00056 #define ALIZE_MixtureServer_cpp
00057 
00058 //#include <cstdlib>
00059 
00060 #include <ctime>
00061 #include "MixtureServer.h"
00062 #include "MixtureFileReader.h"
00063 #include "MixtureServerFileReader.h"
00064 #include "MixtureServerFileWriter.h"
00065 #include "MixtureGD.h"
00066 #include "MixtureGF.h"
00067 #include "DistribGD.h"
00068 #include "DistribGF.h"
00069 #include "Exception.h"
00070 #include "XLine.h"
00071 #include "ULongVector.h"
00072 
00073 using namespace alize;
00074 typedef MixtureServer S;
00075 
00076 //-------------------------------------------------------------------------
00077 S::MixtureServer(const Config& c)
00078 :Object(), _config(c) { reset(); }
00079 //-------------------------------------------------------------------------
00080 S::MixtureServer(const FileName& f, const Config& c)
00081 :Object(), _config(c)
00082 {
00083   reset();
00084   load(f);
00085 }
00086 //-------------------------------------------------------------------------
00087 void S::reset()
00088 {
00089   _mixtureDict.clear(); // delete all mixtures
00090   _distribDict.clear(); // delete all distributions
00091   _lastMixtureId = 0;
00092   _vectSizeDefined = false;
00093 }
00094 //-------------------------------------------------------------------------
00095 Distrib& S::createDistrib()
00096 {
00097   unsigned long vectSize;
00098   if (_vectSizeDefined)
00099     vectSize = _vectSize;
00100   else
00101     vectSize = _config.getParam_vectSize();
00102   return createDistrib(_config.getParam_distribType(), vectSize);
00103 }
00104 //-------------------------------------------------------------------------
00105 DistribGD& S::createDistribGD()
00106 {
00107   unsigned long vectSize;
00108   if (_vectSizeDefined)
00109     vectSize = _vectSize;
00110   else
00111     vectSize = _config.getParam_vectSize();
00112   return static_cast<DistribGD&>(createDistrib(DistribType_GD, vectSize));
00113 }
00114 //-------------------------------------------------------------------------
00115 DistribGF& S::createDistribGF()
00116 {
00117   unsigned long vectSize;
00118   if (_vectSizeDefined)
00119     vectSize = _vectSize;
00120   else
00121     vectSize = _config.getParam_vectSize();
00122   return static_cast<DistribGF&>(createDistrib(DistribType_GF, vectSize));
00123 }
00124 //-------------------------------------------------------------------------
00125 Distrib& S::createDistrib(const DistribType type, unsigned long vectSize)
00126 {
00127   if (_vectSizeDefined && vectSize != _vectSize)
00128     throw Exception("Incompatible vectSize", __FILE__, __LINE__);
00129   Distrib& d = Distrib::create(K::k, type, vectSize);
00130   addDistribToDict(d);
00131   _vectSize = vectSize;
00132   _vectSizeDefined = true;
00133   return d;
00134 }
00135 //-------------------------------------------------------------------------
00136 void S::addDistribToDict(Distrib& d) // private
00137 { d.dictIndex(K::k) = _distribDict.addDistrib(d); }
00138 //-------------------------------------------------------------------------
00139 void S::addMixtureToDict(Mixture& m) // private
00140 { _mixtureDict.addMixture(m); }
00141 //-------------------------------------------------------------------------
00142 Mixture& S::createMixture()
00143 {
00144   return createMixture(_config.getParam_mixtureDistribCount(),
00145                      _config.getParam_distribType());
00146 }
00147 //-------------------------------------------------------------------------
00148 Mixture& S::createMixture(unsigned long dc)
00149 { return createMixture(dc, _config.getParam_distribType()); }
00150 //-------------------------------------------------------------------------
00151 MixtureGD& S::createMixtureGD()
00152 {
00153   Mixture& m = createMixture(_config.getParam_mixtureDistribCount(),
00154                             DistribType_GD);
00155   return static_cast<MixtureGD&>(m);
00156 }
00157 //-------------------------------------------------------------------------
00158 MixtureGF& S::createMixtureGF()
00159 {
00160   Mixture& m = createMixture(_config.getParam_mixtureDistribCount(),
00161                             DistribType_GF);
00162   return static_cast<MixtureGF&>(m);
00163 }
00164 //-------------------------------------------------------------------------
00165 MixtureGD& S::createMixtureGD(unsigned long dc)
00166 { return static_cast<MixtureGD&>(createMixture(dc, DistribType_GD)); }
00167 //-------------------------------------------------------------------------
00168 MixtureGF& S::createMixtureGF(unsigned long dc)
00169 { return static_cast<MixtureGF&>(createMixture(dc, DistribType_GF)); }
00170 //-------------------------------------------------------------------------
00171 Mixture& S::createMixture(unsigned long dc, DistribType type)
00172 {
00173   unsigned long vectSize;
00174   if (_vectSizeDefined)
00175     vectSize = _vectSize;
00176   else
00177     vectSize = _config.getParam_vectSize();
00178   Mixture& m = Mixture::create(K::k, dc, type, newId(), vectSize);
00179   addMixtureToDict(m);
00180   const unsigned long n = m.getDistribCount();
00181   for (unsigned long c=0; c<n; c++)
00182   { addDistribToDict(m.getDistrib(c)); }
00183   _vectSize = vectSize;
00184   _vectSizeDefined = true;
00185   return m;
00186 }
00187 //-------------------------------------------------------------------------
00188 String S::newId() // private
00189 {
00190   String id;
00191   do { id = "#" + String::valueOf(++_lastMixtureId); }
00192   while (_mixtureDict.getIndexOfId(id) != -1);
00193   return id;
00194 }
00195 //-------------------------------------------------------------------------
00196 void S::setMixtureId(Mixture& mixture, const String& id)
00197 {
00198   /*long i = _mixtureDict.getIndexOfId(id);
00199   if (i != -1 && &getMixture(i) != &mixture)
00200     throw IdAlreadyExistsException(id, __FILE__, __LINE__);*/
00201   _mixtureDict.setMixtureId(mixture, id);
00202 }
00203 //-------------------------------------------------------------------------
00204 Mixture& S::duplicateMixture(const Mixture& mixture, DuplDistrib d)
00205 {
00206   Mixture& m = mixture.duplicate(K::k, d);
00207   String id(mixture.getId());
00208   unsigned long cpt = 1;
00209   while (_mixtureDict.getIndexOfId(id) != -1) // if the id already exists
00210     id = mixture.getId() + " #" + String::valueOf(++cpt);
00211   m.setId(K::k, id);
00212   addMixtureToDict(m);
00213 
00214   if (d == DUPL_DISTRIB)
00215   {
00216     const unsigned long n= m.getDistribCount();
00217     for (unsigned long c=0; c<n; c++)
00218     { addDistribToDict(m.getDistrib(c)); }
00219   }
00220   return m;
00221 }
00222 //-------------------------------------------------------------------------
00223 MixtureGD& S::duplicateMixture(const MixtureGD& m, DuplDistrib d)
00224 {
00225   return static_cast<MixtureGD&>(duplicateMixture(
00226             static_cast<const Mixture&>(m), d));
00227 }
00228 //-------------------------------------------------------------------------
00229 MixtureGF& S::duplicateMixture(const MixtureGF& m, DuplDistrib d)
00230 {
00231   return static_cast<MixtureGF&>(duplicateMixture(
00232             static_cast<const Mixture&>(m), d));
00233 }
00234 //-------------------------------------------------------------------------
00235 MixtureGD& S::duplicateMixtureGD(const Mixture& m, DuplDistrib d)
00236 { return static_cast<MixtureGD&>(duplicateMixture(m, d)); }
00237 //-------------------------------------------------------------------------
00238 MixtureGF& S::duplicateMixtureGF(const Mixture& m, DuplDistrib d)
00239 { return static_cast<MixtureGF&>(duplicateMixture(m, d)); }
00240 //-------------------------------------------------------------------------
00241 DistribGD& S::duplicateDistrib(const DistribGD& d)
00242 {
00243   return static_cast<DistribGD&>(duplicateDistrib(
00244                   static_cast<const Distrib&>(d)));
00245 }
00246 //-------------------------------------------------------------------------
00247 DistribGF& S::duplicateDistrib(const DistribGF& d)
00248 {
00249   return static_cast<DistribGF&>(duplicateDistrib(
00250                   static_cast<const Distrib&>(d)));
00251 }
00252 //-------------------------------------------------------------------------
00253 /*DistribGD& S::duplicateDistribGD(const Distrib& d)
00254 {
00255   const DistribGD* p = dynamic_cast<const DistribGD*>(&d);
00256   if (p == NULL)
00257     throw Exception("The distrib is not a GD", __FILE__, __LINE__);
00258   return static_cast<DistribGD&>(duplicateDistrib(*p));
00259 }*/
00260 //-------------------------------------------------------------------------
00261 Distrib& S::duplicateDistrib(const Distrib& distrib)
00262 {
00263   Distrib& d = distrib.duplicate(K::k);
00264   addDistribToDict(d);
00265   return d;
00266 }
00267 //-------------------------------------------------------------------------
00268 long S::getMixtureIndex(const String& id) const
00269 { return _mixtureDict.getIndexOfId(id); }
00270 //-------------------------------------------------------------------------
00271 void S::setDistribToMixture(Mixture& m, Distrib& d, weight_t w,
00272                             unsigned long i)
00273 {
00274   m.setDistrib(K::k, d, i); // can throw IndexOutOfBoundsException
00275   m.weight(i) = w; // can throw IndexOutOfBoundsException
00276 }
00277 //-------------------------------------------------------------------------
00278 void S::addDistribToMixture(Mixture& m, Distrib& d, weight_t w)
00279 { m.addDistrib(K::k, d, w); }
00280 //-------------------------------------------------------------------------
00281 MixtureGD& S::loadMixtureGD(const FileName& f)
00282 { return static_cast<MixtureGD&>(loadMixture(f, DistribType_GD)); }
00283 //-------------------------------------------------------------------------
00284 MixtureGF& S::loadMixtureGF(const FileName& f)
00285 { return static_cast<MixtureGF&>(loadMixture(f, DistribType_GF)); }
00286 //-------------------------------------------------------------------------
00287 Mixture& S::loadMixture(const FileName& f)
00288 {
00289   MixtureFileReader r(f, _config);
00290   const Mixture& m0 = r.readMixture();
00291   if (!_config.existsParam_vectSize)
00292     const_cast<Config&>(_config)
00293                    .setParam("vectSize", String::valueOf(m0.getVectSize()));
00294   Mixture& m = createMixture(m0.getDistribCount(), m0.getType());
00295   m = m0;
00296   autoSetMixtureId(m, f);
00297   return m;
00298 }
00299 //-------------------------------------------------------------------------
00300 void S::loadMixture(Mixture& m, const FileName& f)
00301 {
00302   MixtureFileReader r(f, _config);
00303   m = r.readMixture();
00304   autoSetMixtureId(m, f);
00305 }
00306 //-------------------------------------------------------------------------
00307 Mixture& S::loadMixture(const FileName& f, DistribType type) // private
00308 {
00309   MixtureFileReader r(f, _config);
00310   const Mixture& m0 = r.readMixture(type);    
00311   if (!_config.existsParam_vectSize)
00312     const_cast<Config&>(_config)
00313                    .setParam("vectSize", String::valueOf(m0.getVectSize()));
00314   Mixture& m = createMixture(m0.getDistribCount(), type);
00315   m = m0; // operator= overloaded. // Does not copy Id.
00316   autoSetMixtureId(m, f);
00317   return m;
00318 }
00319 //-------------------------------------------------------------------------
00320 void S::autoSetMixtureId(Mixture& m, String id) // private
00321 {
00322   const String f = id;
00323   unsigned long cpt = 1;
00324   while (_mixtureDict.getIndexOfId(id) != -1) // if the id already exists
00325     id = f + " #" + String::valueOf(++cpt);
00326   _mixtureDict.setMixtureId(m, id);
00327 }
00328 //-------------------------------------------------------------------------
00329 unsigned long S::loadMixture(const XLine& l)
00330 {
00331   unsigned long first = getMixtureCount();
00332   for (unsigned long i=0; i<l.getElementCount(); i++)
00333     loadMixture(l.getElement(i));
00334   return first;
00335 }
00336 //-------------------------------------------------------------------------
00337 unsigned long S::getDistribCount() const { return _distribDict.size(); }
00338 //-------------------------------------------------------------------------
00339 unsigned long S::getVectSize() const
00340 {
00341   if (_vectSizeDefined)
00342     return _vectSize;
00343   throw Exception("Unknown vectSize", __FILE__, __LINE__);
00344 }
00345 //-------------------------------------------------------------------------
00346 Distrib& S::getDistrib(unsigned long i) const
00347 { return _distribDict.getDistrib(i); }
00348 //-------------------------------------------------------------------------
00349 DistribGD& S::getDistribGD(unsigned long i) const
00350 {
00351   DistribGD* p = dynamic_cast<DistribGD*>(&_distribDict.getDistrib(i));
00352   if (p == NULL)
00353     throw Exception("The distrib found is not a GD", __FILE__, __LINE__);
00354   return *p;
00355 }
00356 //-------------------------------------------------------------------------
00357 DistribGF& S::getDistribGF(unsigned long i) const
00358 {
00359   DistribGF* p = dynamic_cast<DistribGF*>(&_distribDict.getDistrib(i));
00360   if (p == NULL)
00361     throw Exception("The distrib found is not a GF", __FILE__, __LINE__);
00362   return *p;
00363 }
00364 //-------------------------------------------------------------------------
00365 unsigned long S::getMixtureCount() const { return _mixtureDict.size(); }
00366 //-------------------------------------------------------------------------
00367 Mixture& S::getMixture(unsigned long i) const
00368 { return _mixtureDict.getMixture(i); }
00369 //-------------------------------------------------------------------------
00370 MixtureGD& S::getMixtureGD(unsigned long i) const
00371 {
00372   MixtureGD* p = dynamic_cast<MixtureGD*>(&_mixtureDict.getMixture(i));
00373   if (p == NULL)
00374     throw Exception("The mixture found is not a GD", __FILE__, __LINE__);
00375   return *p;
00376 }
00377 //-------------------------------------------------------------------------
00378 MixtureGF& S::getMixtureGF(unsigned long i) const
00379 {
00380   MixtureGF* p = dynamic_cast<MixtureGF*>(&_mixtureDict.getMixture(i));
00381   if (p == NULL)
00382     throw Exception("The mixture found is not a GF", __FILE__, __LINE__);
00383   return *p;
00384 }
00385 //-------------------------------------------------------------------------
00386 const String& S::getServerName() const { return _serverName; }
00387 //-------------------------------------------------------------------------
00388 void S::setServerName(const String& s) { _serverName = s; }
00389 //-------------------------------------------------------------------------
00390 void S::load(const FileName& f)
00391 { MixtureServerFileReader(f, _config).readMixtureServer(*this); }
00392 //-------------------------------------------------------------------------
00393 void S::deleteMixtures(unsigned long first, unsigned long last)
00394 {
00395   _mixtureDict.deleteMixtures(first, last);
00396   if (getDistribCount() == 0 && getMixtureCount() == 0)
00397     _vectSizeDefined = false;
00398 }
00399 //-------------------------------------------------------------------------
00400 void S::deleteMixture(const Mixture& m)
00401 {
00402   _mixtureDict.deleteMixture(m);
00403   if (getDistribCount() == 0 && getMixtureCount() == 0)
00404     _vectSizeDefined = false;
00405 }
00406 //-------------------------------------------------------------------------
00407 void S::deleteUnusedDistribs()
00408 {
00409   _distribDict.deleteUnreferencedDistribs();
00410   if (getDistribCount() == 0 && getMixtureCount() == 0)
00411     _vectSizeDefined = false;
00412 }
00413 //-------------------------------------------------------------------------
00414 void S::save(const FileName& f) const
00415 { MixtureServerFileWriter(f, _config).writeMixtureServer(*this); }
00416 //-------------------------------------------------------------------------
00417 String S::toString() const
00418 {
00419   String s = Object::toString()
00420     + "\n  serverName   = '" + _serverName + "'"
00421     + "\n  distribCount = " + String::valueOf(getDistribCount())
00422     + "\n  mixtureCount = " + String::valueOf(getMixtureCount());
00423   for (unsigned long i=0; i<getMixtureCount(); i++)
00424     s += "\n  mixture #" + String::valueOf(i)
00425       + " Id = " + getMixture(i).getId();
00426   return s;
00427 }
00428 //-------------------------------------------------------------------------
00429 String S::getClassName() const { return "MixtureServer"; }
00430 //-------------------------------------------------------------------------
00431 S::~MixtureServer() {}
00432 //-------------------------------------------------------------------------
00433 
00434 #endif // !defined(ALIZE_MixtureServer_cpp)
00435