Loris 1.5

Distiller.h

00001 #ifndef INCLUDE_DISTILLER_H
00002 #define INCLUDE_DISTILLER_H
00003 /*
00004  * This is the Loris C++ Class Library, implementing analysis, 
00005  * manipulation, and synthesis of digitized sounds using the Reassigned 
00006  * Bandwidth-Enhanced Additive Sound Model.
00007  *
00008  * Loris is Copyright (c) 1999-2010 by Kelly Fitz and Lippold Haken
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY, without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  *
00025  * Distiller.h
00026  *
00027  * Definition of class Distiller.
00028  *
00029  * Kelly Fitz, 20 Oct 1999
00030  * loris@cerlsoundgroup.org
00031  *
00032  * http://www.cerlsoundgroup.org/Loris/
00033  *
00034  */
00035 
00036 #include "Partial.h"
00037 #include "PartialList.h"
00038 #include "PartialUtils.h"
00039 
00040 #include "Notifier.h"   //  for debugging only
00041 
00042 #include <algorithm>
00043 
00044 //  begin namespace
00045 namespace Loris {
00046 
00047 // ---------------------------------------------------------------------------
00048 //  class Distiller
00049 //
00068 //
00069 class Distiller
00070 {
00071 //  -- instance variables --
00072 
00073     double _fadeTime, _gapTime;         // distillation parameters
00074         
00075 //  -- public interface --
00076 public:
00077 
00078 //  -- global defaults and constants --
00079 
00080     enum 
00081     {
00082     
00087         DefaultFadeTimeMs = 5,
00088         
00094         DefaultSilentTimeMs = 1
00095     };
00096 
00097 //  -- construction --
00098 
00119     explicit
00120     Distiller( double partialFadeTime = Distiller::DefaultFadeTimeMs/1000.0,
00121                double partialSilentTime = Distiller::DefaultSilentTimeMs/1000.0 );
00122      
00123     //  Use compiler-generated copy, assign, and destroy.
00124     
00125 //  -- distillation --
00126 
00152 #if ! defined(NO_TEMPLATE_MEMBERS)
00153     template< typename Container >
00154     typename Container::iterator distill( Container & partials );
00155 #else
00156     inline
00157     PartialList::iterator distill( PartialList & partials );
00158 #endif
00159 
00161 #if ! defined(NO_TEMPLATE_MEMBERS)
00162     template< typename Container >
00163     typename Container::iterator operator() ( Container & partials );
00164 #else
00165     PartialList::iterator operator() ( PartialList & partials );
00166 #endif
00167     
00189 #if ! defined(NO_TEMPLATE_MEMBERS)
00190     template< typename Container >
00191     static typename Container::iterator 
00192     distill( Container & partials, double partialFadeTime,
00193              double partialSilentTime = DefaultSilentTimeMs/1000.0 );
00194 #else
00195     static inline PartialList::iterator
00196     distill( PartialList & partials, double partialFadeTime,
00197              double partialSilentTime = DefaultSilentTimeMs/1000.0 );
00198 #endif
00199 
00200 private:
00201 
00202 //  -- helpers --
00203 
00223     PartialList::iterator distill_list( PartialList & partials );
00224 
00230     void distillOne( PartialList & partials, Partial::label_type label,
00231                      PartialList & distilled );
00232     
00233 };  //  end of class Distiller
00234 
00235 // ---------------------------------------------------------------------------
00236 //  distill
00237 // ---------------------------------------------------------------------------
00262 //
00263 #if ! defined(NO_TEMPLATE_MEMBERS)
00264 template< typename Container >
00265 typename Container::iterator Distiller::distill( Container & partials )
00266 {
00267     //  This can be done so much more easily and
00268     //  efficiently on a list than on other containers
00269     //  that it is worth copying the Partials to a
00270     //  list for distillation, and then transfering
00271     //  them back.
00272     //
00273     //  See below for a specialization for the case
00274     //  of the Container being a list, so no copy
00275     //  is needed.
00276     PartialList pl( partials.begin(), partials.end() );
00277     PartialList::iterator it = distill_list( pl );
00278         
00279     //  pl has distilled Partials at beginning, and
00280     //  unlabeled Partials at end:
00281     typename Container::iterator beginUnlabeled = 
00282         std::copy( pl.begin(), it, partials.begin() );
00283     
00284     typename Container::iterator endUnlabeled = 
00285         std::copy( it, pl.end(), beginUnlabeled );
00286 
00287     
00288     partials.erase( endUnlabeled, partials.end() );
00289     
00290     return beginUnlabeled;
00291 }
00292 
00293 //  specialization for PartialList container
00294 template< >
00295 inline
00296 PartialList::iterator Distiller::distill( PartialList & partials )
00297 {
00298     debugger << "using PartialList version of distill to avoid copying" << endl;
00299     return distill_list( partials );
00300 }
00301 #else
00302 inline
00303 PartialList::iterator Distiller::distill( PartialList & partials )
00304 {
00305     return distill_list( partials );
00306 }
00307 #endif
00308 
00309 // ---------------------------------------------------------------------------
00310 //  Function call operator 
00311 // ---------------------------------------------------------------------------
00315 //
00316 #if ! defined(NO_TEMPLATE_MEMBERS)
00317 template< typename Container >
00318 typename Container::iterator Distiller::operator()( Container & partials )
00319 #else
00320 inline
00321 PartialList::iterator Distiller::operator()( PartialList & partials )
00322 #endif
00323 { 
00324     return distill( partials );
00325 }
00326 
00327 // ---------------------------------------------------------------------------
00328 //  distill
00329 // ---------------------------------------------------------------------------
00351 //
00352 #if ! defined(NO_TEMPLATE_MEMBERS)
00353 template< typename Container >
00354 typename Container::iterator 
00355 Distiller::distill( Container & partials, double partialFadeTime,
00356                                           double partialSilentTime )
00357 #else
00358 inline
00359 PartialList::iterator 
00360 Distiller::distill( PartialList & partials, double partialFadeTime,
00361                                             double partialSilentTime )
00362 #endif
00363 {
00364     Distiller instance( partialFadeTime, partialSilentTime );
00365     return instance.distill( partials );
00366 }
00367 
00368 }   //  end of namespace Loris
00369 
00370 #endif /* ndef INCLUDE_DISTILLER_H */
 All Classes Functions Variables Typedefs Enumerator Friends