Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

ReBuffer.cc

Go to the documentation of this file.
00001 /*
00002 
00003     Rebuffering class
00004     Copyright (C) 2001-2003 Jussi Laako
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 */
00021 
00022 
00023 #ifdef USE_INTEL_MATH
00024     #include <mathimf.h>
00025 #else
00026     #include <math.h>
00027 #endif
00028 #include <float.h>
00029 #include <string.h>
00030 #include <stdio.h>
00031 
00032 #include <Exception.hh>
00033 
00034 #include "dsp/ReBuffer.hh"
00035 
00036 
00037 inline void clReBuffer::CheckSize (long lDataCount, long lTypeSize)
00038 {
00039     long lTotalSize = (lCount + lDataCount) * lTypeSize;
00040     if (lTotalSize > Buffer.GetSize())
00041     {
00042         #ifndef _ISOC9X_SOURCE
00043         long lTwosExp = (long) ceil(log(lTotalSize) / log(2));
00044         long lNewBufferSize = (long) pow(2, lTwosExp);
00045         #else
00046         long lTwosExp = (long) ceil(log2(lTotalSize));
00047         long lNewBufferSize = (long) exp2(lTwosExp);
00048         #endif
00049         Buffer.Resize(lNewBufferSize);
00050 
00051         long lTailSize = lSize - lGetIndex;
00052         if (lCount > lTailSize)
00053         {
00054             long lHeadSize = lCount - lTailSize;
00055             long lNewSize = lNewBufferSize / lTypeSize;
00056             unsigned char *cpBuffer = Buffer;
00057             unsigned char *cpDest = cpBuffer + lSize * lTypeSize;
00058             memcpy(cpDest, cpBuffer, lHeadSize * lTypeSize);
00059             lPutIndex = lSize + lHeadSize;
00060             if (lPutIndex >= lNewSize)
00061                 lPutIndex -= lNewSize;
00062         }
00063 
00064         lSize = lNewBufferSize / lTypeSize;
00065     }
00066 }
00067 
00068 
00069 void *clReBuffer::Index (const std::type_info &TypeInfo, long lIndex)
00070 {
00071     if (lIndex >= lSize)
00072         throw clException("clReBuffer::Index(): lIndex >= lSize");
00073     long lBufIdx = lGetIndex + lIndex;
00074     if (lBufIdx >= lSize) lBufIdx -= lSize;
00075     if (TypeInfo == typeid(float))
00076     {
00077         float *fpBuffer = Buffer;
00078         return (&fpBuffer[lBufIdx]);
00079     }
00080     else if (TypeInfo == typeid(double))
00081     {
00082         double *dpBuffer = Buffer;
00083         return (&dpBuffer[lBufIdx]);
00084     }
00085     else
00086         throw clException("clReBuffer::Index(): typeid()");
00087 }
00088 
00089 
00090 void *clReBuffer::GetPtr (const std::type_info &TypeInfo)
00091 {
00092     if (lGetIndex == 0 || lCount == 0) 
00093         return ((void *) Buffer);
00094     long lTempCount = lCount;
00095     clAlloc Temp;
00096     if (TypeInfo == typeid(float))
00097     {
00098         Temp.Size(lTempCount * sizeof(float));
00099         Get((float *) Temp, lTempCount);
00100         Clear();
00101         Put((float *) Temp, lTempCount);
00102     }
00103     else if (TypeInfo == typeid(double))
00104     {
00105         Temp.Size(lTempCount * sizeof(double));
00106         Get((double *) Temp, lTempCount);
00107         Clear();
00108         Put((double *) Temp, lTempCount);
00109     }
00110     else
00111         throw clException("clReBuffer::GetPtr(): typeid()");
00112     return ((void *) Buffer);
00113 }
00114 
00115 
00116 clReBuffer::clReBuffer ()
00117 {
00118     lSize = 0;
00119     lPutIndex = 0;
00120     lGetIndex = 0;
00121     lCount = 0;
00122 }
00123 
00124 
00125 clReBuffer::~clReBuffer ()
00126 {
00127 }
00128 
00129 
00130 void clReBuffer::Put (const float *fpSrcData, long lSrcCount)
00131 {
00132     CheckSize(lSrcCount, sizeof(float));
00133     if (lPutIndex >= lSize) lPutIndex = 0;
00134     long lTailSpace = lSize - lPutIndex;
00135     float *fpBuffer = Buffer;
00136     if (lSrcCount > lTailSpace)
00137     {
00138         long lHeadSize = lSrcCount - lTailSpace;
00139         memcpy(&fpBuffer[lPutIndex], fpSrcData, lTailSpace * sizeof(float));
00140         memcpy(fpBuffer, &fpSrcData[lTailSpace], lHeadSize * sizeof(float));
00141         lPutIndex = lHeadSize;
00142     }
00143     else
00144     {
00145         memcpy(&fpBuffer[lPutIndex], fpSrcData, lSrcCount * sizeof(float));
00146         lPutIndex += lSrcCount;
00147     }
00148     lCount += lSrcCount;
00149 }
00150 
00151 
00152 void clReBuffer::Put (const double *dpSrcData, long lSrcCount)
00153 {
00154     CheckSize(lSrcCount, sizeof(double));
00155     if (lPutIndex >= lSize) lPutIndex = 0;
00156     long lTailSpace = lSize - lPutIndex;
00157     double *dpBuffer = Buffer;
00158     if (lSrcCount > lTailSpace)
00159     {
00160         long lHeadSize = lSrcCount - lTailSpace;
00161         memcpy(&dpBuffer[lPutIndex], dpSrcData, lTailSpace * sizeof(double));
00162         memcpy(dpBuffer, &dpSrcData[lTailSpace], lHeadSize * sizeof(double));
00163         lPutIndex = lHeadSize;
00164     }
00165     else
00166     {
00167         memcpy(&dpBuffer[lPutIndex], dpSrcData, lSrcCount * sizeof(double));
00168         lPutIndex += lSrcCount;
00169     }
00170     lCount += lSrcCount;
00171 }
00172 
00173 
00174 bool clReBuffer::Get (float *fpDestData, long lDestCount)
00175 {
00176     if (lCount < lDestCount)
00177         return false;
00178     long lTailSize = lSize - lGetIndex;
00179     float *fpBuffer = Buffer;
00180     if (lDestCount > lTailSize)
00181     {
00182         long lHeadSize = lDestCount - lTailSize;
00183         memcpy(fpDestData, &fpBuffer[lGetIndex], lTailSize * sizeof(float));
00184         memcpy(&fpDestData[lTailSize], fpBuffer, lHeadSize * sizeof(float));
00185         lGetIndex = lHeadSize;
00186     }
00187     else
00188     {
00189         memcpy(fpDestData, &fpBuffer[lGetIndex], lDestCount * sizeof(float));
00190         lGetIndex += lDestCount;
00191     }
00192     lCount -= lDestCount;
00193     return true;
00194 }
00195 
00196 
00197 bool clReBuffer::Get (double *dpDestData, long lDestCount)
00198 {
00199     if (lCount < lDestCount)
00200         return false;
00201     long lTailSize = lSize - lGetIndex;
00202     double *dpBuffer = Buffer;
00203     if (lDestCount > lTailSize)
00204     {
00205         long lHeadSize = lDestCount - lTailSize;
00206         memcpy(dpDestData, &dpBuffer[lGetIndex], lTailSize * sizeof(double));
00207         memcpy(&dpDestData[lTailSize], dpBuffer, lHeadSize * sizeof(double));
00208         lGetIndex = lHeadSize;
00209     }
00210     else
00211     {
00212         memcpy(dpDestData, &dpBuffer[lGetIndex], lDestCount * sizeof(double));
00213         lGetIndex += lDestCount;
00214     }
00215     lCount -= lDestCount;
00216     return true;
00217 }
00218 
00219 
00220 clReBuffer & clReBuffer::operator= (const clReBuffer &Src)
00221 {
00222     lSize = Src.lSize;
00223     lPutIndex = Src.lPutIndex;
00224     lGetIndex = Src.lGetIndex;
00225     lCount = Src.lCount;
00226     Buffer.Copy(Src.Buffer);
00227     return (*this);
00228 }

Generated on Tue Mar 2 19:46:45 2004 for libDSP by doxygen 1.3.6