Loris 1.5

Collator.h

00001 #ifndef INCLUDE_COLLATOR_H
00002 #define INCLUDE_COLLATOR_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  * Collator.h
00026  *
00027  * Definition of class Collator.
00028  *
00029  * Kelly Fitz, 29 April 2005
00030  * loris@cerlsoundgroup.org
00031  *
00032  * http://www.cerlsoundgroup.org/Loris/
00033  *
00034  */
00035 
00036 #include "Distiller.h"  //  for default fade time and silent time
00037 #include "Partial.h"
00038 #include "PartialList.h"
00039 #include "PartialUtils.h"
00040 
00041 #include <algorithm>
00042 
00043 //  begin namespace
00044 namespace Loris {
00045 
00046 // ---------------------------------------------------------------------------
00047 //  class Collator
00048 //
00065 //
00066 class Collator
00067 {
00068 //  -- instance variables --
00069 
00070     double _fadeTime, _gapTime;       
00071         
00072 //  -- public interface --
00073 public:
00074 
00075 //  -- global defaults and constants --
00076 
00077     enum 
00078     {
00079     
00084         DefaultFadeTimeMs = Distiller::DefaultFadeTimeMs,
00085         
00091         DefaultSilentTimeMs = Distiller::DefaultSilentTimeMs
00092     };
00093     
00094 //  -- construction --
00095 
00115     explicit
00116     Collator( double partialFadeTime = Collator::DefaultFadeTimeMs/1000.0,
00117               double partialSilentTime = Collator::DefaultSilentTimeMs/1000.0 );
00118      
00119     //  Use compiler-generated copy, assign, and destroy.
00120     
00121 //  -- collating --
00122 
00147 #if ! defined(NO_TEMPLATE_MEMBERS)
00148     template< typename Container >
00149     typename Container::iterator collate( Container & partials );
00150 #else
00151     inline
00152     PartialList::iterator collate( PartialList & partials );
00153 #endif
00154 
00156 #if ! defined(NO_TEMPLATE_MEMBERS)
00157     template< typename Container >
00158     typename Container::iterator operator() ( Container & partials );
00159 #else
00160     PartialList::iterator operator() ( PartialList & partials );
00161 #endif
00162     
00180 #if ! defined(NO_TEMPLATE_MEMBERS)
00181     template< typename Container >
00182     static typename Container::iterator 
00183     collate( Container & partials, double partialFadeTime,
00184                                    double partialSilentTime );
00185 #else
00186     static inline PartialList::iterator
00187     collate( PartialList & partials, double partialFadeTime,
00188                                      double partialSilentTime );
00189 #endif
00190 
00191 
00192 private:
00193 
00194 //  -- helpers --
00195 
00202     void collateAux( PartialList & unlabled );
00203     
00204 };  //  end of class Collator
00205 
00206 // ---------------------------------------------------------------------------
00207 //  collate
00208 // ---------------------------------------------------------------------------
00231 //
00232 #if ! defined(NO_TEMPLATE_MEMBERS)
00233 template< typename Container >
00234 typename Container::iterator 
00235 Collator::collate( Container & partials )
00236 #else
00237 inline
00238 PartialList::iterator 
00239 Collator::collate( PartialList & partials )
00240 #endif
00241 {
00242 #if ! defined(NO_TEMPLATE_MEMBERS)
00243     typedef typename Container::iterator Iterator;
00244 #else
00245     typedef PartialList::iterator Iterator;
00246 #endif
00247 
00248     // Partition the Partials into labeled and unlabeled, 
00249     // and collate the unlabeled ones and replace the 
00250     // unlabeled range.
00251     // (This requires bidirectional iterator support.)
00252     Iterator beginUnlabeled = 
00253        std::partition( partials.begin(), partials.end(), 
00254                               std::not1( PartialUtils::isLabelEqual(0) ) );
00255         //  this used to be a stable partition, which 
00256         //  is very much slower and seems unnecessary
00257         
00258     // cannot splice if this operation is to be generic
00259     // with respect to container, have to copy:
00260     PartialList collated( beginUnlabeled, partials.end() );
00261     // collated.splice( collated.end(), beginUnlabeled, partials.end() );
00262     
00263     //  determine the label for the first collated Partial:
00264     Partial::label_type labelCollated = 1;
00265     if ( partials.begin() != beginUnlabeled )
00266     {
00267        labelCollated = 
00268         1 + std::max_element( partials.begin(), beginUnlabeled, 
00269                               PartialUtils::compareLabelLess() )->label();
00270     }
00271     if ( labelCollated < 1 )
00272     {
00273         labelCollated = 1;
00274     }
00275 
00276     //  collate unlabeled (zero-labeled) Partials:
00277     collateAux( collated );
00278     
00279     //  label the collated Partials:
00280     for ( Iterator it = collated.begin(); it != collated.end(); ++it )
00281     {
00282         it->setLabel( labelCollated++ );
00283     }
00284     
00285     //  copy the collated Partials back into the source container
00286     //  after the range of labeled Partials     
00287     Iterator endCollated = 
00288         std::copy( collated.begin(), collated.end(), beginUnlabeled );
00289 
00290     //  remove extra Partials from the end of the source container
00291     if ( endCollated != partials.end() )
00292     {
00293         typename Iterator::difference_type numLabeled = 
00294             std::distance( partials.begin(), beginUnlabeled );
00295 
00296         partials.erase( endCollated, partials.end() );
00297 
00298         // restore beginUnlabeled:    
00299         beginUnlabeled = partials.begin();
00300         std::advance( beginUnlabeled, numLabeled );
00301     }
00302     return beginUnlabeled;
00303 }
00304 
00305 // ---------------------------------------------------------------------------
00306 //  Function call operator 
00307 // ---------------------------------------------------------------------------
00311 //
00312 #if ! defined(NO_TEMPLATE_MEMBERS)
00313 template< typename Container >
00314 typename Container::iterator Collator::operator()( Container & partials )
00315 #else
00316 inline
00317 PartialList::iterator Collator::operator()( PartialList & partials )
00318 #endif
00319 { 
00320     return collate( partials );
00321 }
00322 
00323 // ---------------------------------------------------------------------------
00324 //  collate
00325 // ---------------------------------------------------------------------------
00347 //
00348 #if ! defined(NO_TEMPLATE_MEMBERS)
00349 template< typename Container >
00350 typename Container::iterator 
00351 Collator::collate( Container & partials, double partialFadeTime,
00352                                          double partialSilentTime )
00353 #else
00354 inline
00355 PartialList::iterator 
00356 Collator::collate( PartialList & partials, double partialFadeTime,
00357                                            double partialSilentTime )
00358 #endif
00359 {
00360     Collator instance( partialFadeTime, partialSilentTime );
00361     return instance.collate( partials );
00362 }
00363 
00364 }   //  end of namespace Loris
00365 
00366 #endif /* ndef INCLUDE_COLLATOR_H */
 All Classes Functions Variables Typedefs Enumerator Friends