1 |
schoenebeck |
3246 |
/* |
2 |
schoenebeck |
3561 |
* Copyright (c) 2017 - 2019 Christian Schoenebeck |
3 |
schoenebeck |
3246 |
* |
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_FADE_H |
11 |
|
|
#define LS_FADE_H |
12 |
|
|
|
13 |
|
|
#include "EaseInEaseOut.h" |
14 |
|
|
|
15 |
|
|
namespace LinuxSampler { |
16 |
|
|
|
17 |
|
|
#define DEFAULT_FADE_CURVE FADE_CURVE_EASE_IN_EASE_OUT |
18 |
|
|
|
19 |
|
|
enum fade_curve_t { |
20 |
|
|
FADE_CURVE_LINEAR, |
21 |
|
|
FADE_CURVE_EASE_IN_EASE_OUT, |
22 |
|
|
}; |
23 |
|
|
|
24 |
|
|
/** @brief Fades a value with a selected curve. |
25 |
|
|
* |
26 |
|
|
* Implements a smoothed transition from one value to another value in a |
27 |
|
|
* requested amount of time, either with an ease-in and ease-out algorithm |
28 |
|
|
* (default), or linear. |
29 |
|
|
*/ |
30 |
|
|
class Fade : public EaseInEaseOut { |
31 |
|
|
public: |
32 |
|
|
Fade() : EaseInEaseOut() { |
33 |
|
|
curveType = DEFAULT_FADE_CURVE; |
34 |
schoenebeck |
3561 |
Final = false; |
35 |
schoenebeck |
3246 |
} |
36 |
|
|
|
37 |
|
|
/** |
38 |
|
|
* Select another curve type for this Fade and immediately prepare this |
39 |
|
|
* Fade for operation with the new curve type. |
40 |
|
|
*/ |
41 |
|
|
void setCurve(fade_curve_t curve, float sampleRate) { |
42 |
|
|
curveType = curve; |
43 |
|
|
fadeTo(endValue, duration, sampleRate); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
/** |
47 |
|
|
* Only set the new curve type. It is your responsibility to prepare this |
48 |
|
|
* Fade for subsequent operation, i.e. by calling fadeTo() afterwards, |
49 |
|
|
* otherwise the result of render() calls will be undefined. |
50 |
|
|
*/ |
51 |
|
|
void setCurveOnly(fade_curve_t curve) { |
52 |
|
|
curveType = curve; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* Fade the current value in @duration seconds to the new value reflected |
57 |
|
|
* by @a endValue, assuming the given @a sampleRate. |
58 |
|
|
*/ |
59 |
|
|
void fadeTo(float endValue, float duration, float sampleRate) { |
60 |
|
|
if (curveType == FADE_CURVE_EASE_IN_EASE_OUT) |
61 |
|
|
return EaseInEaseOut::fadeTo(endValue, duration, sampleRate); |
62 |
|
|
else { |
63 |
|
|
if (duration <= 0.f) { |
64 |
|
|
setCurrentValue(endValue); |
65 |
|
|
return; |
66 |
|
|
} |
67 |
|
|
this->endValue = endValue; |
68 |
|
|
steps = duration * sampleRate; |
69 |
|
|
c = (endValue - value) / float(steps); |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* Fade the current value in the currently set default duration to the new |
75 |
|
|
* value reflected by @a endValue, assuming the given @a sampleRate. |
76 |
|
|
*/ |
77 |
|
|
void fadeTo(float endValue, float sampleRate) { |
78 |
|
|
fadeTo(endValue, duration, sampleRate); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
/** |
82 |
|
|
* Proceed transition by exactly one sample point and return the current |
83 |
|
|
* smoothed value at this stage. |
84 |
|
|
*/ |
85 |
|
|
inline float render() { |
86 |
|
|
if (curveType == FADE_CURVE_EASE_IN_EASE_OUT) |
87 |
|
|
return EaseInEaseOut::render(); |
88 |
|
|
if (!steps) return endValue; |
89 |
|
|
--steps; |
90 |
|
|
value += c; |
91 |
|
|
return value; |
92 |
|
|
} |
93 |
|
|
|
94 |
schoenebeck |
3561 |
inline void setFinal(bool b) { |
95 |
|
|
Final = b; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
template<typename T> |
99 |
schoenebeck |
3564 |
inline void renderApplyTo(T& dst) { |
100 |
schoenebeck |
3561 |
if (Final) |
101 |
schoenebeck |
3564 |
dst = render(); |
102 |
|
|
else |
103 |
|
|
dst *= render(); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
template<typename T> |
107 |
|
|
inline void applyCurrentValueTo(T& dst) { |
108 |
|
|
if (Final) |
109 |
schoenebeck |
3561 |
dst = currentValue(); |
110 |
|
|
else |
111 |
|
|
dst *= currentValue(); |
112 |
|
|
} |
113 |
|
|
|
114 |
schoenebeck |
3246 |
private: |
115 |
|
|
fade_curve_t curveType; |
116 |
schoenebeck |
3561 |
bool Final; |
117 |
schoenebeck |
3246 |
}; |
118 |
|
|
|
119 |
|
|
} // namespace LinuxSampler |
120 |
|
|
|
121 |
|
|
#endif // LS_FADE_H |