1 |
/* |
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 |
inline float currentValue() const { |
42 |
return value; |
43 |
} |
44 |
|
45 |
/** |
46 |
* Sets a new default duration for fadeTo(), which will be used if no |
47 |
* duration was passed to fadeTo() calls. |
48 |
* |
49 |
* @param defaultDuration - default duration for fadeTo() in seconds |
50 |
*/ |
51 |
void setDefaultDuration(float defaultDuration) { |
52 |
this->duration = defaultDuration; |
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 (duration <= 0.f) { |
61 |
setCurrentValue(endValue); |
62 |
return; |
63 |
} |
64 |
this->endValue = endValue; |
65 |
steps = duration * sampleRate; |
66 |
c = M_PI / duration / sampleRate; |
67 |
denormalizer = (value - endValue) * 0.5f; |
68 |
offset = endValue + denormalizer; |
69 |
real = 1; |
70 |
imag = 0; |
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 (!steps) return endValue; |
87 |
--steps; |
88 |
value = offset + real * denormalizer; |
89 |
real -= c * imag; |
90 |
imag += c * real; |
91 |
return value; |
92 |
} |
93 |
|
94 |
protected: |
95 |
int64_t steps; |
96 |
float value; |
97 |
float endValue; |
98 |
float real; |
99 |
float imag; |
100 |
float c; |
101 |
float denormalizer; |
102 |
float offset; |
103 |
float duration; |
104 |
}; |
105 |
|
106 |
} // namespace LinuxSampler |
107 |
|
108 |
#endif // LS_EASEINEASEOUT_H |