1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
8 |
* it under the terms of the GNU General Public License as published by * |
9 |
* the Free Software Foundation; either version 2 of the License, or * |
10 |
* (at your option) any later version. * |
11 |
* * |
12 |
* This program is distributed in the hope that it will be useful, * |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
* GNU General Public License for more details. * |
16 |
* * |
17 |
* You should have received a copy of the GNU General Public License * |
18 |
* along with this program; if not, write to the Free Software * |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
* MA 02111-1307 USA * |
21 |
***************************************************************************/ |
22 |
|
23 |
#ifndef __RT_MATH_H__ |
24 |
#define __RT_MATH_H__ |
25 |
|
26 |
#include <math.h> |
27 |
#include "global.h" |
28 |
|
29 |
/// Needed for calculating frequency ratio used to pitch a sample |
30 |
#define TWELVEHUNDREDTH_ROOT_OF_TWO 1.000577789506555 |
31 |
|
32 |
/** Real Time Math |
33 |
* |
34 |
* Math functions for real time operation. |
35 |
*/ |
36 |
class RTMath { |
37 |
public: |
38 |
/** |
39 |
* Converts a double to integer type. |
40 |
*/ |
41 |
inline static int DoubleToInt(double f) { |
42 |
#if ARCH_X86 |
43 |
int i; |
44 |
__asm__ ("fistl %0" : "=m"(i) : "st"(f - 0.5) ); |
45 |
return i; |
46 |
#else |
47 |
return (int) f; |
48 |
#endif // ARCH_X86 |
49 |
} |
50 |
|
51 |
/** |
52 |
* Calculates the frequency ratio for a pitch value given in cents |
53 |
* (assuming equal tempered scale of course, divided into 12 |
54 |
* semitones per octave and 100 cents per semitone). |
55 |
* |
56 |
* Note: MAX_PITCH (defined in global.h) has to be defined to an |
57 |
* appropriate value, otherwise the behavior of this function is |
58 |
* undefined, but most probably if MAX_PITCH is too small, the |
59 |
* application will crash due to segmentation fault here. |
60 |
* |
61 |
* @param cents - pitch value in cents (+1200 cents means +1 octave) |
62 |
* @returns frequency ratio (e.g. +2.0 for +1 octave) |
63 |
*/ |
64 |
inline static double CentsToFreqRatio(double Cents) { |
65 |
int index_int = DoubleToInt(Cents); // integer index |
66 |
float index_fract = Cents - index_int; // fractional part of index |
67 |
return pCentsToFreqTable[index_int] + index_fract * (pCentsToFreqTable[index_int+1] - pCentsToFreqTable[index_int]); |
68 |
} |
69 |
|
70 |
template<class T_a, class T_b> inline static T_a Min(T_a a, T_b b) { |
71 |
return (b < a) ? b : a; |
72 |
} |
73 |
|
74 |
template<class T_a, class T_b> inline static T_a Max(T_a a, T_b b) { |
75 |
return (b > a) ? b : a; |
76 |
} |
77 |
private: |
78 |
static float CentsToFreqTable[MAX_PITCH * 1200 * 2 + 1]; |
79 |
static float* pCentsToFreqTable; |
80 |
|
81 |
static float* InitCentsToFreqTable(); |
82 |
}; |
83 |
|
84 |
#endif // __RT_MATH_H__ |