Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

FEAPIExamplePluginLoudness/RingBuffer.h

Go to the documentation of this file.
00001 
00002 //     /*! \file RingBuffer.h: \brief interface of the CRingBuffer class. */
00003 //
00004 //        Copyright (c) 2004-2006  
00005 //        zplane.development
00006 //        Flohrer Lerch Schwerdtfeger GbR
00007 //        All rights reserved.
00008 //
00009 //    This program is free software; you can redistribute it and/or modify
00010 //    it under the terms of the GNU General Public License as published by
00011 //    the Free Software Foundation; either version 2 of the License, or
00012 //    (at your option) any later version.
00013 //
00014 //    This program 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
00017 //    GNU General Public License for more details.
00018 //
00019 //    You should have received a copy of the GNU General Public License
00020 //    along with this program; if not, write to the Free Software
00021 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00024 
00025 #if !defined(__RINGBUFFER_HEADER_INCLUDED__)
00026 #define __RINGBUFFER_HEADER_INCLUDED__
00027 
00028 #ifdef WIN32
00029 #define INLINE __forceinline
00030 #else
00031 #define INLINE
00032 #endif
00033 
00035 static INLINE unsigned int Int2PowTwo (int iValue)
00036 {
00037     unsigned int    iOrder = 0;
00038 
00039     while (iValue>>iOrder)
00040         iOrder++;
00041 
00042     if (!(iValue%(1<<(iOrder-1))))
00043         iOrder--;
00044     
00045     return (1<<(iOrder));
00046 }
00047 
00048 //********************************************************************
00049 //  @class:             CRingBuffer <template>
00050 //  @author:            Tim Flohrer
00051 //  @notes:             implements a Ringbuffer
00052 //  @creation date:     18.7.2001
00053 //  @last modified:     
00054 //********************************************************************
00055 
00056 template <class T>  class CRingBuffer 
00057 {
00058     
00059 private:
00060     
00061     unsigned int    m_uiReadIndex,
00062             m_uiWriteIndex,
00063             m_uiTmpIndex;
00064 
00065     unsigned int    m_uiSize,
00066             m_uiMask;
00067 
00068     T       *m_ptBuffer;
00069 
00070 
00071 public:
00072     static int   CreateInstance  (CRingBuffer*& pCRingBuffer, unsigned int uiLength)
00073     {
00074         int  rErr    = 0;
00075         pCRingBuffer    = 0;
00076     
00077         // create instance
00078         pCRingBuffer    = new CRingBuffer (uiLength);
00079     
00080         if (pCRingBuffer == NULL)
00081             rErr        = -1;
00082         else if (!pCRingBuffer->m_ptBuffer)
00083         {
00084             rErr        = -1;
00085             delete pCRingBuffer;
00086             pCRingBuffer= 0;
00087         }
00088 
00089         return rErr;
00090     };
00091 
00092     static int   DestroyInstance (CRingBuffer*& pCRingBuffer)
00093     {
00094         if (!pCRingBuffer)
00095             return -1;
00096 
00097         delete pCRingBuffer;
00098         pCRingBuffer    = 0;
00099 
00100         return 0;
00101     };
00102 
00103 //********************************************************************
00104 //  @method:            CRingBuffer 
00105 //  @parameter:         unsigned int uiNewSize      desired size of the ring buffer
00106 //  @result:            
00107 //  @author:            Tim Flohrer
00108 //  @notes:             constructor of the CRingbuffer class
00109 //  @creation date:     18.7.2001
00110 //  @last modified:     
00111 //********************************************************************
00112     CRingBuffer(unsigned int uiNewSize)
00113     {
00114 //      m_uiSize            =   (unsigned int)pow(2,1+(int)(log(uiNewSize)/log(2)));
00115         m_uiSize            =   Int2PowTwo(uiNewSize);
00116         m_uiMask            =   m_uiSize-1;
00117         m_uiReadIndex       =   0;
00118         m_uiWriteIndex      =   0;
00119         m_uiTmpIndex        =   0;
00120         m_ptBuffer          =   0;
00121         m_ptBuffer          =   new T[m_uiSize];
00122         for( unsigned int i=0; i<m_uiSize; m_ptBuffer[i++] = 0);
00123     }
00124     
00125 
00126 //********************************************************************
00127 //  @method:            ~CRingBuffer
00128 //  @parameter:         
00129 //  @result:            
00130 //  @author:            Tim Flohrer
00131 //  @notes:             destructor
00132 //  @creation date:     18.7.2001
00133 //  @last modified:     
00134 //********************************************************************
00135     ~CRingBuffer()
00136     {
00137         if (m_ptBuffer)
00138             delete [] m_ptBuffer;
00139     }
00140     
00141     
00142 //********************************************************************
00143 //  @method:            GetOff
00144 //  @parameter:         int iIndex
00145 //  @result:            T
00146 //  @author:            Tim Flohrer
00147 //  @notes:             gets an item at offset iIndex from the
00148 //                      current read pointer, without modifying 
00149 //  @creation date:     18.7.2001
00150 //  @last modified:     
00151 //********************************************************************
00152 INLINE T GetOff(int iIndex)
00153     {       
00154         return m_ptBuffer[(m_uiReadIndex + iIndex + m_uiSize) & m_uiMask];
00155     }
00156 
00157 INLINE T GetOffW(int iIndex)
00158     {       
00159         return m_ptBuffer[(m_uiWriteIndex + iIndex + m_uiSize) & m_uiMask];
00160     }
00161 
00162 
00163 //********************************************************************
00164 //  @method:            PutOff
00165 //  @parameter:         T tItem
00166 //                      int iIndex
00167 //  @result:            
00168 //  @author:            Tim Flohrer
00169 //  @notes:             puts an item at offset iIndex from the
00170 //                      current write pointer, without modifying
00171 //  @creation date:     18.7.2001
00172 //  @last modified:     
00173 //********************************************************************
00174 INLINE void PutOff(T tItem,int iIndex)
00175     {
00176         m_ptBuffer[(m_uiWriteIndex + iIndex + m_uiSize) & m_uiMask] =   tItem;
00177     }
00178     
00179 //********************************************************************
00180 //  @method:            GetOffMod
00181 //  @parameter:         int iIndex
00182 //  @result:            T
00183 //  @author:            Tim Flohrer
00184 //  @notes:             gets an item at offset iIndex from the
00185 //                      current read pointer, with modifying 
00186 //  @creation date:     18.7.2001
00187 //  @last modified:     
00188 //********************************************************************
00189 INLINE T GetOffMod(int iIndex)
00190     {       
00191         m_uiReadIndex               =   ((m_uiReadIndex + iIndex + m_uiSize) & m_uiMask);
00192         return m_ptBuffer[ m_uiReadIndex ];
00193     }
00194     
00195     
00196 //********************************************************************
00197 //  @method:            PutOffMod
00198 //  @parameter:         T tItem
00199 //                      int iIndex
00200 //  @result:            
00201 //  @author:            Tim Flohrer
00202 //  @notes:             puts an item at offset iIndex from the
00203 //                      current write pointer, with modifying
00204 //  @creation date:     18.7.2001
00205 //  @last modified:     
00206 //********************************************************************
00207 INLINE void PutOffMod(T tItem,int iIndex)
00208     {
00209         m_uiWriteIndex              =   (m_uiWriteIndex + iIndex + m_uiSize) & m_uiMask;
00210         m_ptBuffer[m_uiWriteIndex]  =   tItem;
00211     }
00212     
00213 
00214 //********************************************************************
00215 //  @method:            Get
00216 //  @parameter:         
00217 //  @result:            T
00218 //  @author:            Tim Flohrer
00219 //  @notes:             gets an item at current read pointer
00220 //  @creation date:     18.7.2001
00221 //  @last modified:     
00222 //********************************************************************  
00223 INLINE T Get()
00224     {       
00225         return m_ptBuffer[ m_uiReadIndex ];
00226     }
00227     
00228 
00229 //********************************************************************
00230 //  @method:            Put
00231 //  @parameter:         T tItem
00232 //  @result:            
00233 //  @author:            Tim Flohrer
00234 //  @notes:             puts an item at current read pointer
00235 //  @creation date:     18.7.2001
00236 //  @last modified:     
00237 //********************************************************************  
00238 INLINE void Put(T tItem)
00239     {
00240         m_ptBuffer[ m_uiWriteIndex ]    =   tItem;
00241     }
00242     
00243 
00244 //********************************************************************
00245 //  @method:            GetPostInc
00246 //  @parameter:         
00247 //  @result:            T
00248 //  @author:            Tim Flohrer
00249 //  @notes:             gets an item at the current read pointer
00250 //                      and increments the read pointer afterwards
00251 //  @creation date:     18.7.2001
00252 //  @last modified:     
00253 //********************************************************************
00254 INLINE T GetPostInc()
00255     {       
00256         m_uiTmpIndex    = m_uiReadIndex;
00257         m_uiReadIndex   = (m_uiReadIndex + 1) & m_uiMask; 
00258         return m_ptBuffer[ m_uiTmpIndex ];
00259     }
00260 
00261 INLINE void GetPostInc (T* ptBuffer, int iNumOfItems)
00262     {
00263         if (iNumOfItems <= 0)
00264             return;
00265         m_uiTmpIndex    = m_uiReadIndex;
00266         m_uiReadIndex   = (m_uiReadIndex + iNumOfItems) & m_uiMask; 
00267         if (m_uiTmpIndex + iNumOfItems <= m_uiSize)
00268         {
00269             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * iNumOfItems);
00270             return;
00271         }
00272         else
00273         {
00274             ZASSERT (m_uiSize - m_uiTmpIndex <= 0);
00275             ZASSERT (iNumOfItems - m_uiSize + m_uiTmpIndex <= 0);
00276             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * (m_uiSize - m_uiTmpIndex));
00277             memcpy (&ptBuffer[m_uiSize - m_uiTmpIndex], m_ptBuffer, sizeof(T) * (iNumOfItems - m_uiSize + m_uiTmpIndex));
00278             return;
00279         }
00280     }
00281 INLINE void GetOffPostInc(T* ptBuffer, int iNumOfItems, int iIndex)
00282     {       
00283         if (iNumOfItems <= 0)
00284             return;
00285         m_uiTmpIndex    = ((m_uiReadIndex + iIndex + m_uiSize) & m_uiMask);
00286         m_uiReadIndex   = (m_uiReadIndex + iNumOfItems) & m_uiMask; 
00287         if (m_uiTmpIndex + iNumOfItems <= m_uiSize)
00288         {
00289             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * iNumOfItems);
00290             return;
00291         }
00292         else
00293         {
00294             ZASSERT (m_uiSize - m_uiTmpIndex <= 0);
00295             ZASSERT (iNumOfItems - m_uiSize + m_uiTmpIndex <= 0);
00296             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * (m_uiSize - m_uiTmpIndex));
00297             memcpy (&ptBuffer[m_uiSize - m_uiTmpIndex], m_ptBuffer, sizeof(T) * (iNumOfItems - m_uiSize + m_uiTmpIndex));
00298             return;
00299         }
00300     }
00301 INLINE void GetOff(T* ptBuffer, int iNumOfItems, int iIndex)
00302     {       
00303         if (iNumOfItems <= 0)
00304             return;
00305         m_uiTmpIndex    = ((m_uiReadIndex + iIndex + m_uiSize) & m_uiMask);
00306         if (m_uiTmpIndex + iNumOfItems <= m_uiSize)
00307         {
00308             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * iNumOfItems);
00309             return;
00310         }
00311         else
00312         {
00313             ZASSERT (m_uiSize - m_uiTmpIndex <= 0);
00314             ZASSERT (iNumOfItems - m_uiSize + m_uiTmpIndex <= 0);
00315             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * (m_uiSize - m_uiTmpIndex));
00316             memcpy (&ptBuffer[m_uiSize - m_uiTmpIndex], m_ptBuffer, sizeof(T) * (iNumOfItems - m_uiSize + m_uiTmpIndex));
00317             return;
00318         }
00319     }
00320 INLINE void GetOffW(T* ptBuffer, int iNumOfItems, int iIndex)
00321     {       
00322         if (iNumOfItems <= 0)
00323             return;
00324         m_uiTmpIndex    = ((m_uiWriteIndex + iIndex + m_uiSize) & m_uiMask);
00325         if (m_uiTmpIndex + iNumOfItems <= m_uiSize)
00326         {
00327             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * iNumOfItems);
00328             return;
00329         }
00330         else
00331         {
00332             ZASSERT (m_uiSize - m_uiTmpIndex <= 0);
00333             ZASSERT (iNumOfItems - m_uiSize + m_uiTmpIndex <= 0);
00334             memcpy (ptBuffer, &m_ptBuffer[ m_uiTmpIndex ], sizeof(T) * (m_uiSize - m_uiTmpIndex));
00335             memcpy (&ptBuffer[m_uiSize - m_uiTmpIndex], m_ptBuffer, sizeof(T) * (iNumOfItems - m_uiSize + m_uiTmpIndex));
00336             return;
00337         }
00338     }
00339     
00340 //********************************************************************
00341 //  @method:            PutPostInc
00342 //  @parameter:         T tItem
00343 //  @result:            
00344 //  @author:            Tim Flohrer
00345 //  @notes:             puts an item at the current read pointer
00346 //                      and increments the write pointer afterwards
00347 //  @creation date:     18.7.2001
00348 //  @last modified:     
00349 //********************************************************************
00350 INLINE void PutPostInc(T tItem)
00351     {
00352         m_uiTmpIndex    = m_uiWriteIndex;
00353         m_uiWriteIndex = (m_uiWriteIndex + 1) & m_uiMask;
00354         m_ptBuffer[ m_uiTmpIndex ]  =   tItem;
00355     }
00356 
00357 
00358 INLINE void PutPostInc(const T *ptItem, int iNumOfItems)
00359     {
00360         if (iNumOfItems <= 0)
00361             return;
00362         m_uiTmpIndex    = m_uiWriteIndex;
00363         m_uiWriteIndex = (m_uiWriteIndex + iNumOfItems) & m_uiMask;
00364         if (m_uiTmpIndex + iNumOfItems <= m_uiSize)
00365         {
00366             memcpy (&m_ptBuffer[ m_uiTmpIndex ], ptItem, sizeof(T) * iNumOfItems);
00367             return;
00368         }
00369         else
00370         {
00371             ZASSERT (m_uiSize - m_uiTmpIndex <= 0);
00372             ZASSERT (iNumOfItems - m_uiSize + m_uiTmpIndex <= 0);
00373             memcpy (&m_ptBuffer[ m_uiTmpIndex ], ptItem, sizeof(T) * (m_uiSize - m_uiTmpIndex));
00374             memcpy (m_ptBuffer, &ptItem[m_uiSize - m_uiTmpIndex], sizeof(T) * (iNumOfItems - m_uiSize + m_uiTmpIndex));
00375             return;
00376         }
00377 }
00378     
00379 
00380 //********************************************************************
00381 //  @method:            GetPreInc
00382 //  @parameter:         
00383 //  @result:            T
00384 //  @author:            Tim Flohrer
00385 //  @notes:             increments the read pointer and 
00386 //                      gets an item at the current read pointer
00387 //  @creation date:     18.7.2001
00388 //  @last modified:     
00389 //********************************************************************
00390 INLINE T GetPreInc()
00391     {       
00392         m_uiReadIndex = (m_uiReadIndex + 1) & m_uiMask;
00393         return m_ptBuffer[ m_uiReadIndex ];
00394     }
00395     
00396 
00397 //********************************************************************
00398 //  @method:            PutPreInc
00399 //  @parameter:         T tItem
00400 //  @result:            
00401 //  @author:            Tim Flohrer
00402 //  @notes:             increments the write pointer and 
00403 //                      puts an item at the current write pointer
00404 //  @creation date:     18.7.2001
00405 //  @last modified:     
00406 //********************************************************************
00407 INLINE void PutPreInc(T tItem)
00408     {
00409         m_uiWriteIndex = (m_uiWriteIndex + 1) & m_uiMask;
00410         m_ptBuffer[ m_uiWriteIndex ]    =   tItem;
00411     }
00412     
00413 
00414 //********************************************************************
00415 //  @method:            GetPostDec
00416 //  @parameter:         
00417 //  @result:            T
00418 //  @author:            Tim Flohrer
00419 //  @notes:             gets an item at the current read pointer
00420 //                      and Decrements the read pointer afterwards
00421 //  @creation date:     18.7.2001
00422 //  @last modified:     
00423 //********************************************************************
00424 INLINE T GetPostDec()
00425     {   
00426         m_uiTmpIndex    = m_uiReadIndex;
00427         m_uiReadIndex   = (m_uiReadIndex - 1 + m_uiSize) & m_uiMask;
00428         return m_ptBuffer[ m_uiTmpIndex ];
00429     }
00430     
00431 //********************************************************************
00432 //  @method:            PutPostDec
00433 //  @parameter:         T tItem
00434 //  @result:            
00435 //  @author:            Tim Flohrer
00436 //  @notes:             puts an item at the current read pointer
00437 //                      and Decrements the write pointer afterwards
00438 //  @creation date:     18.7.2001
00439 //  @last modified:     
00440 //********************************************************************
00441 INLINE void PutPostDec(T tItem)
00442     {
00443         m_uiTmpIndex    = m_uiWriteIndex;
00444         m_uiWriteIndex  = (m_uiWriteIndex - 1 + m_uiSize) & m_uiMask;       
00445         m_ptBuffer[ m_uiTmpIndex ]  =   tItem;
00446     }
00447     
00448 
00449 //********************************************************************
00450 //  @method:            GetPreDec
00451 //  @parameter:         
00452 //  @result:            T
00453 //  @author:            Tim Flohrer
00454 //  @notes:             Decrements the read pointer and 
00455 //                      gets an item at the current read pointer
00456 //  @creation date:     18.7.2001
00457 //  @last modified:     
00458 //********************************************************************
00459 INLINE T GetPreDec()
00460     {       
00461         m_uiReadIndex = (m_uiReadIndex - 1 + m_uiSize) & m_uiMask;
00462         return m_ptBuffer[ m_uiReadIndex ];
00463     }
00464     
00465 
00466 
00467 //********************************************************************
00468 //  @method:            PutPreDec
00469 //  @parameter:         T tItem
00470 //  @result:            
00471 //  @author:            Tim Flohrer
00472 //  @notes:             Decrements the write pointer and 
00473 //                      puts an item at the current write pointer
00474 //  @creation date:     18.7.2001
00475 //  @last modified:     
00476 //********************************************************************
00477 INLINE void PutPreDec(T tItem)
00478     {
00479         m_uiWriteIndex = (m_uiWriteIndex - 1 + m_uiSize) & m_uiMask;
00480         m_ptBuffer[ m_uiWriteIndex ]    =   tItem;
00481     }
00482     
00483 
00484 //********************************************************************
00485 //  @method:            GetReadPos
00486 //  @parameter:         
00487 //  @result:            int
00488 //  @author:            Tim Flohrer
00489 //  @notes:             returns current read pointer position
00490 //  @creation date:     18.7.2001
00491 //  @last modified:     
00492 //********************************************************************
00493 INLINE int  GetReadPos()
00494     {
00495         return m_uiReadIndex;
00496     }
00497     
00498     
00499 //********************************************************************
00500 //  @method:            GetWritePos
00501 //  @parameter:         
00502 //  @result:            int
00503 //  @author:            Tim Flohrer
00504 //  @notes:             returns current write pointer position
00505 //  @creation date:     18.7.2001
00506 //  @last modified:     
00507 //********************************************************************
00508 INLINE int  GetWritePos()
00509     {
00510         return m_uiWriteIndex;
00511     }
00512     
00513     
00514 //********************************************************************
00515 //  @method:            SetReadPos
00516 //  @parameter:         int iIndex
00517 //  @result:            
00518 //  @author:            Tim Flohrer
00519 //  @notes:             sets the read pointer
00520 //  @creation date:     18.7.2001
00521 //  @last modified:     
00522 //********************************************************************
00523 INLINE void SetReadPos(int iIndex)
00524     {
00525         m_uiReadIndex   = (iIndex + m_uiSize) & m_uiMask; 
00526     }
00527     
00528 
00529 //********************************************************************
00530 //  @method:            SetWritePos
00531 //  @parameter:         int iIndex  
00532 //  @result:            
00533 //  @author:            Tim Flohrer
00534 //  @notes:             sets the write pointer
00535 //  @creation date:     18.7.2001
00536 //  @last modified:     
00537 //********************************************************************  
00538 INLINE void SetWritePos(int iIndex)
00539     {
00540         m_uiWriteIndex  = (iIndex + m_uiSize) & m_uiMask;
00541     }
00542     
00543     
00544 
00545 //********************************************************************
00546 //  @method:            GetSamplesInBuffer
00547 //  @parameter:         
00548 //  @result:            
00549 //  @author:            Tim Flohrer
00550 //  @notes:             returns number of samples in buffer
00551 //                      i.e. the difference between the read and write pointer  
00552 //  @creation date:     18.7.2001
00553 //  @last modified:     
00554 //********************************************************************
00555 INLINE int  GetSamplesInBuffer()
00556     {
00557         int iCount = m_uiWriteIndex - m_uiReadIndex;
00558         
00559         return (iCount < 0 ? m_uiSize + iCount : iCount);
00560     }   
00561 
00562 INLINE void   Reset()
00563     {
00564         m_uiReadIndex   =   0;
00565         m_uiWriteIndex  =   0;
00566         m_uiTmpIndex    =   0;
00567         for( unsigned int i=0; i<m_uiSize; m_ptBuffer[i++] = 0);
00568     }
00569 
00570 };
00571 
00572 #endif //   __RINGBUFFER_HEADER_INCLUDED__
00573 
00574 

Generated on Fri Mar 23 10:28:54 2007 for FEAPI Plugin Documentation by  doxygen 1.3.9.1