/[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 685 by persson, Tue Jul 5 19:30:37 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 260  class __RTMath : public RTMathBase { Line 307  class __RTMath : public RTMathBase {
307                  }                  }
308                  #endif // CONFIG_ASM && ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
309                  default: {                  default: {
310                      return (b < a) ? b : a;                      return std::min(a, b);
311                  }                  }
312              }              }
313          }          }
# Line 285  class __RTMath : public RTMathBase { Line 332  class __RTMath : public RTMathBase {
332                  }                  }
333                  #endif // CONFIG_ASM && ARCH_X86                  #endif // CONFIG_ASM && ARCH_X86
334                  default: {                  default: {
335                      return (b > a) ? b : a;                      return std::max(a, b);
336                  }                  }
337              }              }
338          }          }

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

  ViewVC Help
Powered by ViewVC