Krotos Modules 3
Loading...
Searching...
No Matches
KrotosAudioBuffer.h
Go to the documentation of this file.
1/*
2==============================================================================
3
4KrotosAudioBuffer.h
5Created: 26 June 2018 16:23:00am
6Author: sandyw
7
8==============================================================================
9*/
10
11#pragma once
12
13namespace krotos
14{
16 {
17 public:
18 // Overload + operator to add two StereoSample objects
20 {
21 StereoSample ssamp;
22 ssamp.left = this->left + b.left;
23 ssamp.right = this->right + b.right;
24 return ssamp;
25 }
26 // Overload += operator to add two StereoSample objects
28 {
29 this->left += b.left;
30 this->right += b.right;
31 return *this;
32 }
33 // Overload - operator to subtract two StereoSample objects
35 {
36 StereoSample ssamp;
37 ssamp.left = this->left - b.left;
38 ssamp.right = this->right - b.right;
39 return ssamp;
40 }
41 // Overload * operator to multiply two StereoSample objects
43 {
44 StereoSample ssamp;
45 ssamp.left = this->left * b.left;
46 ssamp.right = this->right * b.right;
47 return ssamp;
48 }
49 // Overload / operator to divide two StereoSample objects
51 {
52 StereoSample ssamp;
53 ssamp.left = this->left / b.left;
54 ssamp.right = this->right / b.right;
55 return ssamp;
56 }
57 // Overload * operator to multiply a StereoSample object by a float
58 inline StereoSample operator*(float b)
59 {
60 StereoSample ssamp;
61 ssamp.left = this->left * b;
62 ssamp.right = this->right * b;
63 return ssamp;
64 }
65 // Overload / operator to divide a StereoSample object by a float
66 inline StereoSample operator/(float b)
67 {
68 StereoSample ssamp;
69 ssamp.left = this->left / b;
70 ssamp.right = this->right / b;
71 return ssamp;
72 }
73 // Pan left/right using linear law - equal combined amplitude - this is going to audibly dip in the centre
74 inline StereoSample panLinear(float pan)
75 {
76 StereoSample ssamp;
77 ssamp.left = this->left * (1.f - pan);
78 ssamp.right = this->right * pan;
79 return ssamp;
80 }
81 // Pan left/right using square law - equal-combined power across both channels
82 inline StereoSample panSquare(float pan)
83 {
84 StereoSample ssamp;
85 ssamp.left = this->left * sqrt(1.f - pan);
86 ssamp.right = this->right * sqrt(pan);
87 return ssamp;
88 }
89 // Pan left/right using sine law - alternative equal-combined power across both channels
90 inline StereoSample panSine(float pan)
91 {
92 StereoSample ssamp;
93 ssamp.left = this->left * sinf((1.f - pan) * MathConstants<float>::halfPi);
94 ssamp.right = this->right * sinf(pan * MathConstants<float>::halfPi);
95 return ssamp;
96 }
97 // Adjust the width of the stereo field
98 inline StereoSample width(float width)
99 {
100 StereoSample ssamp;
101 float mono = (1.0f - width) * 0.5f * (this->left + this->right);
102 ssamp.left = this->left * width + mono;
103 ssamp.right = this->right * width + mono;
104 return ssamp;
105 }
106
107 float left{0.f}, right{0.f};
108 };
109
110 class KrotosAudioBuffer : public juce::AudioBuffer<float>
111 {
112 public:
114 {
117 };
118
127
129 KrotosAudioBuffer(int channels, int size);
130
135 explicit KrotosAudioBuffer(const AudioBuffer<float>& buffer) : AudioBuffer<float>(buffer) {}
136
143 explicit KrotosAudioBuffer(const AudioBuffer<float>& buffer, int numOfChannels);
144
146
147 float getPeak(int channel) const;
148 float getRMS(int channel) const;
149
150 void setSize(int newNumChannels, int numSamples, bool keepExistingContent = true, bool clearExtraSpace = true,
151 bool avoidReallocating = true);
152
153 void setSize(KrotosAudioBuffer& prototypeBuffer);
154 void copyFrom(int destChannel, const float* source, int numSamples);
155 void copyFrom(KrotosAudioBuffer& sourceBuffer);
156 void addFrom(int destChannel, const float* source, int numSamples);
157 void addFrom(KrotosAudioBuffer& srcBuffer);
158 void processWetDry(KrotosAudioBuffer& wetBuffer, SmoothedFloat& wetDryValue);
159 void processGain(SmoothedFloat& gainFactor);
160 void processGain(float gainFactor);
161 void processPan(SmoothedFloat& panValue);
162 bool processClamp(float clampValue);
163 void fillWithNoise();
164 void processMute(MuteStateMachine& state);
165 void setDataValid(bool state);
166 bool isDataValid() const;
167 void setSampleRate(float sampleRate);
168 float getSampleRate(void);
169 void setSourceSampleRate(float sampleRate);
170 float getSourceSampleRate(void);
171 float getNativeNoteFrequency(void);
172 void setInterpolationType(InterpolationType newInterpolationType);
173 StereoSample makeRamp(StereoSample startValue, int samples);
176 float getInterpolatedSampleMono(float playHead);
178 static KrotosAudioBuffer mixToMono(KrotosAudioBuffer& multiChannelBuffer);
179
180 void reverse(int channel, int startSample, int numSamples);
182 void reverse(int startSample, int numSamples);
183 juce::AudioBuffer<float>& getJuceAudioBuffer();
184
185 void equalPowerCrossFadeFrom(const KrotosAudioBuffer& previousBuffer);
186
188 float findAbsMax();
189
191 void normaliseTo(float scale);
192
193 /*
194 void setGainModIndicator(float val)
195 {
196 m_gainModIndicator = m_gainModIndicator * 0.99f + val * 0.01f;
197 }
198
199 float getGainModIndicator()
200 {
201 return m_gainModIndicator;
202 }
203 */
204
205 private:
206 float getCubic3dInterpolationSample(double playhead, const float* buffer);
207 float getCosineInterpolationSample(double playhead, const float* buffer);
208 float getCubicInterpolationSample(double playhead, const float* buffer);
209
210 bool m_isDataValid{true};
211
213 float m_sampleRate{48000.0f};
214 float m_sourceSampleRate{48000.0f};
215 // volatile std::atomic<float> m_gainModIndicator{ 0.f };
216
217 // Frequences of notes in Hz, equal tempered scale, A4 = 440 Hz
218 static const float FREQ_Hz_NOTE_C3;
219 static const float FREQ_Hz_NOTE_D3;
220 static const float FREQ_Hz_NOTE_C4;
221 };
222
223} // namespace krotos
Definition KrotosAudioBuffer.h:111
~KrotosAudioBuffer()
Definition KrotosAudioBuffer.cpp:16
float getRMS(int channel) const
Definition KrotosAudioBuffer.cpp:20
void setInterpolationType(InterpolationType newInterpolationType)
Definition KrotosAudioBuffer.cpp:246
juce::AudioBuffer< float > & getJuceAudioBuffer()
Definition KrotosAudioBuffer.cpp:533
StereoSample getLastSample()
Get the last stereo sample from the audio buffer.
Definition KrotosAudioBuffer.cpp:295
void processMute(MuteStateMachine &state)
Definition KrotosAudioBuffer.cpp:49
static const float FREQ_Hz_NOTE_D3
Definition KrotosAudioBuffer.h:219
float getCosineInterpolationSample(double playhead, const float *buffer)
Definition KrotosAudioBuffer.cpp:421
BufferType
Definition KrotosAudioBuffer.h:114
@ Samples
Definition KrotosAudioBuffer.h:115
@ Spectral
Definition KrotosAudioBuffer.h:116
void processPan(SmoothedFloat &panValue)
Definition KrotosAudioBuffer.cpp:222
static const float FREQ_Hz_NOTE_C4
Definition KrotosAudioBuffer.h:220
float getSourceSampleRate(void)
Definition KrotosAudioBuffer.cpp:510
float getSampleRate(void)
Definition KrotosAudioBuffer.cpp:506
bool processClamp(float clampValue)
Definition KrotosAudioBuffer.cpp:185
float getCubic3dInterpolationSample(double playhead, const float *buffer)
Definition KrotosAudioBuffer.cpp:387
StereoSample getInterpolatedSample(double index)
Get a stereo sample from the audio buffer.
Definition KrotosAudioBuffer.cpp:318
void equalPowerCrossFadeFrom(const KrotosAudioBuffer &previousBuffer)
Definition KrotosAudioBuffer.cpp:538
float getInterpolatedSampleMono(float playHead)
Definition KrotosAudioBuffer.cpp:381
KrotosAudioBuffer getMonoBuffer()
Definition KrotosAudioBuffer.cpp:74
static const float FREQ_Hz_NOTE_C3
Definition KrotosAudioBuffer.h:218
float getCubicInterpolationSample(double playhead, const float *buffer)
Definition KrotosAudioBuffer.cpp:439
KrotosAudioBuffer()
Definition KrotosAudioBuffer.cpp:7
void processWetDry(KrotosAudioBuffer &wetBuffer, SmoothedFloat &wetDryValue)
Definition KrotosAudioBuffer.cpp:155
void fillWithNoise()
Definition KrotosAudioBuffer.cpp:37
KrotosAudioBuffer(const AudioBuffer< float > &buffer)
Creates a krotos buffer from a juce buffer.
Definition KrotosAudioBuffer.h:135
float m_sampleRate
Definition KrotosAudioBuffer.h:213
void setSize(int newNumChannels, int numSamples, bool keepExistingContent=true, bool clearExtraSpace=true, bool avoidReallocating=true)
Definition KrotosAudioBuffer.cpp:25
void copyFrom(int destChannel, const float *source, int numSamples)
Definition KrotosAudioBuffer.cpp:93
void setSampleRate(float sampleRate)
Definition KrotosAudioBuffer.cpp:504
bool isDataValid() const
Definition KrotosAudioBuffer.cpp:244
void reverse(int channel, int startSample, int numSamples)
Definition KrotosAudioBuffer.cpp:519
InterpolationType m_interpoltationType
Definition KrotosAudioBuffer.h:212
void normaliseTo(float scale)
Definition KrotosAudioBuffer.cpp:571
StereoSample makeRamp(StereoSample startValue, int samples)
Create a ramp in the buffer.
Definition KrotosAudioBuffer.cpp:257
float findAbsMax()
Definition KrotosAudioBuffer.cpp:557
float getNativeNoteFrequency(void)
Definition KrotosAudioBuffer.cpp:512
InterpolationType
Definition KrotosAudioBuffer.h:120
@ NUM_TYPES
Definition KrotosAudioBuffer.h:125
@ Cosine
Definition KrotosAudioBuffer.h:123
@ Cubic3d
Definition KrotosAudioBuffer.h:124
@ Cubic
Definition KrotosAudioBuffer.h:122
@ Linear
Definition KrotosAudioBuffer.h:121
void setSourceSampleRate(float sampleRate)
Definition KrotosAudioBuffer.cpp:508
float m_sourceSampleRate
Definition KrotosAudioBuffer.h:214
float getPeak(int channel) const
Definition KrotosAudioBuffer.cpp:18
void setDataValid(bool state)
Definition KrotosAudioBuffer.cpp:91
void processGain(SmoothedFloat &gainFactor)
Definition KrotosAudioBuffer.cpp:173
void addFrom(int destChannel, const float *source, int numSamples)
Definition KrotosAudioBuffer.cpp:122
static KrotosAudioBuffer mixToMono(KrotosAudioBuffer &multiChannelBuffer)
Definition KrotosAudioBuffer.cpp:87
bool m_isDataValid
Definition KrotosAudioBuffer.h:210
Definition MuteStateMachine.h:6
Definition SmoothedFloat.h:6
Definition KrotosAudioBuffer.h:16
float left
Definition KrotosAudioBuffer.h:107
StereoSample operator+(const StereoSample &b)
Definition KrotosAudioBuffer.h:19
float right
Definition KrotosAudioBuffer.h:107
StereoSample panSquare(float pan)
Definition KrotosAudioBuffer.h:82
StereoSample panLinear(float pan)
Definition KrotosAudioBuffer.h:74
StereoSample operator*(float b)
Definition KrotosAudioBuffer.h:58
StereoSample operator/(const StereoSample &b)
Definition KrotosAudioBuffer.h:50
StereoSample operator-(const StereoSample &b)
Definition KrotosAudioBuffer.h:34
StereoSample operator+=(const StereoSample &b)
Definition KrotosAudioBuffer.h:27
StereoSample operator/(float b)
Definition KrotosAudioBuffer.h:66
StereoSample width(float width)
Definition KrotosAudioBuffer.h:98
StereoSample operator*(const StereoSample &b)
Definition KrotosAudioBuffer.h:42
StereoSample panSine(float pan)
Definition KrotosAudioBuffer.h:90
Definition AirAbsorptionFilter.cpp:2