AiffFile.h

00001 /*
00002  * This is the Loris C++ Class Library, implementing analysis, 
00003  * manipulation, and synthesis of digitized sounds using the Reassigned 
00004  * Bandwidth-Enhanced Additive Sound Model.
00005  *
00006  * Loris is Copyright (c) 1999-2007 by Kelly Fitz and Lippold Haken
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY, without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  *
00023  * AiffFile.h
00024  *
00025  * Definition of AiffFile class for sample import and export in Loris.
00026  *
00027  * Kelly Fitz, 8 Jan 2003 
00028  * loris@cerlsoundgroup.org
00029  *
00030  * http://www.cerlsoundgroup.org/Loris/
00031  *
00032  */
00033 #include "Marker.h"
00034 #include "Synthesizer.h"
00035 
00036 #if defined(NO_TEMPLATE_MEMBERS)
00037 #include "PartialList.h"
00038 #endif
00039 
00040 #include <memory>
00041 #include <string>
00042 #include <vector>
00043 
00044 //  begin namespace
00045 namespace Loris {
00046 
00047 class Partial;
00048 
00049 // ---------------------------------------------------------------------------
00050 //  class AiffFile
00051 //
00057 //  
00058 class AiffFile
00059 {
00060 //  -- public interface --
00061 public:
00062 
00063 //  -- types --
00064 
00066     typedef std::vector< double > samples_type;
00067     
00069     typedef samples_type::size_type size_type;
00070     
00072     typedef std::vector< Marker > markers_type;
00073 
00074 //  -- construction --
00075 
00080     explicit AiffFile( const std::string & filename );
00081 
00099 #if !defined(NO_TEMPLATE_MEMBERS)
00100     template<typename Iter>
00101     AiffFile( Iter begin_partials, Iter end_partials, 
00102               double samplerate, double fadeTime = .001 ); // default fade is 1ms
00103 #else
00104     AiffFile( PartialList::const_iterator begin_partials, 
00105               PartialList::const_iterator end_partials,
00106               double samplerate, double fadeTime = .001 ); // default fade is 1ms
00107 #endif
00108 
00117     AiffFile( double samplerate, size_type numFrames = 0, 
00118               unsigned int numChannels = 1 );
00119     
00126     AiffFile( const double * buffer, size_type bufferlength, double samplerate );
00127 
00138     //
00139     AiffFile( const double * buffer_left, const double * buffer_right, 
00140               size_type bufferlength, double samplerate );
00141      
00147     AiffFile( const std::vector< double > & vec, double samplerate );
00148     
00158     //
00159     AiffFile( const std::vector< double > & vec_left,
00160               const std::vector< double > & vec_right, 
00161               double samplerate );    
00162      
00167     AiffFile( const AiffFile & other );
00168      
00174     AiffFile & operator= ( const AiffFile & rhs );
00175 
00176 //  -- access --
00177 
00180     markers_type & markers( void );
00181 
00184     const markers_type & markers( void ) const;
00185      
00188     double midiNoteNumber( void ) const;
00189 
00192     unsigned int numChannels( void ) const;
00193 
00198     size_type numFrames( void ) const;
00199 
00202     size_type sampleFrames( void ) const { return numFrames(); }
00203 
00206     double sampleRate( void ) const;
00207     
00210     samples_type & samples( void );
00211 
00214     const samples_type & samples( void ) const;
00215 
00216 //  -- mutation --
00217 
00226     void addPartial( const Loris::Partial & p, double fadeTime = .001 /* 1 ms */ );
00227      
00243 #if !defined(NO_TEMPLATE_MEMBERS)
00244     template<typename Iter>
00245     void addPartials( Iter begin_partials, Iter end_partials, double fadeTime = .001 /* 1 ms */  );
00246 #else
00247     void addPartials( PartialList::const_iterator begin_partials, 
00248                       PartialList::const_iterator end_partials,
00249                       double fadeTime = .001 /* 1 ms */  );
00250 #endif
00251 
00256     void setMidiNoteNumber( double nn );
00257      
00258 //  -- export --
00259 
00269     void write( const std::string & filename, unsigned int bps = 16 );
00270 
00271 private:
00272 //  -- implementation --
00273     double notenum_, rate_;     // MIDI note number and sample rate
00274     unsigned int numchans_;
00275     markers_type markers_;      // AIFF Markers
00276     samples_type samples_;      // floating point samples [-1.0, 1.0]
00277 
00278     std::auto_ptr< Synthesizer > psynth_;   //  Synthesizer for rendering Partials,
00279                                             //  lazy-initialized (not until needed)
00280 
00281 //  -- helpers --
00282     void configureSynthesizer( double fadeTime );
00283     void readAiffData( const std::string & filename );
00284 
00285 };  //  end of class AiffFile
00286 
00287 // -- template members --
00288 
00289 // ---------------------------------------------------------------------------
00290 //  constructor from Partial range
00291 // ---------------------------------------------------------------------------
00309 //
00310 #if !defined(NO_TEMPLATE_MEMBERS)
00311 template< typename Iter >
00312  AiffFile::AiffFile( Iter begin_partials, Iter end_partials, 
00313                      double samplerate, double fadeTime ) : // default fade is 1ms
00314 #else
00315  AiffFile::AiffFile( PartialList::const_iterator begin_partials, 
00316                      PartialList::const_iterator end_partials,
00317                      double samplerate, double fadeTime ) : // default fade is 1ms
00318 #endif
00319 //  initializers:
00320     notenum_( 60 ),
00321     rate_( samplerate ),
00322     numchans_( 1 )
00323 {
00324     addPartials( begin_partials, end_partials, fadeTime );
00325 }
00326 
00327 // ---------------------------------------------------------------------------
00328 //  addPartials 
00329 // ---------------------------------------------------------------------------
00345 //
00346 #if !defined(NO_TEMPLATE_MEMBERS)
00347 template< typename Iter >
00348 void 
00349  AiffFile::addPartials( Iter begin_partials, Iter end_partials, double fadeTime /*= .001  1 ms */  )
00350 #else
00351 void 
00352  AiffFile::addPartials( PartialList::const_iterator begin_partials, 
00353                         PartialList::const_iterator end_partials,
00354                         double fadeTime /* = .001 1 ms */  )
00355 #endif
00356 { 
00357     configureSynthesizer( fadeTime );
00358     psynth_->synthesize( begin_partials, end_partials );
00359 } 
00360 
00361 }   //  end of namespace Loris

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