Analyzer.h

00001 #ifndef INCLUDE_ANALYZER_H
00002 #define INCLUDE_ANALYZER_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-2007 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  * Analyzer.h
00026  *
00027  * Definition of class Loris::Analyzer.
00028  *
00029  * Kelly Fitz, 5 Dec 99
00030  * loris@cerlsoundgroup.org
00031  *
00032  * http://www.cerlsoundgroup.org/Loris/
00033  *
00034  */
00035 #include <memory>
00036 #include <vector>
00037 #include "LinearEnvelope.h"
00038 #include "Partial.h"
00039 #include "PartialList.h"
00040 #include "SpectralPeaks.h"
00041 
00042 //  begin namespace
00043 namespace Loris {
00044 
00045 class Envelope;
00046 class LinearEnvelopeBuilder;
00047 
00048 // ---------------------------------------------------------------------------
00049 //  class Analyzer
00050 //
00098 //
00099 class Analyzer
00100 {
00101 //  -- public interface --
00102 public:
00103 
00104 //  -- construction --
00105 
00112     explicit Analyzer( double resolutionHz );
00113     
00123     Analyzer( double resolutionHz, double windowWidthHz );
00124 
00130     Analyzer( const Analyzer & other );
00131 
00133     ~Analyzer( void );
00134 
00140     Analyzer & operator=( const Analyzer & rhs );
00141 
00142 //  -- configuration --
00143 
00151     void configure( double resolutionHz );
00152 
00169     void configure( double resolutionHz, double windowWidthHz );
00170     
00171 //  -- analysis --
00172 
00179     void analyze( const std::vector<double> & vec, double srate );
00180     
00189     void analyze( const double * bufBegin, const double * bufEnd, double srate );
00190     
00191 //  -- tracking analysis --
00192 
00202     void analyze( const std::vector<double> & vec, double srate, 
00203                   const Envelope & reference );
00204     
00216     void analyze( const double * bufBegin, const double * bufEnd, double srate,
00217                   const Envelope & reference );
00218     
00219 //  -- parameter access --
00220 
00223     double ampFloor( void ) const;
00224 
00229     double cropTime( void ) const;
00230 
00233     double freqDrift( void ) const;
00234 
00237     double freqFloor( void ) const;
00238 
00241     double freqResolution( void ) const;
00242 
00246     double hopTime( void ) const;
00247 
00254     double sidelobeLevel( void ) const;
00255 
00258     double windowWidth( void ) const;
00259      
00266     bool phaseCorrect( void ) const;
00267 
00268 
00269 //  -- parameter mutation --
00270 
00275     void setAmpFloor( double x );
00276 
00283     void setCropTime( double x );
00284 
00289     void setFreqDrift( double x );
00290 
00295     void setFreqFloor( double x );
00296 
00302     void setFreqResolution( double x );
00303 
00308     void setHopTime( double x );
00309 
00318     void setSidelobeLevel( double x );
00319 
00324     void setWindowWidth( double x );
00325 
00332     void setPhaseCorrect( bool TF = true );
00333     
00334     
00335 //  -- bandwidth envelope specification --
00336 
00337     enum { Default_ResidueBandwidth_RegionWidth = 2000,
00338            Default_ConvergenceBandwidth_TolerancePct = 10 };
00339            
00350     void storeResidueBandwidth( double regionWidth = Default_ResidueBandwidth_RegionWidth );
00351     
00364     void storeConvergenceBandwidth( double tolerancePct = 
00365             0.01 * (double)Default_ConvergenceBandwidth_TolerancePct );
00366     
00369     void storeNoBandwidth( void );
00370     
00374     bool bandwidthIsResidue( void ) const;
00375     
00379     bool bandwidthIsConvergence( void ) const;
00380     
00385     double bwRegionWidth( void ) const;
00386 
00391     double bwConvergenceTolerance( void ) const;
00392 
00397     bool associateBandwidth( void ) const
00398         { return bandwidthIsResidue() || bandwidthIsConvergence(); }
00399 
00401     void setBwRegionWidth( double x ) 
00402         { 
00403             if ( x != 0 )
00404             {
00405                 storeResidueBandwidth( x ); 
00406             }
00407             else
00408             {
00409                 storeNoBandwidth();
00410             }
00411         }
00412            
00413 
00414 //  -- PartialList access --
00415 
00418     PartialList & partials( void );
00419 
00422     const PartialList & partials( void ) const;
00423 
00424 //  -- envelope access --
00425 
00426     enum { Default_FundamentalEnv_ThreshDb = -60, 
00427            Default_FundamentalEnv_ThreshHz = 8000 };
00428 
00446     void buildFundamentalEnv( double fmin, double fmax, 
00447                               double threshDb = Default_FundamentalEnv_ThreshDb, 
00448                               double threshHz = Default_FundamentalEnv_ThreshHz );                                     
00449 
00450 
00457     const LinearEnvelope & fundamentalEnv( void ) const;
00458 
00463     const LinearEnvelope & ampEnv( void ) const;
00464     
00465     
00466 //  -- legacy support --
00467     
00468     //  Fundamental and amplitude envelopes are always constructed during
00469     //  analysis, these members do nothing, and are retained for backwards
00470     //  compatibility.
00471     void buildAmpEnv( bool TF = true ) { TF = TF; }
00472     void buildFundamentalEnv( bool TF = true ) { TF = TF; }
00473 
00474 //  -- private member variables --
00475 
00476 private:
00477 
00478     double m_freqResolution;    
00479 
00480 
00481     
00482     double m_ampFloor;          
00483 
00484     
00485     double m_windowWidth;       
00486 
00487 
00488 
00489     
00490     double m_freqFloor;         
00491 
00492     
00493     double m_freqDrift;         
00494 
00495 
00496     
00497     double m_hopTime;           
00498 
00499     
00500     double m_cropTime;          
00501 
00502 
00503     
00504     double m_bwRegionWidth;     
00505 
00506 
00507 
00508 
00509 
00510                                                         
00511     double m_sidelobeLevel;     
00512 
00513                                 
00514     bool m_phaseCorrect;        
00515 
00516                             
00517     PartialList m_partials;     
00518         
00521     std::auto_ptr< LinearEnvelopeBuilder > m_f0Builder;
00522 
00525     std::auto_ptr< LinearEnvelopeBuilder > m_ampEnvBuilder;
00526 
00527 //  -- private auxiliary functions --
00528 //  future development
00529 /*
00530 
00531     //  These members make up the sequence of operations in an
00532     //  analysis. If analysis were ever to be made into a 
00533     //  template method, these would be the operations that
00534     //  derived classes could override. Or each of these could
00535     //  be represented by a strategy class.
00536 
00538     void computeSpectrum( void );
00539     
00542     void selectPeaks( void );
00543     
00546     void associateBandwidth( void );
00547     
00552     void formPartials( Peaks & peaks );
00553 */
00554     //  Reject peaks that are too close in frequency to a louder peak that is
00555     //  being retained, and peaks that are too quiet. Peaks that are retained,
00556     //  but are quiet enough to be in the specified fadeRange should be faded.
00557     //  
00558     //  Rejected peaks are placed at the end of the peak collection.
00559     //  Return the first position in the collection containing a rejected peak,
00560     //  or the end of the collection if no peaks are rejected.
00561     Peaks::iterator thinPeaks( Peaks & peaks, double frameTime  );
00562                 
00563     //  Fix the bandwidth value stored in the specified Peaks. 
00564     //  This function is invoked if the spectral residue method is
00565     //  not used to compute bandwidth (that method overwrites the
00566     //  bandwidth already). If the convergence method is used to 
00567     //  compute bandwidth, the appropriate scaling is applied
00568     //  to the stored mixed phase derivative. Otherwise, the
00569     //  Peak bandwidth is set to zero.
00570     void fixBandwidth( Peaks & peaks );
00571                     
00572 };  //  end of class Analyzer
00573 
00574 }   //  end of namespace Loris
00575 
00576 #endif /* ndef INCLUDE_ANALYZER_H */

Generated on Sat Jan 19 19:02:50 2008 for Loris by  doxygen 1.5.2