Loris 1.5

Morpher.h

00001 #ifndef INCLUDE_MORPHER_H
00002 #define INCLUDE_MORPHER_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  * Morpher.h
00026  *
00027  * Definition of class Morpher.
00028  *
00029  * Kelly Fitz, 15 Oct 1999
00030  * loris@cerlsoundgroup.org
00031  *
00032  * http://www.cerlsoundgroup.org/Loris/
00033  *
00034  */
00035 #include "PartialList.h"
00036 #include "Partial.h"
00037 
00038 #include <memory>   // for auto_ptr
00039 
00040 //  begin namespace
00041 namespace Loris {
00042 
00043 class Envelope;
00044 
00045 // ---------------------------------------------------------------------------
00046 //  Class Morpher
00047 //
00066 //
00067 class Morpher
00068 {
00069 //  -- instance variables --
00070 
00071     std::auto_ptr< Envelope > _freqFunction;  
00072     std::auto_ptr< Envelope > _ampFunction;   
00073     std::auto_ptr< Envelope > _bwFunction;    
00074     
00075     PartialList _partials;                    
00076     
00077     Partial _srcRefPartial;         
00078     Partial _tgtRefPartial;         
00079 
00080 
00081 
00082 
00083     double _freqFixThresholdDb;     
00084 
00085 
00086     
00087     double _logMorphShape;          
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095                                                             
00096     double _minBreakpointGapSec;    
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104                                     
00105                                     
00106     bool _doLogAmpMorphing;         
00107 
00108 
00109                                     
00110     bool _doLogFreqMorphing;        
00111 
00112 
00113     
00114     
00115 //  -- public interface --
00116 public:
00117 //  -- construction --
00118 
00124     Morpher( const Envelope & f );
00125 
00132     Morpher( const Envelope & ff, const Envelope & af, const Envelope & bwf );
00133 
00137     Morpher( const Morpher & rhs );
00138     
00140     ~Morpher( void );
00141     
00145     Morpher & operator= ( const Morpher & rhs );
00146 
00147 //  -- morphed parameter computation --
00148 
00149 //  -- Partial morphing --
00150 
00167     Partial morphPartials( Partial src, Partial tgt, int assignLabel );
00168     
00171     Partial morphPartial( Partial src, Partial tgt, int assignLabel )
00172         { return morphPartials( src, tgt, assignLabel ); }
00173 
00194     void morph( PartialList::const_iterator beginSrc, 
00195                 PartialList::const_iterator endSrc,
00196                 PartialList::const_iterator beginTgt, 
00197                 PartialList::const_iterator endTgt );
00198 
00224     void crossfade( PartialList::const_iterator beginSrc, 
00225                     PartialList::const_iterator endSrc,
00226                     PartialList::const_iterator beginTgt, 
00227                     PartialList::const_iterator endTgt,
00228                     Partial::label_type label = 0 );
00229 
00230 
00242     //
00243     Breakpoint
00244     morphBreakpoints( Breakpoint srcBkpt, Breakpoint tgtBkpt, 
00245                       double time  ) const;
00246                                
00263     Breakpoint 
00264     morphSrcBreakpoint( const Breakpoint & bp, const Partial & tgtPartial, 
00265                         double time ) const;
00266 
00283     Breakpoint 
00284     morphTgtBreakpoint( const Breakpoint & bp, const Partial & srcPartial, 
00285                         double time ) const;
00286 
00297     Breakpoint fadeSrcBreakpoint( Breakpoint bp, double time ) const;
00298 
00309     Breakpoint fadeTgtBreakpoint( Breakpoint bp, double time ) const;
00310     
00311 //  -- morphing function access/mutation --
00312 
00314     void setFrequencyFunction( const Envelope & f );
00315     
00317     void setAmplitudeFunction( const Envelope & f );
00318     
00320     void setBandwidthFunction( const Envelope & f );
00321 
00323     const Envelope & frequencyFunction( void ) const;
00324     
00326     const Envelope & amplitudeFunction( void ) const;
00327     
00329     const Envelope & bandwidthFunction( void ) const;
00330     
00335     double amplitudeShape( void ) const;
00336     
00344     void setAmplitudeShape( double x );
00345     
00348     void enableLogAmpMorphing( bool enable = true ) { _doLogAmpMorphing = enable; }
00349     
00352     void enableLogFreqMorphing( bool enable = true )  { _doLogFreqMorphing = enable; }
00353     
00354     
00362     double minBreakpointGap( void ) const;
00363 
00375     void setMinBreakpointGap( double x );
00376 
00377 
00378 //  -- reference Partial label access/mutation --
00379     
00388     const Partial & sourceReferencePartial( void ) const;
00389     
00398     Partial & sourceReferencePartial( void );
00399     
00408     const Partial & targetReferencePartial( void ) const;
00409 
00418     Partial & targetReferencePartial( void );
00419     
00429     void setSourceReferencePartial( const Partial & p = Partial() );
00430     
00444     void setSourceReferencePartial( const PartialList & partials, 
00445                                     Partial::label_type refLabel );
00446     
00456     void setTargetReferencePartial( const Partial & p = Partial() );
00457 
00471     void setTargetReferencePartial( const PartialList & partials, 
00472                                     Partial::label_type refLabel );
00473     
00474 //  -- PartialList access --
00475 
00477     PartialList & partials( void ); 
00478 
00480     const PartialList & partials( void ) const; 
00481 
00482 //  -- global morphing defaults and constants --
00483 
00488     static const double DefaultFixThreshold;
00489 
00498     static const double DefaultAmpShape;    
00499 
00503     static const double DefaultBreakpointGap;
00504 
00505 private:
00506     
00507 //  -- helper --
00508 
00509     //  PartialCorrespondence represents a map from non-zero Partial 
00510     //  labels to pairs of pointers to Partials that should be morphed 
00511     //  into a single Partial that is assigned that label. 
00512     //  MorphingPair is a pair of pointers to Partials that are
00513     //  initialized to zero, and it is the element type for the
00514     //  PartialCorrespondence map.
00515     struct MorphingPair
00516     {
00517         Partial src;
00518         Partial tgt;    
00519     };
00520     typedef std::map< Partial::label_type, MorphingPair > PartialCorrespondence;
00521     
00525     void morph_aux( PartialCorrespondence & correspondence );
00526     
00544     //
00545     void appendMorphedSrc( Breakpoint srcBkpt, const Partial & tgtPartial, 
00546                            double time, Partial & newp  );
00547                            
00565     //
00566     void appendMorphedTgt( Breakpoint tgtBkpt, const Partial & srcPartial, 
00567                            double time, Partial & newp  );
00568                            
00569                            
00571     Breakpoint 
00572     interpolateParameters( const Breakpoint & srcBkpt, const Breakpoint & tgtBkpt,
00573                            double fweight, double aweight, double bweight ) const;
00574 
00575     double interpolateAmplitude( double srcAmp, double tgtAmp, double alpha ) const;
00576     double interpolateBandwidth( double srcBw, double tgtBw, double alpha ) const;
00577     double interpolateFrequency( double srcFreq, double tgtFreq, double alpha ) const;
00578     double interpolatePhase( double srcphase, double tgtphase, double alpha ) const;
00579     
00580 
00581     //  Recompute phases for a morphed Partial, so that the synthesized phases 
00582     //  match the source phases as closesly as possible at times when the 
00583     //  frequency morphing function is equal to 0 or 1. 
00584     void fixMorphedPhases( Partial & newp ) const;                           
00585 
00586 };  //  end of class Morpher
00587 
00588 }   //  end of namespace Loris
00589 
00590 #endif /* ndef INCLUDE_MORPHER_H */
 All Classes Functions Variables Typedefs Enumerator Friends