LMMS
Loading...
Searching...
No Matches
inertia.h
Go to the documentation of this file.
1/* Calf DSP Library
2 * Basic "inertia" (parameter smoothing) classes.
3 * Copyright (C) 2001-2007 Krzysztof Foltman
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02111-1307, USA.
19 */
20#ifndef __CALF_INERTIA_H
21#define __CALF_INERTIA_H
22
23#include "primitives.h"
24
25namespace dsp {
26
29{
30public:
32 float mul, delta;
33public:
35 linear_ramp(int _ramp_len) {
36 ramp_len = _ramp_len;
37 mul = (float)(1.0f / ramp_len);
38 delta = 0.f;
39 }
40
41 inline void set_length(int _ramp_len) {
42 ramp_len = _ramp_len;
43 mul = (float)(1.0f / ramp_len);
44 }
45 inline int length()
46 {
47 return ramp_len;
48 }
49 inline void start_ramp(float start, float end)
50 {
51 delta = mul * (end - start);
52 }
53
54 inline float ramp(float value)
55 {
56 return value + delta;
57 }
58
59 inline float ramp_many(float value, int count)
60 {
61 return value + delta * count;
62 }
63};
64
67{
68public:
70 float root, delta;
71public:
72 exponential_ramp(int _ramp_len) {
73 ramp_len = _ramp_len;
74 root = (float)(1.0f / ramp_len);
75 delta = 1.0;
76 }
77 inline void set_length(int _ramp_len) {
78 ramp_len = _ramp_len;
79 root = (float)(1.0f / ramp_len);
80 }
81 inline int length()
82 {
83 return ramp_len;
84 }
85 inline void start_ramp(float start, float end)
86 {
87 delta = pow(end / start, root);
88 }
89
90 inline float ramp(float value)
91 {
92 return value * delta;
93 }
94
95 inline float ramp_many(float value, float count)
96 {
97 return value * pow(delta, count);
98 }
99};
100
106template<class Ramp>
108{
109public:
111 float value;
112 unsigned int count;
113 Ramp ramp;
114
115public:
116 inertia(const Ramp &_ramp, float init_value = 0.f)
117 : ramp(_ramp)
118 {
119 value = old_value = init_value;
120 count = 0;
121 }
122
123 void set_now(float _value)
124 {
125 value = old_value = _value;
126 count = 0;
127 }
128
129 void set_inertia(float source)
130 {
131 if (source != old_value) {
132 ramp.start_ramp(value, source);
133 count = ramp.length();
134 old_value = source;
135 }
136 }
137
138 inline float get(float source)
139 {
140 if (source != old_value) {
141 ramp.start_ramp(value, source);
142 count = ramp.length();
143 old_value = source;
144 }
145 if (!count)
146 return old_value;
147 value = ramp.ramp(value);
148 count--;
149 if (!count) // finished ramping, set to desired value to get rid of accumulated rounding errors
151 return value;
152 }
153
154 inline float get()
155 {
156 if (!count)
157 return old_value;
158 value = ramp.ramp(value);
159 count--;
160 if (!count) // finished ramping, set to desired value to get rid of accumulated rounding errors
162 return value;
163 }
164
165 inline void step()
166 {
167 if (count) {
168 value = ramp.ramp(value);
169 count--;
170 if (!count) // finished ramping, set to desired value to get rid of accumulated rounding errors
172 }
173 }
174
175 inline void step_many(unsigned int steps)
176 {
177 if (steps < count) {
178 // Skip only a part of the current ramping period
179 value = ramp.ramp_many(value, steps);
180 count -= steps;
181 if (!count) // finished ramping, set to desired value to get rid of accumulated rounding errors
183 }
184 else
185 {
186 // The whole ramping period has been skipped, just go to destination
188 count = 0;
189 }
190 }
191
192 inline float get_last() const
193 {
194 return value;
195 }
196
197 inline bool active() const
198 {
199 return count > 0;
200 }
201};
202
204{
205public:
206 unsigned int frequency;
207 unsigned int left;
208public:
209 once_per_n(unsigned int _frequency)
210 : frequency(_frequency), left(_frequency)
211 {}
212 inline void start()
213 {
214 left = frequency;
215 }
216
217 inline void signal()
218 {
219 left = 0;
220 }
221 inline unsigned int get(unsigned int desired)
222 {
223 if (desired > left) {
224 desired = left;
225 left = 0;
226 return desired;
227 }
228 left -= desired;
229 return desired;
230 }
231 inline bool elapsed()
232 {
233 if (!left) {
234 left = frequency;
235 return true;
236 }
237 return false;
238 }
239};
240
241class gain_smoothing: public inertia<linear_ramp>
242{
243public:
248 void set_sample_rate(int sr)
249 {
250 ramp = linear_ramp(sr / 100);
251 }
252 // to change param, use set_inertia(value)
253 // to read param, use get()
254};
255
256template < typename T >
258{
259private:
263 double step;
264 double acc;
265public:
266 switcher(unsigned int samples) : cont_val_cur(), cont_val_prev()
267 {
268 step = 1.0/samples;
269 acc = 0;
270 is_active = false;
271 }
272
273 void set(T ctr)
274 {
275 cont_val_cur = ctr;
276 is_active = true;
277 }
278
279 void set_previous(T ctr)
280 {
281 cont_val_prev = ctr;
282 }
283
285 {
286 return cont_val_prev;
287 }
288
289 double get_ramp()
290 {
291 if(is_active) {
292 if(acc < 0.5) {
294 acc+= step;
295 return 1 - 2*acc;
296 }
297 else if(acc >= 0.5 && acc <= 1.0) {
300 acc+= step;
301 return (acc - 0.5)*2;
302 }
303 else if(acc > 1) {
305 acc = 0;
306 is_active = false;
307 return 1;
308 }
309 else
310 return 1;
311 }
312 else
313 return 1;
314 }
315};
316
317}
318
319#endif
int length()
Definition inertia.h:81
float root
Definition inertia.h:70
void start_ramp(float start, float end)
Definition inertia.h:85
int ramp_len
Definition inertia.h:69
void set_length(int _ramp_len)
Definition inertia.h:77
float ramp(float value)
Return value after single step.
Definition inertia.h:90
float delta
Definition inertia.h:70
exponential_ramp(int _ramp_len)
Definition inertia.h:72
float ramp_many(float value, float count)
Return value after many steps.
Definition inertia.h:95
gain_smoothing()
Definition inertia.h:244
void set_sample_rate(int sr)
Definition inertia.h:248
float value
Definition inertia.h:111
inertia(const Ramp &_ramp, float init_value=0.f)
Definition inertia.h:116
bool active() const
Is it still ramping?
Definition inertia.h:197
void step_many(unsigned int steps)
Do many inertia steps, without returning the new value and without changing destination value.
Definition inertia.h:175
float get(float source)
Get smoothed value of given source value.
Definition inertia.h:138
float old_value
Definition inertia.h:110
float get_last() const
Get last smoothed value, without affecting anything.
Definition inertia.h:192
unsigned int count
Definition inertia.h:112
void set_inertia(float source)
Set with inertia.
Definition inertia.h:129
Ramp ramp
Definition inertia.h:113
void set_now(float _value)
Set value immediately (no inertia).
Definition inertia.h:123
void step()
Do one inertia step, without returning the new value and without changing destination value.
Definition inertia.h:165
float get()
Get smoothed value assuming no new input.
Definition inertia.h:154
Algorithm for a constant time linear ramp.
Definition inertia.h:29
int ramp_len
Definition inertia.h:31
float delta
Definition inertia.h:32
void start_ramp(float start, float end)
Definition inertia.h:49
float ramp(float value)
Return value after single step.
Definition inertia.h:54
void set_length(int _ramp_len)
Change ramp length.
Definition inertia.h:41
float mul
Definition inertia.h:32
int length()
Definition inertia.h:45
linear_ramp(int _ramp_len)
Construct for given ramp length.
Definition inertia.h:35
float ramp_many(float value, int count)
Return value after many steps.
Definition inertia.h:59
unsigned int frequency
Definition inertia.h:206
once_per_n(unsigned int _frequency)
Definition inertia.h:209
unsigned int left
Definition inertia.h:207
bool elapsed()
Definition inertia.h:231
void start()
Definition inertia.h:212
unsigned int get(unsigned int desired)
Definition inertia.h:221
void signal()
Set timer to "elapsed" state (elapsed() will return true during next call).
Definition inertia.h:217
T cont_val_cur
Definition inertia.h:260
void set(T ctr)
Definition inertia.h:273
double step
Definition inertia.h:263
double acc
Definition inertia.h:264
void set_previous(T ctr)
Definition inertia.h:279
double get_ramp()
Definition inertia.h:289
bool is_active
Definition inertia.h:262
switcher(unsigned int samples)
Definition inertia.h:266
T cont_val_prev
Definition inertia.h:261
T get_state()
Definition inertia.h:284
static PuglViewHint int value
Definition pugl.h:1708
virtual ASIOError start()=0
int desired
Definition jpeglib.h:1038
Definition audio_fx.h:36
_WDL_CSTRING_PREFIX void INT_PTR count
Definition wdlcstring.h:263