Krotos Modules 3
Loading...
Searching...
No Matches
OscillatorUtils.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 OscillatorUtils.cpp
5 Author: sandy
6
7 ==============================================================================
8*/
9
10namespace krotos
11{
12 void Grain::setLifetime(int grainPlaybackLength)
13 {
14 // For display and calculation of grain lifetime
15 length = grainPlaybackLength;
16
17 // Calculate the increment needed to take grainWindow from 0 to 1 during lifetime of grain
18 windowDelta = 1.f / static_cast<float>(grainPlaybackLength);
19
20 // Reset the window value
21 grainWindow = 0.f;
22 }
23
25 {
27 if (m_phase >= 1.0f) // Phase wrap around ?
28 {
29 m_phase -= 1.0f;
30
32 if (m_loopPhaseCounter >= m_loopSize) // Loop wrap around ?
33 {
35 m_newLoop = true;
36 }
37 else
38 {
39 m_newLoop = false;
40 }
41
43 if (m_loopPhaseCounter2 >= m_loopSize2) // Loop wrap around ?
44 {
46 m_newLoop2 = true;
47 }
48 else
49 {
50 m_newLoop2 = false;
51 }
52
53 m_newCycle = true;
54 }
55 else if (m_phase < 0.0f) // Wrap around backwards ?
56 {
57 m_phase += 1.0f;
58 m_newCycle = false;
59 }
60 else
61 {
62 m_newCycle = false;
63 m_newLoop = false;
64 m_newLoop2 = false;
65 }
66 }
67
68 inline float PhaseGenerator::getPhase() { return m_phase; }
69
70 inline float PhaseGenerator::getDelta() { return m_delta; }
71
72 inline void PhaseGenerator::setPhaseDelta(float val) { m_delta = val; }
73
74 inline void PhaseGenerator::setLoopSize(int val) { m_loopSize = val; }
75
76 inline void PhaseGenerator::setLoopSize2(int val) { m_loopSize2 = val; }
77
78 inline void PhaseGenerator::setFrequency(float val)
79 {
81 m_frequency = val;
82 }
83
84 inline float PhaseGenerator::getFrequency() { return m_frequency; }
85
86 inline void PhaseGenerator::setPhase(float val)
87 {
88 m_phase = val;
89 m_newCycle = true;
90 }
91
93 {
94 setPhase(1.0f); // Reset grain timer to trigger a wraparound, and new grain
95 }
96
97 inline void PhaseGenerator::mixPhase(float val) { m_phase = (m_phase + val * 2.0f) * 0.3333333333f; }
98
99 inline bool PhaseGenerator::hasWrapped() { return m_newCycle; }
100
101 inline bool PhaseGenerator::hasLooped() { return m_newLoop; }
102
103 inline bool PhaseGenerator::hasLooped2() { return m_newLoop2; }
104
105 inline void PhaseGenerator::setSampleRate(float val)
106 {
107 m_sampleRate = val;
109 }
110
111 // Randomiser
112
113 inline bool Randomiser::randomiseIf(bool enable)
114 {
115 if (enable)
116 {
117 if (m_range >= 2)
118 {
119 m_value = (std::rand() % m_range) - m_range / 2;
120 }
121 else
122 {
123 m_value = 0;
124 }
125 }
126 return enable;
127 }
128
129 inline void Randomiser::setRange(int val) { m_range = val; }
130
131 inline int Randomiser::getValue() { return m_value; }
132
133 // DeClicker
134
135 inline float DeClicker::processSample(float sample) // Inline for speed
136 {
137 float difference = sample - m_previousSample;
138
139 if (m_triggered) // Has de-click been triggered ?
140 {
141 m_ramp = -difference;
142 m_delta = difference / m_length;
143 m_triggered = false;
144 }
145
146 float retval = sample + m_ramp;
147
148 m_ramp += m_delta;
149 if ((m_delta > 0.0f && m_ramp > 0.0f) || (m_delta < 0.0f && m_ramp < 0.0f))
150 {
151 m_ramp = m_delta = 0.0f;
152 }
153
154 m_previousSample = retval; // We keep a copy of the result for comparison next time around
155
156 return retval; // and return the result
157 }
158
159 inline float DeClicker::getRamp() // Inline for speed
160 {
161 if (m_triggered) // Has de-click been triggered ?
162 {
163 m_ramp = 1.0f;
165 m_triggered = false;
166 }
167
168 m_ramp -= m_delta;
169
170 if (m_ramp < 0.0f)
171 {
172 m_ramp = 0.0f;
173 m_delta = 0.0f;
174 }
175
176 return m_ramp; // and return the result
177 }
178
179 inline void DeClicker::setLength(float val) { m_length = val; }
180
181 inline void DeClicker::trigger() { m_triggered = true; }
182
183 inline bool DeClicker::triggerIf(bool enable)
184 {
185 if (enable)
186 {
187 m_triggered = true;
188 }
189 return enable;
190 }
191
192 inline bool DeClicker::isActive() { return (m_delta != 0.0f) || m_triggered; }
193
194 // SlewLimiter
195
196 inline float SlewLimiter::processSample(float sample) // Inline for speed
197 {
198 float difference = sample - m_previousSample;
199
200 if (difference > m_maxSlewPosative) // Are we getting bigger at a rate faster than max slew posative ?
201 {
202 sample = m_previousSample + m_maxSlewPosative; // ... if yes, we get bigger only by max slew posative
203 }
204 else if (difference <
205 m_maxSlewNegative) // Else are we getting smaller at a rate faster than max slew negative ?
206 {
207 sample = m_previousSample + m_maxSlewNegative; // ... if yes, we get smaller only by max slew negative
208 }
209
210 // Otherwise we do not change the input sample at all
211
212 m_previousSample = sample; // We keep a copy of the result for comparison next time around
213 return sample; // and return the result
214 }
215
216 inline float SlewLimiter::processSample(float sample, bool enable) // Inline for speed
217 {
218 float difference = sample - m_previousSample;
219
220 if (enable)
221 {
222 if (difference > m_maxSlewPosative) // Are we getting bigger at a rate faster than max slew posative ?
223 {
224 sample = m_previousSample + m_maxSlewPosative; // ... if yes, we get bigger only by max slew posative
225 }
226 else if (difference <
227 m_maxSlewNegative) // Else are we getting smaller at a rate faster than max slew negative ?
228 {
229 sample = m_previousSample + m_maxSlewNegative; // ... if yes, we get smaller only by max slew negative
230 }
231 }
232 // Otherwise we do not change the input sample at all
233
234 m_previousSample = sample; // We keep a copy of the result for comparison next time around
235 return sample; // and return the result
236 }
237
239 {
240 m_maxSlewPosative = val;
241 m_maxSlewNegative = -val;
242 }
243
244 void SlewLimiter::setMaxSlewRates(float valPosative, float valNegative)
245 {
246 m_maxSlewPosative = valPosative;
247 m_maxSlewNegative = valNegative;
248 }
249
251 {
252 // Everything which can be done on a per-block basis
256 }
257
258 void MouseVelocityExtractor::setInput(float val) { m_input = val; }
259
261 {
262 auto ret = jmax<float>(jmin<float>(m_smoother.processSample(m_smoothedVelocity.getSmoothedValue()), 1.f), 0.f);
263
264 if (ret == 0.f)
265 {
266 m_smoother.reset(); // Kill bounce
267 }
268 return ret;
269 }
270
271 void MouseVelocityExtractor::prepare(double sampleRate, int samplesPerBlock)
272 {
273 m_sampleRate = sampleRate;
274 m_samplesPerBlock = samplesPerBlock;
275 m_blockRateHZ = static_cast<float>(m_sampleRate) / static_cast<float>(samplesPerBlock);
277 m_smoother.coefficients =
278 dsp::IIR::Coefficients<float>::makeLowPass(sampleRate, VELOCITY_LP_FILTER_HZ, VELOCITY_LP_FILTER_Q);
279 }
280
281} // namespace krotos
float m_delta
Definition OscillatorUtils.h:170
void setLength(float val)
Definition OscillatorUtils.cpp:179
float m_previousSample
Definition OscillatorUtils.h:171
float m_ramp
Definition OscillatorUtils.h:169
void trigger()
Definition OscillatorUtils.cpp:181
bool isActive()
Definition OscillatorUtils.cpp:192
bool triggerIf(bool enable)
Definition OscillatorUtils.cpp:183
float getRamp()
Definition OscillatorUtils.cpp:159
bool m_triggered
Definition OscillatorUtils.h:173
float processSample(float sample)
Definition OscillatorUtils.cpp:135
float m_length
Definition OscillatorUtils.h:172
int length
Definition OscillatorUtils.h:61
float windowDelta
Definition OscillatorUtils.h:74
void setLifetime(int grainPlaybackLength)
Set the approriate variables to control the lifetime of the grain.
Definition OscillatorUtils.cpp:12
float grainWindow
Definition OscillatorUtils.h:62
dsp::IIR::Filter< float > m_smoother
Definition OscillatorUtils.h:268
const float CALIBRATION_FACTOR
Definition OscillatorUtils.h:267
void process()
Do the calculation.
Definition OscillatorUtils.cpp:250
float m_calibration
Definition OscillatorUtils.h:256
int m_samplesPerBlock
Definition OscillatorUtils.h:251
float getOutput()
Fetch the processed output.
Definition OscillatorUtils.cpp:260
const float VELOCITY_LP_FILTER_Q
Definition OscillatorUtils.h:263
float m_input
Definition OscillatorUtils.h:253
float m_velocity
Definition OscillatorUtils.h:255
const float VELOCITY_LP_FILTER_HZ
Definition OscillatorUtils.h:261
SmoothedFloat m_smoothedVelocity
Definition OscillatorUtils.h:257
double m_sampleRate
Definition OscillatorUtils.h:250
float m_previousInput
Definition OscillatorUtils.h:254
void prepare(double sampleRate, int samplesPerBlock)
Set the sample rate and block size.
Definition OscillatorUtils.cpp:271
void setInput(float val)
Input the value to be processed.
Definition OscillatorUtils.cpp:258
float m_blockRateHZ
Definition OscillatorUtils.h:252
float m_frequency
Definition OscillatorUtils.h:137
void setLoopSize2(int val)
Definition OscillatorUtils.cpp:76
volatile float m_delta
Definition OscillatorUtils.h:124
volatile int m_loopPhaseCounter2
Definition OscillatorUtils.h:131
volatile int m_loopSize
Definition OscillatorUtils.h:128
volatile bool m_newLoop2
Definition OscillatorUtils.h:133
float getPhase()
Definition OscillatorUtils.cpp:68
volatile float m_phase
Definition OscillatorUtils.h:123
volatile bool m_newCycle
Definition OscillatorUtils.h:125
void setSampleRate(float val)
Definition OscillatorUtils.cpp:105
bool hasLooped2()
Definition OscillatorUtils.cpp:103
void setPhaseDelta(float val)
Definition OscillatorUtils.cpp:72
volatile int m_loopPhaseCounter
Definition OscillatorUtils.h:127
volatile int m_loopSize2
Definition OscillatorUtils.h:132
float m_sampleRateReciprocal
Definition OscillatorUtils.h:136
volatile bool m_newLoop
Definition OscillatorUtils.h:129
void setPhase(float val)
Definition OscillatorUtils.cpp:86
bool hasLooped()
Definition OscillatorUtils.cpp:101
float m_sampleRate
Definition OscillatorUtils.h:135
void triggerNewGrain()
Definition OscillatorUtils.cpp:92
void mixPhase(float val)
Definition OscillatorUtils.cpp:97
void setFrequency(float val)
Definition OscillatorUtils.cpp:78
bool hasWrapped()
Definition OscillatorUtils.cpp:99
void nextPhase()
Definition OscillatorUtils.cpp:24
void setLoopSize(int val)
Definition OscillatorUtils.cpp:74
float getDelta()
Definition OscillatorUtils.cpp:70
float getFrequency()
Definition OscillatorUtils.cpp:84
int m_range
Definition OscillatorUtils.h:152
int getValue()
Definition OscillatorUtils.cpp:131
void setRange(int val)
Definition OscillatorUtils.cpp:129
int m_value
Definition OscillatorUtils.h:151
bool randomiseIf(bool enable)
Definition OscillatorUtils.cpp:113
float processSample(float sample)
Definition OscillatorUtils.cpp:196
float m_previousSample
Definition OscillatorUtils.h:211
float m_maxSlewNegative
Definition OscillatorUtils.h:210
void setMaxSlewRates(float valPosative, float valNegative)
Definition OscillatorUtils.cpp:244
void setMaxSlewRate(float val)
Definition OscillatorUtils.cpp:238
float m_maxSlewPosative
Definition OscillatorUtils.h:209
void setTarget(int blockSize, float newTarget)
sets the target with a block size
Definition SmoothedFloat.cpp:13
float getSmoothedValue()
increments the value by the required increment and gets smootedValue
Definition SmoothedFloat.cpp:5
Definition AirAbsorptionFilter.cpp:2