|
Loris 1.5
|
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-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 * 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 00097 Envelope * env; 00098 }; 00099 00100 // --------------------------------------------------------------------------- 00101 // AmplitudeScaler 00102 // 00105 // 00106 class AmplitudeScaler : public PartialMutator 00107 { 00108 public: 00109 00111 AmplitudeScaler( double x ) : PartialMutator( x ) {} 00112 00115 AmplitudeScaler( const Envelope & e ) : PartialMutator( e ) {} 00116 00119 void operator()( Partial & p ) const; 00120 }; 00121 00122 // --------------------------------------------------------------------------- 00123 // scaleAmplitude 00124 // --------------------------------------------------------------------------- 00131 // 00132 template< class Arg > 00133 void scaleAmplitude( Partial & p, const Arg & arg ) 00134 { 00135 AmplitudeScaler scaler( arg ); 00136 scaler( p ); 00137 } 00138 00139 // --------------------------------------------------------------------------- 00140 // scaleAmplitude 00141 // --------------------------------------------------------------------------- 00149 // 00150 template< class Iter, class Arg > 00151 void scaleAmplitude( Iter b, Iter e, const Arg & arg ) 00152 { 00153 AmplitudeScaler scaler( arg ); 00154 while ( b != e ) 00155 { 00156 scaler( *b++ ); 00157 } 00158 } 00159 00160 // --------------------------------------------------------------------------- 00161 // BandwidthScaler 00162 // 00165 // 00166 class BandwidthScaler : public PartialMutator 00167 { 00168 public: 00169 00171 BandwidthScaler( double x ) : PartialMutator( x ) {} 00172 00175 BandwidthScaler( const Envelope & e ) : PartialMutator( e ) {} 00176 00179 void operator()( Partial & p ) const; 00180 }; 00181 00182 // --------------------------------------------------------------------------- 00183 // scaleBandwidth 00184 // --------------------------------------------------------------------------- 00191 // 00192 template< class Arg > 00193 void scaleBandwidth( Partial & p, const Arg & arg ) 00194 { 00195 BandwidthScaler scaler( arg ); 00196 scaler( p ); 00197 } 00198 00199 // --------------------------------------------------------------------------- 00200 // scaleBandwidth 00201 // --------------------------------------------------------------------------- 00209 // 00210 template< class Iter, class Arg > 00211 void scaleBandwidth( Iter b, Iter e, const Arg & arg ) 00212 { 00213 BandwidthScaler scaler( arg ); 00214 while ( b != e ) 00215 { 00216 scaler( *b++ ); 00217 } 00218 } 00219 00220 // --------------------------------------------------------------------------- 00221 // BandwidthSetter 00222 // 00225 // 00226 class BandwidthSetter : public PartialMutator 00227 { 00228 public: 00229 00231 BandwidthSetter( double x ) : PartialMutator( x ) {} 00232 00235 BandwidthSetter( const Envelope & e ) : PartialMutator( e ) {} 00236 00239 void operator()( Partial & p ) const; 00240 }; 00241 00242 // --------------------------------------------------------------------------- 00243 // setBandwidth 00244 // --------------------------------------------------------------------------- 00251 // 00252 template< class Arg > 00253 void setBandwidth( Partial & p, const Arg & arg ) 00254 { 00255 BandwidthSetter setter( arg ); 00256 setter( p ); 00257 } 00258 00259 // --------------------------------------------------------------------------- 00260 // setBandwidth 00261 // --------------------------------------------------------------------------- 00269 // 00270 template< class Iter, class Arg > 00271 void setBandwidth( Iter b, Iter e, const Arg & arg ) 00272 { 00273 BandwidthSetter setter( arg ); 00274 while ( b != e ) 00275 { 00276 setter( *b++ ); 00277 } 00278 } 00279 00280 // --------------------------------------------------------------------------- 00281 // FrequencyScaler 00282 // 00285 // 00286 class FrequencyScaler : public PartialMutator 00287 { 00288 public: 00289 00291 FrequencyScaler( double x ) : PartialMutator( x ) {} 00292 00295 FrequencyScaler( const Envelope & e ) : PartialMutator( e ) {} 00296 00299 void operator()( Partial & p ) const; 00300 }; 00301 00302 // --------------------------------------------------------------------------- 00303 // scaleFrequency 00304 // --------------------------------------------------------------------------- 00311 // 00312 template< class Arg > 00313 void scaleFrequency( Partial & p, const Arg & arg ) 00314 { 00315 FrequencyScaler scaler( arg ); 00316 scaler( p ); 00317 } 00318 00319 // --------------------------------------------------------------------------- 00320 // scaleFrequency 00321 // --------------------------------------------------------------------------- 00329 // 00330 template< class Iter, class Arg > 00331 void scaleFrequency( Iter b, Iter e, const Arg & arg ) 00332 { 00333 FrequencyScaler scaler( arg ); 00334 while ( b != e ) 00335 { 00336 scaler( *b++ ); 00337 } 00338 } 00339 00340 // --------------------------------------------------------------------------- 00341 // NoiseRatioScaler 00342 // 00345 // 00346 class NoiseRatioScaler : public PartialMutator 00347 { 00348 public: 00349 00351 NoiseRatioScaler( double x ) : PartialMutator( x ) {} 00352 00355 NoiseRatioScaler( const Envelope & e ) : PartialMutator( e ) {} 00356 00359 void operator()( Partial & p ) const; 00360 }; 00361 00362 // --------------------------------------------------------------------------- 00363 // scaleNoiseRatio 00364 // --------------------------------------------------------------------------- 00371 // 00372 template< class Arg > 00373 void scaleNoiseRatio( Partial & p, const Arg & arg ) 00374 { 00375 NoiseRatioScaler scaler( arg ); 00376 scaler( p ); 00377 } 00378 00379 // --------------------------------------------------------------------------- 00380 // scaleNoiseRatio 00381 // --------------------------------------------------------------------------- 00389 // 00390 template< class Iter, class Arg > 00391 void scaleNoiseRatio( Iter b, Iter e, const Arg & arg ) 00392 { 00393 NoiseRatioScaler scaler( arg ); 00394 while ( b != e ) 00395 { 00396 scaler( *b++ ); 00397 } 00398 } 00399 00400 // --------------------------------------------------------------------------- 00401 // PitchShifter 00402 // 00406 // 00407 class PitchShifter : public PartialMutator 00408 { 00409 public: 00410 00412 PitchShifter( double x ) : PartialMutator( x ) {} 00413 00416 PitchShifter( const Envelope & e ) : PartialMutator( e ) {} 00417 00420 void operator()( Partial & p ) const; 00421 }; 00422 00423 // --------------------------------------------------------------------------- 00424 // shiftPitch 00425 // --------------------------------------------------------------------------- 00433 // 00434 template< class Arg > 00435 void shiftPitch( Partial & p, const Arg & arg ) 00436 { 00437 PitchShifter shifter( arg ); 00438 shifter( p ); 00439 } 00440 00441 // --------------------------------------------------------------------------- 00442 // shiftPitch 00443 // --------------------------------------------------------------------------- 00452 // 00453 template< class Iter, class Arg > 00454 void shiftPitch( Iter b, Iter e, const Arg & arg ) 00455 { 00456 PitchShifter shifter( arg ); 00457 while ( b != e ) 00458 { 00459 shifter( *b++ ); 00460 } 00461 } 00462 00463 // These ones are not derived from PartialMutator, because 00464 // they don't use an Envelope and cannot be time-varying. 00465 00466 // --------------------------------------------------------------------------- 00467 // Cropper 00468 // 00471 class Cropper 00472 { 00473 public: 00474 00478 Cropper( double t1, double t2 ) : 00479 minTime( std::min( t1, t2 ) ), 00480 maxTime( std::max( t1, t2 ) ) 00481 { 00482 } 00483 00488 void operator()( Partial & p ) const; 00489 00490 private: 00491 double minTime, maxTime; 00492 }; 00493 00494 // --------------------------------------------------------------------------- 00495 // crop 00496 // --------------------------------------------------------------------------- 00508 // 00509 inline 00510 void crop( Partial & p, double t1, double t2 ) 00511 { 00512 Cropper cropper( t1, t2 ); 00513 cropper( p ); 00514 } 00515 00516 // --------------------------------------------------------------------------- 00517 // crop 00518 // --------------------------------------------------------------------------- 00531 // 00532 template< class Iter > 00533 void crop( Iter b, Iter e, double t1, double t2 ) 00534 { 00535 Cropper cropper( t1, t2 ); 00536 while ( b != e ) 00537 { 00538 cropper( *b++ ); 00539 } 00540 } 00541 00542 // --------------------------------------------------------------------------- 00543 // TimeShifter 00544 // 00547 // 00548 class TimeShifter 00549 { 00550 public: 00551 00553 TimeShifter( double x ) : offset( x ) {} 00554 00557 void operator()( Partial & p ) const; 00558 00559 private: 00560 double offset; 00561 }; 00562 00563 // --------------------------------------------------------------------------- 00564 // shiftTime 00565 // --------------------------------------------------------------------------- 00571 // 00572 inline 00573 void shiftTime( Partial & p, double offset ) 00574 { 00575 TimeShifter shifter( offset ); 00576 shifter( p ); 00577 } 00578 00579 // --------------------------------------------------------------------------- 00580 // shiftTime 00581 // --------------------------------------------------------------------------- 00588 // 00589 template< class Iter > 00590 void shiftTime( Iter b, Iter e, double offset ) 00591 { 00592 TimeShifter shifter( offset ); 00593 while ( b != e ) 00594 { 00595 shifter( *b++ ); 00596 } 00597 } 00598 00599 // --------------------------------------------------------------------------- 00600 // timeSpan 00601 // --------------------------------------------------------------------------- 00605 // 00606 template < typename Iterator > 00607 std::pair< double, double > 00608 timeSpan( Iterator begin, Iterator end ) 00609 { 00610 double tmin = 0., tmax = 0.; 00611 if ( begin != end ) 00612 { 00613 Iterator it = begin; 00614 tmin = it->startTime(); 00615 tmax = it->endTime(); 00616 while( it != end ) 00617 { 00618 tmin = std::min( tmin, it->startTime() ); 00619 tmax = std::max( tmax, it->endTime() ); 00620 ++it; 00621 } 00622 } 00623 return std::make_pair( tmin, tmax ); 00624 } 00625 00626 // --------------------------------------------------------------------------- 00627 // peakAmplitude 00628 // --------------------------------------------------------------------------- 00634 // 00635 double peakAmplitude( const Partial & p ); 00636 00637 // --------------------------------------------------------------------------- 00638 // avgAmplitude 00639 // --------------------------------------------------------------------------- 00645 // 00646 double avgAmplitude( const Partial & p ); 00647 00648 // --------------------------------------------------------------------------- 00649 // avgFrequency 00650 // --------------------------------------------------------------------------- 00656 // 00657 double avgFrequency( const Partial & p ); 00658 00659 // --------------------------------------------------------------------------- 00660 // weightedAvgFrequency 00661 // --------------------------------------------------------------------------- 00668 // 00669 double weightedAvgFrequency( const Partial & p ); 00670 00671 // -- phase maintenance functions -- 00672 00673 // --------------------------------------------------------------------------- 00674 // fixPhaseBefore 00675 // --------------------------------------------------------------------------- 00689 // 00690 void fixPhaseBefore( Partial & p, double t ); 00691 00692 // --------------------------------------------------------------------------- 00693 // fixPhaseBefore (range) 00694 // --------------------------------------------------------------------------- 00711 // 00712 template < class Iter > 00713 void fixPhaseBefore( Iter b, Iter e, double t ) 00714 { 00715 while ( b != e ) 00716 { 00717 fixPhaseBefore( *b, t ); 00718 ++b; 00719 } 00720 } 00721 00722 // --------------------------------------------------------------------------- 00723 // fixPhaseAfter 00724 // --------------------------------------------------------------------------- 00737 // 00738 void fixPhaseAfter( Partial & p, double t ); 00739 00740 // --------------------------------------------------------------------------- 00741 // fixPhaseAfter (range) 00742 // --------------------------------------------------------------------------- 00758 // 00759 template < class Iter > 00760 void fixPhaseAfter( Iter b, Iter e, double t ) 00761 { 00762 while ( b != e ) 00763 { 00764 fixPhaseAfter( *b, t ); 00765 ++b; 00766 } 00767 } 00768 00769 // --------------------------------------------------------------------------- 00770 // fixPhaseForward 00771 // --------------------------------------------------------------------------- 00789 // 00790 void fixPhaseForward( Partial & p, double tbeg, double tend ); 00791 00792 // --------------------------------------------------------------------------- 00793 // fixPhaseForward (range) 00794 // --------------------------------------------------------------------------- 00814 // 00815 template < class Iter > 00816 void fixPhaseForward( Iter b, Iter e, double tbeg, double tend ) 00817 { 00818 while ( b != e ) 00819 { 00820 fixPhaseForward( *b, tbeg, tend ); 00821 ++b; 00822 } 00823 } 00824 00825 00826 // --------------------------------------------------------------------------- 00827 // fixPhaseAt 00828 // --------------------------------------------------------------------------- 00845 // 00846 void fixPhaseAt( Partial & p, double t ); 00847 00848 // --------------------------------------------------------------------------- 00849 // fixPhaseAt (range) 00850 // --------------------------------------------------------------------------- 00870 // 00871 template < class Iter > 00872 void fixPhaseAt( Iter b, Iter e, double t ) 00873 { 00874 while ( b != e ) 00875 { 00876 fixPhaseAt( *b, t ); 00877 ++b; 00878 } 00879 } 00880 00881 // --------------------------------------------------------------------------- 00882 // fixPhaseBetween 00883 // --------------------------------------------------------------------------- 00912 // 00913 void fixPhaseBetween( Partial & p, double t1, double t2 ); 00914 00915 // --------------------------------------------------------------------------- 00916 // fixPhaseBetween (range) 00917 // --------------------------------------------------------------------------- 00948 // 00949 template < class Iter > 00950 void fixPhaseBetween( Iter b, Iter e, double t1, double t2 ) 00951 { 00952 while ( b != e ) 00953 { 00954 fixPhaseBetween( *b, t1, t2 ); 00955 ++b; 00956 } 00957 } 00958 00959 00960 00961 // -- predicates -- 00962 00963 // --------------------------------------------------------------------------- 00964 // isDurationLess 00965 // 00969 // 00970 class isDurationLess : public std::unary_function< const Partial, bool > 00971 { 00972 public: 00974 isDurationLess( double x ) : mDurationSecs(x) {} 00975 00977 bool operator()( const Partial & p ) const 00978 { return p.duration() < mDurationSecs; } 00979 00981 bool operator()( const Partial * p ) const 00982 { return p->duration() < mDurationSecs; } 00983 00984 private: 00985 double mDurationSecs; 00986 }; 00987 00988 // --------------------------------------------------------------------------- 00989 // isLabelEqual 00990 // 00993 // 00994 class isLabelEqual : public std::unary_function< const Partial, bool > 00995 { 00996 public: 00998 isLabelEqual( int l ) : label(l) {} 00999 01001 bool operator()( const Partial & p ) const 01002 { return p.label() == label; } 01003 01005 bool operator()( const Partial * p ) const 01006 { return p->label() == label; } 01007 01008 private: 01009 int label; 01010 }; 01011 01012 // --------------------------------------------------------------------------- 01013 // isLabelGreater 01014 // 01017 // 01018 class isLabelGreater : public std::unary_function< const Partial, bool > 01019 { 01020 public: 01022 isLabelGreater( int l ) : label(l) {} 01023 01025 bool operator()( const Partial & p ) const 01026 { return p.label() > label; } 01027 01029 bool operator()( const Partial * p ) const 01030 { return p->label() > label; } 01031 01032 private: 01033 int label; 01034 }; 01035 01036 // --------------------------------------------------------------------------- 01037 // isLabelLess 01038 // 01041 // 01042 class isLabelLess : public std::unary_function< const Partial, bool > 01043 { 01044 public: 01046 isLabelLess( int l ) : label(l) {} 01047 01049 bool operator()( const Partial & p ) const 01050 { return p.label() < label; } 01051 01053 bool operator()( const Partial * p ) const 01054 { return p->label() < label; } 01055 01056 private: 01057 int label; 01058 }; 01059 01060 // --------------------------------------------------------------------------- 01061 // isPeakLess 01062 // 01066 // 01067 class isPeakLess : public std::unary_function< const Partial, bool > 01068 { 01069 public: 01071 isPeakLess( double x ) : thresh(x) {} 01072 01074 bool operator()( const Partial & p ) const 01075 { return peakAmplitude( p ) < thresh; } 01076 01078 bool operator()( const Partial * p ) const 01079 { return peakAmplitude( *p ) < thresh; } 01080 01081 private: 01082 double thresh; 01083 }; 01084 01085 // -- comparitors -- 01086 01087 // --------------------------------------------------------------------------- 01088 // compareLabelLess 01089 // 01093 // 01094 class compareLabelLess : 01095 public std::binary_function< const Partial, const Partial, bool > 01096 { 01097 public: 01101 bool operator()( const Partial & lhs, const Partial & rhs ) const 01102 { return lhs.label() < rhs.label(); } 01103 01107 bool operator()( const Partial * lhs, const Partial * rhs ) const 01108 { return lhs->label() < rhs->label(); } 01109 }; 01110 01111 // --------------------------------------------------------------------------- 01112 // compareDurationLess 01113 // 01117 // 01118 class compareDurationLess : 01119 public std::binary_function< const Partial, const Partial, bool > 01120 { 01121 public: 01125 bool operator()( const Partial & lhs, const Partial & rhs ) const 01126 { return lhs.duration() < rhs.duration(); } 01127 01131 bool operator()( const Partial * lhs, const Partial * rhs ) const 01132 { return lhs->duration() < rhs->duration(); } 01133 }; 01134 01135 // --------------------------------------------------------------------------- 01136 // compareDurationGreater 01137 // 01141 // 01142 class compareDurationGreater : 01143 public std::binary_function< const Partial, const Partial, bool > 01144 { 01145 public: 01149 bool operator()( const Partial & lhs, const Partial & rhs ) const 01150 { return lhs.duration() > rhs.duration(); } 01151 01155 bool operator()( const Partial * lhs, const Partial * rhs ) const 01156 { return lhs->duration() > rhs->duration(); } 01157 }; 01158 01159 // --------------------------------------------------------------------------- 01160 // compareStartTimeLess 01161 // 01165 // 01166 class compareStartTimeLess : 01167 public std::binary_function< const Partial, const Partial, bool > 01168 { 01169 public: 01173 bool operator()( const Partial & lhs, const Partial & rhs ) const 01174 { return lhs.startTime() < rhs.startTime(); } 01175 01179 bool operator()( const Partial * lhs, const Partial * rhs ) const 01180 { return lhs->startTime() < rhs->startTime(); } 01181 }; 01182 01183 01184 01185 } // end of namespace PartialUtils 01186 01187 } // end of namespace Loris 01188 01189 #endif /* ndef INCLUDE_PARTIALUTILS_H */