Partial.h

00001 #ifndef INCLUDE_PARTIAL_H
00002 #define INCLUDE_PARTIAL_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  * Partial.h
00026  *
00027  * Definition of class Loris::Partial, and definitions and implementations of
00028  * classes of const and non-const iterators over Partials, and the exception
00029  * class InvalidPartial, thrown by some Partial members when invoked on a 
00030  * degenerate Partial having no Breakpoints.
00031  *
00032  * Kelly Fitz, 16 Aug 1999
00033  * loris@cerlsoundgroup.org
00034  *
00035  * http://www.cerlsoundgroup.org/Loris/
00036  *
00037  */
00038 
00039 #include "Breakpoint.h"
00040 #include "LorisExceptions.h"
00041 
00042 #include <map>
00043 //#include <utility>
00044 //#include <vector>
00045 
00046 //  begin namespace
00047 namespace Loris {
00048 
00049 class Partial_Iterator;
00050 class Partial_ConstIterator;
00051 
00052 // ---------------------------------------------------------------------------
00053 //  class Partial
00054 //
00092 //
00093 class Partial
00094 {   
00095 //  -- public interface --
00096 public:
00097 
00098 //  -- types --
00099 
00102     typedef std::map< double, Breakpoint > container_type;
00103     
00104     //  typedef std::vector< std::pair< double, Breakpoint > > container_type;
00105     //  see Partial.C for a discussion of issues surrounding the 
00106     //  choice of std::map as a Breakpoint container.
00107 
00109     typedef int label_type; 
00110     
00112     typedef Partial_Iterator iterator;  
00113     
00115     typedef Partial_ConstIterator const_iterator;
00116     
00118     typedef container_type::size_type size_type;
00119 
00120 //  -- construction --
00121 
00123     Partial( void );
00124      
00132     Partial( const_iterator beg, const_iterator end );
00133      
00139     Partial( const Partial & other );
00140      
00142     ~Partial( void );
00143      
00144 //  -- assignment --
00145 
00151     Partial & operator=( const Partial & other );
00152 
00153 //  -- container-dependent implementation --
00154 
00158     iterator begin( void );
00159      
00163     const_iterator begin( void ) const;
00164     
00169     iterator end( void );
00170 
00175     const_iterator end( void ) const;
00176 
00186     iterator erase( iterator beg, iterator end );
00187 
00196     iterator findAfter( double time );
00197 
00206     const_iterator findAfter( double time ) const;
00207 
00217     iterator insert( double time, const Breakpoint & bp );
00218 
00222     size_type size( void ) const;
00223             
00224 //  -- access --
00225 
00230     double duration( void ) const;
00231      
00236     double endTime( void ) const;
00237      
00242     Breakpoint & first( void );
00243 
00248     const Breakpoint & first( void ) const;
00249      
00254     double initialPhase( void ) const;
00255          
00257     label_type label( void ) const;
00258 
00263     Breakpoint & last( void );
00264     
00269     const Breakpoint & last( void ) const;
00270      
00272     size_type numBreakpoints( void ) const;
00273 
00278     double startTime( void ) const;
00279      
00280 //  -- mutation --
00281 
00288     void absorb( const Partial & other );
00289 
00291     void setLabel( label_type l );
00292 
00303     iterator erase( iterator pos );
00304      
00311     iterator findNearest( double time );
00312 
00319     const_iterator findNearest( double time ) const;
00320 
00333     Partial split( iterator pos );
00334      
00335 //  -- parameter interpolation/extrapolation --
00336 
00344     static const double ShortestSafeFadeTime;   
00345 
00360     double amplitudeAt( double time, double fadeTime = ShortestSafeFadeTime ) const;
00361 
00372     double bandwidthAt( double time ) const;
00373      
00383     double frequencyAt( double time ) const;
00384      
00395     double phaseAt( double time ) const;
00396 
00414     Breakpoint parametersAt( double time, double fadeTime = ShortestSafeFadeTime ) const;
00415 
00416 //  -- implementation --
00417 private:
00418 
00419     label_type _label;
00420     container_type _breakpoints;    //  Breakpoint envelope
00421      
00422 };  //  end of class Partial
00423 
00424 // ---------------------------------------------------------------------------
00425 //  class Partial_Iterator
00426 //
00433 //
00434 class Partial_Iterator
00435 {
00436 //  -- instance variables --
00437 
00438     typedef Partial::container_type BaseContainer;
00439     typedef BaseContainer::iterator BaseIterator;
00440     BaseIterator _iter;
00441     
00442 //  -- public interface --
00443 public:
00444 //  -- bidirectional iterator interface --
00445 
00448     typedef BaseIterator::iterator_category iterator_category;
00449     
00452     typedef Breakpoint                      value_type;
00453     
00456     typedef BaseIterator::difference_type   difference_type;
00457     
00460     typedef Breakpoint *                    pointer;
00461 
00464     typedef Breakpoint &                    reference;
00465 
00466 //  construction:
00467     
00470     Partial_Iterator( void ) {}
00471     
00472     //  (allow compiler to generate copy, assignment, and destruction)
00473     
00474 //  pre-increment/decrement:
00475 
00482     Partial_Iterator& operator ++ () { ++_iter; return *this; }
00483 
00490     Partial_Iterator& operator -- () { --_iter; return *this; }
00491 
00492 //  post-increment/decrement:
00493 
00502     Partial_Iterator operator ++ ( int ) { return Partial_Iterator( _iter++ ); } 
00503 
00512     Partial_Iterator operator -- ( int ) { return Partial_Iterator( _iter-- ); } 
00513     
00514 //  dereference (for treating Partial like a 
00515 //  STL collection of Breakpoints):
00516 
00521     Breakpoint & operator * ( void ) const { return breakpoint(); }
00522 
00523 
00528     //Breakpoint & operator * ( void ) { return breakpoint(); }
00529     
00534     Breakpoint * operator -> ( void ) const  { return & breakpoint(); }
00535 
00540     //Breakpoint * operator -> ( void )  { return & breakpoint(); }
00541         
00542 //  comparison:
00543 
00550     friend bool operator == ( const Partial_Iterator & lhs, 
00551                               const Partial_Iterator & rhs )
00552         { return lhs._iter == rhs._iter; }
00553 
00560     friend bool operator != ( const Partial_Iterator & lhs, 
00561                               const Partial_Iterator & rhs )
00562         { return lhs._iter != rhs._iter; }
00563     
00564 //  -- time and Breakpoint access --
00565 
00570     Breakpoint & breakpoint( void ) const 
00571         { return _iter->second; }
00572 
00577     //Breakpoint & breakpoint( void ) 
00578     //  { return _iter->second; }
00579 
00584     double time( void ) const  
00585         { return _iter->first; }
00586 
00587 //  -- BaseIterator conversions --
00588 private:
00589     //  construction by GenericBreakpointContainer from a BaseIterator:
00590     Partial_Iterator( const BaseIterator & it ) :
00591         _iter(it) {}
00592 
00593     friend class Partial;
00594     
00595     //  befriend  Partial_ConstIterator, 
00596     //  for const construction from non-const:
00597     friend class Partial_ConstIterator; 
00598     
00599 };  //  end of class Partial_Iterator
00600 
00601 // ---------------------------------------------------------------------------
00602 //  class Partial_ConstIterator
00603 //
00610 //
00611 class Partial_ConstIterator
00612 {
00613 //  -- instance variables --
00614     typedef Partial::container_type BaseContainer;
00615     typedef BaseContainer::const_iterator BaseIterator;
00616     BaseIterator _iter;
00617     
00618 //  -- public interface --
00619 public:
00620 //  -- bidirectional iterator interface --
00621 
00624     typedef BaseIterator::iterator_category iterator_category;
00625     
00628     typedef Breakpoint                      value_type;
00629     
00632     typedef BaseIterator::difference_type   difference_type;
00633     
00636     typedef const Breakpoint *                  pointer;
00637 
00640     typedef const Breakpoint &                  reference;
00641 
00642 //  construction:
00643 
00646     Partial_ConstIterator( void ) {}
00647     
00652     Partial_ConstIterator( const Partial_Iterator & other ) :
00653         _iter( other._iter ) {}
00654     
00655     //  (allow compiler to generate copy, assignment, and destruction):
00656 
00657 //  pre-increment/decrement:
00658 
00665     Partial_ConstIterator& operator ++ () { ++_iter; return *this; }
00666 
00673     Partial_ConstIterator& operator -- () { --_iter; return *this; }
00674 
00675 //  post-increment/decrement:
00676 
00685     Partial_ConstIterator operator ++ ( int ) { return Partial_ConstIterator( _iter++ ); } 
00686 
00695     Partial_ConstIterator operator -- ( int ) { return Partial_ConstIterator( _iter-- ); } 
00696     
00697 //  dereference (for treating Partial like a 
00698 //  STL collection of Breakpoints):
00699 
00704     const Breakpoint & operator * ( void ) const { return breakpoint(); }
00705 
00710     const Breakpoint * operator -> ( void ) const { return & breakpoint(); }
00711     
00712 //  comparison:
00713 
00720     friend bool operator == ( const Partial_ConstIterator & lhs, 
00721                               const Partial_ConstIterator & rhs )
00722         { return lhs._iter == rhs._iter; }
00723 
00730     friend bool operator != ( const Partial_ConstIterator & lhs, 
00731                               const Partial_ConstIterator & rhs )
00732         { return lhs._iter != rhs._iter; }
00733     
00734 //  -- time and Breakpoint access --
00735 
00740     const Breakpoint & breakpoint( void ) const 
00741         { return _iter->second; }
00742 
00747     double time( void ) const  
00748         { return _iter->first; }
00749     
00750 //  -- BaseIterator conversions --
00751 private:
00752     //  construction by GenericBreakpointContainer from a BaseIterator:
00753     Partial_ConstIterator( BaseIterator it ) :
00754         _iter(it) {}
00755     
00756     friend class Partial;
00757 
00758 };  //  end of class Partial_ConstIterator
00759 
00760 // ---------------------------------------------------------------------------
00761 //  class InvalidPartial
00762 //
00765 //
00766 class InvalidPartial : public InvalidObject
00767 {
00768 public: 
00769 
00779     InvalidPartial( const std::string & str, const std::string & where = "" ) : 
00780         InvalidObject( std::string("Invalid Partial -- ").append( str ), where ) {}
00781         
00782 };  //  end of class InvalidPartial
00783 
00784 }   //  end of namespace Loris
00785 
00786 #endif /* ndef INCLUDE_PARTIAL_H */

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