Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CAT/CATMathUtil.h Source File

CATMathUtil.h

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file CATMathUtil.h
00003 /// \brief Generic math/audio utility functions
00004 /// 
00005 ///
00006 /// Copyright (c) 2004-2008 by Michael Ellison.
00007 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00008 ///
00009 // $Author: mikeellison $
00010 // $Date: 2008-01-19 19:19:35 -0600 (Sat, 19 Jan 2008) $
00011 // $Revision:   $
00012 // $NoKeywords: $
00013 ///
00014 //---------------------------------------------------------------------------
00015 #ifndef _CATMathUtil_H_
00016 #define _CATMathUtil_H_
00017 
00018 #include "CATInternal.h"
00019 #include <math.h>
00020 
00021 /// CATLinearToDBValue() converts a linear value
00022 /// to a dB value from -inf to +6 for volume sliders and
00023 /// the like. 0dB is at about 0.708142, +6 is at 1.0f.
00024 ///
00025 /// \param linearVal - input linear value (0 - 1.0f)
00026 /// \return CATFloat32 - dbVal (-inf to +6)
00027 inline CATFloat32 CATLinearToDBValue(CATFloat32 linearVal)
00028 {
00029     return (CATFloat32)(40 * (log10(linearVal*10))) - 34;
00030 }
00031 
00032 /// CATDBValueToLinear() inverts the above CATLinearToDBValue()
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 /// CATLinearToDBGain() converts a linear value
00040 /// to a gain multiplier for volume sliders and the like.
00041 /// \param linearVal - input linear value (0 - 1.0f)
00042 /// \return CATFloat32 - gain multiplier (0.0f - 2.0f)
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 /// CATSampleToDBF() converts a sample value to
00056 /// dBF value
00057 ///
00058 /// \param sample - sample value (-1.0f -> 1.0f)
00059 /// \return CATFloat32 - dBF value (-inf to 0.0)
00060 inline CATFloat32 CATSampleToDBF( CATFloat32 sample )
00061 {
00062     CATFloat32 dBF = (CATFloat32)(20 * log10(fabs(sample)));
00063     return dBF;
00064 }
00065 
00066 /// CATSampleToDBFMeter converts a sample value to a dBF value,
00067 /// then from -96.0 to 0 dBF on a linear scale for a meter.
00068 ///
00069 /// \param sample - sample value (-1.0f -> 1.0f)
00070 /// \return meter value (0 - 1.0f)
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 /// CATSamplesToBeats() converts the number of samples at a given sample rate and tempo into
00083 /// the number of beats
00084 /// \param numSamples - number of samples
00085 /// \param sampleRate - sample rate (e.g. 44100.0f)
00086 /// \param tempo - tempo in bpm
00087 /// \return CATFloat32 - number of beats (fractional)
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 /// CATBeatsToSamples() converts the number of beats at a given sample rate and tempo to
00095 /// the number of samples (Frames)
00096 /// \param numBeats - number of beats to convert - may be fractional
00097 /// \param sampleRate - sample rate (e.g. 44100.0f)
00098 /// \param tempo - tempo in bpm
00099 /// \return CATFloat32 - number of samples (may be fractional)
00100 inline CATFloat32 CATBeatsToSamples( CATFloat32 numBeats, CATFloat32 sampleRate, CATFloat32 tempo)
00101 {
00102     return (numBeats * sampleRate) / (tempo / 60.0f);
00103 }
00104 
00105 /// CATSamplesToBeats() converts the number of samples at a given sample rate and tempo into
00106 /// the number of beats
00107 /// \param numSamples - number of samples
00108 /// \param sampleRate - sample rate (e.g. 44100.0f)
00109 /// \param tempo - tempo in bpm
00110 /// \return CATFloat32 - number of beats (fractional)
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 /// CATBeatsToSamples() converts the number of beats at a given sample rate and tempo to
00118 /// the number of samples (Frames)
00119 /// \param numBeats - number of beats to convert - may be fractional
00120 /// \param sampleRate - sample rate (e.g. 44100.0f)
00121 /// \param tempo - tempo in bpm
00122 /// \return CATFloat32 - number of samples (may be fractional)
00123 inline CATFloat64 CATBeatsToSamples( CATFloat64 numBeats, CATFloat64 sampleRate, CATFloat64 tempo)
00124 {
00125     return (numBeats * sampleRate) / (tempo / 60.0f);
00126 }
00127 
00128 
00129 /// CATInterpolateCubic() returns the cubic interpolation of a sample in a buffer of samples.
00130 ///
00131 /// Note: if you attempt to use on the final sample in a buffer, result is weighted towards
00132 ///       the base sample.
00133 ///
00134 /// \param buffer - ptr to buffer to interpolate from
00135 /// \param position - floating point index into buffer to interpolate
00136 /// \param bufSize - number of samples in buffer
00137 /// \return CATFloat32 - interpolated sample value
00138 inline CATFloat32 CATInterpolateCubic( CATFloat32* buffer, CATFloat32 position, CATUInt32 bufSize)
00139 {
00140     // Cubic interpolation of sound
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 // Hermite interpolation
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 /// CATTruncDenormals() truncates denormal floats to 0.
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 /// CATIsDenormal() detects denormal floats
00184 inline bool CATIsDenormal(CATFloat32 value)
00185 {
00186     return (( (*(unsigned int*)&value) & 0x7f800000) == 0);
00187 }
00188 
00189 /// CATUndenormal() adds a tiny value to a float to avoid denormals
00190 inline CATFloat32 CATUndenormal(CATFloat32 value)
00191 {
00192     return (value += 0.0000000000000000000000001f);
00193 }
00194 
00195 /// CATInterpolateLinear() returns the linear interpolation of a sample in a buffer of samples.
00196 ///
00197 /// Note: if you attempt to use on the final sample in a buffer, result is weighted towards
00198 ///       the base sample.
00199 ///
00200 /// \param buffer - ptr to buffer to interpolate from
00201 /// \param position - floating point index into buffer to interpolate
00202 /// \param bufSize - number of samples in buffer
00203 /// \return CATFloat32 - interpolated sample value
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_

Generated on Mon Feb 11 04:09:45 2008 for Game Accessibility Suite by doxygen 1.5.4