iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IVSpectrumAnalyzerControl.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9 */
10
11#pragma once
12
19#include "IControl.h"
20#include "ISender.h"
21#include "IPlugStructs.h"
22
23BEGIN_IPLUG_NAMESPACE
24BEGIN_IGRAPHICS_NAMESPACE
25
30template <int MAXNC = 2, int MAX_FFT_SIZE = 4096>
32 , public IVectorBase
33{
34public:
35 enum MsgTags
36 {
37 kMsgTagSampleRate = 1,
38 kMsgTagFFTSize,
39 kMsgTagOverlap,
40 kMsgTagWindowType,
41 kMsgTagOctaveGain
42 };
43
44 static constexpr auto numExtraPoints = 2;
45 using TDataPacket = std::array<float, MAX_FFT_SIZE>;
46 enum class EChannelType { Left = 0, Right, LeftAndRight };
47 enum class EFrequencyScale { Linear, Log };
48 enum class EAmplitudeScale { Linear, Decibel };
49
62 IVSpectrumAnalyzerControl(const IRECT& bounds, const char* label = "", const IVStyle& style = DEFAULT_STYLE,
63 std::initializer_list<IColor> colors = {COLOR_RED, COLOR_GREEN},
64 EFrequencyScale freqScale = EFrequencyScale::Log,
65 EAmplitudeScale ampScale = EAmplitudeScale::Decibel,
66 float curveThickness = 2.0,
67 float gridThickness = 1.0,
68 float fillOpacity = 0.25,
69 float attackTimeMs = 3.0,
70 float decayTimeMs = 50.0)
71 : IControl(bounds)
72 , IVectorBase(style)
73 , mChannelColors(colors)
74 , mFreqScale(freqScale)
75 , mAmpScale(ampScale)
76 , mCurveThickness(curveThickness)
77 , mGridThickness(gridThickness)
78 , mFillOpacity(fillOpacity)
79 , mAttackTimeMs(attackTimeMs)
80 , mDecayTimeMs(decayTimeMs)
81 {
82 assert(colors.size() >= MAXNC);
83 AttachIControl(this, label);
84 SetFFTSize(1024);
85 SetFreqRange(FirstBinFreq(), NyquistFreq());
86 SetAmpRange(DBToAmp(-90.0f), 1.0f);
87 }
88
89 void OnMouseDown(float x, float y, const IMouseMod& mod) override
90 {
91 mMenu.Clear(true);
92 auto* pFftSizeMenu = mMenu.AddItem("FFT Size", new IPopupMenu("FFT Size", { "64", "128", "256", "512", "1024", "2048", "4096"}))->GetSubmenu();
93 auto* pChansMenu = mMenu.AddItem("Channels", new IPopupMenu("Channels", { "L", "R", "L + R"}))->GetSubmenu();
94 auto* pFreqScaleMenu = mMenu.AddItem("Freq Scaling", new IPopupMenu("Freq Scaling", { "Linear", "Log"}))->GetSubmenu();
95 auto* pOverlapMenu = mMenu.AddItem("Overlap", new IPopupMenu("Overlap", { "1x", "2x", "4x", "8x" }))->GetSubmenu();
96 auto* pWindowMenu = mMenu.AddItem("Window", new IPopupMenu("Window", { "Hann", "Blackman Harris", "Hamming", "Flattop", "Rectangular" }))->GetSubmenu();
97
98 pFftSizeMenu->CheckItem(0, mFFTSize == 64);
99 pFftSizeMenu->CheckItem(1, mFFTSize == 128);
100 pFftSizeMenu->CheckItem(2, mFFTSize == 256);
101 pFftSizeMenu->CheckItem(3, mFFTSize == 512);
102 pFftSizeMenu->CheckItem(4, mFFTSize == 1024);
103 pFftSizeMenu->CheckItem(5, mFFTSize == 2048);
104 pFftSizeMenu->CheckItem(6, mFFTSize == 4096);
105
106 pChansMenu->CheckItem(0, mChanType == EChannelType::Left);
107 pChansMenu->CheckItem(1, mChanType == EChannelType::Right);
108 pChansMenu->CheckItem(2, mChanType == EChannelType::LeftAndRight);
109 pFreqScaleMenu->CheckItem(0, mFreqScale == EFrequencyScale::Linear);
110 pFreqScaleMenu->CheckItem(1, mFreqScale == EFrequencyScale::Log);
111
112 // Overlap checks
113 pOverlapMenu->CheckItem(0, mOverlap == 1);
114 pOverlapMenu->CheckItem(1, mOverlap == 2);
115 pOverlapMenu->CheckItem(2, mOverlap == 4);
116 pOverlapMenu->CheckItem(3, mOverlap == 8);
117
118 // Window checks (indices match enum order)
119 pWindowMenu->CheckItem(0, mWindowType == 0);
120 pWindowMenu->CheckItem(1, mWindowType == 1);
121 pWindowMenu->CheckItem(2, mWindowType == 2);
122 pWindowMenu->CheckItem(3, mWindowType == 3);
123 pWindowMenu->CheckItem(4, mWindowType == 4);
124
125 GetUI()->CreatePopupMenu(*this, mMenu, x, y);
126 }
127
128 void OnMouseOver(float x, float y, const IMouseMod& mod) override
129 {
130 mWidgetBounds.Constrain(x, y);
131 mCursorAmp = CalcYNorm(1.0 - y/mWidgetBounds.H(), mAmpScale, true);
132 mCursorFreq = CalcXNorm(x/mWidgetBounds.W(), mFreqScale, true) * NyquistFreq();
133 }
134
135 void OnPopupMenuSelection(IPopupMenu* pSelectedMenu, int valIdx) override
136 {
137 if (pSelectedMenu)
138 {
139 const char* title = pSelectedMenu->GetRootTitle();
140
141 if (strcmp(title, "FFT Size") == 0)
142 {
143 int fftSize = atoi(pSelectedMenu->GetChosenItem()->GetText());
144 GetDelegate()->SendArbitraryMsgFromUI(kMsgTagFFTSize, kNoTag, sizeof(int), &fftSize);
145 SetFFTSize(fftSize);
146 }
147 else if (strcmp(title, "Channels") == 0)
148 {
149 const char* chanStr = pSelectedMenu->GetChosenItem()->GetText();
150 if (strcmp(chanStr, "L") == 0) mChanType = EChannelType::Left;
151 else if (strcmp(chanStr, "R") == 0) mChanType = EChannelType::Right;
152 else if (strcmp(chanStr, "L + R") == 0) mChanType = EChannelType::LeftAndRight;
153 }
154 else if (strcmp(title, "Freq Scaling") == 0)
155 {
156 auto index = pSelectedMenu->GetChosenItemIdx();
157 SetFrequencyScale(index == 0 ? EFrequencyScale::Linear : EFrequencyScale::Log);
158 }
159 else if (strcmp(title, "Overlap") == 0)
160 {
161 const char* txt = pSelectedMenu->GetChosenItem()->GetText();
162 int overlap = atoi(txt); // works for strings like "1x", "2x"
163 if (overlap <= 0)
164 overlap = 1;
165 GetDelegate()->SendArbitraryMsgFromUI(kMsgTagOverlap, kNoTag, sizeof(int), &overlap);
166 mOverlap = overlap;
167 }
168 else if (strcmp(title, "Window") == 0)
169 {
170 int idx = pSelectedMenu->GetChosenItemIdx();
171 GetDelegate()->SendArbitraryMsgFromUI(kMsgTagWindowType, kNoTag, sizeof(int), &idx);
172 mWindowType = idx;
173 }
174 }
175 }
176
177 void OnResize() override
178 {
179 SetTargetRECT(MakeRects(mRECT));
180 SetDirty(false);
181 }
182
183 void OnMsgFromDelegate(int msgTag, int dataSize, const void* pData) override
184 {
185 IByteStream stream(pData, dataSize);
186
187 if (!IsDisabled() && msgTag == ISender<>::kUpdateMessage)
188 {
190 stream.Get(&d, 0);
191
192 for (auto c = d.chanOffset; c < (d.chanOffset + d.nChans); c++)
193 {
194 CalculateYPoints(c, d.vals[c]);
195 }
196 }
197 else if (msgTag == kMsgTagSampleRate)
198 {
199 double sr;
200 stream.Get(&sr, 0);
201 SetSampleRate(sr);
202 }
203 else if (msgTag == kMsgTagFFTSize)
204 {
205 int fftSize;
206 stream.Get(&fftSize, 0);
207 SetFFTSize(fftSize);
208 }
209 else if (msgTag == kMsgTagOverlap)
210 {
211 int overlap;
212 stream.Get(&overlap, 0);
213 mOverlap = overlap;
214 }
215 else if (msgTag == kMsgTagWindowType)
216 {
217 int windowType;
218 stream.Get(&windowType, 0);
219 mWindowType = windowType;
220 }
221 else if (msgTag == kMsgTagOctaveGain)
222 {
223 double octaveGain;
224 stream.Get(&octaveGain, 0);
225 SetOctaveGain(octaveGain);
226 }
227 }
228
229 void Draw(IGraphics& g) override
230 {
231 DrawBackground(g, mRECT);
232 DrawWidget(g);
233 DrawLabel(g);
234 DrawCursorValues(g);
235
236 if (mStyle.drawFrame)
237 g.DrawRect(GetColor(kFR), mWidgetBounds, &mBlend, mStyle.frameThickness);
238 }
239
240private:
241 void DrawGrids(IGraphics& g)
242 {
243 // Frequency Grid
244 auto freq = mFreqLo;
245
246 while (freq <= mFreqHi)
247 {
248 auto t = CalcXNorm(freq, mFreqScale);
249 auto x0 = t * mWidgetBounds.W();
250 auto y0 = mWidgetBounds.B;
251 auto x1 = x0;
252 auto y1 = mWidgetBounds.T;
253
254 g.DrawLine(GetColor(kFG), x0, y0, x1, y1, 0, mGridThickness);
255
256 if (freq < 10.0)
257 freq += 1.0;
258 else if (freq < 100.0)
259 freq += 10.0;
260 else if (freq < 1000.0)
261 freq += 100.0;
262 else if (freq < 10000.0)
263 freq += 1000.0;
264 else
265 freq += 10000.0;
266 }
267
268 // Amplitude Grid
269 if (mAmpScale == EAmplitudeScale::Decibel)
270 {
271 auto ampDB = AmpToDB(mAmpLo);
272 const auto dBYHi = AmpToDB(mAmpHi);
273
274 while (ampDB <= dBYHi)
275 {
276 auto t = Clip(CalcYNorm(ampDB, EAmplitudeScale::Decibel), 0.0f, 1.0f);
277
278 auto x0 = mWidgetBounds.L;
279 auto y0 = t * mWidgetBounds.H();
280 auto x1 = mWidgetBounds.R;
281 auto y1 = y0;
282
283 g.DrawLine(GetColor(kFG), x0, y0, x1, y1, 0, mGridThickness);
284
285 ampDB += 10.0;
286 }
287 }
288 }
289
290 void DrawWidget(IGraphics& g) override
291 {
292 DrawGrids(g);
293
294 for (auto c = 0; c < MAXNC; c++)
295 {
296 if ((c == 0) && (mChanType == EChannelType::Right))
297 continue;
298 if ((c == 1) && (mChanType == EChannelType::Left))
299 continue;
300
301 const int nBins = NumBins();
302 const IColor baseColor = mChannelColors[c];
303
304 // Build the spectrum path (optionally smoothed using cubic Beziers)
305 g.PathClear();
306 float x0 = mWidgetBounds.L + mXPoints[0] * mWidgetBounds.W();
307 float y0 = mWidgetBounds.B - mYPoints[c][0] * mWidgetBounds.H();
308 g.PathMoveTo(x0, y0);
309
310 if (mCurveSmoothing > 0.f && nBins > 3)
311 {
312 // Catmull-Rom to Bezier conversion with adjustable tension (mCurveSmoothing in [0,1])
313 const float s = mCurveSmoothing; // 0 -> straight lines, 1 -> classic Catmull-Rom
314 for (int i = 0; i < nBins - 1; ++i)
315 {
316 const int i0 = std::max(i - 1, 0);
317 const int i1 = i;
318 const int i2 = i + 1;
319 const int i3 = std::min(i + 2, nBins - 1);
320
321 const float x1 = mWidgetBounds.L + mXPoints[i1] * mWidgetBounds.W();
322 const float y1 = mWidgetBounds.B - mYPoints[c][i1] * mWidgetBounds.H();
323 const float x2 = mWidgetBounds.L + mXPoints[i2] * mWidgetBounds.W();
324 const float y2 = mWidgetBounds.B - mYPoints[c][i2] * mWidgetBounds.H();
325
326 const float x0s = mWidgetBounds.L + mXPoints[i0] * mWidgetBounds.W();
327 const float y0s = mWidgetBounds.B - mYPoints[c][i0] * mWidgetBounds.H();
328 const float x3 = mWidgetBounds.L + mXPoints[i3] * mWidgetBounds.W();
329 const float y3 = mWidgetBounds.B - mYPoints[c][i3] * mWidgetBounds.H();
330
331 const float c1x = x1 + (x2 - x0s) * (s / 6.f);
332 const float c1y = y1 + (y2 - y0s) * (s / 6.f);
333 const float c2x = x2 - (x3 - x1) * (s / 6.f);
334 const float c2y = y2 - (y3 - y1) * (s / 6.f);
335
336 g.PathCubicBezierTo(c1x, c1y, c2x, c2y, x2, y2);
337 }
338 }
339 else
340 {
341 for (int i = 1; i < nBins; ++i)
342 {
343 float xi = mWidgetBounds.L + mXPoints[i] * mWidgetBounds.W();
344 float yi = mWidgetBounds.B - mYPoints[c][i] * mWidgetBounds.H();
345 g.PathLineTo(xi, yi);
346 }
347 }
348
349 // Fill under the curve with a vertical gradient to transparent
350 if (FillCurves())
351 {
352 // Close the path down to the baseline and back to the start
353 float xLast = mWidgetBounds.L + mXPoints[nBins-1] * mWidgetBounds.W();
354 g.PathLineTo(xLast, mWidgetBounds.B);
355 g.PathLineTo(x0, mWidgetBounds.B);
356 g.PathClose();
357
358 const IColor topCol = baseColor.WithOpacity(mFillOpacity);
359 const IColor botCol = baseColor.WithOpacity(0.f);
360 IPattern fill = IPattern::CreateLinearGradient(mWidgetBounds, EDirection::Vertical, { IColorStop(topCol, 0.f), IColorStop(botCol, 1.f) });
361 g.PathFill(fill);
362
363 // Recreate the path for stroking (PathFill may consume it on some backends)
364 g.PathClear();
365 g.PathMoveTo(x0, y0);
366 if (mCurveSmoothing > 0.f && nBins > 3)
367 {
368 const float s = mCurveSmoothing;
369 for (int i = 0; i < nBins - 1; ++i)
370 {
371 const int i0 = std::max(i - 1, 0);
372 const int i1 = i;
373 const int i2 = i + 1;
374 const int i3 = std::min(i + 2, nBins - 1);
375
376 const float x1 = mWidgetBounds.L + mXPoints[i1] * mWidgetBounds.W();
377 const float y1 = mWidgetBounds.B - mYPoints[c][i1] * mWidgetBounds.H();
378 const float x2 = mWidgetBounds.L + mXPoints[i2] * mWidgetBounds.W();
379 const float y2 = mWidgetBounds.B - mYPoints[c][i2] * mWidgetBounds.H();
380
381 const float x0s = mWidgetBounds.L + mXPoints[i0] * mWidgetBounds.W();
382 const float y0s = mWidgetBounds.B - mYPoints[c][i0] * mWidgetBounds.H();
383 const float x3 = mWidgetBounds.L + mXPoints[i3] * mWidgetBounds.W();
384 const float y3 = mWidgetBounds.B - mYPoints[c][i3] * mWidgetBounds.H();
385
386 const float c1x = x1 + (x2 - x0s) * (s / 6.f);
387 const float c1y = y1 + (y2 - y0s) * (s / 6.f);
388 const float c2x = x2 - (x3 - x1) * (s / 6.f);
389 const float c2y = y2 - (y3 - y1) * (s / 6.f);
390
391 g.PathCubicBezierTo(c1x, c1y, c2x, c2y, x2, y2);
392 }
393 }
394 else
395 {
396 for (int i = 1; i < nBins; ++i)
397 {
398 float xi = mWidgetBounds.L + mXPoints[i] * mWidgetBounds.W();
399 float yi = mWidgetBounds.B - mYPoints[c][i] * mWidgetBounds.H();
400 g.PathLineTo(xi, yi);
401 }
402 }
403 }
404
405 // Stroke the curve for crispness
406 g.PathStroke(IPattern(baseColor), mCurveThickness);
407 }
408 }
409
410 void DrawCursorValues(IGraphics& g)
411 {
412 WDL_String label;
413
414 if (mCursorFreq >= 0.0)
415 {
416 label.SetFormatted(64, "%.1fHz", mCursorFreq);
417 g.DrawText(mStyle.valueText, label.Get(), mWidgetBounds.GetFromTRHC(100, 50).FracRectVertical(0.5));
418 }
419
420 if (mAmpScale == EAmplitudeScale::Linear)
421 label.SetFormatted(64, "%.3fs", mCursorAmp);
422 else
423 label.SetFormatted(64, "%ddB", (int) mCursorAmp);
424
425 g.DrawText(mStyle.valueText, label.Get(), mWidgetBounds.GetFromTRHC(100, 50).FracRectVertical(0.5, true));
426 }
427
428#pragma mark -
429
430 void SetFFTSize(int fftSize)
431 {
432 assert(fftSize > 0);
433 assert(fftSize <= MAX_FFT_SIZE);
434 mFFTSize = fftSize;
435
436 ResizePoints();
437 CalculateXPoints();
438 SetFreqRange(FirstBinFreq(), NyquistFreq());
439 SetSmoothing(mAttackTimeMs, mDecayTimeMs);
440 SetDirty(false);
441 }
442
443 void SetSampleRate(double sampleRate)
444 {
445 mSampleRate = sampleRate;
446 SetFreqRange(FirstBinFreq(), NyquistFreq());
447 SetSmoothing(mAttackTimeMs, mDecayTimeMs);
448 SetDirty(false);
449 }
450
451 void SetFreqRange(float freqLo, float freqHi)
452 {
453 mFreqLo = freqLo;
454 mFreqHi = freqHi;
455 SetDirty(false);
456 }
457
458 void SetFrequencyScale(EFrequencyScale scale)
459 {
460 mFreqScale = scale;
461 CalculateXPoints();
462 SetDirty(false);
463 }
464
465 void SetAmpRange(float ampLo, float ampHi)
466 {
467 mAmpLo = ampLo;
468 mAmpHi = ampHi;
469 SetDirty(false);
470 }
471
472 void SetOctaveGain(float octaveGain)
473 {
474 mOctaveGain = octaveGain;
475 SetDirty(false);
476 }
477
478 void SetCurveSmoothing(float amount)
479 {
480 mCurveSmoothing = Clip(amount, 0.f, 1.f);
481 SetDirty(false);
482 }
483
484 void SetSmoothing(float attackTimeMs, float releaseTimeMs)
485 {
486 auto attackTimeSec = attackTimeMs * 0.001f;
487 auto releaseTimeSec = releaseTimeMs * 0.001f;
488 auto updatePeriod = (float) mFFTSize / (float) mSampleRate;
489 mAttackCoeff = exp(-updatePeriod / attackTimeSec);
490 mReleaseCoeff = exp(-updatePeriod / releaseTimeSec);
491 }
492
493protected:
494 float ApplyOctaveGain(float amp, float freqNorm)
495 {
496 // Center on 500Hz
497 const float centerFreq = 500.0f;
498 float centerFreqNorm = (centerFreq - mFreqLo)/(mFreqHi - mFreqLo);
499
500 if (mOctaveGain > 0.0)
501 {
502 amp *= freqNorm/centerFreqNorm;
503 }
504
505 return amp;
506 }
507
508 void ResizePoints()
509 {
510 mXPoints.resize(NumPoints());
511
512 for (auto c = 0; c < MAXNC; c++)
513 {
514 mYPoints[c].assign(NumPoints(), 0.0f);
515 mEnvelopeValues[c].assign(NumPoints(), 0.0f);
516 }
517 }
518
519 void CalculateXPoints()
520 {
521 const auto numBins = NumBins();
522 const auto xIncr = (1.0f / static_cast<float>(numBins-1)) * NyquistFreq();
523 mXPoints[0] = 0.0f;
524 for (auto i = 1; i < numBins; i++)
525 {
526 auto xVal = CalcXNorm(float(i) * xIncr, mFreqScale);
527 mXPoints[i] = xVal;
528 }
529 mXPoints[numBins] = mXPoints[numBins-1];
530 mXPoints[numBins+1] = mXPoints[0];
531 }
532
533 void CalculateYPoints(int ch, const TDataPacket& powerSpectrum)
534 {
535 const auto numBins = NumBins();
536
537 for (auto i = 0; i < numBins; i++)
538 {
539 const auto adjustedAmp = ApplyOctaveGain(powerSpectrum[i], static_cast<float>(numBins-1));
540 float rawVal = (mAmpScale == EAmplitudeScale::Decibel)
541 ? AmpToDB(adjustedAmp + 1e-30f)
542 : adjustedAmp;
543 rawVal = Clip(CalcYNorm(rawVal, mAmpScale), 0.0f, 1.0f);
544
545 float prevVal = mEnvelopeValues[ch][i];
546 float newVal;
547 if (rawVal > prevVal)
548 newVal = mAttackCoeff * prevVal + (1.0f - mAttackCoeff) * rawVal; // Attack phase
549 else
550 newVal = mReleaseCoeff * prevVal + (1.0f - mReleaseCoeff) * rawVal; // Release phase
551
552 mEnvelopeValues[ch][i] = newVal; // Store smoothed value
553 mYPoints[ch][i] = newVal; // Use smoothed value for drawing
554 }
555
556 if (FillCurves())
557 {
558 // Used to close the path outside the bounds of the control
559 auto offset = mCurveThickness/mWidgetBounds.H();
560
561 mYPoints[ch][numBins] = -offset;
562 mYPoints[ch][numBins+1] = -offset;
563 }
564
565 SetDirty(false);
566 }
567
568 float CalcXNorm(float x, EFrequencyScale scale, bool inverted = false)
569 {
570 const auto nyquist = NyquistFreq();
571
572 switch (scale)
573 {
574 case EFrequencyScale::Linear:
575 {
576 if (!inverted)
577 return (x - mFreqLo) / (mFreqHi - mFreqLo);
578 else
579 return (mFreqLo + x * (mFreqHi - mFreqLo)) / nyquist;
580 }
581 case EFrequencyScale::Log:
582 {
583 const auto logXLo = std::log(mFreqLo / nyquist);
584 const auto logXHi = std::log(mFreqHi / nyquist);
585
586 if (!inverted)
587 return (std::log(x / nyquist) - logXLo) / (logXHi - logXLo);
588 else
589 return std::exp(logXLo + x * (logXHi - logXLo));
590 }
591 }
592 }
593
594 // Amplitudes
595 float CalcYNorm(float y, EAmplitudeScale scale, bool inverted = false) const
596 {
597 switch (scale)
598 {
599 case EAmplitudeScale::Linear:
600 {
601 if (!inverted)
602 return (y - mAmpLo) / (mAmpHi - mAmpLo);
603 else
604 return mAmpLo + y * (mAmpHi - mAmpLo);
605 }
606 case EAmplitudeScale::Decibel:
607 {
608 const auto dBYLo = AmpToDB(mAmpLo);
609 const auto dBYHi = AmpToDB(mAmpHi);
610
611 if (!inverted)
612 return (y - dBYLo) / (dBYHi - dBYLo);
613 else
614 return dBYLo + y * (dBYHi - dBYLo);
615 }
616 }
617 }
618
619 int NumPoints() const { return FillCurves() ? NumBins() + numExtraPoints : NumBins(); }
620 int NumBins() const { return mFFTSize / 2; }
621 double FirstBinFreq() const { return NyquistFreq()/mFFTSize; }
622 double NyquistFreq() const { return mSampleRate * 0.5; }
623 bool FillCurves() const { return mFillOpacity > 0.0f; }
624
625private:
626 std::vector<IColor> mChannelColors;
627 EFrequencyScale mFreqScale;
628 EAmplitudeScale mAmpScale;
629
630 double mSampleRate = 44100.0;
631 int mFFTSize = 1024;
632 float mOctaveGain = 0.0;
633 float mFreqLo = 20.0;
634 float mFreqHi = 22050.0;
635 float mAmpLo = 0.0;
636 float mAmpHi = 1.0;
637 float mAttackTimeMs = 3.0;
638 float mDecayTimeMs = 50.0;
639 EChannelType mChanType = EChannelType::LeftAndRight;
640
641 float mCurveThickness = 1.0f;
642 float mGridThickness = 1.0f;
643 float mFillOpacity = 0.5f;
644 float mCursorAmp = 0.0;
645 float mCursorFreq = -1.0;
646 IPopupMenu mMenu {"Options"};
647
648 std::vector<float> mXPoints;
649 std::array<std::vector<float>, MAXNC> mYPoints;
650 std::array<std::vector<float>, MAXNC> mEnvelopeValues;
651 float mAttackCoeff = 0.2f;
652 float mReleaseCoeff = 0.99f;
653 int mOverlap = 1;
654 int mWindowType = 0; // matches ISpectrumSender<>::EWindowType::Hann
655 float mCurveSmoothing = 0.6f; // 0 = straight lines, 1 = full Catmull-Rom
656};
657
658END_IGRAPHICS_NAMESPACE
659END_IPLUG_NAMESPACE
This file contains the base IControl implementation, along with some base classes for specific types ...
Manages a non-owned block of memory, for receiving arbitrary message byte streams.
Definition: IPlugStructs.h:268
int Get(T *pDst, int startPos) const
Get arbitary typed data from the stream.
Definition: IPlugStructs.h:289
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
IGraphics * GetUI()
Definition: IControl.h:472
bool IsDisabled() const
Definition: IControl.h:367
void SetTargetRECT(const IRECT &bounds)
Set the rectangular mouse tracking target area, within the graphics context for this control.
Definition: IControl.h:328
IGEditorDelegate * GetDelegate()
Gets a pointer to the class implementing the IEditorDelegate interface that handles parameter changes...
Definition: IControl.h:454
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:198
virtual void SendArbitraryMsgFromUI(int msgTag, int ctrlTag=kNoTag, int dataSize=0, const void *pData=nullptr)
SendArbitraryMsgFromUI (Abbreviation: SAMFUI)
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
virtual void DrawRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f)
Draw a rectangle to the graphics context.
Definition: IGraphics.cpp:2515
virtual void PathFill(const IPattern &pattern, const IFillOptions &options=IFillOptions(), const IBlend *pBlend=0)=0
Fill the current current path.
void DrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend=0)
Draw some text to the graphics context in a specific rectangle.
Definition: IGraphics.cpp:683
void CreatePopupMenu(IControl &control, IPopupMenu &menu, const IRECT &bounds, int valIdx=0)
Shows a pop up/contextual menu in relation to a rectangular region of the graphics context.
Definition: IGraphics.cpp:1976
virtual void PathClear()=0
Clear the stack of path drawing commands.
virtual void PathClose()=0
Close the path that is being specified.
virtual void PathStroke(const IPattern &pattern, float thickness, const IStrokeOptions &options=IStrokeOptions(), const IBlend *pBlend=0)=0
Stroke the current current path.
virtual void DrawLine(const IColor &color, float x1, float y1, float x2, float y2, const IBlend *pBlend=0, float thickness=1.f)
Draw a line to the graphics context.
Definition: IGraphics.cpp:2434
virtual void PathMoveTo(float x, float y)=0
Move the current point in the current path.
virtual void PathLineTo(float x, float y)=0
Add a line to the current path from the current point to the specified location.
virtual void PathCubicBezierTo(float c1x, float c1y, float c2x, float c2y, float x2, float y2)=0
Add a cubic bezier to the current path from the current point to the specified location.
A class for setting the contents of a pop up menu.
ISender is a utility class which can be used to defer data from the realtime audio processing and sen...
Definition: ISender.h:66
Vectorial multi-channel capable spectrum analyzer controlDerived from work by Alex Harker and Matthew...
void OnMsgFromDelegate(int msgTag, int dataSize, const void *pData) override
Implement to receive messages sent to the control, see IEditorDelegate:SendControlMsgFromDelegate()
IVSpectrumAnalyzerControl(const IRECT &bounds, const char *label="", const IVStyle &style=DEFAULT_STYLE, std::initializer_list< IColor > colors={COLOR_RED, COLOR_GREEN}, EFrequencyScale freqScale=EFrequencyScale::Log, EAmplitudeScale ampScale=EAmplitudeScale::Decibel, float curveThickness=2.0, float gridThickness=1.0, float fillOpacity=0.25, float attackTimeMs=3.0, float decayTimeMs=50.0)
Create a IVSpectrumAnalyzerControl.
void OnPopupMenuSelection(IPopupMenu *pSelectedMenu, int valIdx) override
Implement this method to handle popup menu selection after IGraphics::CreatePopupMenu/IControlPromptU...
void OnResize() override
Called when IControl is constructed or resized using SetRect().
void Draw(IGraphics &g) override
Draw the control to the graphics context.
void OnMouseOver(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouseover event on this control.
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
A base interface to be combined with IControl for vectorial controls "IVControls",...
Definition: IControl.h:762
IRECT MakeRects(const IRECT &parent, bool hasHandle=false)
Calculate the rectangles for the various areas, depending on the style.
Definition: IControl.h:1163
virtual void DrawBackground(IGraphics &g, const IRECT &rect)
Draw the IVControl background (usually transparent)
Definition: IControl.h:881
void AttachIControl(IControl *pControl, const char *label)
Call in the constructor of your IVControl to link the IVectorBase and IControl.
Definition: IControl.h:780
virtual void DrawLabel(IGraphics &g)
Draw the IVControl label text.
Definition: IControl.h:894
const IColor & GetColor(EVColor color) const
Get value of a specific EVColor in the IVControl.
Definition: IControl.h:806
BEGIN_IPLUG_NAMESPACE T Clip(T x, T lo, T hi)
Clips the value x between lo and hi.
static double AmpToDB(double amp)
static double DBToAmp(double dB)
Calculates gain from a given dB value.
Used to manage color data, independent of draw class/platform.
IColor WithOpacity(float alpha) const
Returns a new IColor with a different opacity.
Used to represent a point/stop in a gradient.
Used to manage mouse modifiers i.e.
Used to store pattern information for gradients.
static IPattern CreateLinearGradient(float x1, float y1, float x2, float y2, const std::initializer_list< IColorStop > &stops={})
Create a linear gradient IPattern.
Used to manage a rectangular area, independent of draw class/platform.
IRECT GetFromTRHC(float w, float h) const
Get a subrect of this IRECT expanding from the top-right corner.
IRECT FracRectVertical(float frac, bool fromTop=false) const
Returns a new IRECT with a height that is multiplied by frac.
float W() const
void Constrain(float &x, float &y) const
Ensure the point (x,y) is inside this IRECT.
float H() const
ISenderData is used to represent a typed data packet, that may contain values for multiple channels.
Definition: ISender.h:35
A struct encapsulating a set of properties used to configure IVControls.