Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

PartialUtils.h

00001 #ifndef INCLUDE_PARTIALUTILS_H
00002 #define INCLUDE_PARTIALUTILS_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-2004 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  * PartialUtils.h
00026  *
00027  *  A group of Partial utility function objects for use with STL 
00028  *  searching and sorting algorithms. PartialUtils is a namespace
00029  *  within the Loris namespace.
00030  *
00031  * This file defines three kinds of functors:
00032  * - Partial mutators
00033  * - predicates on Partials
00034  * - Partial comparitors
00035  *
00036  * Kelly Fitz, 6 July 2000
00037  * loris@cerlsoundgroup.org
00038  *
00039  * http://www.cerlsoundgroup.org/Loris/
00040  *
00041  */
00042 
00043 #include "Envelope.h"
00044 #include "Partial.h"
00045 
00046 #include <functional>
00047 #include <utility>
00048 
00049 //  begin namespace
00050 namespace Loris {
00051 
00052 namespace PartialUtils {
00053 
00054 //  -- Partial mutating functors --
00055 
00056 // ---------------------------------------------------------------------------
00057 //  PartialMutator
00058 //  
00066 class PartialMutator : public std::unary_function< Partial, void >
00067 {
00068 public:
00069 
00071     PartialMutator( double x );
00072 
00075     PartialMutator( const Envelope & e );
00076 
00078     PartialMutator( const PartialMutator & rhs );
00079 
00081     virtual ~PartialMutator( void );
00082     
00086     PartialMutator & operator=( const PartialMutator & rhs );
00087     
00091     virtual void operator()( Partial & p ) const = 0;
00092 
00093 protected:
00094     Envelope * env;
00095 };
00096 
00097 // ---------------------------------------------------------------------------
00098 //  AmplitudeScaler
00099 //  
00102 //
00103 class AmplitudeScaler : public PartialMutator
00104 {
00105 public:
00106 
00108     AmplitudeScaler( double x ) : PartialMutator( x ) {}
00109     
00112     AmplitudeScaler( const Envelope & e ) : PartialMutator( e ) {}
00113     
00116     void operator()( Partial & p ) const;
00117 };
00118 
00119 // ---------------------------------------------------------------------------
00120 //  scaleAmplitude
00121 // ---------------------------------------------------------------------------
00128 //
00129 template< class Arg >
00130 void scaleAmplitude( Partial & p, const Arg & arg )
00131 {
00132     AmplitudeScaler scaler( arg );
00133     scaler( p );
00134 }
00135 
00136 // ---------------------------------------------------------------------------
00137 //  scaleAmplitude
00138 // ---------------------------------------------------------------------------
00146 //
00147 template< class Iter, class Arg >
00148 void scaleAmplitude( Iter b, Iter e, const Arg & arg )
00149 {
00150     AmplitudeScaler scaler( arg );
00151     while ( b != e )
00152     {
00153         scaler( *b++ );
00154     }
00155 }
00156 
00157 // ---------------------------------------------------------------------------
00158 //  BandwidthScaler
00159 //  
00162 //
00163 class BandwidthScaler : public PartialMutator
00164 {
00165 public:
00166 
00168     BandwidthScaler( double x ) : PartialMutator( x ) {}
00169 
00172     BandwidthScaler( const Envelope & e ) : PartialMutator( e ) {}
00173     
00176     void operator()( Partial & p ) const;
00177 };
00178 
00179 // ---------------------------------------------------------------------------
00180 //  scaleBandwidth
00181 // ---------------------------------------------------------------------------
00188 //
00189 template< class Arg >
00190 void scaleBandwidth( Partial & p, const Arg & arg )
00191 {
00192     BandwidthScaler scaler( arg );
00193     scaler( p );
00194 }
00195 
00196 // ---------------------------------------------------------------------------
00197 //  scaleBandwidth
00198 // ---------------------------------------------------------------------------
00206 //
00207 template< class Iter, class Arg >
00208 void scaleBandwidth( Iter b, Iter e, const Arg & arg )
00209 {
00210     BandwidthScaler scaler( arg );
00211     while ( b != e )
00212     {
00213         scaler( *b++ );
00214     }
00215 }
00216 
00217 // ---------------------------------------------------------------------------
00218 //  FrequencyScaler
00219 //  
00222 //
00223 class FrequencyScaler : public PartialMutator
00224 {
00225 public:
00226 
00228     FrequencyScaler( double x ) : PartialMutator( x ) {}
00229     
00232     FrequencyScaler( const Envelope & e ) : PartialMutator( e ) {}
00233     
00236     void operator()( Partial & p ) const;
00237 };
00238 
00239 // ---------------------------------------------------------------------------
00240 //  scaleFrequency
00241 // ---------------------------------------------------------------------------
00248 //
00249 template< class Arg >
00250 void scaleFrequency( Partial & p, const Arg & arg )
00251 {
00252     FrequencyScaler scaler( arg );
00253     scaler( p );
00254 }
00255 
00256 // ---------------------------------------------------------------------------
00257 //  scaleFrequency
00258 // ---------------------------------------------------------------------------
00266 //
00267 template< class Iter, class Arg >
00268 void scaleFrequency( Iter b, Iter e, const Arg & arg )
00269 {
00270     FrequencyScaler scaler( arg );
00271     while ( b != e )
00272     {
00273         scaler( *b++ );
00274     }
00275 }
00276 
00277 // ---------------------------------------------------------------------------
00278 //  NoiseRatioScaler
00279 //  
00282 //
00283 class NoiseRatioScaler : public PartialMutator
00284 {
00285 public:
00286 
00288     NoiseRatioScaler( double x ) : PartialMutator( x ) {}
00289 
00292     NoiseRatioScaler( const Envelope & e ) : PartialMutator( e ) {}
00293     
00296     void operator()( Partial & p ) const;
00297 };
00298 
00299 // ---------------------------------------------------------------------------
00300 //  scaleNoiseRatio
00301 // ---------------------------------------------------------------------------
00308 //
00309 template< class Arg >
00310 void scaleNoiseRatio( Partial & p, const Arg & arg )
00311 {
00312     NoiseRatioScaler scaler( arg );
00313     scaler( p );
00314 }
00315 
00316 // ---------------------------------------------------------------------------
00317 //  scaleNoiseRatio
00318 // ---------------------------------------------------------------------------
00326 //
00327 template< class Iter, class Arg >
00328 void scaleNoiseRatio( Iter b, Iter e, const Arg & arg )
00329 {
00330     NoiseRatioScaler scaler( arg );
00331     while ( b != e )
00332     {
00333         scaler( *b++ );
00334     }
00335 }
00336 
00337 // ---------------------------------------------------------------------------
00338 //  PitchShifter
00339 //  
00343 //
00344 class PitchShifter : public PartialMutator
00345 {
00346 public:
00347 
00349     PitchShifter( double x ) : PartialMutator( x ) {}
00350 
00353     PitchShifter( const Envelope & e ) : PartialMutator( e ) {}
00354     
00357     void operator()( Partial & p ) const;
00358 };
00359 
00360 // ---------------------------------------------------------------------------
00361 //  shiftPitch
00362 // ---------------------------------------------------------------------------
00370 //
00371 template< class Arg >
00372 void shiftPitch( Partial & p, const Arg & arg )
00373 {
00374     PitchShifter shifter( arg );
00375     shifter( p );
00376 }
00377 
00378 // ---------------------------------------------------------------------------
00379 //  shiftPitch
00380 // ---------------------------------------------------------------------------
00389 //
00390 template< class Iter, class Arg >
00391 void shiftPitch( Iter b, Iter e, const Arg & arg )
00392 {
00393     PitchShifter shifter( arg );
00394     while ( b != e )
00395     {
00396         shifter( *b++ );
00397     }
00398 }
00399 
00400 //  These ones are not derived from PartialMutator, because
00401 //  they don't use an Envelope and cannot be time-varying.
00402 
00403 // ---------------------------------------------------------------------------
00404 //  Cropper
00405 //  
00408 class Cropper
00409 {
00410 public:
00411 
00415     Cropper( double t1, double t2 ) : 
00416         minTime( std::min( t1, t2 ) ),
00417         maxTime( std::max( t1, t2 ) )
00418     {
00419     }
00420     
00422     void operator()( Partial & p ) const;
00423     
00424 private:
00425     double minTime, maxTime;
00426 };
00427 
00428 // ---------------------------------------------------------------------------
00429 //  crop
00430 // ---------------------------------------------------------------------------
00439 //
00440 inline
00441 void crop( Partial & p, double t1, double t2 )
00442 {
00443     Cropper cropper( t1, t2 );
00444     cropper( p );
00445 }
00446 
00447 // ---------------------------------------------------------------------------
00448 //  crop
00449 // ---------------------------------------------------------------------------
00459 //
00460 template< class Iter >
00461 void crop( Iter b, Iter e, double t1, double t2 )
00462 {
00463     Cropper cropper( t1, t2 );
00464     while ( b != e )
00465     {
00466         cropper( *b++ );
00467     }
00468 }
00469 
00470 // ---------------------------------------------------------------------------
00471 //  TimeShifter
00472 //  
00475 //
00476 class TimeShifter
00477 {
00478 public:
00479 
00481     TimeShifter( double x ) : offset( x ) {}
00482     
00485     void operator()( Partial & p ) const;
00486     
00487 private:
00488     double offset;
00489 };
00490 
00491 // ---------------------------------------------------------------------------
00492 //  shiftTime
00493 // ---------------------------------------------------------------------------
00499 //
00500 inline
00501 void shiftTime( Partial & p, double offset )
00502 {
00503     TimeShifter shifter( arg );
00504     shifter( p );
00505 }
00506 
00507 // ---------------------------------------------------------------------------
00508 //  shiftTime
00509 // ---------------------------------------------------------------------------
00516 //
00517 template< class Iter >
00518 void shiftTime( Iter b, Iter e, double offset )
00519 {
00520     TimeShifter shifter( arg );
00521     while ( b != e )
00522     {
00523         shifter( *b++ );
00524     }
00525 }
00526 
00527     
00528 // ---------------------------------------------------------------------------
00529 //  crop
00530 // ---------------------------------------------------------------------------
00534 //
00535 template < typename Iterator >
00536 std::pair< double, double > 
00537 timeSpan( Iterator begin, Iterator end ) 
00538 {
00539     double tmin = 0., tmax = 0.;
00540     if ( begin != end )
00541     {
00542         Iterator it = begin;
00543         tmin = it->startTime();
00544         tmax = it->endTime();
00545         while( it != end )
00546         {
00547             tmin = std::min( tmin, it->startTime() );
00548             tmax = std::max( tmax, it->endTime() );
00549             ++it;
00550         }
00551     }
00552     return std::make_pair( tmin, tmax );
00553 }
00554     
00555 //  -- predicates --
00556 
00557 // ---------------------------------------------------------------------------
00558 //  isLabelEqual
00559 //  
00562 //
00563 class isLabelEqual : public std::unary_function< const Partial, bool >
00564 {
00565 public:
00567     isLabelEqual( int l ) : label(l) {}
00568     
00570     bool operator()( const Partial & p ) const 
00571         { return p.label() == label; }
00572         
00574     bool operator()( const Partial * p ) const 
00575         { return p->label() == label; }
00576 
00577 private:    
00578     int label;
00579 };
00580     
00581 // ---------------------------------------------------------------------------
00582 //  isLabelGreater
00583 //  
00586 //
00587 class isLabelGreater : public std::unary_function< const Partial, bool >
00588 {
00589 public:
00591     isLabelGreater( int l ) : label(l) {}
00592     
00594     bool operator()( const Partial & p ) const 
00595         { return p.label() > label; }
00596         
00598     bool operator()( const Partial * p ) const 
00599         { return p->label() > label; }
00600 
00601 private:    
00602     int label;
00603 };
00604         
00605 // ---------------------------------------------------------------------------
00606 //  isLabelLess
00607 //  
00610 //
00611 class isLabelLess : public std::unary_function< const Partial, bool >
00612 {
00613 public:
00615     isLabelLess( int l ) : label(l) {}
00616     
00618     bool operator()( const Partial & p ) const 
00619         { return p.label() < label; }
00620         
00622     bool operator()( const Partial * p ) const 
00623         { return p->label() < label; }
00624 
00625 private:    
00626     int label;
00627 };
00628         
00629 //  -- comparitors --
00630 
00631 // ---------------------------------------------------------------------------
00632 //  compareLabelLess
00633 //  
00637 //
00638 class compareLabelLess : 
00639     public std::binary_function< const Partial, const Partial, bool >
00640 {
00641 public:
00645     bool operator()( const Partial & lhs, const Partial & rhs ) const 
00646         { return lhs.label() < rhs.label(); }
00647 
00651     bool operator()( const Partial * lhs, const Partial * rhs ) const 
00652         { return lhs->label() < rhs->label(); }
00653 };
00654 
00655 // ---------------------------------------------------------------------------
00656 //  compareDurationLess
00657 //  
00661 //
00662 class compareDurationLess : 
00663     public std::binary_function< const Partial, const Partial, bool >
00664 {
00665 public:
00669     bool operator()( const Partial & lhs, const Partial & rhs ) const 
00670         { return lhs.duration() < rhs.duration(); }
00671 
00675     bool operator()( const Partial * lhs, const Partial * rhs ) const 
00676         { return lhs->duration() < rhs->duration(); }
00677 };
00678 
00679 // ---------------------------------------------------------------------------
00680 //  compareDurationGreater
00681 //  
00685 //
00686 class compareDurationGreater : 
00687     public std::binary_function< const Partial, const Partial, bool >
00688 {
00689 public:
00693     bool operator()( const Partial & lhs, const Partial & rhs ) const 
00694         { return lhs.duration() > rhs.duration(); }
00695 
00699     bool operator()( const Partial * lhs, const Partial * rhs ) const 
00700         { return lhs->duration() > rhs->duration(); }
00701 };
00702 
00703 
00704 }   //  end of namespace PartialUtils
00705 
00706 }   //  end of namespace Loris
00707 
00708 #endif /* ndef INCLUDE_PARTIALUTILS_H */

Generated on Thu Apr 14 22:01:55 2005 for Loris by doxygen 1.3.4