1 |
schoenebeck |
3188 |
/* |
2 |
|
|
* Copyright (c) 2017 Christian Schoenebeck |
3 |
|
|
* |
4 |
|
|
* http://www.linuxsampler.org |
5 |
|
|
* |
6 |
|
|
* This file is part of LinuxSampler and released under the same terms. |
7 |
|
|
* See README file for details. |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#ifndef LS_EASEINEASEOUT_H |
11 |
|
|
#define LS_EASEINEASEOUT_H |
12 |
|
|
|
13 |
|
|
#include <stdint.h> |
14 |
|
|
|
15 |
|
|
namespace LinuxSampler { |
16 |
|
|
|
17 |
|
|
/** @brief Value transition with ease-in and ease-out (sinusoidial) curve. |
18 |
|
|
* |
19 |
|
|
* Implements a smoothed transition from one value to another value in a |
20 |
|
|
* requested amount of time with an ease-in and ease-out algorithm, |
21 |
|
|
* based on a sinusoidial shaped curve. |
22 |
|
|
*/ |
23 |
|
|
class EaseInEaseOut { |
24 |
|
|
public: |
25 |
|
|
EaseInEaseOut() { |
26 |
|
|
steps = 0; |
27 |
|
|
value = 1.f; |
28 |
|
|
endValue = 1.f; |
29 |
|
|
duration = 1.f; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
/** |
33 |
|
|
* Set the current value @b immediately to the given value. |
34 |
|
|
*/ |
35 |
|
|
void setCurrentValue(float value) { |
36 |
|
|
this->value = value; |
37 |
|
|
endValue = value; |
38 |
|
|
steps = 0; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
/** |
42 |
|
|
* Sets a new default duration for fadeTo(), which will be used if no |
43 |
|
|
* duration was passed to fadeTo() calls. |
44 |
|
|
* |
45 |
|
|
* @param defaultDuration - default duration for fadeTo() in seconds |
46 |
|
|
*/ |
47 |
|
|
void setDefaultDuration(float defaultDuration) { |
48 |
|
|
this->duration = defaultDuration; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
/** |
52 |
|
|
* Fade the current value in @duration seconds to the new value reflected |
53 |
|
|
* by @a endValue, assuming the given @a sampleRate. |
54 |
|
|
*/ |
55 |
|
|
void fadeTo(float endValue, float duration, float sampleRate) { |
56 |
|
|
if (duration <= 0.f) { |
57 |
|
|
setCurrentValue(endValue); |
58 |
|
|
return; |
59 |
|
|
} |
60 |
|
|
this->endValue = endValue; |
61 |
|
|
steps = duration * sampleRate; |
62 |
|
|
c = M_PI / duration / sampleRate; |
63 |
|
|
denormalizer = (value - endValue) * 0.5f; |
64 |
|
|
offset = endValue + denormalizer; |
65 |
|
|
real = 1; |
66 |
|
|
imag = 0; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
/** |
70 |
|
|
* Fade the current value in the currently set default duration to the new |
71 |
|
|
* value reflected by @a endValue, assuming the given @a sampleRate. |
72 |
|
|
*/ |
73 |
|
|
void fadeTo(float endValue, float sampleRate) { |
74 |
|
|
fadeTo(endValue, duration, sampleRate); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
/** |
78 |
|
|
* Proceed transition by exactly one sample point and return the current |
79 |
|
|
* smoothed value at this stage. |
80 |
|
|
*/ |
81 |
|
|
inline float render() { |
82 |
|
|
if (!steps) return endValue; |
83 |
|
|
--steps; |
84 |
|
|
value = offset + real * denormalizer; |
85 |
|
|
real -= c * imag; |
86 |
|
|
imag += c * real; |
87 |
|
|
return value; |
88 |
|
|
} |
89 |
|
|
|
90 |
schoenebeck |
3246 |
protected: |
91 |
schoenebeck |
3188 |
int64_t steps; |
92 |
|
|
float value; |
93 |
|
|
float endValue; |
94 |
|
|
float real; |
95 |
|
|
float imag; |
96 |
|
|
float c; |
97 |
|
|
float denormalizer; |
98 |
|
|
float offset; |
99 |
|
|
float duration; |
100 |
|
|
}; |
101 |
|
|
|
102 |
|
|
} // namespace LinuxSampler |
103 |
|
|
|
104 |
|
|
#endif // LS_EASEINEASEOUT_H |