pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_Constants.h
00001 /*
00002  -------------------------------------------------------------------------------
00003  This file is part of the Plink, Plonk, Plank libraries
00004   by Martin Robinson
00005  
00006  http://code.google.com/p/pl-nk/
00007  
00008  Copyright University of the West of England, Bristol 2011-14
00009  All rights reserved.
00010  
00011  Redistribution and use in source and binary forms, with or without
00012  modification, are permitted provided that the following conditions are met:
00013  
00014  * Redistributions of source code must retain the above copyright
00015    notice, this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright
00017    notice, this list of conditions and the following disclaimer in the
00018    documentation and/or other materials provided with the distribution.
00019  * Neither the name of University of the West of England, Bristol nor 
00020    the names of its contributors may be used to endorse or promote products
00021    derived from this software without specific prior written permission.
00022  
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026  DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 
00027  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00028  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00029  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00030  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00032  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00033  
00034  This software makes use of third party libraries. For more information see:
00035  doc/license.txt included in the distribution.
00036  -------------------------------------------------------------------------------
00037  */
00038 
00039 #ifndef PLONK_CONSTANTS_H
00040 #define PLONK_CONSTANTS_H
00041 
00042 
00045 template<class Type>
00046 class Math
00047 {
00048 public:
00049     static inline const Type& get0() throw()          { static const Type v ((Type)0); return v; }
00050     static inline const Type& get1() throw()          { static const Type v ((Type)1); return v; }
00051     static inline const Type& get2() throw()          { static const Type v ((Type)2); return v; }
00052     static inline const Type& get3() throw()          { static const Type v ((Type)3); return v; }
00053     static inline const Type& get4() throw()          { static const Type v ((Type)4); return v; }
00054     static inline const Type& get10() throw()         { static const Type v ((Type)10); return v; }
00055     static inline const Type& get20() throw()         { static const Type v ((Type)20); return v; }
00056     static inline const Type& get30() throw()         { static const Type v ((Type)30); return v; }
00057     static inline const Type& get40() throw()         { static const Type v ((Type)40); return v; }
00058     static inline const Type& get50() throw()         { static const Type v ((Type)50); return v; }
00059     static inline const Type& get100() throw()        { static const Type v ((Type)100); return v; }
00060 
00061     static inline const Type& get_1() throw()         { static const Type v ((Type)-1); return v; }
00062     static inline const Type& get0_5() throw()        { static const Type v ((Type)0.5); return v; }
00063     static inline const Type& get_0_5() throw()       { static const Type v ((Type)-0.5); return v; }
00064     static inline const Type& get0_25() throw()       { static const Type v ((Type)0.25); return v; }
00065     static inline const Type& get1_5() throw()        { static const Type v ((Type)1.5); return v; }
00066     
00067     static inline const Type& get440() throw()        { static const Type v ((Type)440); return v; }
00068     static inline const Type& get12() throw()         { static const Type v ((Type)12); return v; }
00069     static inline const Type& get69() throw()         { static const Type v ((Type)69); return v; }
00070     static inline const Type& get360() throw()        { static const Type v ((Type)360); return v; }    
00071 
00072     static inline const Type& get1_3() throw()       { static const Type v ((Type)(1.0 / 3.0)); return v; }
00073     static inline const Type& get1_6() throw()       { static const Type v ((Type)(1.0 / 6.0)); return v; }
00074 
00075     static inline const Type& get1_40() throw()       { static const Type v ((Type)(1.0 / 40.0)); return v; }
00076     static inline const Type& get1_440() throw()      { static const Type v ((Type)(1.0 / 440.0)); return v; }
00077     static inline const Type& get1_12() throw()       { static const Type v ((Type)(1.0 / 12.0)); return v; }
00078     static inline const Type& get1_360() throw()      { static const Type v ((Type)(1.0 / 360.0)); return v; }
00079     static inline const Type& get1_1200() throw()     { static const Type v ((Type)(1.0 / 1200.0)); return v; }
00080     
00081     static inline const Type& getPi() throw()         { static const Type v ((Type)std::acos (-1.0)); return v; }
00082     static inline const Type& getLog0_001() throw()   { static const Type v ((Type)std::log (0.001)); return v; }
00083     static inline const Type& getLog0_01() throw()    { static const Type v ((Type)std::log (0.01)); return v; }
00084     static inline const Type& getLog0_1() throw()     { static const Type v ((Type)std::log (0.1)); return v; }
00085     static inline const Type& getLog2() throw()       { static const Type v ((Type)std::log (2.0)); return v; }
00086     static inline const Type& getLog10() throw()      { static const Type v ((Type)std::log (10.0)); return v; }
00087     static inline const Type& get1_Log2() throw()     { static const Type v ((Type)(1.0 / std::log (2.0))); return v; }
00088     static inline const Type& get1_Log10() throw()    { static const Type v ((Type)(1.0 /std::log (10.0))); return v; }
00089     static inline const Type& getSqrt2() throw()      { static const Type v ((Type)std::sqrt (2.0)); return v; }
00090     static inline const Type& get1_Sqrt2() throw()    { static const Type v ((Type)(1.0 / Math<double>::getSqrt2())); return v; }
00091     static inline const Type& getSqrt2_2() throw()    { static const Type v ((Type)(Math<double>::getSqrt2() * 0.5)); return v; }
00092     static inline const Type& getSqrt2_1() throw()    { static const Type v ((Type)(Math<double>::getSqrt2() - 1.0)); return v; }
00093 
00094     static inline const Type& getLogSqrt2() throw()   { static const Type v ((Type)std::log (Math<double>::getSqrt2())); return v; }
00095     static inline const Type& getE() throw()          { static const Type v ((Type)std::exp (1.0)); return v; }
00096     static inline const Type& getE4() throw()         { static const Type v ((Type)(std::exp (1.0) * std::exp (1.0) * std::exp (1.0) * std::exp (1.0))); return v; }
00097 
00098     static inline const Type& get2Pi() throw()        { static const Type v ((Type)(Math<double>::getPi() * 2.0)); return v; }
00099     static inline const Type& get4Pi() throw()        { static const Type v ((Type)(Math<double>::getPi() * 4.0)); return v; }
00100     
00101     static inline const Type& getPi_2() throw()       { static const Type v ((Type)(Math<double>::getPi() * 0.5)); return v; }
00102     static inline const Type& getPi_4() throw()       { static const Type v ((Type)(Math<double>::getPi() * 0.25)); return v; }
00103     static inline const Type& get3Pi_2() throw()      { static const Type v ((Type)(Math<double>::getPi() * 1.5)); return v; }
00104     static inline const Type& get3Pi_4() throw()      { static const Type v ((Type)(Math<double>::getPi() * 0.75)); return v; }
00105     static inline const Type& get1_2Pi() throw()      { static const Type v ((Type)(1.0 / Math<double>::getPi())); return v; }
00106     static inline const Type& get4_Pi() throw()       { static const Type v ((Type)(4.0 / Math<double>::getPi())); return v; }
00107     static inline const Type& get_4_Pi() throw()      { static const Type v ((Type)(-4.0 / Math<double>::getPi())); return v; }
00108 
00109 };
00110 
00111 template<class Type, unsigned N>
00112 class Series
00113 {
00114 public:
00115     static inline const Type& getFactorial() throw()        { static const Type v ((Type)getFactorialInternal()); return v; }
00116     static inline const Type& get1_Factorial() throw()      { static const Type v ((Type)(1.0 / getFactorialInternal())); return v; }
00117     static inline const Type& getSum() throw()              { static const Type v ((Type)getSumInternal()); return v; }
00118     static inline const Type& get1_Sum() throw()            { static const Type v ((Type)(1.0 / getSumInternal())); return v; }
00119     
00120     static inline const Type& getFibonacciSum() throw()     { static const Type v ((Type)getFibonacciInternal<double> (N)); return v; }
00121     static inline const Type& getGolden() throw()           
00122     { 
00123         static const Type v ((Type)(getFibonacciInternal<double> (N - 1) / getFibonacciInternal<double> (N)));
00124         return v;
00125     }
00126     
00127 private:
00128 #if PLONK_DEBUG
00129     static void checkN() { plonk_staticassert (N != 0); }
00130 #endif
00131     
00132     static inline const double getFactorialInternal() throw() 
00133     { 
00134         double v = 1.0;
00135         
00136         for (int i = 2; i <= N; ++i)
00137             v *= double (i);
00138         
00139         return v;
00140     }
00141     
00142     template<class CalcType>
00143     static inline const double getFibonacciInternal (const int n) throw() 
00144     { 
00145         CalcType y0 = 1;
00146         CalcType y1 = 1;
00147         
00148         for (int i = 2; i <= N; ++i)
00149         {
00150             const CalcType temp = y0 + y1;
00151             y1 = y0;
00152             y0 = temp;
00153         }
00154         
00155         return y0;
00156     }
00157 
00158     
00159     static inline const double getSumInternal() throw() 
00160     { 
00161         double v = 1.0;
00162         
00163         for (int i = 2; i <= N; ++i)
00164             v += double (i);
00165         
00166         return v;
00167     }
00168 
00169 };
00170 
00171 typedef Math<Float> FloatMath;
00172 typedef Math<Double> DoubleMath;
00173 
00174 
00175 #endif // PLONK_CONSTANTS_H
 All Classes Functions Typedefs Enumerations Enumerator Properties