Loris 1.5

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-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  * 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 // class Peaks;
00048 // class Peaks::iterator;
00049 //  oooo, this is nasty, need to fix it!
00050 class SpectralPeak;
00051 typedef std::vector< SpectralPeak > Peaks;
00052 
00053 // ---------------------------------------------------------------------------
00054 //  class Analyzer
00055 //
00103 //
00104 class Analyzer
00105 {
00106 //  -- public interface --
00107 public:
00108 
00109 //  -- construction --
00110 
00117     explicit Analyzer( double resolutionHz );
00118     
00128     Analyzer( double resolutionHz, double windowWidthHz );
00129 
00139     Analyzer( const Envelope & resolutionEnv, double windowWidthHz );
00140 
00146     Analyzer( const Analyzer & other );
00147 
00149     ~Analyzer( void );
00150 
00156     Analyzer & operator=( const Analyzer & rhs );
00157 
00158 //  -- configuration --
00159 
00167     void configure( double resolutionHz );
00168 
00185     void configure( double resolutionHz, double windowWidthHz );
00186 
00204     //
00205     void configure( const Envelope & resolutionEnv, double windowWidthHz );    
00206     
00207 //  -- analysis --
00208 
00215     void analyze( const std::vector<double> & vec, double srate );
00216     
00225     void analyze( const double * bufBegin, const double * bufEnd, double srate );
00226     
00227 //  -- tracking analysis --
00228 
00238     void analyze( const std::vector<double> & vec, double srate, 
00239                   const Envelope & reference );
00240     
00252     void analyze( const double * bufBegin, const double * bufEnd, double srate,
00253                   const Envelope & reference );
00254     
00255 //  -- parameter access --
00256 
00259     double ampFloor( void ) const;
00260 
00265     double cropTime( void ) const;
00266 
00269     double freqDrift( void ) const;
00270 
00273     double freqFloor( void ) const;
00274     
00282     double freqResolution( double time = 0.0 ) const; 
00283 
00287     double hopTime( void ) const;
00288 
00295     double sidelobeLevel( void ) const;
00296 
00299     double windowWidth( void ) const;
00300      
00304     bool phaseCorrect( void ) const;
00305 
00306 
00307 //  -- parameter mutation --
00308 
00313     void setAmpFloor( double x );
00314 
00321     void setCropTime( double x );
00322 
00327     void setFreqDrift( double x );
00328 
00333     void setFreqFloor( double x );
00334 
00340     void setFreqResolution( double x );
00341     
00347     void setFreqResolution( const Envelope & e );    
00348 
00353     void setHopTime( double x );
00354 
00363     void setSidelobeLevel( double x );
00364 
00369     void setWindowWidth( double x );
00370 
00377     void setPhaseCorrect( bool TF = true );
00378     
00379     
00380 //  -- bandwidth envelope specification --
00381 
00382     enum { Default_ResidueBandwidth_RegionWidth = 2000,
00383            Default_ConvergenceBandwidth_TolerancePct = 10 };
00384            
00395     void storeResidueBandwidth( double regionWidth = Default_ResidueBandwidth_RegionWidth );
00396     
00409     void storeConvergenceBandwidth( double tolerancePct = 
00410             0.01 * (double)Default_ConvergenceBandwidth_TolerancePct );
00411     
00414     void storeNoBandwidth( void );
00415     
00419     bool bandwidthIsResidue( void ) const;
00420     
00424     bool bandwidthIsConvergence( void ) const;
00425     
00430     double bwRegionWidth( void ) const;
00431 
00436     double bwConvergenceTolerance( void ) const;
00437 
00442     bool associateBandwidth( void ) const
00443         { return bandwidthIsResidue() || bandwidthIsConvergence(); }
00444 
00446     void setBwRegionWidth( double x ) 
00447         { 
00448             if ( x != 0 )
00449             {
00450                 storeResidueBandwidth( x ); 
00451             }
00452             else
00453             {
00454                 storeNoBandwidth();
00455             }
00456         }
00457            
00458 
00459 //  -- PartialList access --
00460 
00463     PartialList & partials( void );
00464 
00467     const PartialList & partials( void ) const;
00468 
00469 //  -- envelope access --
00470 
00471     enum { Default_FundamentalEnv_ThreshDb = -60, 
00472            Default_FundamentalEnv_ThreshHz = 8000 };
00473 
00491     void buildFundamentalEnv( double fmin, double fmax, 
00492                               double threshDb = Default_FundamentalEnv_ThreshDb, 
00493                               double threshHz = Default_FundamentalEnv_ThreshHz );                                     
00494 
00495 
00501     const LinearEnvelope & fundamentalEnv( void ) const;
00502 
00505     const LinearEnvelope & ampEnv( void ) const;
00506     
00507     
00508 //  -- legacy support --
00509     
00510     //  Fundamental and amplitude envelopes are always constructed during
00511     //  analysis, these members do nothing, and are retained for backwards
00512     //  compatibility.
00513     void buildAmpEnv( bool TF = true ) { TF = TF; }
00514     void buildFundamentalEnv( bool TF = true ) { TF = TF; }
00515 
00516 //  -- private member variables --
00517 
00518 private:
00519 
00520     std::auto_ptr< Envelope > m_freqResolutionEnv;    
00524     
00525     double m_ampFloor;          
00526 
00527     
00528     double m_windowWidth;       
00529 
00530 
00531 
00532     
00533     // std::auto_ptr< Envelope > m_freqFloorEnv; 
00534     double m_freqFloor;         
00535 
00536     
00537     double m_freqDrift;         
00538 
00539 
00540     
00541     double m_hopTime;           
00542 
00543     
00544     double m_cropTime;          
00545 
00546 
00547     
00548     double m_bwAssocParam;      
00549 
00550 
00551 
00552 
00553 
00554                                                         
00555     double m_sidelobeLevel;     
00556 
00557                                 
00558     bool m_phaseCorrect;        
00559 
00560                             
00561     PartialList m_partials;     
00562         
00565     std::auto_ptr< LinearEnvelopeBuilder > m_f0Builder;
00566 
00569     std::auto_ptr< LinearEnvelopeBuilder > m_ampEnvBuilder;
00570 
00571 //  -- private auxiliary functions --
00572 //  future development
00573 /*
00574 
00575     //  These members make up the sequence of operations in an
00576     //  analysis. If analysis were ever to be made into a 
00577     //  template method, these would be the operations that
00578     //  derived classes could override. Or each of these could
00579     //  be represented by a strategy class.
00580 
00582     void computeSpectrum( void );
00583     
00586     void selectPeaks( void );
00587     
00590     void associateBandwidth( void );
00591     
00596     void formPartials( Peaks & peaks );
00597 */
00598     //  Reject peaks that are too close in frequency to a louder peak that is
00599     //  being retained, and peaks that are too quiet. Peaks that are retained,
00600     //  but are quiet enough to be in the specified fadeRange should be faded.
00601     //  
00602     //  Rejected peaks are placed at the end of the peak collection.
00603     //  Return the first position in the collection containing a rejected peak,
00604     //  or the end of the collection if no peaks are rejected.
00605     Peaks::iterator thinPeaks( Peaks & peaks, double frameTime  );
00606                 
00607     //  Fix the bandwidth value stored in the specified Peaks. 
00608     //  This function is invoked if the spectral residue method is
00609     //  not used to compute bandwidth (that method overwrites the
00610     //  bandwidth already). If the convergence method is used to 
00611     //  compute bandwidth, the appropriate scaling is applied
00612     //  to the stored mixed phase derivative. Otherwise, the
00613     //  Peak bandwidth is set to zero.
00614     void fixBandwidth( Peaks & peaks );
00615                     
00616 };  //  end of class Analyzer
00617 
00618 }   //  end of namespace Loris
00619 
00620 #endif /* ndef INCLUDE_ANALYZER_H */
 All Classes Functions Variables Typedefs Enumerator Friends