00001 #ifndef INCLUDE_PARTIALUTILS_H
00002 #define INCLUDE_PARTIALUTILS_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "Envelope.h"
00044 #include "Partial.h"
00045
00046 #include <functional>
00047 #include <utility>
00048
00049
00050 namespace Loris {
00051
00052 namespace PartialUtils {
00053
00054
00055
00056
00057
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
00464
00465
00466
00467
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
00485 void operator()( Partial & p ) const;
00486
00487 private:
00488 double minTime, maxTime;
00489 };
00490
00491
00492
00493
00502
00503 inline
00504 void crop( Partial & p, double t1, double t2 )
00505 {
00506 Cropper cropper( t1, t2 );
00507 cropper( p );
00508 }
00509
00510
00511
00512
00522
00523 template< class Iter >
00524 void crop( Iter b, Iter e, double t1, double t2 )
00525 {
00526 Cropper cropper( t1, t2 );
00527 while ( b != e )
00528 {
00529 cropper( *b++ );
00530 }
00531 }
00532
00533
00534
00535
00538
00539 class TimeShifter
00540 {
00541 public:
00542
00544 TimeShifter( double x ) : offset( x ) {}
00545
00548 void operator()( Partial & p ) const;
00549
00550 private:
00551 double offset;
00552 };
00553
00554
00555
00556
00562
00563 inline
00564 void shiftTime( Partial & p, double offset )
00565 {
00566 TimeShifter shifter( offset );
00567 shifter( p );
00568 }
00569
00570
00571
00572
00579
00580 template< class Iter >
00581 void shiftTime( Iter b, Iter e, double offset )
00582 {
00583 TimeShifter shifter( offset );
00584 while ( b != e )
00585 {
00586 shifter( *b++ );
00587 }
00588 }
00589
00590
00591
00592
00596
00597 template < typename Iterator >
00598 std::pair< double, double >
00599 timeSpan( Iterator begin, Iterator end )
00600 {
00601 double tmin = 0., tmax = 0.;
00602 if ( begin != end )
00603 {
00604 Iterator it = begin;
00605 tmin = it->startTime();
00606 tmax = it->endTime();
00607 while( it != end )
00608 {
00609 tmin = std::min( tmin, it->startTime() );
00610 tmax = std::max( tmax, it->endTime() );
00611 ++it;
00612 }
00613 }
00614 return std::make_pair( tmin, tmax );
00615 }
00616
00617
00618
00619
00625
00626 double peakAmplitude( const Partial & p );
00627
00628
00629
00630
00636
00637 double avgAmplitude( const Partial & p );
00638
00639
00640
00641
00647
00648 double avgFrequency( const Partial & p );
00649
00650
00651
00652
00659
00660 double weightedAvgFrequency( const Partial & p );
00661
00662
00663
00664
00665
00666
00680
00681 void fixPhaseBefore( Partial & p, double t );
00682
00683
00684
00685
00702
00703 template < class Iter >
00704 void fixPhaseBefore( Iter b, Iter e, double t )
00705 {
00706 while ( b != e )
00707 {
00708 fixPhaseBefore( *b, t );
00709 ++b;
00710 }
00711 }
00712
00713
00714
00715
00728
00729 void fixPhaseAfter( Partial & p, double t );
00730
00731
00732
00733
00749
00750 template < class Iter >
00751 void fixPhaseAfter( Iter b, Iter e, double t )
00752 {
00753 while ( b != e )
00754 {
00755 fixPhaseAfter( *b, t );
00756 ++b;
00757 }
00758 }
00759
00760
00761
00762
00780
00781 void fixPhaseForward( Partial & p, double tbeg, double tend );
00782
00783
00784
00785
00805
00806 template < class Iter >
00807 void fixPhaseForward( Iter b, Iter e, double tbeg, double tend )
00808 {
00809 while ( b != e )
00810 {
00811 fixPhaseForward( *b, tbeg, tend );
00812 ++b;
00813 }
00814 }
00815
00816
00817
00818
00819
00836
00837 void fixPhaseAt( Partial & p, double t );
00838
00839
00840
00841
00861
00862 template < class Iter >
00863 void fixPhaseAt( Iter b, Iter e, double t )
00864 {
00865 while ( b != e )
00866 {
00867 fixPhaseAt( *b, t );
00868 ++b;
00869 }
00870 }
00871
00872
00873
00874
00903
00904 void fixPhaseBetween( Partial & p, double t1, double t2 );
00905
00906
00907
00908
00939
00940 template < class Iter >
00941 void fixPhaseBetween( Iter b, Iter e, double t1, double t2 )
00942 {
00943 while ( b != e )
00944 {
00945 fixPhaseBetween( *b, t1, t2 );
00946 ++b;
00947 }
00948 }
00949
00950
00951
00952
00953
00954
00958
00959 class isDurationLess : public std::unary_function< const Partial, bool >
00960 {
00961 public:
00963 isDurationLess( double x ) : mDurationSecs(x) {}
00964
00966 bool operator()( const Partial & p ) const
00967 { return p.duration() < mDurationSecs; }
00968
00970 bool operator()( const Partial * p ) const
00971 { return p->duration() < mDurationSecs; }
00972
00973 private:
00974 double mDurationSecs;
00975 };
00976
00977
00978
00979
00982
00983 class isLabelEqual : public std::unary_function< const Partial, bool >
00984 {
00985 public:
00987 isLabelEqual( int l ) : label(l) {}
00988
00990 bool operator()( const Partial & p ) const
00991 { return p.label() == label; }
00992
00994 bool operator()( const Partial * p ) const
00995 { return p->label() == label; }
00996
00997 private:
00998 int label;
00999 };
01000
01001
01002
01003
01006
01007 class isLabelGreater : public std::unary_function< const Partial, bool >
01008 {
01009 public:
01011 isLabelGreater( int l ) : label(l) {}
01012
01014 bool operator()( const Partial & p ) const
01015 { return p.label() > label; }
01016
01018 bool operator()( const Partial * p ) const
01019 { return p->label() > label; }
01020
01021 private:
01022 int label;
01023 };
01024
01025
01026
01027
01030
01031 class isLabelLess : public std::unary_function< const Partial, bool >
01032 {
01033 public:
01035 isLabelLess( int l ) : label(l) {}
01036
01038 bool operator()( const Partial & p ) const
01039 { return p.label() < label; }
01040
01042 bool operator()( const Partial * p ) const
01043 { return p->label() < label; }
01044
01045 private:
01046 int label;
01047 };
01048
01049
01050
01051
01055
01056 class isPeakLess : public std::unary_function< const Partial, bool >
01057 {
01058 public:
01060 isPeakLess( double x ) : thresh(x) {}
01061
01063 bool operator()( const Partial & p ) const
01064 { return peakAmplitude( p ) < thresh; }
01065
01067 bool operator()( const Partial * p ) const
01068 { return peakAmplitude( *p ) < thresh; }
01069
01070 private:
01071 double thresh;
01072 };
01073
01074
01075
01076
01077
01078
01082
01083 class compareLabelLess :
01084 public std::binary_function< const Partial, const Partial, bool >
01085 {
01086 public:
01090 bool operator()( const Partial & lhs, const Partial & rhs ) const
01091 { return lhs.label() < rhs.label(); }
01092
01096 bool operator()( const Partial * lhs, const Partial * rhs ) const
01097 { return lhs->label() < rhs->label(); }
01098 };
01099
01100
01101
01102
01106
01107 class compareDurationLess :
01108 public std::binary_function< const Partial, const Partial, bool >
01109 {
01110 public:
01114 bool operator()( const Partial & lhs, const Partial & rhs ) const
01115 { return lhs.duration() < rhs.duration(); }
01116
01120 bool operator()( const Partial * lhs, const Partial * rhs ) const
01121 { return lhs->duration() < rhs->duration(); }
01122 };
01123
01124
01125
01126
01130
01131 class compareDurationGreater :
01132 public std::binary_function< const Partial, const Partial, bool >
01133 {
01134 public:
01138 bool operator()( const Partial & lhs, const Partial & rhs ) const
01139 { return lhs.duration() > rhs.duration(); }
01140
01144 bool operator()( const Partial * lhs, const Partial * rhs ) const
01145 { return lhs->duration() > rhs->duration(); }
01146 };
01147
01148
01149
01150
01154
01155 class compareStartTimeLess :
01156 public std::binary_function< const Partial, const Partial, bool >
01157 {
01158 public:
01162 bool operator()( const Partial & lhs, const Partial & rhs ) const
01163 { return lhs.startTime() < rhs.startTime(); }
01164
01168 bool operator()( const Partial * lhs, const Partial * rhs ) const
01169 { return lhs->startTime() < rhs->startTime(); }
01170 };
01171
01172
01173
01174 }
01175
01176 }
01177
01178 #endif