-
Notifications
You must be signed in to change notification settings - Fork 4
/
PagedParam.h
151 lines (131 loc) · 3.18 KB
/
PagedParam.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#pragma once
#define CC_TO_VAL(x, min, max) (min + (x / 127.0f) * (max - min))
// expect a range of -1 to 1
#define PB_TO_VAL(x) (x / 8191.0f)
// All in values are expected to be in the 0 to 1 range
// cur_val is stored as a range between 0 and 1
class PagedParam
{
public:
PagedParam() {}
~PagedParam() {}
void Init(uint8_t page, float init, float min, float max, float thresh)
{
page_ = page;
thresh_ = thresh;
locked_ = true;
midi_locked_ = true;
changed_ = false;
min_ = min;
max_ = max;
range_ = max_ - min_;
init_val_ = (init - min_) / range_;
cur_val_ = init_val_;
}
void SetRange(float min, float max)
{
min_ = min;
max_ = max;
range_ = max - min;
}
// val is the user value of the param ie, between min and max
void Lock(float val)
{
locked_ = true;
midi_locked_ = true;
cur_val_ = (val - min_) / range_;
}
// val is the user value of the param ie, between min and max
void Set(float val)
{
locked_ = true;
cur_val_ = (val - min_) / range_;
}
// val is a raw value of the param i.e. between 0 and 1
void RawLock(float val)
{
locked_ = true;
midi_locked_ = true;
cur_val_ = fminf(1.0f, fmaxf(0.0f, val));
}
// val is a raw value of the param i.e. between 0 and 1
void RawSet(float val)
{
locked_ = true;
cur_val_ = fminf(1.0f, fmaxf(0.0f, val));
}
float Process(float in, uint8_t current_page)
{
if (current_page == page_) {
if (locked_) {
if (abs(in - cur_val_) < thresh_) {
changed_ = true;
locked_ = false;
midi_locked_ = true;
cur_val_ = in;
}
} else {
if (in != cur_val_) {
changed_ = true;
cur_val_ = in;
} else {
changed_ = false;
}
}
} else {
changed_ = false;
locked_ = true;
}
return fminf(max_, fmaxf(min_, (min_ + cur_val_ * range_)));
}
inline bool Changed()
{
return changed_;
}
/*
* Midi input will change the current value once it gets within the threshold, regardless of the page
* It will then lock the value to prevent the physical controls from overriding the midi message.
*
*/
void MidiCCIn(uint8_t midi_byte)
{
float in = CC_TO_VAL(midi_byte, 0.0f, 1.0f);
if (midi_locked_) {
if (abs(in - cur_val_) < thresh_) {
midi_locked_ = false;
locked_ = true;
changed_ = true;
cur_val_ = in;
}
} else {
if (in != cur_val_) {
changed_ = true;
cur_val_ = in;
} else {
changed_ = false;
}
}
}
/*
* As above but for we ignore midi locks
*/
void MidiPBIn(int16_t midi_data)
{
float in = PB_TO_VAL(midi_data);
changed_ = true;
locked_ = true;
if (in < 0) {
cur_val_ = init_val_ + (in * init_val_);
} else {
cur_val_ = init_val_ + (in * (1.0f - init_val_));
}
}
float CurVal()
{
return cur_val_;
}
private:
bool locked_, midi_locked_, changed_;
float thresh_, init_val_, cur_val_, min_, max_, range_;
uint8_t page_;
};