Loris 1.5

Synthesizer.h

00001 #ifndef INCLUDE_SYNTHESIZER_H
00002 #define INCLUDE_SYNTHESIZER_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  * Synthesizer.h
00026  *
00027  * Definition of class Loris::Synthesizer, a renderer of 
00028  * bandwidth-enhanced Partials.
00029  *
00030  * Kelly Fitz, 16 Aug 1999
00031  * loris@cerlsoundgroup.org
00032  *
00033  * http://www.cerlsoundgroup.org/Loris/
00034  *
00035  */
00036  
00037  
00038  // TODO:
00039  // Passing the sample vector in to Synthesizer no longer makes any sense,
00040  // if the Synthesizer can (must) resize the buffer anyway, it may as well 
00041  // own it. In a future version, the Synthesizer owns a vector (need to add
00042  // a method to clear it), and provides access to it (as it currently does). 
00043  // When this change is made, it probably makes sense to remove the function
00044  // call operators.
00045  
00046 
00047 #include "Oscillator.h"
00048 #include "PartialList.h"
00049 #include "PartialUtils.h"
00050 
00051 #include <vector>
00052 
00053 //  begin namespace
00054 namespace Loris {
00055 
00056 // ---------------------------------------------------------------------------
00057 //  class Synthesizer
00058 //
00069 //
00070 class Synthesizer
00071 {
00072 //  -- public interface --
00073 public:
00074 //  -- construction --
00075 
00076     struct Parameters;  //  defined below
00077 
00090     Synthesizer( std::vector<double> & buffer );    
00091 
00092 
00105     Synthesizer( Parameters params, std::vector<double> & buffer ); 
00106     
00120     Synthesizer( double srate, std::vector<double> & buffer );
00121                  
00137     Synthesizer( double srate, std::vector<double> & buffer, double fadeTime );
00138     
00139     //  Compiler can generate copy, assign, and destroy.
00140     //  Synthesizer( const Synthesizer & other );
00141     //  ~Synthesizer( void );
00142     //  Synthesizer & operator= ( const Synthesizer & other );
00143      
00144 //  -- synthesis --
00145 
00162     void synthesize( Partial p );   
00163      
00165     void operator() ( const Partial & p ) { synthesize( p ) ; }
00166 
00167      
00187 #if ! defined(NO_TEMPLATE_MEMBERS)
00188     template< typename Iter >
00189     void synthesize( Iter begin_partials, Iter end_partials );
00190 #else
00191     inline
00192     void synthesize( PartialList::iterator begin_partials, 
00193                      PartialList::iterator end_partials );  
00194 #endif
00195      
00198 #if ! defined(NO_TEMPLATE_MEMBERS)
00199     template< typename Iter >
00200     void operator() ( Iter begin_partials, Iter end_partials );
00201 #else
00202     inline
00203     void operator() ( PartialList::iterator begin_partials, 
00204                       PartialList::iterator end_partials );
00205 #endif
00206     
00207 //  -- sample access --
00208 
00211     const std::vector<double> & samples( void ) const;
00212 
00215     std::vector<double> & samples( void );
00216     
00217     
00218 //  -- parameter access and mutation --
00219 
00220 
00222     double fadeTime( void ) const;
00223      
00225     double sampleRate( void ) const;
00226 
00232     void setFadeTime( double partialFadeTime );
00233 
00239     void setSampleRate( double rate );
00240 
00245     Filter & filter( void );
00246     
00247 
00248 //  -- parameters structure --
00249 
00250     enum
00251     {
00252         Default_FadeTime_Ms = 1,
00253         Default_SampleRate_Hz = 44100
00254     };
00255     
00256     // enum EnhancementFlag { Sinusoidal = 0,  BwEnhanced = 1 };
00257 
00263     struct Parameters
00264     {
00265         double fadeTime;
00266         double sampleRate;
00267         // EnhancementFlag enhancement;
00268         
00269         Filter filter;
00270         
00271         //  default constructor
00272         //
00275         Parameters( void );
00276         
00277         //  copy, assign, and destroy are free
00278     };      
00279     
00283     static const Parameters & DefaultParameters( void );
00284 
00292     static void SetDefaultParameters( const Parameters & params );
00293     
00301     static bool IsValidParameters( const Parameters & params );
00302     
00303 
00304 //  -- implementation --
00305 private:
00306 
00307     Oscillator m_osc;   //  the Synthesizer has-a Oscillator that it uses to render
00308                         //  all the Partials one by one. 
00309     
00310     std::vector< double > * m_sampleBuffer; //  samples are computed and stored here, BUT
00311                                             //  Synthesizer does NOT own this buffer.
00312     
00313     double m_fadeTimeSec;                   //  Partial fade in/out time in seconds
00314     double m_srateHz;                       //  sample rate in Hz
00315         
00316 };  //  end of class Synthesizer
00317 
00318 
00319 // ---------------------------------------------------------------------------
00320 //  synthesize 
00321 // ---------------------------------------------------------------------------
00340 //
00341 #if ! defined(NO_TEMPLATE_MEMBERS)
00342 template<typename Iter>
00343 void 
00344 Synthesizer::synthesize( Iter begin_partials, Iter end_partials ) 
00345 #else
00346 inline void 
00347 Synthesizer::synthesize( PartialList::iterator begin_partials, 
00348                          PartialList::iterator end_partials ) 
00349 #endif
00350 { 
00351     //  grow the sample buffer, if necessary, to accommodate the latest
00352     //  Partial, with the fade time tacked on the end
00353     double duration = 
00354         PartialUtils::timeSpan( begin_partials, end_partials ).second + 
00355         m_fadeTimeSec;
00356     
00357     typedef std::vector< double >::size_type Sz_Type;
00358     Sz_Type Nsamps = 1 + Sz_Type( duration * m_srateHz );    
00359     if ( m_sampleBuffer->size() < Nsamps )
00360     {
00361         m_sampleBuffer->resize( Nsamps );
00362     }
00363     
00364     while ( begin_partials != end_partials ) 
00365     {
00366         synthesize( *(begin_partials++) ); 
00367     }
00368 }
00369 
00370 // ---------------------------------------------------------------------------
00371 //  operator() 
00372 // ---------------------------------------------------------------------------
00375 //
00376 #if ! defined(NO_TEMPLATE_MEMBERS)
00377 template<typename Iter>
00378 void
00379 Synthesizer::operator() ( Iter begin_partials, Iter end_partials ) 
00380 #else
00381 inline void
00382 Synthesizer::operator() ( PartialList::iterator begin_partials, 
00383                           PartialList::iterator end_partials ) 
00384 #endif
00385 { 
00386     synthesize( begin_partials, end_partials ); 
00387 }
00388 
00389 }   //  end of namespace Loris
00390 
00391 #endif /* ndef INCLUDE_SYNTHESIZER_H */
 All Classes Functions Variables Typedefs Enumerator Friends