/[svn]/linuxsampler/trunk/src/common/RTMath.h
ViewVC logotype

Diff of /linuxsampler/trunk/src/common/RTMath.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 617 by schoenebeck, Wed Jun 8 21:00:06 2005 UTC revision 2931 by schoenebeck, Sat Jul 9 14:38:33 2016 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6   *   Copyright (C) 2005 Christian Schoenebeck                              *   *   Copyright (C) 2005 - 2016 Christian Schoenebeck                       *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 26  Line 26 
26    
27  #include <math.h>  #include <math.h>
28  #include <stdint.h>  #include <stdint.h>
29  #include "global.h"  #include "global_private.h"
30    
31  /// Needed for calculating frequency ratio used to pitch a sample  /// Needed for calculating frequency ratio used to pitch a sample
32  #define TWELVEHUNDREDTH_ROOT_OF_TWO     1.000577789506555  #define TWELVEHUNDREDTH_ROOT_OF_TWO     1.000577789506555
# Line 75  class RTMathBase { Line 75  class RTMathBase {
75              return pCentsToFreqTable[index_int] + index_fract * (pCentsToFreqTable[index_int+1] - pCentsToFreqTable[index_int]);              return pCentsToFreqTable[index_int] + index_fract * (pCentsToFreqTable[index_int+1] - pCentsToFreqTable[index_int]);
76          }          }
77    
78            /**
79             * Slower version of CentsToFreqRatio, for big values.
80             *
81             * @param cents - pitch value in cents (+1200 cents means +1 octave)
82             * @returns  frequency ratio (e.g. +2.0 for +1 octave)
83             */
84            static double CentsToFreqRatioUnlimited(double Cents) {
85                int octaves = int(Cents / 1200);
86                double x = CentsToFreqRatio(Cents - octaves * 1200);
87                return  octaves < 0 ? x / (1 << -octaves) : x * (1 << octaves);
88            }
89    
90            /**
91             * Inverse function to CentsToFreqRatio(). This function is a bit
92             * slow, so it should not be called too frequently.
93             */
94            static double FreqRatioToCents(double FreqRatio) {
95                return log(FreqRatio) / log(TWELVEHUNDREDTH_ROOT_OF_TWO);
96            }
97    
98            /**
99             * Calculates the line ratio value representation (linear scale)
100             * of the @a decibel value provided (exponential scale).
101             *
102             * The context of audio acoustic sound pressure levels is assumed, and
103             * hence the field version of the dB unit is used here (which uses a
104             * linear factor of 20). This function is a bit slow, so it should
105             * not be called too frequently.
106             *
107             * @param decibel - sound pressure level in dB
108             * @returns linear ratio of the supplied dB value
109             */
110            static float DecibelToLinRatio(float decibel) {
111                return powf(10.f, decibel / 20.f);
112            }
113    
114            /**
115             * Calculates the relatively summed average of a set of values.
116             *
117             * @param current - the current avaerage value of all previously summed values
118             * @param sample - new value to be applied as summed average to the existing values
119             * @param n - amount of sample values applied so far
120             * @returns new average value of all summed values (including the new @a sample)
121             */
122            inline static float RelativeSummedAvg(float current, float sample, int n) {
123                return current + (sample - current) / float(n);
124            }
125    
126      private:      private:
         static float  CentsToFreqTable[CONFIG_MAX_PITCH * 1200 * 2 + 1];  
127          static float* pCentsToFreqTable;          static float* pCentsToFreqTable;
128    
129          static float* InitCentsToFreqTable();          static float* InitCentsToFreqTable();
# Line 95  class __RTMath : public RTMathBase { Line 142  class __RTMath : public RTMathBase {
142          // conversion using truncate          // conversion using truncate
143          inline static int Int(const float a) {          inline static int Int(const float a) {
144              switch (IMPL) {              switch (IMPL) {
                 case CPP: {  
                     return (int) a;  
                 }  
145                  #if CONFIG_ASM && ARCH_X86                  #if CONFIG_ASM && ARCH_X86
146                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
147                      int ret;                      int ret;
# Line 109  class __RTMath : public RTMathBase { Line 153  class __RTMath : public RTMathBase {
153                      return ret;                      return ret;
154                  }                  }
155                  #endif // CONFIG_ASM && ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
156                    default: {
157                        return (int) a;
158                    }
159              }              }
160          }          }
161    
# Line 119  class __RTMath : public RTMathBase { Line 166  class __RTMath : public RTMathBase {
166    
167          inline static float Float(const int a) {          inline static float Float(const int a) {
168              switch (IMPL) {              switch (IMPL) {
                 case CPP: {  
                     return (float) a;  
                 }  
169                  #if CONFIG_ASM && ARCH_X86                  #if CONFIG_ASM && ARCH_X86
170                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
171                      float ret;                      float ret;
# Line 134  class __RTMath : public RTMathBase { Line 178  class __RTMath : public RTMathBase {
178                      return ret;                      return ret;
179                  }                  }
180                  #endif // CONFIG_ASM && ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
181                    default: {
182                        return (float) a;
183                    }
184              }              }
185          }          }
186    
# Line 146  class __RTMath : public RTMathBase { Line 193  class __RTMath : public RTMathBase {
193    
194          inline static float Sum(const float& a, const float& b) {          inline static float Sum(const float& a, const float& b) {
195              switch (IMPL) {              switch (IMPL) {
                 case CPP: {  
                     return (a + b);  
                 }  
196                  #if CONFIG_ASM && ARCH_X86                  #if CONFIG_ASM && ARCH_X86
197                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
198                      float ret;                      float ret;
# Line 162  class __RTMath : public RTMathBase { Line 206  class __RTMath : public RTMathBase {
206                      return ret;                      return ret;
207                  }                  }
208                  #endif // CONFIG_ASM && ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
209                    default: {
210                        return (a + b);
211                    }
212              }              }
213          }          }
214    
# Line 171  class __RTMath : public RTMathBase { Line 218  class __RTMath : public RTMathBase {
218    
219          inline static float Sub(const float& a, const float& b) {          inline static float Sub(const float& a, const float& b) {
220              switch (IMPL) {              switch (IMPL) {
                 case CPP: {  
                     return (a - b);  
                 }  
221                  #if CONFIG_ASM && ARCH_X86                  #if CONFIG_ASM && ARCH_X86
222                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
223                      float ret;                      float ret;
# Line 187  class __RTMath : public RTMathBase { Line 231  class __RTMath : public RTMathBase {
231                      return ret;                      return ret;
232                  }                  }
233                  #endif // CONFIG_ASM && ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
234                    default: {
235                        return (a - b);
236                    }
237              }              }
238          }          }
239    
# Line 196  class __RTMath : public RTMathBase { Line 243  class __RTMath : public RTMathBase {
243    
244          inline static float Mul(const float a, const float b) {          inline static float Mul(const float a, const float b) {
245              switch (IMPL) {              switch (IMPL) {
                 case CPP: {  
                     return (a * b);  
                 }  
246                  #if CONFIG_ASM && ARCH_X86                  #if CONFIG_ASM && ARCH_X86
247                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
248                      float ret;                      float ret;
# Line 212  class __RTMath : public RTMathBase { Line 256  class __RTMath : public RTMathBase {
256                      return ret;                      return ret;
257                  }                  }
258                  #endif // CONFIG_ASM && ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
259                    default: {
260                        return (a * b);
261                    }
262              }              }
263          }          }
264    
# Line 221  class __RTMath : public RTMathBase { Line 268  class __RTMath : public RTMathBase {
268    
269          inline static float Div(const float a, const float b) {          inline static float Div(const float a, const float b) {
270              switch (IMPL) {              switch (IMPL) {
                 case CPP: {  
                     return (a / b);  
                 }  
271                  #if CONFIG_ASM && ARCH_X86                  #if CONFIG_ASM && ARCH_X86
272                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
273                      float ret;                      float ret;
# Line 237  class __RTMath : public RTMathBase { Line 281  class __RTMath : public RTMathBase {
281                      return ret;                      return ret;
282                  }                  }
283                  #endif // CONFIG_ASM && ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
284                    default: {
285                        return (a / b);
286                    }
287              }              }
288          }          }
289    
# Line 246  class __RTMath : public RTMathBase { Line 293  class __RTMath : public RTMathBase {
293    
294          inline static float Min(const float a, const float b) {          inline static float Min(const float a, const float b) {
295              switch (IMPL) {              switch (IMPL) {
                 case CPP: {  
                     return (b < a) ? b : a;  
                 }  
296                  #if CONFIG_ASM && ARCH_X86                  #if CONFIG_ASM && ARCH_X86
297                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
298                      float ret;                      float ret;
# Line 262  class __RTMath : public RTMathBase { Line 306  class __RTMath : public RTMathBase {
306                      return ret;                      return ret;
307                  }                  }
308                  #endif // CONFIG_ASM && ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
309                    default: {
310                        return std::min(a, b);
311                    }
312              }              }
313          }          }
314    
# Line 271  class __RTMath : public RTMathBase { Line 318  class __RTMath : public RTMathBase {
318    
319          inline static float Max(const float a, const float b) {          inline static float Max(const float a, const float b) {
320              switch (IMPL) {              switch (IMPL) {
                 case CPP: {  
                     return (b > a) ? b : a;  
                 }  
321                  #if CONFIG_ASM && ARCH_X86                  #if CONFIG_ASM && ARCH_X86
322                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
323                      float ret;                      float ret;
# Line 287  class __RTMath : public RTMathBase { Line 331  class __RTMath : public RTMathBase {
331                      return ret;                      return ret;
332                  }                  }
333                  #endif // CONFIG_ASM && ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
334                    default: {
335                        return std::max(a, b);
336                    }
337              }              }
338          }          }
339    
# Line 296  class __RTMath : public RTMathBase { Line 343  class __RTMath : public RTMathBase {
343    
344          inline static float Fmodf(const float &a, const float &b) {          inline static float Fmodf(const float &a, const float &b) {
345              switch (IMPL) {              switch (IMPL) {
                 case CPP: {  
                     return fmodf(a, b);  
                 }  
346                  #if CONFIG_ASM && ARCH_X86                  #if CONFIG_ASM && ARCH_X86
347                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
348                      float ret;                      float ret;
# Line 319  class __RTMath : public RTMathBase { Line 363  class __RTMath : public RTMathBase {
363                      return ret;                      return ret;
364                  }                  }
365                  #endif // CONFIG_ASM && ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
366                    default: {
367                        return fmodf(a, b);
368                    }
369              }              }
370          }          }
371  };  };

Legend:
Removed from v.617  
changed lines
  Added in v.2931

  ViewVC Help
Powered by ViewVC