forked from RebelTechnology/OwlProgram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatchProcessor.cpp
More file actions
174 lines (158 loc) · 5.46 KB
/
PatchProcessor.cpp
File metadata and controls
174 lines (158 loc) · 5.46 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "PatchProcessor.h"
#include "MemoryBuffer.hpp"
#include "device.h"
#include "main.h"
#include <string.h>
#include "ProgramVector.h"
#include "SmoothValue.h"
PatchProcessor::PatchProcessor()
: patch(NULL), bufferCount(0), parameterCount(0) {
for(int i=0; i<MAX_NUMBER_OF_PARAMETERS; ++i)
parameters[i] = NULL;
}
PatchProcessor::~PatchProcessor(){
clear();
}
void PatchProcessor::clear(){
for(int i=0; i<bufferCount; ++i){
delete buffers[i];
buffers[i] = NULL;
}
bufferCount = 0;
for(int i=0; i<parameterCount; ++i){
delete parameters[i];
parameters[i] = NULL;
}
parameterCount = 0;
delete patch;
patch = NULL;
index = -1;
// memset(parameterNames, 0, sizeof(parameterNames));
}
void PatchProcessor::setPatch(Patch* p, const char* n){
patch = p;
name = n;
}
void PatchProcessor::setParameterValues(int16_t *params){
if(getProgramVector()->hardware_version == OWL_MODULAR_HARDWARE){
for(int i=0; i<4 && i<parameterCount; ++i)
parameters[i]->update(4095 - params[i]);
for(int i=4; i<parameterCount; ++i)
parameters[i]->update(params[i]);
}else{
for(int i=0; i<parameterCount; ++i)
parameters[i]->update(params[i]);
}
}
template<typename T, typename V>
class LinearParameterUpdater : public ParameterUpdater {
private:
PatchParameter<T>* parameter;
T minimum;
T maximum;
V value;
public:
LinearParameterUpdater(T min, T max, V initialValue)
: parameter(NULL), minimum(min), maximum(max), value(initialValue) {}
void update(int16_t newValue){
value = (newValue*(maximum-minimum))/4096+minimum;
if(parameter != NULL)
parameter->update((T)value);
}
void setParameter(PatchParameter<T>* p){
parameter = p;
}
};
// void setSkew(float mid){
// if (maximum > minimum)
// skew = log (0.5) / log ((mid - minimum) / (maximum - minimum));
// }
template<typename T, typename V>
class ExponentialParameterUpdater : public ParameterUpdater {
private:
PatchParameter<T>* parameter;
float skew;
T minimum;
T maximum;
V value;
public:
ExponentialParameterUpdater(float skw, T min, T max, V initialValue)
: parameter(NULL), skew(skw), minimum(min), maximum(max), value(initialValue) {
// ASSERT(skew > 0.0 && skew <= 2.0, "Invalid exponential skew value");
ASSERT(skew > 0.0, "Invalid exponential skew value");
}
void update(int16_t newValue){
float v = newValue/4096.0f;
v = expf(logf(v)/skew);
value = v*(maximum-minimum)+minimum;
if(parameter != NULL)
parameter->update((T)value);
}
void setParameter(PatchParameter<T>* p){
parameter = p;
}
};
double PatchProcessor::getSampleRate(){
return getProgramVector()->audio_samplingrate;
}
int PatchProcessor::getBlockSize(){
return getProgramVector()->audio_blocksize;
}
void PatchProcessor::setDefaultValue(int pid, float value){
doSetPatchParameter(pid, value*4096);
}
void PatchProcessor::setDefaultValue(int pid, int value){
doSetPatchParameter(pid, value);
}
template<typename T>
PatchParameter<T> PatchProcessor::getParameter(const char* name, T min, T max, T defaultValue, float lambda, float delta, float skew){
int pid = 0;
int blocksize = getBlockSize();
if(parameterCount < MAX_NUMBER_OF_PARAMETERS &&
parameterCount < getProgramVector()->parameters_size){
pid = parameterCount++;
if(getProgramVector()->registerPatchParameter != NULL)
getProgramVector()->registerPatchParameter(pid, name);
setDefaultValue(pid, defaultValue);
if(parameters[pid] != NULL)
delete parameters[pid];
ParameterUpdater* updater = NULL;
T l = SmoothValue<T>::normal(lambda, blocksize);
T d = StiffValue<T>::normal(delta)*abs(max-min);
if(skew == 1.0){
if(lambda == 0.0 && delta == 0.0){
updater = new LinearParameterUpdater<T, T>(min, max, defaultValue);
}else if(delta == 0.0){
updater = new LinearParameterUpdater<T, SmoothValue<T> >(min, max, SmoothValue<T>(l, defaultValue));
}else if(lambda == 0.0){
updater = new LinearParameterUpdater<T, StiffValue<T> >(min, max, StiffValue<T>(d, defaultValue));
}else{
updater = new LinearParameterUpdater<T, SmoothStiffValue<T> >(min, max, SmoothStiffValue<T>(l, d, defaultValue));
}
}else{
if(lambda == 0.0 && delta == 0.0){
updater = new ExponentialParameterUpdater<T, T>(skew, min, max, defaultValue);
}else if(delta == 0.0){
updater = new ExponentialParameterUpdater<T, SmoothValue<T> >(skew, min, max, SmoothValue<T>(l, defaultValue));
}else if(lambda == 0.0){
updater = new ExponentialParameterUpdater<T, StiffValue<T> >(skew, min, max, StiffValue<T>(d, defaultValue));
}else{
updater = new ExponentialParameterUpdater<T, SmoothStiffValue<T> >(skew, min, max, SmoothStiffValue<T>(l, d, defaultValue));
}
}
parameters[pid] = updater;
}
PatchParameter<T> pp(pid);
return pp;
}
// explicit instantiation
template PatchParameter<float> PatchProcessor::getParameter(const char* name, float min, float max, float defaultValue, float lambda, float delta, float skew);
template PatchParameter<int> PatchProcessor::getParameter(const char* name, int min, int max, int defaultValue, float lambda, float delta, float skew);
void PatchProcessor::setPatchParameter(int pid, FloatParameter* param){
if(pid < parameterCount && parameters[pid] != NULL)
parameters[pid]->setParameter(param);
}
void PatchProcessor::setPatchParameter(int pid, IntParameter* param){
if(pid < parameterCount && parameters[pid] != NULL)
parameters[pid]->setParameter(param);
}