forked from pichenettes/eurorack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcv_scaler.h
144 lines (116 loc) · 3.94 KB
/
cv_scaler.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
// Copyright 2014 Olivier Gillet.
//
// Author: Olivier Gillet ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
//
// -----------------------------------------------------------------------------
//
// Calibration settings.
#ifndef CLOUDS_CV_SCALER_H_
#define CLOUDS_CV_SCALER_H_
#include "stmlib/stmlib.h"
#include "clouds/settings.h"
#include "clouds/drivers/adc.h"
#include "clouds/drivers/gate_input.h"
#include "clouds/dsp/parameters.h"
namespace clouds {
enum BlendParameter {
BLEND_PARAMETER_DRY_WET,
BLEND_PARAMETER_STEREO_SPREAD,
BLEND_PARAMETER_FEEDBACK,
BLEND_PARAMETER_REVERB,
BLEND_PARAMETER_LAST
};
struct CvTransformation {
bool flip;
bool remove_offset;
float filter_coefficient;
};
class CvScaler {
public:
CvScaler() { }
~CvScaler() { }
void Init(CalibrationData* calibration_data);
void Read(Parameters* parameters);
void CalibrateC1() {
cv_c1_ = adc_.float_value(ADC_V_OCT_CV);
}
void CalibrateOffsets() {
for (size_t i = 0; i < ADC_CHANNEL_LAST; ++i) {
calibration_data_->offset[i] = adc_.float_value(i);
}
}
bool CalibrateC3() {
float c3 = adc_.float_value(ADC_V_OCT_CV); // 0.4848 v0.1 ; 0.3640 v0.2
float c1 = cv_c1_; // 0.6666 v0.1 ; 0.6488 v0.2
float delta = c3 - c1;
if (delta > -0.5f && delta < -0.0f) {
calibration_data_->pitch_scale = 24.0f / (c3 - c1);
calibration_data_->pitch_offset = 12.0f - \
calibration_data_->pitch_scale * c1;
return true;
} else {
return false;
}
}
inline uint8_t adc_value(size_t index) const {
return adc_.value(index) >> 8;
}
inline bool gate(size_t index) const {
return index == 0 ? gate_input_.freeze() : gate_input_.trigger();
}
inline void set_blend_parameter(BlendParameter parameter) {
blend_parameter_ = parameter;
blend_knob_origin_ = previous_blend_knob_value_;
}
inline BlendParameter blend_parameter() const {
return blend_parameter_;
}
inline float blend_value(BlendParameter parameter) const {
return blend_[parameter];
}
inline void set_blend_value(BlendParameter parameter, float value) {
blend_[parameter] = value;
}
inline bool blend_knob_touched() const {
return blend_knob_touched_;
}
private:
void UpdateBlendParameters(float knob, float cv);
Adc adc_;
GateInput gate_input_;
CalibrationData* calibration_data_;
float smoothed_adc_value_[ADC_CHANNEL_LAST];
static CvTransformation transformations_[ADC_CHANNEL_LAST];
float note_;
BlendParameter blend_parameter_;
float blend_[BLEND_PARAMETER_LAST];
float blend_mod_[BLEND_PARAMETER_LAST];
float previous_blend_knob_value_;
float blend_knob_origin_;
float blend_knob_quantized_;
bool blend_knob_touched_;
float cv_c1_;
DISALLOW_COPY_AND_ASSIGN(CvScaler);
};
} // namespace clouds
#endif // CLOUDS_CV_SCALER_H_