FeatureInputStreamModifier.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_FeatureInputStreamModifier_cpp)
00056 #define ALIZE_FeatureInputStreamModifier_cpp
00057 
00058 #include <new>
00059 #include "FeatureInputStreamModifier.h"
00060 #include "Exception.h"
00061 #include "LabelServer.h"
00062 #include "Label.h"
00063 #include "FeatureFlags.h"
00064 #include "XLine.h"
00065 #include "Config.h"
00066 
00067 using namespace alize;
00068 typedef FeatureInputStreamModifier M;
00069 
00070 //-------------------------------------------------------------------------
00071 M::FeatureInputStreamModifier(FeatureInputStream& is, const String& m,
00072                               bool ownStream)
00073 :FeatureInputStream(is.getConfig()), _pInput(&is), _useMask(false),
00074 _ownStream(ownStream) { setMask(m); }
00075 //-------------------------------------------------------------------------
00076 M& M::create(FeatureInputStream& is, const String& m, bool ownStream)
00077 {
00078   M* p = new (std::nothrow) M(is, m, ownStream);
00079   assertMemoryIsAllocated(p, __FILE__, __LINE__);
00080   return *p;
00081 }
00082 //-------------------------------------------------------------------------
00083 void M::setMask(const String& m)
00084 {
00085   if (m == "NO_MASK")
00086   {
00087     _useMask = false;
00088     return;
00089   }
00090   _tmpMask.reset();
00091   unsigned long i = 0;
00092   String begin, end;
00093 
00094   if (m.isEmpty())
00095     goto xend;
00096   while (true)
00097   {
00098     if (m[i] < "0" || m[i] > "9")
00099       throw Exception("Invalid feature mask", __FILE__, __LINE__);
00100     begin += m[i++];
00101     end = begin;
00102     if (i == m.length())
00103     {
00104       updateMask(begin, end);
00105       goto xend;
00106     }
00107     if (m[i] == "-")
00108     {
00109       i++;
00110       if (i == m.length())
00111         throw Exception("Invalid feature mask", __FILE__, __LINE__);
00112       end.reset();
00113       do
00114       {
00115         if (m[i] < "0" || m[i] > "9")
00116           throw Exception("Invalid feature mask", __FILE__, __LINE__);
00117         end += m[i++];
00118         if (i == m.length())
00119         {
00120           updateMask(begin, end);
00121           goto xend;
00122         }
00123       }
00124       while (m[i] != ",");
00125     }
00126     if (m[i] == ",")
00127     {
00128       updateMask(begin, end);
00129       i++;
00130       if (i == m.length())
00131         throw Exception("Invalid feature mask", __FILE__, __LINE__);
00132       begin.reset();
00133       end.reset();
00134     }
00135   }
00136 xend:
00137   _useMask = true;
00138   _mask = m;
00139   _selection.clear();
00140   char* p = const_cast<char*>(_tmpMask.c_str());
00141   for (i=0; i<_tmpMask.length(); i++)
00142     if (p[i] == '1')
00143      _selection.addValue(i);
00144   _selectionSize = _selection.size();
00145 }
00146 //-------------------------------------------------------------------------
00147 void M::updateMask(const String& b, const String& e) // private
00148 {
00149   unsigned long ee = e.toULong();
00150   unsigned long bb = b.toULong();
00151   if (ee < bb)
00152     throw Exception("Invalid feature mask", __FILE__, __LINE__);
00153   for (unsigned long i=bb; i<=ee; i++)
00154   {
00155     while (i >= _tmpMask.length())
00156       _tmpMask += "0";
00157     const_cast<char*>(_tmpMask.c_str())[i] = '1';
00158   }
00159 }
00160 //-------------------------------------------------------------------------
00161 bool M::readFeature(Feature& f, unsigned long step)
00162 {
00163   bool ok;
00164   if (!_useMask)
00165     ok = _pInput->readFeature(f, step);
00166   else if ( (ok = _pInput->readFeature(_feature, step)) )
00167   {
00168     f.setVectSize(K::k, _selectionSize);
00169     f.copySelectedData(_feature, _selection);
00170   }
00171   _error = _pInput->getError();
00172   return ok;
00173 }
00174 //-------------------------------------------------------------------------
00175 bool M::writeFeature(const Feature& f, unsigned long step)
00176 {
00177   bool ok;
00178   if (!_useMask)
00179     ok = _pInput->writeFeature(f, step);
00180   else
00181   {
00182     if (_selectionSize != f.getVectSize())
00183        throw Exception("Invalid feature mask : " + _mask,
00184                        __FILE__, __LINE__);
00185     _feature.setVectSize(K::k, _pInput->getVectSize());
00186     Feature::data_t* outputVect = _feature.getDataVector();
00187     Feature::data_t* inputVect = f.getDataVector();
00188     for (unsigned long i=0; i<_selectionSize; i++)
00189       outputVect[_selection[i]] = inputVect[i];
00190     ok = _pInput->writeFeature(_feature, step);
00191   }
00192   _error = _pInput->getError();
00193   return ok;
00194 }
00195 //-------------------------------------------------------------------------
00196 unsigned long M::getFeatureCount() { return _pInput->getFeatureCount(); }
00197 //-------------------------------------------------------------------------
00198 unsigned long M::getVectSize() 
00199 {
00200   if (_useMask)
00201     return _selectionSize;
00202   return _pInput->getVectSize();
00203 }
00204 //-------------------------------------------------------------------------
00205 const FeatureFlags& M::getFeatureFlags() 
00206 { return _pInput->getFeatureFlags(); }
00207 // TODO : que faire des flags si _useMask = true ?
00208 //-------------------------------------------------------------------------
00209 void M::seekFeature(unsigned long i, const String& s)
00210 { _pInput->seekFeature(i, s); }
00211 //-------------------------------------------------------------------------
00212 real_t M::getSampleRate() { return _pInput->getSampleRate(); }
00213 //-------------------------------------------------------------------------
00214 void M::reset() { _pInput->reset(); }
00215 //-------------------------------------------------------------------------
00216 void M::close() { _pInput->close(); }
00217 //-------------------------------------------------------------------------
00218 unsigned long M::getSourceCount() {return _pInput->getSourceCount();}
00219 //-------------------------------------------------------------------------
00220 unsigned long M::getFeatureCountOfASource(unsigned long srcIdx)
00221 { return _pInput->getFeatureCountOfASource(srcIdx); }
00222 //-------------------------------------------------------------------------
00223 unsigned long M::getFeatureCountOfASource(const String& f)
00224 { return _pInput->getFeatureCountOfASource(f); }
00225 //-------------------------------------------------------------------------
00226 unsigned long M::getFirstFeatureIndexOfASource(unsigned long srcIdx)
00227 { return _pInput->getFirstFeatureIndexOfASource(srcIdx); }
00228 //-------------------------------------------------------------------------
00229 unsigned long M::getFirstFeatureIndexOfASource(const String& srcName)
00230 { return _pInput->getFirstFeatureIndexOfASource(srcName); }
00231 //-------------------------------------------------------------------------
00232 const String& M::getNameOfASource(unsigned long srcIdx)
00233 { return _pInput->getNameOfASource(srcIdx); }
00234 //-------------------------------------------------------------------------
00235 String M::getClassName() const { return "FeatureInputStreamModifier"; }
00236 //-------------------------------------------------------------------------
00237 String M::toString() const
00238 {
00239   String s =  FeatureInputStream::toString()
00240     + "\n  input stream = " + _pInput->getClassName()
00241     + "[" + getAddress() + "]";
00242   if (_useMask)
00243     s += "\n  mask = '" + _mask + "'";
00244   else
00245     s += "\n  mask = no mask";
00246   return s;
00247 }
00248 //-------------------------------------------------------------------------
00249 M::~FeatureInputStreamModifier()
00250 {
00251   if (_ownStream)
00252     delete _pInput;
00253 }
00254 //-------------------------------------------------------------------------
00255 
00256 #endif // !defined(ALIZE_FeatureInputStreamModifier_cpp)
00257