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, 2006 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 * |
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 |
44 |
class RTMathBase { |
class RTMathBase { |
45 |
public: |
public: |
46 |
/** |
/** |
47 |
* Highly accurate time stamp. |
* High resolution time stamp. |
48 |
*/ |
*/ |
49 |
typedef uint32_t time_stamp_t; |
typedef uint32_t time_stamp_t; |
50 |
|
|
51 |
|
typedef uint64_t usecs_t; |
52 |
|
|
53 |
/** |
/** |
54 |
* We read the processor's cycle count register as a reference |
* We read the processor's cycle count register as a reference |
55 |
* for the real time. These are of course only abstract values |
* for the real time. These are of course only abstract values |
56 |
* with arbitrary time entity, but that's not a problem as long |
* with arbitrary time entity, but that's not a problem as long |
57 |
* as we calculate relatively. |
* as we calculate relatively. |
58 |
|
* |
59 |
|
* @see unsafeMicroSeconds() |
60 |
*/ |
*/ |
61 |
static time_stamp_t CreateTimeStamp(); |
static time_stamp_t CreateTimeStamp(); |
62 |
|
|
80 |
} |
} |
81 |
|
|
82 |
/** |
/** |
83 |
|
* Slower version of CentsToFreqRatio, for big values. |
84 |
|
* |
85 |
|
* @param cents - pitch value in cents (+1200 cents means +1 octave) |
86 |
|
* @returns frequency ratio (e.g. +2.0 for +1 octave) |
87 |
|
*/ |
88 |
|
static double CentsToFreqRatioUnlimited(double Cents) { |
89 |
|
int octaves = int(Cents / 1200); |
90 |
|
double x = CentsToFreqRatio(Cents - octaves * 1200); |
91 |
|
return octaves < 0 ? x / (1 << -octaves) : x * (1 << octaves); |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
* Inverse function to CentsToFreqRatio(). This function is a bit |
* Inverse function to CentsToFreqRatio(). This function is a bit |
96 |
* slow, so it should not be called too frequently. |
* slow, so it should not be called too frequently. |
97 |
*/ |
*/ |
99 |
return log(FreqRatio) / log(TWELVEHUNDREDTH_ROOT_OF_TWO); |
return log(FreqRatio) / log(TWELVEHUNDREDTH_ROOT_OF_TWO); |
100 |
} |
} |
101 |
|
|
102 |
|
/** |
103 |
|
* Calculates the line ratio value representation (linear scale) |
104 |
|
* of the @a decibel value provided (exponential scale). |
105 |
|
* |
106 |
|
* The context of audio acoustic sound pressure levels is assumed, and |
107 |
|
* hence the field version of the dB unit is used here (which uses a |
108 |
|
* linear factor of 20). This function is a bit slow, so it should |
109 |
|
* not be called too frequently. |
110 |
|
* |
111 |
|
* @param decibel - sound pressure level in dB |
112 |
|
* @returns linear ratio of the supplied dB value |
113 |
|
*/ |
114 |
|
static float DecibelToLinRatio(float decibel) { |
115 |
|
return powf(10.f, decibel / 20.f); |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
|
* Calculates the relatively summed average of a set of values. |
120 |
|
* |
121 |
|
* @param current - the current avaerage value of all previously summed values |
122 |
|
* @param sample - new value to be applied as summed average to the existing values |
123 |
|
* @param n - amount of sample values applied so far |
124 |
|
* @returns new average value of all summed values (including the new @a sample) |
125 |
|
*/ |
126 |
|
template<typename T_int> |
127 |
|
inline static float RelativeSummedAvg(float current, float sample, T_int n) { |
128 |
|
return current + (sample - current) / float(n); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
|
* Clock source to use for getting the current time. |
133 |
|
*/ |
134 |
|
enum clock_source_t { |
135 |
|
real_clock, ///< Use this to measure time that passed in reality (no matter if process got suspended). |
136 |
|
process_clock, ///< Use this to measure only the CPU execution time of the current process (if the process got suspended, the clock is paused as well). |
137 |
|
thread_clock, ///< Use this to measure only the CPU execution time of the current thread (if the process got suspended or another thread is executed, the clock is paused as well). |
138 |
|
}; |
139 |
|
|
140 |
|
/** |
141 |
|
* Returns a time stamp of the current time in microseconds (in |
142 |
|
* probably real-time @b unsafe way). There is no guarantee about |
143 |
|
* what the returned amount of microseconds relates to (i.e. |
144 |
|
* microseconds since epoch, microseconds since system uptime, ...). |
145 |
|
* So you should only use it to calculate time differences between |
146 |
|
* values taken with this method. |
147 |
|
* |
148 |
|
* @b CAUTION: This method may not @b NOT be real-time safe! On some |
149 |
|
* systems it could be RT safe, but there is no guarantee whatsoever! |
150 |
|
* So this method should only be used for debugging, benchmarking and |
151 |
|
* other developing purposes ! |
152 |
|
* |
153 |
|
* For creating time stamps in real-time context, use |
154 |
|
* CreateTimeStamp() instead. |
155 |
|
* |
156 |
|
* @param source - the actual clock to use for getting the current |
157 |
|
* time, note that the various clock sources may not |
158 |
|
* be implemented on all systems |
159 |
|
* @returns time stamp in microseconds |
160 |
|
* |
161 |
|
* @see CreateTimeStamp() |
162 |
|
*/ |
163 |
|
static usecs_t unsafeMicroSeconds(clock_source_t source); |
164 |
|
|
165 |
private: |
private: |
|
static float CentsToFreqTable[CONFIG_MAX_PITCH * 1200 * 2 + 1]; |
|
166 |
static float* pCentsToFreqTable; |
static float* pCentsToFreqTable; |
167 |
|
|
168 |
static float* InitCentsToFreqTable(); |
static float* InitCentsToFreqTable(); |