Loris 1.5

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-2010 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 //
00059 //  
00060 class AiffFile
00061 {
00062 //  -- public interface --
00063 public:
00064 
00065 //  -- types --
00066 
00068     typedef std::vector< double > samples_type;
00069     
00071     typedef samples_type::size_type size_type;
00072     
00074     typedef std::vector< Marker > markers_type;
00075 
00076 //  -- construction --
00077 
00082     explicit AiffFile( const std::string & filename );
00083 
00104 #if !defined(NO_TEMPLATE_MEMBERS)
00105     template<typename Iter>
00106     AiffFile( Iter begin_partials, Iter end_partials, 
00107               double samplerate, double fadeTime = FadeTimeUnspecified );
00108 #else
00109     AiffFile( PartialList::const_iterator begin_partials, 
00110               PartialList::const_iterator end_partials,
00111               double samplerate, double fadeTime = FadeTimeUnspecified ); 
00112 #endif
00113 
00122     AiffFile( double samplerate, size_type numFrames = 0, 
00123               unsigned int numChannels = 1 );
00124     
00131     AiffFile( const double * buffer, size_type bufferlength, double samplerate );
00132 
00143     //
00144     AiffFile( const double * buffer_left, const double * buffer_right, 
00145               size_type bufferlength, double samplerate );
00146      
00152     AiffFile( const std::vector< double > & vec, double samplerate );
00153     
00163     //
00164     AiffFile( const std::vector< double > & vec_left,
00165               const std::vector< double > & vec_right, 
00166               double samplerate );    
00167      
00172     AiffFile( const AiffFile & other );
00173      
00179     AiffFile & operator= ( const AiffFile & rhs );
00180 
00181 //  -- access --
00182 
00185     markers_type & markers( void );
00186 
00189     const markers_type & markers( void ) const;
00190      
00193     double midiNoteNumber( void ) const;
00194 
00197     unsigned int numChannels( void ) const;
00198 
00203     size_type numFrames( void ) const;
00204 
00207     size_type sampleFrames( void ) const { return numFrames(); }
00208 
00211     double sampleRate( void ) const;
00212     
00215     samples_type & samples( void );
00216 
00219     const samples_type & samples( void ) const;
00220 
00221 //  -- mutation --
00222 
00235     void addPartial( const Loris::Partial & p, double fadeTime = FadeTimeUnspecified );
00236      
00255 #if !defined(NO_TEMPLATE_MEMBERS)
00256     template<typename Iter>
00257     void addPartials( Iter begin_partials, Iter end_partials, 
00258                       double fadeTime = FadeTimeUnspecified  );
00259 #else
00260     void addPartials( PartialList::const_iterator begin_partials, 
00261                       PartialList::const_iterator end_partials,
00262                       double fadeTime = FadeTimeUnspecified  );
00263 #endif
00264 
00269     void setMidiNoteNumber( double nn );
00270      
00271 //  -- export --
00272 
00282     void write( const std::string & filename, unsigned int bps = 16 );
00283 
00284 private:
00285 //  -- implementation --
00286     double notenum_, rate_;     // MIDI note number and sample rate
00287     unsigned int numchans_;
00288     markers_type markers_;      // AIFF Markers
00289     samples_type samples_;      // floating point samples [-1.0, 1.0]
00290 
00291 
00292 //  -- helpers --
00293 
00294     //  Construct a Synthesizer for rendering Partials and set its fadeTime.
00295     //  Modify the default synthesizer parameters with this file's sample rate
00296     //  and, if specified (not equal to FadeTimeUnspecified), the fade time.
00297     Synthesizer configureSynthesizer( double fadeTime );
00298 
00299     //  Import data from an AIFF file on disk.
00300     void readAiffData( const std::string & filename );
00301     
00302     enum { FadeTimeUnspecified = -9999999 };
00303     //  This is not pretty, but it is better (perhaps) than defining two 
00304     //  of every member having an optional fade time parameter.
00305 
00306 };  //  end of class AiffFile
00307 
00308 // -- template members --
00309 
00310 // ---------------------------------------------------------------------------
00311 //  constructor from Partial range
00312 // ---------------------------------------------------------------------------
00333 //
00334 #if !defined(NO_TEMPLATE_MEMBERS)
00335 template< typename Iter >
00336  AiffFile::AiffFile( Iter begin_partials, Iter end_partials, 
00337                      double samplerate, double fadeTime ) :
00338 #else
00339  AiffFile::AiffFile( PartialList::const_iterator begin_partials, 
00340                      PartialList::const_iterator end_partials,
00341                      double samplerate, double fadeTime ) :
00342 #endif
00343 //  initializers:
00344     notenum_( 60 ),
00345     rate_( samplerate ),
00346     numchans_( 1 )
00347 {
00348     addPartials( begin_partials, end_partials, fadeTime );
00349 }
00350 
00351 // ---------------------------------------------------------------------------
00352 //  addPartials 
00353 // ---------------------------------------------------------------------------
00372 //
00373 #if !defined(NO_TEMPLATE_MEMBERS)
00374 template< typename Iter >
00375 void 
00376  AiffFile::addPartials( Iter begin_partials, Iter end_partials, double fadeTime )
00377 #else
00378 void 
00379  AiffFile::addPartials( PartialList::const_iterator begin_partials, 
00380                         PartialList::const_iterator end_partials,
00381                         double fadeTime )
00382 #endif
00383 { 
00384     Synthesizer synth = configureSynthesizer( fadeTime );    
00385     synth.synthesize( begin_partials, end_partials );
00386 } 
00387 
00388 }   //  end of namespace Loris
 All Classes Functions Variables Typedefs Enumerator Friends