Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

/root/src/subsynth/syn/Core/SampleBuffer.h

Go to the documentation of this file.
00001 
00002 /****************** <SYN heading BEGIN do not edit this line> *****************
00003  *
00004  * subsynth - modular audio synthesizer
00005  * subsynth is (C) Copyright 2001-2002 by Kevin Meinert
00006  *
00007  * Original Author: Kevin Meinert
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Library General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Library General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Library General Public
00020  * License along with this library; if not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00022  * Boston, MA 02111-1307, USA.
00023  *
00024  * -----------------------------------------------------------------
00025  * File:          $RCSfile: SampleBuffer.h,v $
00026  * Date modified: $Date: 2002/04/15 01:55:54 $
00027  * Version:       $Revision: 1.15 $
00028  * -----------------------------------------------------------------
00029  *
00030  ****************** <SYN heading END do not edit this line> ******************/
00031 #ifndef SUBSYNTH_SAMPLE_BUFFER
00032 #define SUBSYNTH_SAMPLE_BUFFER
00033 
00034 #include <vector>
00035 #include <string>
00036 
00037 #include "syn/Util/Math.h"
00038 #include "syn/Util/AudioFormat.h"
00039 
00040 namespace syn
00041 {
00045    template <typename DATA_TYPE = float, int CHAN = 1>
00046    class SampleBuffer
00047    {
00048    public:
00049          typedef DATA_TYPE DataType;
00050 
00051          SampleBuffer() : mSize( 0 )
00052          {  
00053          }
00054          
00056          unsigned int size() const
00057          {
00058             return mSize;
00059          }         
00060 
00062          void resize( unsigned int x )
00063          {
00064             if (x > mBuffer.size())
00065                this->recapacity( x );
00066             mSize = x;
00067          }  
00068          
00070          unsigned int bytes() const
00071          {
00072             return mSize * (unsigned int)syn::audio_data_traits<DATA_TYPE>::bytes();
00073          }
00074 
00076          unsigned int capacity() const
00077          {
00078             return mBuffer.size();
00079          }         
00080 
00082          void recapacity( unsigned int x )
00083          {
00084             if (x != mBuffer.size())
00085                mBuffer.resize( x * CHAN );
00086          }         
00087 
00089          DATA_TYPE* data()
00090          {
00091             return (DATA_TYPE*)&mBuffer[0];
00092          }
00093          
00095          DATA_TYPE& operator[]( unsigned int x )
00096          {
00097             return mBuffer[x];
00098          }
00099          
00101          const DATA_TYPE& operator[]( unsigned int x ) const 
00102          {
00103             return mBuffer[x];
00104          }   
00105          
00112          DATA_TYPE* frame( unsigned int x )
00113          {
00114             return &mBuffer[x * CHAN];
00115          }
00116          
00118          const DATA_TYPE* frame( unsigned int x ) const
00119          {
00120             return &mBuffer[x * CHAN];
00121          }
00122 
00123          class iterator
00124          {
00125          public:
00126             iterator( std::vector<DATA_TYPE>* ptr = NULL, unsigned int index = 0)
00127             {
00128                mBufferPtr = ptr;
00129                mIndex = index;
00130             }         
00131             inline void operator++()
00132             {
00133                ++mIndex;
00134             }
00135             inline DATA_TYPE& operator*()
00136             {
00137                return (*mBufferPtr)[mIndex];
00138             }    
00139             bool operator==( const iterator& rhs )
00140             {
00141                return bool( this->mBufferPtr == rhs.mBufferPtr && 
00142                      this->mIndex == rhs.mIndex );
00143             }
00144             bool operator!=( const iterator& rhs )
00145             {
00146                return bool( !this->operator==( rhs ) );
00147             }
00148          protected:
00149             friend class SampleBuffer<DATA_TYPE, CHAN>;
00150             std::vector<DATA_TYPE>* mBufferPtr;
00151             unsigned int mIndex;
00152          };         
00153          
00155          iterator begin()
00156          {
00157             return iterator( &mBuffer, 0 );
00158          }
00160          iterator end()
00161          {
00162             return iterator( &mBuffer, mBuffer.size() );
00163          }
00164          
00165          class frame_iterator
00166          {
00167          public:
00168             frame_iterator( std::vector<DATA_TYPE>* ptr = NULL, unsigned int index = 0)
00169             {
00170                mBufferPtr = ptr;
00171                mIndex = index;
00172             }         
00173             inline void operator++()
00174             {
00175                ++mIndex;
00176             }
00177             inline DATA_TYPE* operator*()
00178             {
00179                return &(*mBufferPtr)[mIndex * CHAN];
00180             }
00181             bool operator==( const frame_iterator& rhs )
00182             {
00183                return bool( this->mBufferPtr == rhs.mBufferPtr && 
00184                      this->mIndex == rhs.mIndex );
00185             }
00186             bool operator!=( const frame_iterator& rhs )
00187             {
00188                return bool( !this->operator==( rhs ) );
00189             }
00190          protected:
00191             friend class SampleBuffer<DATA_TYPE, CHAN>;
00192             std::vector<DATA_TYPE>* mBufferPtr;
00193             unsigned int mIndex;
00194          };         
00195          
00197          frame_iterator fbegin()
00198          {
00199             return frame_iterator( &mBuffer, 0 );
00200          }
00202          frame_iterator fend()
00203          {
00204             return frame_iterator( &mBuffer, mSize/2 );
00205          }
00206          
00207          
00209          void fill( iterator pos, iterator f, iterator l )
00210          {
00211             unsigned int size = (l.mIndex - f.mIndex);
00212             assert( pos.mBufferPtr == &mBuffer && "not my iterator" );
00213             assert( f.mIndex <= l.mIndex && (size + pos.mIndex) < mSize && "out of bounds" );
00214             memcpy( &(*pos), &(*f), sizeof( DATA_TYPE ) * size );
00215          }
00216          
00218          void fill( iterator pos, DATA_TYPE* d, unsigned int size )
00219          {
00220             assert( pos.mBufferPtr == &mBuffer && "not my iterator" );
00221             assert( (size + pos.mIndex) <= mSize && "out of bounds" );
00222             memcpy( &(*pos), d, sizeof( DATA_TYPE ) * size );
00223          }
00224                 
00225    private:
00226       std::vector<DATA_TYPE> mBuffer;
00227       unsigned int mSize;
00228    };
00229   
00230 
00233    typedef SampleBuffer<float, 1> SampleBuffer1f;
00234    typedef SampleBuffer<float, 2> SampleBuffer2f;
00235    typedef SampleBuffer<signed short, 1> SampleBuffer1ss;
00236    typedef SampleBuffer<signed short, 2> SampleBuffer2ss;
00237    typedef SampleBuffer<unsigned short, 1> SampleBuffer1us;
00238    typedef SampleBuffer<unsigned short, 2> SampleBuffer2us;
00240 
00243     
00247    template <typename DATA_TYPE, unsigned CHAN>
00248    inline SampleBuffer<DATA_TYPE, CHAN>& clamp( SampleBuffer<DATA_TYPE, CHAN>& buf )
00249    {
00250       SampleBuffer<DATA_TYPE, CHAN>::iterator it;
00251       for (it = buf.begin(); it != buf.end(); ++it)
00252       {
00253          (*it) = audio_clamp<DATA_TYPE>( (*it) );
00254       }
00255       return buf;
00256    }
00257    
00258    inline SampleBuffer1f& operator+=( SampleBuffer1f& lhs, const SampleBuffer1f& rhs )
00259    {
00260       assert( lhs.size() == rhs.size() && "+= between different sizes undefined..." );
00261       for (unsigned int x = 0; x < rhs.size(); ++x)
00262          lhs.frame(x)[0] += rhs.frame(x)[0];
00263       return lhs;
00264    }
00265 
00266    inline SampleBuffer1f& operator*=( SampleBuffer1f& lhs, const SampleBuffer1f& rhs )
00267    {
00268       assert( lhs.size() == rhs.size() && "*= between different sizes undefined..." );
00269       for (unsigned int x = 0; x < rhs.size(); ++x)
00270          lhs.frame(x)[0] *= rhs.frame(x)[0];
00271       return lhs;
00272    }
00274    
00275 } // end syn namespace...
00276 
00277 #endif

Generated at Mon Apr 15 09:26:06 2002 for subsynth by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001