XList.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_XList_cpp)
00056 #define ALIZE_XList_cpp
00057 
00058 #include <new>
00059 #include "XList.h"
00060 #include "Exception.h"
00061 #include "XListFileReader.h"
00062 #include "Config.h"
00063 #include <fstream>
00064 
00065 using namespace alize;
00066 
00067 //-------------------------------------------------------------------------
00068 XList::XList()
00069 :Object(), _current(0) {}
00070 //-------------------------------------------------------------------------
00071 XList::XList(const FileName& f)
00072 :Object(), _current(0) { load(f, Config()); }
00073 //-------------------------------------------------------------------------
00074 XList::XList(const FileName& f, const Config& c)
00075 :Object(), _current(0) { load(f, c); }
00076 //-------------------------------------------------------------------------
00077 XList& XList::create() // static method
00078 {
00079   XList* p = new (std::nothrow) XList();
00080   assertMemoryIsAllocated(p, __FILE__, __LINE__);
00081   return *p;
00082 }
00083 //-------------------------------------------------------------------------
00084 XList::XList(const XList& l)
00085 :Object(), _current(0)
00086 {
00087   for (unsigned long i=0; i<l._vector.size(); i++)
00088     _vector.addObject(l._vector.getObject(i).duplicate());
00089 }
00090 //-------------------------------------------------------------------------
00091 const XList& XList::operator=(const XList& l)
00092 {
00093   if (this != &l)
00094   {
00095     _vector.deleteAllObjects();
00096     for (unsigned long i=0; i<l._vector.size(); i++)
00097       _vector.addObject(l._vector.getObject(i).duplicate());
00098     _current = 0;
00099   }
00100   return *this;
00101 }  
00102 //-------------------------------------------------------------------------
00103 bool XList::operator==(const XList& l) const
00104 {
00105   if (_vector.size() != l._vector.size())
00106     return false;
00107   for (unsigned long i=0; i<l._vector.size(); i++)
00108     if (_vector.getObject(i) != l._vector.getObject(i))
00109       return false;
00110   return true;
00111 }  
00112 //-------------------------------------------------------------------------
00113 bool XList::operator!=(const XList& l) const { return !(*this==l); }
00114 //-------------------------------------------------------------------------
00115 void XList::load(const FileName& f, const Config& c)
00116 {
00117   XListFileReader(f, c).readList(*this);
00118   // can throw FileNotFoundException, IOException
00119 }
00120 //-------------------------------------------------------------------------
00121 void XList::save(const FileName& f) const { save(f, Config()); }
00122 //-------------------------------------------------------------------------
00123 void XList::save(const FileName& f, const Config& c) const
00124 {
00125   std::ofstream ff(f.c_str(), std::ios::out);
00126   if (!ff)
00127     throw IOException("Cannot open file", __FILE__, __LINE__, f);
00128   for (unsigned long i=0; i<getLineCount(); i++)
00129   {
00130     XLine& line = _vector.getObject(i);
00131     unsigned long  count = line.getElementCount();
00132     for (unsigned long j=0; j<count; j++)
00133     {
00134       ff << line.getElement(j, false); // false = does not change current seg
00135       if (j+1<count)
00136         ff << " ";
00137     }
00138     ff << std::endl;
00139   }
00140 }
00141 //-------------------------------------------------------------------------
00142 XLine& XList::addLine()
00143 {
00144   XLine& l = XLine::create();
00145   _current = _vector.addObject(l);
00146   return l;
00147 }
00148 //-------------------------------------------------------------------------
00149 /* Modified 19/07/07 - richard.dufour@lium.univ-lemans.fr
00150  */
00151 XLine& XList::addLine(String& key, String& value)
00152 {
00153   XLine& l = XLine::create(key,value);
00154   _current = _vector.addObject(l);
00155   return l;
00156 }
00157 //-------------------------------------------------------------------------
00158 void XList::rewind() const { _current = 0; }
00159 //-------------------------------------------------------------------------
00160 XLine& XList::getLine(unsigned long i) const
00161 {
00162   XLine& line = _vector.getObject(i);
00163   // getObject(i) can throw IndexOutOfBoundsException
00164   _current = i;
00165   line.rewind();
00166   return line;
00167 }
00168 //-------------------------------------------------------------------------
00169 XLine* XList::getLine() const
00170 {
00171   if (_current >= _vector.size())
00172     return NULL;
00173   _current++;
00174   XLine& line = _vector.getObject(_current-1);
00175   line.rewind();
00176   return &line;
00177 }
00178 //-------------------------------------------------------------------------
00179 XLine* XList::findLine(const String& key, unsigned long idx) const
00180 {
00181   for (unsigned long i=0; i<_vector.size(); i++)
00182   {
00183     XLine& line = _vector.getObject(i);
00184     if (idx < line.getElementCount() && line.getElement(idx) == key)
00185     {
00186       _current = i;
00187       return &line;
00188     }
00189   }
00190   return NULL;
00191 }
00192 //-------------------------------------------------------------------------
00193 XLine& XList::getAllElements() const
00194 {
00195   _line.reset();
00196   for (unsigned long i=0; i<_vector.size(); i++)
00197   {
00198     XLine& l = _vector.getObject(i);
00199     for (unsigned long j=0; j<l.getElementCount(); j++)
00200       _line.addElement(l.getElement(j, false));
00201   }
00202   return _line; 
00203 }
00204 //-------------------------------------------------------------------------
00205 void XList::reset()
00206 {
00207   _vector.deleteAllObjects();
00208   _current = 0;
00209 }
00210 //-------------------------------------------------------------------------
00211 unsigned long XList::getLineCount() const { return _vector.size(); }
00212 //-------------------------------------------------------------------------
00213 String XList::getClassName() const { return "XList"; }
00214 //-------------------------------------------------------------------------
00215 /* Modified 19/07/07 - richard.dufour@lium.univ-lemans.fr
00216  */
00217 String XList::searchValue(String& index)
00218 {
00219   String result = "";
00220   for (unsigned long i=0; i<_vector.size(); i++)
00221   {
00222     XLine& l = _vector.getObject(i);
00223     if (l.getElement(0) == index) {
00224       return l.getElement(1);
00225       break;
00226     }
00227   }
00228   return result;
00229 }
00230 //-------------------------------------------------------------------------
00231 String XList::toString() const
00232 {
00233   String s;
00234   for (unsigned long i=0; i<_vector.size(); i++)
00235   {
00236     s += "\n";
00237     XLine& l = _vector.getObject(i);
00238     for (unsigned long j=0; j<l.getElementCount(); j++)
00239     { s += " " + l.getElement(j); }
00240   }
00241   return Object::toString() + s;
00242 }
00243 //-------------------------------------------------------------------------
00244 XList::~XList() { _vector.deleteAllObjects(); }
00245 //-------------------------------------------------------------------------
00246 
00247 #endif // !defined(ALIZE_XList_cpp)
00248