-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmoothParam.h
More file actions
93 lines (68 loc) · 1.91 KB
/
Copy pathSmoothParam.h
File metadata and controls
93 lines (68 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
==============================================================================
Author: Benjamin Quiedeville
==============================================================================
*/
#ifndef PARAM_SMOOTHER_H
#define PARAM_SMOOTHER_H
#include <math.h>
#include <cstdint>
#include "OnepoleFilter.h"
#define SMOOTH_PARAM_TIME 0.02
struct SmoothParamLinear {
void init(double initValue) {
target = initValue;
currentValue = initValue;
prevTarget = target;
normValue = 0.0;
isSmoothing = false;
}
void newTarget(double newTarget, double rampTimeMs, double samplerate) {
prevTarget = target;
target = newTarget;
stepHeight = 1.0 / (rampTimeMs * 0.001 * samplerate);
isSmoothing = true;
}
double nextValue() {
if (!isSmoothing) { return target; }
normValue += stepHeight;
if (normValue >= 1.0) {
isSmoothing = false;
normValue = 1.0;
currentValue = target;
return currentValue;
}
currentValue = normValue * (target - prevTarget) + prevTarget;
return currentValue;
}
double target;
double prevTarget;
double stepHeight;
double normValue;
double currentValue;
bool isSmoothing;
};
struct SmoothParamIIR {
void init(double initValue) {
currentValue = initValue;
target = initValue;
y1 = initValue;
b0 = 1.0;
a1 = 0.0;
}
void newTarget(double newTarget, double tauMs, double samplerate) {
b0 = std::sin(M_PI / (samplerate * tauMs * 0.001));
a1 = b0 - 1.0;
target = newTarget;
}
double nextValue() {
currentValue = target * b0 - y1 * a1;
y1 = currentValue;
return currentValue;
}
double currentValue;
double target;
double b0, a1;
double y1;
};
#endif // PARAM_SMOOTHER_H