-
Notifications
You must be signed in to change notification settings - Fork 3
/
fredBufferedValue.h
50 lines (43 loc) · 1.01 KB
/
fredBufferedValue.h
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
#ifndef FREDBUFFEREDVALUE_H
#define FREDBUFFEREDVALUE_H
class BufferedValue{
protected:
float value = 0;
int steps = 20;
float oneMinusStep = 0.95, step = 0.05;
public:
BufferedValue(float val):value(val){
}
void update(float newValue){
value = oneMinusStep * value + step * newValue;
}
void update(float newValue, int timesteps){
if(timesteps >= steps){
value = newValue;
return;
}
if(timesteps == 1){
update(newValue);
return;
}
float step = timesteps * this->step;
float oms = 1-step;
value = oms * value + step * newValue;
}
//more steps => value changes slower
void setSteps(int steps){
this->steps = steps;
step = 1.0/steps;
oneMinusStep = 1 - step;
}
float getSteps(){
return steps;
}
void reset(float val = -1){
value = val;
}
float getValue(){
return value;
}
};
#endif