00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef _CATMathUtil_H_
00016 #define _CATMathUtil_H_
00017
00018 #include "CATInternal.h"
00019 #include <math.h>
00020
00021
00022
00023
00024
00025
00026
00027 inline CATFloat32 CATLinearToDBValue(CATFloat32 linearVal)
00028 {
00029 return (CATFloat32)(40 * (log10(linearVal*10))) - 34;
00030 }
00031
00032
00033 inline CATFloat32 CATDBValueToLinear( CATFloat32 dbVal )
00034 {
00035 CATFloat32 linearVal = (CATFloat32)pow(10,(dbVal + 34.0f)/40.0f) / 10.0f;
00036 return linearVal;
00037 }
00038
00039
00040
00041
00042
00043 inline CATFloat32 CATLinearToDBGain( CATFloat32 linearVal)
00044 {
00045 if (linearVal == 0.0f)
00046 return 0.0f;
00047
00048 CATFloat32 dbVal = (CATLinearToDBValue(linearVal));
00049
00050 CATFloat32 powVal = (CATFloat32)pow(10, dbVal/20.0f);
00051
00052 return (CATFloat32)powVal;
00053 }
00054
00055
00056
00057
00058
00059
00060 inline CATFloat32 CATSampleToDBF( CATFloat32 sample )
00061 {
00062 CATFloat32 dBF = (CATFloat32)(20 * log10(fabs(sample)));
00063 return dBF;
00064 }
00065
00066
00067
00068
00069
00070
00071 inline CATFloat32 CATSampleToDBFMeter( CATFloat32 sample)
00072 {
00073 CATFloat32 dBF = CATSampleToDBF(sample);
00074 if (dBF <= -96.0f)
00075 {
00076 return 0.0f;
00077 }
00078
00079 return 1.0f + (dBF / 96.0f);
00080 }
00081
00082
00083
00084
00085
00086
00087
00088 inline CATFloat32 CATSamplesToBeats( CATUInt32 numSamples, CATFloat32 sampleRate, CATFloat32 tempo)
00089 {
00090 CATASSERT(sampleRate != 0.0f, "Invalid sample rate!");
00091 return (numSamples / sampleRate) * (tempo / 60.0f);
00092 }
00093
00094
00095
00096
00097
00098
00099
00100 inline CATFloat32 CATBeatsToSamples( CATFloat32 numBeats, CATFloat32 sampleRate, CATFloat32 tempo)
00101 {
00102 return (numBeats * sampleRate) / (tempo / 60.0f);
00103 }
00104
00105
00106
00107
00108
00109
00110
00111 inline CATFloat64 CATSamplesToBeats( CATUInt32 numSamples, CATFloat64 sampleRate, CATFloat64 tempo)
00112 {
00113 CATASSERT(sampleRate != 0.0f, "Invalid sample rate!");
00114 return (numSamples / sampleRate) * (tempo / 60.0f);
00115 }
00116
00117
00118
00119
00120
00121
00122
00123 inline CATFloat64 CATBeatsToSamples( CATFloat64 numBeats, CATFloat64 sampleRate, CATFloat64 tempo)
00124 {
00125 return (numBeats * sampleRate) / (tempo / 60.0f);
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 inline CATFloat32 CATInterpolateCubic( CATFloat32* buffer, CATFloat32 position, CATUInt32 bufSize)
00139 {
00140
00141 CATUInt32 basePos = (CATUInt32)position;
00142 CATFloat32 diff = position - basePos;
00143
00144 CATFloat32 xm1 = (basePos > 0)?buffer[basePos - 1] : buffer[basePos];
00145 CATFloat32 x0 = buffer[basePos];
00146 CATFloat32 x1 = (basePos < bufSize - 1)?buffer[basePos + 1] : buffer[basePos];
00147 CATFloat32 x2 = (basePos < bufSize - 2)?buffer[basePos + 2] : buffer[basePos];
00148
00149 CATFloat32 a = (3 * (x0-x1) - xm1 + x2) / 2.0f;
00150 CATFloat32 b = 2*x1 + xm1 - (5*x0 + x2) / 2.0f;
00151 CATFloat32 c = (x1 - xm1) / 2.0f;
00152 return ((((a * diff) + b) * diff + c) * diff + x0);
00153 }
00154
00155
00156 inline CATFloat32 CATInterpolateHermite( CATFloat32* buffer, CATFloat32 position, CATUInt32 bufSize)
00157 {
00158 CATUInt32 basePos = (CATUInt32)position;
00159 CATFloat32 diff = position - basePos;
00160
00161 CATFloat32 xm1 = (basePos > 0)?buffer[basePos - 1] : buffer[basePos];
00162 CATFloat32 x0 = buffer[basePos];
00163 CATFloat32 x1 = (basePos < bufSize - 1)?buffer[basePos + 1] : buffer[basePos];
00164 CATFloat32 x2 = (basePos < bufSize - 2)?buffer[basePos + 2] : buffer[basePos];
00165
00166 CATFloat32 c = (x1 - xm1) * 0.5f;
00167 CATFloat32 v = x0 - x1;
00168 CATFloat32 w = c + v;
00169 CATFloat32 a = w + v + (x2 - x0) * 0.5f;
00170 CATFloat32 b = w + a;
00171 return ((((a * diff) - b) * diff + c) * diff + x0);
00172 }
00173
00174
00175 inline CATFloat32 CATTruncDenormals(CATFloat32 value)
00176 {
00177 if (( (*(unsigned int*)&value) & 0x7f800000) == 0)
00178 value = 0.0f;
00179
00180 return value;
00181 }
00182
00183
00184 inline bool CATIsDenormal(CATFloat32 value)
00185 {
00186 return (( (*(unsigned int*)&value) & 0x7f800000) == 0);
00187 }
00188
00189
00190 inline CATFloat32 CATUndenormal(CATFloat32 value)
00191 {
00192 return (value += 0.0000000000000000000000001f);
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 inline CATFloat32 CATInterpolateLinear( CATFloat32* buffer, CATFloat32 position, CATUInt32 bufSize)
00205 {
00206 CATUInt32 basePos = (CATUInt32)position;
00207 CATFloat32 diff = position - basePos;
00208 CATFloat32 x0 = buffer[basePos];
00209 CATFloat32 x1 = (basePos < bufSize-1)?buffer[basePos] : buffer[basePos];
00210 return (x0 * (1.0f - diff) + (x1 * diff));
00211 }
00212
00213
00214
00215 #endif // _CATMathUtil_H_