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 * |
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 * |
25 |
#define __RT_MATH_H__ |
#define __RT_MATH_H__ |
26 |
|
|
27 |
#include <math.h> |
#include <math.h> |
28 |
|
#include <stdint.h> |
29 |
#include "global.h" |
#include "global.h" |
30 |
|
|
31 |
/// Needed for calculating frequency ratio used to pitch a sample |
/// Needed for calculating frequency ratio used to pitch a sample |
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 |
/** |
/** |
47 |
|
* Highly accurate time stamp. |
48 |
|
*/ |
49 |
|
typedef uint32_t time_stamp_t; |
50 |
|
|
51 |
|
/** |
52 |
|
* We read the processor's cycle count register as a reference |
53 |
|
* for the real time. These are of course only abstract values |
54 |
|
* with arbitrary time entity, but that's not a problem as long |
55 |
|
* as we calculate relatively. |
56 |
|
*/ |
57 |
|
static time_stamp_t CreateTimeStamp(); |
58 |
|
|
59 |
|
/** |
60 |
* Calculates the frequency ratio for a pitch value given in cents |
* Calculates the frequency ratio for a pitch value given in cents |
61 |
* (assuming equal tempered scale of course, divided into 12 |
* (assuming equal tempered scale of course, divided into 12 |
62 |
* semitones per octave and 100 cents per semitone). |
* semitones per octave and 100 cents per semitone). |
63 |
* |
* |
64 |
* Note: MAX_PITCH (defined in global.h) has to be defined to an |
* Note: CONFIG_MAX_PITCH (defined in config.h) has to be defined to an |
65 |
* appropriate value, otherwise the behavior of this function is |
* appropriate value, otherwise the behavior of this function is |
66 |
* undefined, but most probably if MAX_PITCH is too small, the |
* undefined, but most probably if CONFIG_MAX_PITCH is too small, the |
67 |
* application will crash due to segmentation fault here. |
* application will crash due to segmentation fault here. |
68 |
* |
* |
69 |
* @param cents - pitch value in cents (+1200 cents means +1 octave) |
* @param cents - pitch value in cents (+1200 cents means +1 octave) |
76 |
} |
} |
77 |
|
|
78 |
private: |
private: |
79 |
static float CentsToFreqTable[MAX_PITCH * 1200 * 2 + 1]; |
static float CentsToFreqTable[CONFIG_MAX_PITCH * 1200 * 2 + 1]; |
80 |
static float* pCentsToFreqTable; |
static float* pCentsToFreqTable; |
81 |
|
|
82 |
static float* InitCentsToFreqTable(); |
static float* InitCentsToFreqTable(); |
83 |
}; |
}; |
84 |
|
|
85 |
/** Real Time Math |
/** @brief Real Time Math |
86 |
* |
* |
87 |
* Math functions for real time operation. |
* This is a template which provides customized methods for the desired low |
88 |
|
* level implementation. The ASM_X86_MMX_SSE implementation of each method |
89 |
|
* for example doesn't use 387 FPU instruction. This is needed for MMX |
90 |
|
* algorithms which do not allow mixed MMX and 387 instructions. |
91 |
*/ |
*/ |
92 |
template<implementation_t IMPL = CPP> |
template<implementation_t IMPL = CPP> |
93 |
class __RTMath : public RTMathBase { |
class __RTMath : public RTMathBase { |
95 |
// conversion using truncate |
// conversion using truncate |
96 |
inline static int Int(const float a) { |
inline static int Int(const float a) { |
97 |
switch (IMPL) { |
switch (IMPL) { |
98 |
case CPP: { |
#if CONFIG_ASM && ARCH_X86 |
|
return (int) a; |
|
|
} |
|
99 |
case ASM_X86_MMX_SSE: { |
case ASM_X86_MMX_SSE: { |
100 |
int ret; |
int ret; |
101 |
asm ( |
asm ( |
105 |
); |
); |
106 |
return ret; |
return ret; |
107 |
} |
} |
108 |
|
#endif // CONFIG_ASM && ARCH_X86 |
109 |
|
default: { |
110 |
|
return (int) a; |
111 |
|
} |
112 |
} |
} |
113 |
} |
} |
114 |
|
|
119 |
|
|
120 |
inline static float Float(const int a) { |
inline static float Float(const int a) { |
121 |
switch (IMPL) { |
switch (IMPL) { |
122 |
case CPP: { |
#if CONFIG_ASM && ARCH_X86 |
|
return (float) a; |
|
|
} |
|
123 |
case ASM_X86_MMX_SSE: { |
case ASM_X86_MMX_SSE: { |
124 |
float ret; |
float ret; |
125 |
asm ( |
asm ( |
130 |
); |
); |
131 |
return ret; |
return ret; |
132 |
} |
} |
133 |
|
#endif // CONFIG_ASM && ARCH_X86 |
134 |
|
default: { |
135 |
|
return (float) a; |
136 |
|
} |
137 |
} |
} |
138 |
} |
} |
139 |
|
|
146 |
|
|
147 |
inline static float Sum(const float& a, const float& b) { |
inline static float Sum(const float& a, const float& b) { |
148 |
switch (IMPL) { |
switch (IMPL) { |
149 |
case CPP: { |
#if CONFIG_ASM && ARCH_X86 |
|
return (a + b); |
|
|
} |
|
150 |
case ASM_X86_MMX_SSE: { |
case ASM_X86_MMX_SSE: { |
151 |
float ret; |
float ret; |
152 |
asm ( |
asm ( |
158 |
); |
); |
159 |
return ret; |
return ret; |
160 |
} |
} |
161 |
|
#endif // CONFIG_ASM && ARCH_X86 |
162 |
|
default: { |
163 |
|
return (a + b); |
164 |
|
} |
165 |
} |
} |
166 |
} |
} |
167 |
|
|
171 |
|
|
172 |
inline static float Sub(const float& a, const float& b) { |
inline static float Sub(const float& a, const float& b) { |
173 |
switch (IMPL) { |
switch (IMPL) { |
174 |
case CPP: { |
#if CONFIG_ASM && ARCH_X86 |
|
return (a - b); |
|
|
} |
|
175 |
case ASM_X86_MMX_SSE: { |
case ASM_X86_MMX_SSE: { |
176 |
float ret; |
float ret; |
177 |
asm ( |
asm ( |
183 |
); |
); |
184 |
return ret; |
return ret; |
185 |
} |
} |
186 |
|
#endif // CONFIG_ASM && ARCH_X86 |
187 |
|
default: { |
188 |
|
return (a - b); |
189 |
|
} |
190 |
} |
} |
191 |
} |
} |
192 |
|
|
196 |
|
|
197 |
inline static float Mul(const float a, const float b) { |
inline static float Mul(const float a, const float b) { |
198 |
switch (IMPL) { |
switch (IMPL) { |
199 |
case CPP: { |
#if CONFIG_ASM && ARCH_X86 |
|
return (a * b); |
|
|
} |
|
200 |
case ASM_X86_MMX_SSE: { |
case ASM_X86_MMX_SSE: { |
201 |
float ret; |
float ret; |
202 |
asm ( |
asm ( |
208 |
); |
); |
209 |
return ret; |
return ret; |
210 |
} |
} |
211 |
|
#endif // CONFIG_ASM && ARCH_X86 |
212 |
|
default: { |
213 |
|
return (a * b); |
214 |
|
} |
215 |
} |
} |
216 |
} |
} |
217 |
|
|
221 |
|
|
222 |
inline static float Div(const float a, const float b) { |
inline static float Div(const float a, const float b) { |
223 |
switch (IMPL) { |
switch (IMPL) { |
224 |
case CPP: { |
#if CONFIG_ASM && ARCH_X86 |
|
return (a / b); |
|
|
} |
|
225 |
case ASM_X86_MMX_SSE: { |
case ASM_X86_MMX_SSE: { |
226 |
float ret; |
float ret; |
227 |
asm ( |
asm ( |
233 |
); |
); |
234 |
return ret; |
return ret; |
235 |
} |
} |
236 |
|
#endif // CONFIG_ASM && ARCH_X86 |
237 |
|
default: { |
238 |
|
return (a / b); |
239 |
|
} |
240 |
} |
} |
241 |
} |
} |
242 |
|
|
246 |
|
|
247 |
inline static float Min(const float a, const float b) { |
inline static float Min(const float a, const float b) { |
248 |
switch (IMPL) { |
switch (IMPL) { |
249 |
case CPP: { |
#if CONFIG_ASM && ARCH_X86 |
|
return (b < a) ? b : a; |
|
|
} |
|
250 |
case ASM_X86_MMX_SSE: { |
case ASM_X86_MMX_SSE: { |
251 |
float ret; |
float ret; |
252 |
asm ( |
asm ( |
258 |
); |
); |
259 |
return ret; |
return ret; |
260 |
} |
} |
261 |
|
#endif // CONFIG_ASM && ARCH_X86 |
262 |
|
default: { |
263 |
|
return std::min(a, b); |
264 |
|
} |
265 |
} |
} |
266 |
} |
} |
267 |
|
|
271 |
|
|
272 |
inline static float Max(const float a, const float b) { |
inline static float Max(const float a, const float b) { |
273 |
switch (IMPL) { |
switch (IMPL) { |
274 |
case CPP: { |
#if CONFIG_ASM && ARCH_X86 |
|
return (b > a) ? b : a; |
|
|
} |
|
275 |
case ASM_X86_MMX_SSE: { |
case ASM_X86_MMX_SSE: { |
276 |
float ret; |
float ret; |
277 |
asm ( |
asm ( |
283 |
); |
); |
284 |
return ret; |
return ret; |
285 |
} |
} |
286 |
|
#endif // CONFIG_ASM && ARCH_X86 |
287 |
|
default: { |
288 |
|
return std::max(a, b); |
289 |
|
} |
290 |
} |
} |
291 |
} |
} |
292 |
|
|
296 |
|
|
297 |
inline static float Fmodf(const float &a, const float &b) { |
inline static float Fmodf(const float &a, const float &b) { |
298 |
switch (IMPL) { |
switch (IMPL) { |
299 |
case CPP: { |
#if CONFIG_ASM && ARCH_X86 |
|
return fmodf(a, b); |
|
|
} |
|
300 |
case ASM_X86_MMX_SSE: { |
case ASM_X86_MMX_SSE: { |
301 |
float ret; |
float ret; |
302 |
asm ( |
asm ( |
315 |
); |
); |
316 |
return ret; |
return ret; |
317 |
} |
} |
318 |
|
#endif // CONFIG_ASM && ARCH_X86 |
319 |
|
default: { |
320 |
|
return fmodf(a, b); |
321 |
|
} |
322 |
} |
} |
323 |
} |
} |
324 |
}; |
}; |