ULongVector.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_ULongVector_cpp)
00056 #define ALIZE_ULongVector_cpp
00057 
00058 #include <new>
00059 #include <math.h>
00060 #include <memory.h>
00061 #include <cstdlib>
00062 #include <iostream>
00063 #include <fstream>
00064 #include "ULongVector.h"
00065 #include "alizeString.h"
00066 #include "Exception.h"
00067 
00068 using namespace alize;
00069 using namespace std;
00070 
00071 //-------------------------------------------------------------------------
00072 ULongVector::ULongVector(unsigned long capacity, unsigned long size)
00073 :Object(), _size(0), _capacity(capacity!=0?capacity:1), 
00074 _array(createArray()) { setSize(size); }
00075 //-------------------------------------------------------------------------
00076 ULongVector::ULongVector(const ULongVector& v)
00077 :Object(), _size(v._size), _capacity(v._size!=0?v._size:1),
00078  _array(createArray())
00079 { memcpy(_array, v._array, _size*sizeof(_array[0])); }
00080 //-------------------------------------------------------------------------
00081 const ULongVector& ULongVector::operator=(const ULongVector& v)
00082 {
00083   if (this->isSameObject(v))
00084     return v;
00085   assert(_array != NULL);
00086   _size = v._size;
00087   if (_capacity < _size)
00088   {
00089     delete[] _array;
00090     _capacity = _size!=0?_size:1;
00091     _array = createArray();
00092   }
00093   memcpy(_array, v._array, _size*sizeof(_array[0]));
00094   return *this;
00095 }
00096 //-------------------------------------------------------------------------
00097 bool ULongVector::operator==(const ULongVector& v) const
00098 {
00099   if (_size != v._size)
00100     return false;
00101   for (unsigned long i=0; i<_size; i++)
00102   {
00103     if (_array[i] != v._array[i])
00104       return false;
00105   }
00106   return true;
00107 }
00108 //-------------------------------------------------------------------------
00109 bool ULongVector::operator!=(const ULongVector& v) const
00110 { return !(*this == v); }
00111 //-------------------------------------------------------------------------
00112 ULongVector::_type* ULongVector::createArray() const // private
00113 {
00114   assert(_capacity != 0);
00115   _type* p = new (std::nothrow) _type[_capacity];
00116   assertMemoryIsAllocated(p, __FILE__, __LINE__);
00117   return p;
00118 }
00119 //-------------------------------------------------------------------------
00120 ULongVector::_type& ULongVector::operator[](unsigned long i)
00121 {
00122   assertIsInBounds(__FILE__, __LINE__, i, _size);
00123   assert(_array != NULL);
00124   return _array[i];
00125 }
00126 //-------------------------------------------------------------------------
00127 ULongVector::_type ULongVector::operator[](unsigned long i) const
00128 {
00129   assertIsInBounds(__FILE__, __LINE__, i, _size);
00130   assert(_array != NULL);
00131   return _array[i];
00132 }
00133 //-------------------------------------------------------------------------
00134 ULongVector& ULongVector::addValue(_type element)
00135 {
00136   assert(_array != NULL);
00137   if (_size == _capacity)
00138   {
00139     _capacity += _capacity;
00140     _type* oldArray = _array;
00141     _array = createArray(); // can throw OutOfMemoryException
00142     memcpy(_array, oldArray, _size*sizeof(_array[0]));
00143     delete[] oldArray;
00144   }
00145   _array[_size] = element;
00146   _size++;
00147   return *this;
00148 }
00149 //-------------------------------------------------------------------------
00150 void ULongVector::addValue(const ULongVector& v)
00151 {
00152   for (unsigned long i=0; i<v._size; i++)
00153   { addValue(v._array[i]); }
00154   // TODO : methode a optimiser
00155 }
00156 //-------------------------------------------------------------------------
00157 void ULongVector::setSize(unsigned long size)
00158 {
00159   assert(_array != NULL);
00160   if (size > _capacity)
00161   {
00162     unsigned long oldSize = _size;
00163     _size = size;
00164     _capacity = _size;
00165     _type* oldArray = _array;
00166     _array = createArray(); // can throw OutOfMemoryException
00167     memcpy(_array, oldArray, oldSize*sizeof(_array[0]));
00168     delete[] oldArray;
00169   }
00170   else
00171     _size = size;
00172 }
00173 //-------------------------------------------------------------------------
00174 int ULongVector::compare(const void *s1, const void *s2) // static method
00175 {
00176   if (*((_type*)s1) > *((_type*)s2))
00177     return 1;
00178   if (*((_type*)s1) < *((_type*)s2))
00179     return -1;
00180   return 0;
00181 }
00182 //-------------------------------------------------------------------------
00183 void ULongVector::ascendingSort() const
00184 {
00185   assert(_array != NULL);
00186   qsort(_array, _size, sizeof(_type), compare);
00187 }
00188 //-------------------------------------------------------------------------
00189 void ULongVector::removeValues(unsigned long first, unsigned long last) 
00190 {
00191   if (last>_size-1)
00192     last = _size-1;
00193   if (first > last)
00194     return;
00195   last++;
00196   assert(_array != NULL);
00197   for (; last<_size; first++, last++)
00198   { _array[first] = _array[last]; }
00199   _size -= last-first;
00200 }
00201 //-------------------------------------------------------------------------
00202 ULongVector::_type* ULongVector::getArray() const { return _array; }
00203 //-------------------------------------------------------------------------
00204 void ULongVector::clear() { _size = 0; }
00205 //-------------------------------------------------------------------------
00206 unsigned long ULongVector::size() const { return _size; }
00207 //-------------------------------------------------------------------------
00208 String ULongVector::getClassName() const { return "ULongVector"; }
00209 //-------------------------------------------------------------------------
00210 String ULongVector::toString() const
00211 {
00212   String s = Object::toString() + " size = " + String::valueOf(_size);
00213   for (unsigned long i=0; i<_size; i++)
00214   {
00215     s += "  \n  [" + String::valueOf(i) + "] = "
00216     + String::valueOf(_array[i]);
00217   }
00218   return s;
00219 }
00220 //-------------------------------------------------------------------------
00221 ULongVector::~ULongVector() { delete[] _array; }
00222 //-------------------------------------------------------------------------
00223 void ULongVector::setAllValues(unsigned long u) {
00224         for (unsigned long i=0; i< _size; i++)
00225         _array[i] = u;
00226 }
00227 //-------------------------------------------------------------------------
00228 void ULongVector::save(const FileName& f) {
00229       try {
00230             ofstream outputMat(f.c_str(),ios::out|ios::binary);
00231             if(!outputMat)
00232                 throw IOException("Cannot open file", __FILE__, __LINE__,f);
00233             outputMat.write((char*)&_size,sizeof(_size));
00234                         outputMat.write((char*)_array,_size*sizeof(unsigned long));
00235             outputMat.close();
00236           }
00237           catch (Exception& e) {cout << e.toString().c_str() << endl;}
00238 }
00239 //-------------------------------------------------------------------------
00240 void ULongVector::load(const FileName& f) {
00241       try {
00242             ifstream inputVect(f.c_str(),ios::in|ios::binary);
00243             if(!inputVect)
00244                 throw IOException("Cannot open file", __FILE__, __LINE__,f);
00245             inputVect.read((char*)&_size,sizeof(_size));
00246                         if (_capacity != 0) delete[] _array;
00247                         _array = new unsigned long[_size];
00248                         if (!_array)
00249                                 throw Exception("Memory allocation exception", __FILE__, __LINE__);
00250                         _capacity=_size;
00251             inputVect.read((char*)_array,_size*sizeof(unsigned long));
00252             inputVect.close();
00253           }
00254           catch (Exception& e) {cout << e.toString().c_str() << endl;}
00255 }
00256 //-------------------------------------------------------------------------
00257 
00258 #endif  // ALIZE_ULongVector_cpp