/[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 554 by schoenebeck, Thu May 19 19:25:14 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 36  enum implementation_t { Line 36  enum implementation_t {
36      ASM_X86_MMX_SSE      ASM_X86_MMX_SSE
37  };  };
38    
39    /** @brief Real Time Math Base Class
40     *
41     * Math functions for real time operation. This base class contains all
42     * non-template methods.
43     */
44  class RTMathBase {  class RTMathBase {
45      public:      public:
46          /**          /**
# Line 70  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();
130  };  };
131    
132  /** Real Time Math  /** @brief Real Time Math
133   *   *
134   * Math functions for real time operation.   * This is a template which provides customized methods for the desired low
135     * level implementation. The ASM_X86_MMX_SSE implementation of each method
136     * for example doesn't use 387 FPU instruction. This is needed for MMX
137     * algorithms which do not allow mixed MMX and 387 instructions.
138   */   */
139  template<implementation_t IMPL = CPP>  template<implementation_t IMPL = CPP>
140  class __RTMath : public RTMathBase {  class __RTMath : public RTMathBase {
# Line 87  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) {
145                  case CPP: {                  #if CONFIG_ASM && ARCH_X86
                     return (int) a;  
                 }  
                 #if ARCH_X86  
146                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
147                      int ret;                      int ret;
148                      asm (                      asm (
# Line 100  class __RTMath : public RTMathBase { Line 152  class __RTMath : public RTMathBase {
152                      );                      );
153                      return ret;                      return ret;
154                  }                  }
155                  #endif // ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
156                    default: {
157                        return (int) a;
158                    }
159              }              }
160          }          }
161    
# Line 111  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) {
169                  case CPP: {                  #if CONFIG_ASM && ARCH_X86
                     return (float) a;  
                 }  
                 #if ARCH_X86  
170                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
171                      float ret;                      float ret;
172                      asm (                      asm (
# Line 125  class __RTMath : public RTMathBase { Line 177  class __RTMath : public RTMathBase {
177                      );                      );
178                      return ret;                      return ret;
179                  }                  }
180                  #endif // ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
181                    default: {
182                        return (float) a;
183                    }
184              }              }
185          }          }
186    
# Line 138  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) {
196                  case CPP: {                  #if CONFIG_ASM && ARCH_X86
                     return (a + b);  
                 }  
                 #if ARCH_X86  
197                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
198                      float ret;                      float ret;
199                      asm (                      asm (
# Line 153  class __RTMath : public RTMathBase { Line 205  class __RTMath : public RTMathBase {
205                      );                      );
206                      return ret;                      return ret;
207                  }                  }
208                  #endif // ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
209                    default: {
210                        return (a + b);
211                    }
212              }              }
213          }          }
214    
# Line 163  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) {
221                  case CPP: {                  #if CONFIG_ASM && ARCH_X86
                     return (a - b);  
                 }  
                 #if ARCH_X86  
222                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
223                      float ret;                      float ret;
224                      asm (                      asm (
# Line 178  class __RTMath : public RTMathBase { Line 230  class __RTMath : public RTMathBase {
230                      );                      );
231                      return ret;                      return ret;
232                  }                  }
233                  #endif // ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
234                    default: {
235                        return (a - b);
236                    }
237              }              }
238          }          }
239    
# Line 188  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) {
246                  case CPP: {                  #if CONFIG_ASM && ARCH_X86
                     return (a * b);  
                 }  
                 #if ARCH_X86  
247                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
248                      float ret;                      float ret;
249                      asm (                      asm (
# Line 203  class __RTMath : public RTMathBase { Line 255  class __RTMath : public RTMathBase {
255                      );                      );
256                      return ret;                      return ret;
257                  }                  }
258                  #endif // ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
259                    default: {
260                        return (a * b);
261                    }
262              }              }
263          }          }
264    
# Line 213  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) {
271                  case CPP: {                  #if CONFIG_ASM && ARCH_X86
                     return (a / b);  
                 }  
                 #if ARCH_X86  
272                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
273                      float ret;                      float ret;
274                      asm (                      asm (
# Line 228  class __RTMath : public RTMathBase { Line 280  class __RTMath : public RTMathBase {
280                      );                      );
281                      return ret;                      return ret;
282                  }                  }
283                  #endif // ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
284                    default: {
285                        return (a / b);
286                    }
287              }              }
288          }          }
289    
# Line 238  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) {
296                  case CPP: {                  #if CONFIG_ASM && ARCH_X86
                     return (b < a) ? b : a;  
                 }  
                 #if ARCH_X86  
297                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
298                      float ret;                      float ret;
299                      asm (                      asm (
# Line 253  class __RTMath : public RTMathBase { Line 305  class __RTMath : public RTMathBase {
305                      );                      );
306                      return ret;                      return ret;
307                  }                  }
308                  #endif // ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
309                    default: {
310                        return std::min(a, b);
311                    }
312              }              }
313          }          }
314    
# Line 263  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) {
321                  case CPP: {                  #if CONFIG_ASM && ARCH_X86
                     return (b > a) ? b : a;  
                 }  
                 #if ARCH_X86  
322                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
323                      float ret;                      float ret;
324                      asm (                      asm (
# Line 278  class __RTMath : public RTMathBase { Line 330  class __RTMath : public RTMathBase {
330                      );                      );
331                      return ret;                      return ret;
332                  }                  }
333                  #endif // ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
334                    default: {
335                        return std::max(a, b);
336                    }
337              }              }
338          }          }
339    
# Line 288  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) {
346                  case CPP: {                  #if CONFIG_ASM && ARCH_X86
                     return fmodf(a, b);  
                 }  
                 #if ARCH_X86  
347                  case ASM_X86_MMX_SSE: {                  case ASM_X86_MMX_SSE: {
348                      float ret;                      float ret;
349                      asm (                      asm (
# Line 310  class __RTMath : public RTMathBase { Line 362  class __RTMath : public RTMathBase {
362                      );                      );
363                      return ret;                      return ret;
364                  }                  }
365                  #endif // ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
366                    default: {
367                        return fmodf(a, b);
368                    }
369              }              }
370          }          }
371  };  };

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

  ViewVC Help
Powered by ViewVC