/
EqComponent.h
416 lines (294 loc) · 9.2 KB
/
EqComponent.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* ===========================================================================
*
* This file is part of HISE.
* Copyright 2016 Christoph Hart
*
* HISE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HISE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HISE. If not, see <http://www.gnu.org/licenses/>.
*
* Commercial licenses for using HISE in an closed source project are
* available on request. Please visit the project's website to get more
* information about commercial licensing:
*
* http://www.hise.audio/
*
* HISE is based on the JUCE library,
* which must be separately licensed for closed source applications:
*
* http://www.juce.com
*
* ===========================================================================
*/
#ifndef __MAINCOMPONENT_H_5C0D5756__
#define __MAINCOMPONENT_H_5C0D5756__
namespace hise { using namespace juce;
class ModulatorSynth;
struct AnalyserRingBuffer: public AsyncUpdater
{
void handleAsyncUpdate()
{
if (auto l = SingleWriteLockfreeMutex::ScopedWriteLock(lock))
{
analyseBufferSize = currentSize;
indexInBuffer = 0;
internalBuffer.setSize(2, analyseBufferSize);
internalBuffer.clear();
}
else
triggerAsyncUpdate();
}
bool isActive() const
{
return currentSize != 0;
}
void setAnalyserBufferSize(int newValue)
{
jassert(isPowerOfTwo(newValue));
currentSize = newValue;
handleAsyncUpdate();
}
void pushSamples(const AudioSampleBuffer& b, int startSample, int numSamples)
{
if (internalBuffer.getNumSamples() == 0)
return;
if (auto sl = SingleWriteLockfreeMutex::ScopedReadLock(lock))
{
auto l = b.getReadPointer(0, startSample);
auto r = b.getReadPointer(1, startSample);
const int numToCopy = jmin<int>(internalBuffer.getNumSamples(), numSamples);
const int numBeforeWrap = jmin<int>(numToCopy, internalBuffer.getNumSamples() - indexInBuffer);
FloatVectorOperations::copy(internalBuffer.getWritePointer(0, indexInBuffer), l, numBeforeWrap);
FloatVectorOperations::copy(internalBuffer.getWritePointer(1, indexInBuffer), r, numBeforeWrap);
const int numAfterWrap = numToCopy - numBeforeWrap;
if (numAfterWrap > 0)
{
FloatVectorOperations::copy(internalBuffer.getWritePointer(0, 0), l, numAfterWrap);
FloatVectorOperations::copy(internalBuffer.getWritePointer(1, 0), r, numAfterWrap);
}
indexInBuffer = (indexInBuffer + numToCopy) % internalBuffer.getNumSamples();
}
}
mutable SingleWriteLockfreeMutex lock;
int indexInBuffer = 0;
int currentSize = 0;
int analyseBufferSize = -1;
AudioSampleBuffer internalBuffer;
};
class OscilloscopeBase
{
protected:
OscilloscopeBase(const AnalyserRingBuffer& ringBuffer_) :
ringBuffer(ringBuffer_)
{};
virtual ~OscilloscopeBase() {};
virtual Colour getColourForAnalyserBase(int colourId) = 0;
void drawWaveform(Graphics& g);
private:
const AnalyserRingBuffer& ringBuffer;
void drawPath(const float* l_, int numSamples, int width, Path& p)
{
int stride = roundToInt((float)numSamples / width);
stride = jmax<int>(1, stride * 2);
if (numSamples != 0)
{
p.clear();
p.startNewSubPath(0.0f, 0.0f);
p.lineTo(0.0f, 1.0f);
p.lineTo(0.0f, -1.0f);
for (int i = stride; i < numSamples; i += stride)
{
const int numToCheck = jmin<int>(stride, numSamples - i);
auto value = jmax<float>(0.0f, FloatVectorOperations::findMaximum(l_ + i, numToCheck));
p.lineTo((float)i, -1.0f * value);
};
for (int i = numSamples - 1; i > 0; i -= stride)
{
const int numToCheck = jmin<int>(stride, numSamples - i);
auto value = jmin<float>(0.0f, FloatVectorOperations::findMinimum(l_ + i, numToCheck));
p.lineTo((float)i, -1.0f * value);
};
p.closeSubPath();
}
else
{
p.clear();
}
}
void drawOscilloscope(Graphics &g, const AudioSampleBuffer &b)
{
AudioSampleBuffer b2(2, b.getNumSamples());
auto dataL = b2.getWritePointer(0);
auto dataR = b2.getWritePointer(1);
int size = b.getNumSamples();
auto readIndex = ringBuffer.indexInBuffer;
int numBeforeWrap = size - readIndex;
int numAfterWrap = size - numBeforeWrap;
FloatVectorOperations::copy(dataL, b.getReadPointer(0, readIndex), numBeforeWrap);
FloatVectorOperations::copy(dataL + numBeforeWrap, b.getReadPointer(0, 0), numAfterWrap);
FloatVectorOperations::copy(dataR, b.getReadPointer(1, readIndex), numBeforeWrap);
FloatVectorOperations::copy(dataR + numBeforeWrap, b.getReadPointer(1, 0), numAfterWrap);
auto asComponent = dynamic_cast<Component*>(this);
drawPath(dataL, b.getNumSamples(), asComponent->getWidth(), lPath);
drawPath(dataR, b.getNumSamples(), asComponent->getWidth(), rPath);
lPath.scaleToFit(0.0f, 0.0f, (float)asComponent->getWidth(), (float)(asComponent->getHeight() / 2), false);
rPath.scaleToFit(0.0f, (float)(asComponent->getHeight() / 2), (float)asComponent->getWidth(), (float)(asComponent->getHeight() / 2), false);
g.fillPath(lPath);
g.fillPath(rPath);
}
Path lPath;
Path rPath;
};
class FFTDisplayBase
{
public:
enum WindowType
{
Rectangle,
BlackmannHarris,
Hann,
Flattop,
numWindowTypes
};
enum Domain
{
Phase,
Amplitude,
numDomains
};
using ConverterFunction = std::function<float(float)>;
struct Properties
{
WindowType window = BlackmannHarris;
Range<double> dbRange = { -70.0, 0.0 };
Domain domain = Amplitude;
ConverterFunction freq2x;
ConverterFunction gain2y;
};
Properties fftProperties;
protected:
FFTDisplayBase(const AnalyserRingBuffer& ringBuffer_):
ringBuffer(ringBuffer_)
#if USE_IPP
, fftObject(IppFFT::DataType::RealFloat)
#endif
{}
#if USE_IPP
IppFFT fftObject;
#endif
virtual Colour getColourForAnalyserBase(int colourId) = 0;
virtual double getSamplerate() const = 0;
virtual ~FFTDisplayBase() {};
void drawSpectrum(Graphics& g);
const AnalyserRingBuffer& ringBuffer;
Path lPath;
Path rPath;
WindowType lastWindowType = numWindowTypes;
AudioSampleBuffer windowBuffer;
AudioSampleBuffer fftBuffer;
};
class CurveEq;
class FilterDragOverlay : public Component,
public SettableTooltipClient,
public SafeChangeListener,
public Timer
{
public:
struct Factory : public PathFactory
{
String getId() const override { return "FilterIcons"; }
Path createPath(const String& url) const override;
};
enum ColourIds
{
bgColour = 125160,
textColour
};
struct Panel;
int offset = 12;
struct Listener
{
virtual ~Listener() {};
virtual void bandRemoved(int index) = 0;
virtual void filterBandSelected(int index) = 0;
JUCE_DECLARE_WEAK_REFERENCEABLE(Listener);
};
FilterDragOverlay(CurveEq* eq_, bool isInFloatingTile_ = false);
virtual ~FilterDragOverlay();
void changeListenerCallback(SafeChangeBroadcaster *b) override;
void checkEnabledBands();
void timerCallback() override;
void resized() override;
void paint(Graphics &g);
void mouseMove(const MouseEvent &e);
void mouseDown(const MouseEvent &e);
void mouseDrag(const MouseEvent &e) override;
void mouseUp(const MouseEvent& e);
void addFilterDragger(int index);
void updateFilters();
void updateCoefficients();
void addFilterToGraph(int filterIndex, int filterType);
void updatePositions(bool forceUpdate);
Point<int> getPosition(int index);
virtual void fillPopupMenu(PopupMenu& m, int handleIndex);
virtual void popupMenuAction(int result, int handleIndex);
Font font;
bool isInFloatingTile = false;
ScopedPointer<LookAndFeel> plaf;
class FilterDragComponent : public Component
{
public:
FilterDragComponent(FilterDragOverlay& parent_, int index_);;
void setConstrainer(ComponentBoundsConstrainer *constrainer_);;
void mouseDown(const MouseEvent& e);
void mouseUp(const MouseEvent& e);
void mouseDrag(const MouseEvent& e);
void mouseWheelMove(const MouseEvent &e, const MouseWheelDetails &d) override;
void setSelected(bool shouldBeSelected);
void paint(Graphics &g);;
void setIndex(int newIndex);;
private:
bool draggin = false;
int index;
bool selected;
ComponentBoundsConstrainer *constrainer;
ComponentDragger dragger;
FilterDragOverlay& parent;
};
void removeFilter(int index);
double getGain(int y);
void selectDragger(int index);
void addListener(Listener* l);
void removeListener(Listener* l);
protected:
CurveEq *eq;
int numFilters = 0;
public:
struct FFTDisplay : public Component,
public FFTDisplayBase
{
FFTDisplay(FilterDragOverlay& parent_);;
void paint(Graphics& g) override;
double getSamplerate() const override;
Colour getColourForAnalyserBase(int colourId);
FilterDragOverlay& parent;
} fftAnalyser;
FilterGraph filterGraph;
private:
Array<WeakReference<Listener>> listeners;
UpdateMerger repaintUpdater;
int selectedIndex;
ScopedPointer<ComponentBoundsConstrainer> constrainer;
OwnedArray<FilterDragComponent> dragComponents;
};
} // namespace hise;
#endif // __MAINCOMPONENT_H_5C0D5756__