Krotos Modules 3
Loading...
Searching...
No Matches
OscillatorUtils.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 OscillatorUtils.h
5 Author: sandy
6
7 ==============================================================================
8*/
9
10#pragma once
11
12#include <algorithm>
13#include <vector>
14
15namespace krotos
16{
17 // TODO: might be better not to have these be global and have them stored together in a class/namespace
18 // In Juce, MIDI notes are passed around as 32 bit ints
19 // We take advantage of the fact that only the lowest 7 bits are required to represent the MIDI note number ...
20 // ... and use some of the remaining 25 bits to pass some flags along with the note message
21 static const int MIDI_NOTE_MASK{0x7f}; // This masks the actual MIDI note number
22 static const int MIDI_NOTE_FLAG_DRONE{0x80}; // Bit is set if the note event is a drone
23 static const int MIDI_NOTE_FLAG_DRONE_NOTEUP{0x100}; // Bit is set if the note event is a drone note-up
24 static const int MIDI_NOTE_FLAG_FLUSH{0x200}; // Bit is set if the note event is a note flush event
25 static const int MIDI_NOTE_FLAG_NOR{0x400}; // Bit is set if the note event is a Note On Release event
26 static const int MIDI_NOTE_NUMBER_C3{60};
27 static const int MIDI_NOTE_NUMBER_CS3{61};
28 static const int MIDI_NOTE_NUMBER_D3{62};
29 static const int MIDI_NOTE_NUMBER_DS3{63};
30 static const int MIDI_NOTE_NUMBER_E3{64};
31
32 static const int NUM_VOICES_POLY = 8;
33 static const int NUM_VOICES_DRONE = 1;
35
36 static const int SEMITONES_PER_OCTAVE{12};
37
38 class Grain
39 {
40 public:
45 void setLifetime(int grainPlaybackLength);
46
47 // Used within playback engine and for display
57 float mix{1.f}; // mix positions between flavours (percentage)
58 int playHeadStart{0}; // start of grain (samples)
59
60 float grainSize{0.f}; // The size in samples of the grain as calculated by analysis
61 int length{0}; // Compensated length of grain in samples - how many will we actually play back
62 float grainWindow{0.f}; // playback position in grain (percentage)
63 float envelope{0.f}; // amplitude of grain "shape" (percentage)
64
65 double playHead{0.0};
66 int segmentIndex{0}; // Keep a copy of which segment index we are playing
67 float resampleRatio{1.f};
68 float pitchRand{0.01f};
69 float gainRand{1.0f};
70 float panRand{0.5f};
71 float widthRand{0.5f};
72 float ramp{0.0f}; // Ramp value for anti-click ramp when looping
73 bool isPlaying{false};
74 float windowDelta{0.f};
75 int voiceIndex{-1};
76 float gainMod{0.f}; // Audio ADSR output value used in the grain display
77 float pitchMod{0.f}; // Pitch ADSR output value used in the grain display
78
79 float principalX{0.f}; // ML principals
80 float principalY{0.f};
81 float principalZ{0.f};
82 float principalQ{0.f};
83
84 private:
85 friend class SampleEngine;
86 };
87
89 {
90 public:
93
94 inline void nextPhase();
95 inline float getPhase();
96 inline float getDelta();
97
98 inline void synchronise(double& grainPhase)
99 {
100 int grain = int(grainPhase);
101 if (m_newCycle)
102 {
103 grain++;
104 }
105 grainPhase = double(grain) + m_phase;
106 }
107
108 inline void setPhaseDelta(float val);
109 inline void setLoopSize(int val);
110 inline void setLoopSize2(int val);
111 inline void setFrequency(float val);
112 inline float getFrequency();
113 inline void setPhase(float val);
114 inline void triggerNewGrain();
115 inline void mixPhase(float val);
116
117 inline bool hasWrapped();
118 inline bool hasLooped();
119 inline bool hasLooped2();
120 inline void setSampleRate(float val);
121
122 private:
123 volatile float m_phase{0.0f};
124 volatile float m_delta{0.1f};
125 volatile bool m_newCycle{false};
126
127 volatile int m_loopPhaseCounter{0};
128 volatile int m_loopSize{1};
129 volatile bool m_newLoop{false};
130
131 volatile int m_loopPhaseCounter2{0};
132 volatile int m_loopSize2{1};
133 volatile bool m_newLoop2{false};
134
135 float m_sampleRate{48000.0f};
137 float m_frequency{1.0f};
138 };
139
141 {
142 public:
145
146 inline void setRange(int val);
147 inline bool randomiseIf(bool enable);
148 inline int getValue();
149
150 private:
151 int m_value{0};
152 int m_range{0};
153 };
154
156 {
157 public:
160
161 inline float processSample(float sample); // Inline for speed
162 inline float getRamp();
163 inline void setLength(float val);
164 inline void trigger();
165 inline bool triggerIf(bool enable);
166 inline bool isActive();
167
168 private:
169 float m_ramp{0.0f};
170 float m_delta{0.0f};
171 float m_previousSample{0.0f};
172 float m_length{20.0f};
173 bool m_triggered{false};
174 };
175
176 template <class T> class WatchValue
177 {
178 public:
181
182 inline void setValue(T val)
183 {
184 m_hasChanged = val != m_value;
185 m_value = val;
186 }
187
188 inline T getValue() { return m_value; }
189
190 inline bool hasChanged() { return m_hasChanged; }
191
192 private:
193 bool m_hasChanged{false};
195 };
196
198 {
199 public:
202
203 inline float processSample(float sample); // Inline for speed
204 inline float processSample(float sample, bool enable); // Inline for speed
205 void setMaxSlewRate(float val);
206 void setMaxSlewRates(float valPosative, float valNegative);
207
208 private:
209 float m_maxSlewPosative{100.0f};
210 float m_maxSlewNegative{-100.0f};
211 float m_previousSample{0.0f};
212 };
213
220 {
221 public:
224
228 void process();
229
235 void prepare(double sampleRate, int samplesPerBlock);
236
241 void setInput(float val);
242
247 float getOutput();
248
249 private:
250 double m_sampleRate{0.0};
252 float m_blockRateHZ{0.f};
253 float m_input{0.0f};
254 float m_previousInput{0.0f};
255 float m_velocity{0.0f};
258 // VELOCITY_LP_FILTER_HZ defines the cut-off frequency of the low pass filter applied to puck X and Y movement.
259 // Mouse driven parameters are very noisy, this value needs to balance effective noise filtering against
260 // 'sluggish' response to mouse movement.
261 const float VELOCITY_LP_FILTER_HZ{4.5f};
262 // We don't need a "peaky" filter so Q left at 1
263 const float VELOCITY_LP_FILTER_Q{1.f};
264 // CALIBRATION_FACTOR defines how sensitive the velocity modulator is to movement of the puck. Larger means more
265 // sensitive. It has been experimentally chosen to 'feel' responsive. If anything, it is set slightly 'hot'
266 // because it can be toned down using the modulator amount slider
267 const float CALIBRATION_FACTOR{0.1f};
268 dsp::IIR::Filter<float> m_smoother;
269 };
270} // namespace krotos
Definition OscillatorUtils.h:156
float m_delta
Definition OscillatorUtils.h:170
~DeClicker()
Definition OscillatorUtils.h:159
DeClicker()
Definition OscillatorUtils.h:158
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
Definition OscillatorUtils.h:39
float resampleRatio
Definition OscillatorUtils.h:67
enum krotos::Grain::Flavour vehicleMoving
int length
Definition OscillatorUtils.h:61
float principalZ
Definition OscillatorUtils.h:81
float gainRand
Definition OscillatorUtils.h:69
float principalX
Definition OscillatorUtils.h:79
float panRand
Definition OscillatorUtils.h:70
bool isPlaying
Definition OscillatorUtils.h:73
double playHead
Definition OscillatorUtils.h:65
float envelope
Definition OscillatorUtils.h:63
float windowDelta
Definition OscillatorUtils.h:74
float gainMod
Definition OscillatorUtils.h:76
float principalY
Definition OscillatorUtils.h:80
int segmentIndex
Definition OscillatorUtils.h:66
float widthRand
Definition OscillatorUtils.h:71
float pitchMod
Definition OscillatorUtils.h:77
float pitchRand
Definition OscillatorUtils.h:68
float ramp
Definition OscillatorUtils.h:72
void setLifetime(int grainPlaybackLength)
Set the approriate variables to control the lifetime of the grain.
Definition OscillatorUtils.cpp:12
float grainSize
Definition OscillatorUtils.h:60
int voiceIndex
Definition OscillatorUtils.h:75
float principalQ
Definition OscillatorUtils.h:82
Flavour
Definition OscillatorUtils.h:49
@ foreground
Definition OscillatorUtils.h:52
@ vehicleStatic
Definition OscillatorUtils.h:51
@ tagged
Definition OscillatorUtils.h:54
@ predicted
Definition OscillatorUtils.h:55
@ background
Definition OscillatorUtils.h:53
int playHeadStart
Definition OscillatorUtils.h:58
float mix
Definition OscillatorUtils.h:57
float grainWindow
Definition OscillatorUtils.h:62
A class specifically designed to extract some kind of useable velocity value from parameters whose va...
Definition OscillatorUtils.h:220
MouseVelocityExtractor()
Definition OscillatorUtils.h:222
dsp::IIR::Filter< float > m_smoother
Definition OscillatorUtils.h:268
~MouseVelocityExtractor()
Definition OscillatorUtils.h:223
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
Definition OscillatorUtils.h:89
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
PhaseGenerator()
Definition OscillatorUtils.h:91
void synchronise(double &grainPhase)
Definition OscillatorUtils.h:98
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
~PhaseGenerator()
Definition OscillatorUtils.h:92
float getDelta()
Definition OscillatorUtils.cpp:70
float getFrequency()
Definition OscillatorUtils.cpp:84
Definition OscillatorUtils.h:141
int m_range
Definition OscillatorUtils.h:152
int getValue()
Definition OscillatorUtils.cpp:131
~Randomiser()
Definition OscillatorUtils.h:144
void setRange(int val)
Definition OscillatorUtils.cpp:129
int m_value
Definition OscillatorUtils.h:151
bool randomiseIf(bool enable)
Definition OscillatorUtils.cpp:113
Randomiser()
Definition OscillatorUtils.h:143
Definition SampleEngine.h:84
Definition OscillatorUtils.h:198
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
SlewLimiter()
Definition OscillatorUtils.h:200
float m_maxSlewPosative
Definition OscillatorUtils.h:209
~SlewLimiter()
Definition OscillatorUtils.h:201
Definition SmoothedFloat.h:6
Definition OscillatorUtils.h:177
~WatchValue()
Definition OscillatorUtils.h:180
T getValue()
Definition OscillatorUtils.h:188
void setValue(T val)
Definition OscillatorUtils.h:182
T m_value
Definition OscillatorUtils.h:194
WatchValue()
Definition OscillatorUtils.h:179
bool hasChanged()
Definition OscillatorUtils.h:190
bool m_hasChanged
Definition OscillatorUtils.h:193
Definition AirAbsorptionFilter.cpp:2
static const int MIDI_NOTE_MASK
Definition OscillatorUtils.h:21
static const int NUM_VOICES_POLY
Definition OscillatorUtils.h:32
static const int NUM_VOICES_DRONE
Definition OscillatorUtils.h:33
static const int MIDI_NOTE_NUMBER_CS3
Definition OscillatorUtils.h:27
static const int MIDI_NOTE_FLAG_FLUSH
Definition OscillatorUtils.h:24
static const int MIDI_NOTE_FLAG_NOR
Definition OscillatorUtils.h:25
static const int MIDI_NOTE_NUMBER_DS3
Definition OscillatorUtils.h:29
static const int MIDI_NOTE_FLAG_DRONE_NOTEUP
Definition OscillatorUtils.h:23
static const int MIDI_NOTE_NUMBER_E3
Definition OscillatorUtils.h:30
static const int MIDI_NOTE_FLAG_DRONE
Definition OscillatorUtils.h:22
static const int SEMITONES_PER_OCTAVE
Definition OscillatorUtils.h:36
static const int MIDI_NOTE_NUMBER_C3
Definition OscillatorUtils.h:26
static const int MIDI_NOTE_NUMBER_D3
Definition OscillatorUtils.h:28
static const int NUM_VOICES_TOTAL
Definition OscillatorUtils.h:34