26#if defined OS_IOS || defined OS_MAC
27#include <Accelerate/Accelerate.h>
33template <
int MAXNC = 1,
typename T =
float>
39 std::array<T, MAXNC> vals;
46 ISenderData(
int ctrlTag,
int nChans,
int chanOffset)
49 , chanOffset(chanOffset)
54 ISenderData(
int ctrlTag,
const std::array<T, MAXNC>& vals,
int nChans = MAXNC,
int chanOffset = 0)
57 , chanOffset(chanOffset)
64template <
int MAXNC = 1,
int QUEUE_SIZE = 64,
typename T =
float>
68 static constexpr int kUpdateMessage = 0;
83 while (mQueue.ElementsAvailable())
87 assert(d.ctrlTag != kNoTag &&
"You must supply a control tag");
99 while(mQueue.ElementsAvailable())
104 for (
auto tag : ctrlTags)
123template <
int MAXNC = 1,
int QUEUE_SIZE = 64>
127 IPeakSender(
double minThresholdDb = -90.,
float windowSizeMs = 5.0f)
129 , mThreshold(
static_cast<float>(
DBToAmp(minThresholdDb)))
130 , mWindowSizeMs(windowSizeMs)
132 Reset(DEFAULT_SAMPLE_RATE);
135 void Reset(
double sampleRate)
137 SetWindowSizeMs(mWindowSizeMs, sampleRate);
138 std::fill(mPeaks.begin(), mPeaks.end(), 0.0f);
141 void SetWindowSizeMs(
double timeMs,
double sampleRate)
143 mWindowSizeMs =
static_cast<float>(timeMs);
144 mWindowSize =
static_cast<int>(timeMs * 0.001 * sampleRate);
153 void ProcessBlock(sample** inputs,
int nFrames,
int ctrlTag = kNoTag,
int nChans = MAXNC,
int chanOffset = 0)
155 for (
auto s = 0; s < nFrames; s++)
163 for (
auto c = chanOffset; c < (chanOffset + nChans); c++)
165 d.vals[c] = mPeaks[c] / mWindowSize;
170 if (sum > mThreshold || mPreviousSum > mThreshold)
176 for (
auto c = chanOffset; c < (chanOffset + nChans); c++)
178 mPeaks[c] += std::fabs(
static_cast<float>(inputs[c][s]));
182 mCount %= mWindowSize;
186 float mPreviousSum = 1.f;
187 float mThreshold = 0.01f;
188 float mWindowSizeMs = 5.0f;
189 int mWindowSize = 32;
191 std::array<float, MAXNC> mPeaks = {0.0};
197template <
int MAXNC = 1,
int QUEUE_SIZE = 64>
204 inline float Process(
float& input,
float& attackTimeSamples,
float& decayTimeSamples)
206 if (input > mPreviousOutput)
208 if (attackTimeSamples > 1.0f)
210 mPreviousOutput += (input - mPreviousOutput) / attackTimeSamples;
214 mPreviousOutput = input;
217 else if (input < mPreviousOutput)
219 if (decayTimeSamples > 1.0f)
221 mPreviousOutput += (input - mPreviousOutput) / decayTimeSamples;
225 mPreviousOutput = input;
229 denormal_fix(&mPreviousOutput);
230 return mPreviousOutput;
234 float mPreviousOutput = 0.0f;
237 IPeakAvgSender(
double minThresholdDb = -90.0,
bool rmsMode =
true,
float windowSizeMs = 5.0f,
float attackTimeMs = 1.0f,
float decayTimeMs = 100.0f,
float peakHoldTimeMs = 500.0f)
238 :
ISender<MAXNC, QUEUE_SIZE, std::pair<float, float>>()
239 , mThreshold(static_cast<float>(
DBToAmp(minThresholdDb)))
241 , mWindowSizeMs(windowSizeMs)
242 , mAttackTimeMs(attackTimeMs)
243 , mDecayTimeMs(decayTimeMs)
244 , mPeakHoldTimeMs(peakHoldTimeMs)
246 Reset(DEFAULT_SAMPLE_RATE);
249 void Reset(
double sampleRate)
251 SetWindowSizeMs(mWindowSizeMs, sampleRate);
252 SetAttackTimeMs(mAttackTimeMs, sampleRate);
253 SetDecayTimeMs(mDecayTimeMs, sampleRate);
254 SetPeakHoldTimeMs(mPeakHoldTimeMs, sampleRate);
255 std::fill(mHeldPeaks.begin(), mHeldPeaks.end(), 0.0f);
258 void SetAttackTimeMs(
double timeMs,
double sampleRate)
260 mAttackTimeMs =
static_cast<float>(timeMs);
261 mAttackTimeSamples =
static_cast<float>(timeMs * 0.001 * (sampleRate / double(mWindowSize)));
264 void SetDecayTimeMs(
double timeMs,
double sampleRate)
266 mDecayTimeMs =
static_cast<float>(timeMs);
267 mDecayTimeSamples =
static_cast<float>(timeMs * 0.001 * (sampleRate / mWindowSize));
270 void SetWindowSizeMs(
double timeMs,
double sampleRate)
272 mWindowSizeMs =
static_cast<float>(timeMs);
273 mWindowSize =
static_cast<int>(timeMs * 0.001 * sampleRate);
276 for (
auto i=0; i<MAXNC; i++)
278 mBuffers[i].resize(mWindowSize);
279 std::fill(mBuffers[i].begin(), mBuffers[i].end(), 0.0f);
283 void SetPeakHoldTimeMs(
double timeMs,
double sampleRate)
285 mPeakHoldTimeMs =
static_cast<float>(timeMs);
286 mPeakHoldTime =
static_cast<int>(timeMs * 0.001 * sampleRate);
287 std::fill(mPeakHoldCounters.begin(), mPeakHoldCounters.end(), mPeakHoldTime);
296 void ProcessBlock(sample** inputs,
int nFrames,
int ctrlTag = kNoTag,
int nChans = MAXNC,
int chanOffset = 0)
298 for (
auto s = 0; s < nFrames; s++)
300 for (
auto c = chanOffset; c < (chanOffset + nChans); c++)
302 mBuffers[c][mCount] =
static_cast<float>(inputs[c][s]);
310 bool peakHeld =
false;
312 for (
auto c = chanOffset; c < (chanOffset + nChans); c++)
316#if defined OS_IOS || defined OS_MAC
317 vDSP_vabs(mBuffers[c].data(), 1, mBuffers[c].data(), 1, mWindowSize);
318 vDSP_maxv(mBuffers[c].data(), 1, &peakVal, mWindowSize);
322 vDSP_rmsqv(mBuffers[c].data(), 1, &avgVal, mWindowSize);
326 vDSP_meanv(mBuffers[c].data(), 1, &avgVal, mWindowSize);
329 for (
auto i=0; i<mWindowSize; i++)
331 auto absVal = std::fabs(mBuffers[c][i]);
333 if (absVal > peakVal)
340 absVal = absVal * absVal;
346 avgVal /=
static_cast<float>(mWindowSize);
350 avgVal = std::sqrt(avgVal);
355 if (mPeakHoldCounters[c] <= 0)
357 mHeldPeaks[c] = 0.0f;
360 if (mHeldPeaks[c] < peakVal)
362 mHeldPeaks[c] = peakVal;
363 mPeakHoldCounters[c] = mPeakHoldTime;
367 if (mPeakHoldCounters[c] > 0)
369 mPeakHoldCounters[c] -= mWindowSize;
373 std::get<0>(d.vals[c]) = mHeldPeaks[c];
374 peakHeld |= mHeldPeaks[c] > 0.0f;
377 auto smoothedAvg = mEnvFollowers[c].Process(avgVal, mAttackTimeSamples, mDecayTimeSamples);
378 std::get<1>(d.vals[c]) = smoothedAvg;
380 avgSum += smoothedAvg;
383 if (mPreviousSum > mThreshold)
393 for (
auto c = chanOffset; c < (chanOffset + nChans); c++)
395 std::get<1>(d.vals[c]) = 0.0f;
398 if (peakHeld || mPeakWasHeld)
404 mPreviousSum = avgSum;
405 mPeakWasHeld = peakHeld;
409 mCount %= mWindowSize;
413 float mThreshold = 0.01f;
414 bool mRMSMode =
false;
415 float mPreviousSum = 1.0f;
416 bool mPeakWasHeld =
false;
417 int mWindowSize = 32;
418 int mPeakHoldTime = 1 << 16;
420 float mWindowSizeMs = 5.f;
421 float mAttackTimeMs = 1.f;
422 float mDecayTimeMs = 100.f;
423 float mPeakHoldTimeMs = 100.f;
424 float mAttackTimeSamples = 1.0f;
425 float mDecayTimeSamples = DEFAULT_SAMPLE_RATE/10.0f;
426 std::array<float, MAXNC> mHeldPeaks = {0};
427 std::array<std::vector<float>, MAXNC> mBuffers;
428 std::array<int, MAXNC> mPeakHoldCounters;
429 std::array<EnvelopeFollower, MAXNC> mEnvFollowers;
433template <
int MAXNC = 1,
int QUEUE_SIZE = 64,
int MAXBUF = 128>
437 using TDataPacket = std::array<float, MAXBUF>;
439 const double kNoThresholdDb = -100;
441 IBufferSender(
double minThresholdDb = -90.,
int bufferSize = MAXBUF)
444 if (minThresholdDb <= kNoThresholdDb)
447 mThreshold =
DBToAmp(minThresholdDb);
449 SetBufferSize(bufferSize);
458 void ProcessBlock(sample** inputs,
int nFrames,
int ctrlTag = kNoTag,
int nChans = MAXNC,
int chanOffset = 0)
460 for (
auto s = 0; s < nFrames; s++)
462 if (mBufCount == mBufferSize)
465 for (
auto c = chanOffset; c < (chanOffset + nChans); c++)
467 sum += mRunningSum[c];
468 mRunningSum[c] = 0.0f;
471 if (sum > mThreshold || mPreviousSum > mThreshold)
473 mBuffer.ctrlTag = ctrlTag;
474 mBuffer.nChans = nChans;
475 mBuffer.chanOffset = chanOffset;
483 for (
auto c = chanOffset; c < (chanOffset + nChans); c++)
485 const float inputSample =
static_cast<float>(inputs[c][s]);
486 mBuffer.vals[c][mBufCount] = inputSample;
487 mRunningSum[c] += std::fabs(inputSample);
494 void SetBufferSize(
int bufferSize)
496 assert(bufferSize > 0);
497 assert(bufferSize <= MAXBUF);
499 mBufferSize = bufferSize;
503 int GetBufferSize()
const {
return mBufferSize; }
508 int mBufferSize = MAXBUF;
509 std::array<float, MAXNC> mRunningSum {0.};
510 float mPreviousSum = 1.f;
511 float mThreshold = 0.01f;
515template <
int MAXNC = 1,
int QUEUE_SIZE = 64,
int MAX_FFT_SIZE = 4096>
519 using TDataPacket = std::array<float, MAX_FFT_SIZE>;
522 enum class EWindowType {
530 enum class EOutputType {
535 ISpectrumSender(
int fftSize = 1024,
int overlap = 1, EWindowType window = EWindowType::Hann, EOutputType outputType = EOutputType::MagPhase,
double minThresholdDb = -100.0)
537 , mWindowType(window)
538 , mOutputType(outputType)
541 SetFFTSizeAndOverlap(fftSize, overlap);
544 void SetFFTSize(
int fftSize)
546 SetFFTSizeAndOverlap(fftSize, mOverlap);
549 void SetFFTSizeAndOverlap(
int fftSize,
int overlap)
553 int hopSize = fftSize / overlap;
554 TBufferSender::SetBufferSize(hopSize);
557 CalculateScalingFactors();
560 void SetWindowType(EWindowType windowType)
562 mWindowType = windowType;
564 CalculateScalingFactors();
567 void SetOutputType(EOutputType outputType)
569 mOutputType = outputType;
574 int hopSize = TBufferSender::GetBufferSize();
576 for (
auto s = 0; s < hopSize; s++)
578 for (
auto stftFrameIdx = 0; stftFrameIdx < mOverlap; stftFrameIdx++)
580 auto& stftFrame = mSTFTFrames[stftFrameIdx];
582 for (
auto ch = 0; ch < MAXNC; ch++)
584 auto windowedValue = (float) d.vals[ch][s] * mWindow[stftFrame.pos];
585 stftFrame.bins[ch][stftFrame.pos].re = windowedValue;
586 stftFrame.bins[ch][stftFrame.pos].im = 0.0f;
591 if (stftFrame.pos >= mFFTSize)
595 for (
auto ch = 0; ch < MAXNC; ch++)
597 Permute(ch, stftFrameIdx);
598 memcpy(d.vals[ch].data(), mSTFTOutput[ch].data(), mFFTSize *
sizeof(
float));
605 int GetFFTSize()
const
610 int GetOverlap()
const
615 int GetHopSize()
const
617 return TBufferSender::GetBufferSize();
620 EWindowType GetWindowType()
const
626 void InitSTFTFrames()
628 if (mSTFTFrames.size() != mOverlap)
630 mSTFTFrames.resize(mOverlap);
633 int hopSize = mFFTSize / mOverlap;
635 for (
int i = 0; i < mOverlap; i++)
637 auto& frame = mSTFTFrames[i];
638 for (
auto ch = 0; ch < MAXNC; ch++)
640 std::fill(frame.bins[ch].begin(), frame.bins[ch].end(), WDL_FFT_COMPLEX{0.0f, 0.0f});
643 frame.pos = i * hopSize;
646 for (
auto ch = 0; ch < MAXNC; ch++)
648 std::fill(mSTFTOutput[ch].begin(), mSTFTOutput[ch].end(), 0.0f);
652 void CalculateWindow()
654 const float M =
static_cast<float>(mFFTSize - 1);
658 case EWindowType::Hann:
659 for (
auto i = 0; i < mFFTSize; i++) { mWindow[i] = 0.5f * (1.0f - std::cos(PI * 2.0f * i / M)); }
661 case EWindowType::BlackmanHarris:
662 for (
auto i = 0; i < mFFTSize; i++) {
663 mWindow[i] = 0.35875 - (0.48829f * std::cos(2.0f * PI * i / M)) +
664 (0.14128f * std::cos(4.0f * PI * i / M)) -
665 (0.01168f * std::cos(6.0f * PI * i / M));
668 case EWindowType::Hamming:
669 for (
auto i = 0; i < mFFTSize; i++) { mWindow[i] = 0.54f - 0.46f * std::cos(2.0f * PI * i / M); }
671 case EWindowType::Flattop:
672 for (
auto i = 0; i < mFFTSize; i++) {
673 mWindow[i] = 0.21557895f - 0.41663158f * std::cos(2.0f * PI * i / M) +
674 0.277263158f * std::cos(4.0f * PI * i / M) -
675 0.083578947f * std::cos(6.0f * PI * i / M) +
676 0.006947368f * std::cos(8.0f * PI * i / M);
679 case EWindowType::Rectangular:
680 std::fill(mWindow.begin(), mWindow.end(), 1.0f);
687 void CalculateScalingFactors()
691 for (
auto i = 0; i < mFFTSize; i++)
693 scaling += mWindow[i];
696 mScalingFactor = scaling * scaling;
699 void Permute(
int ch,
int frameIdx)
701 WDL_fft(mSTFTFrames[frameIdx].bins[ch].data(), mFFTSize,
false);
703 if (mOutputType == EOutputType::Complex)
705 auto nBins = mFFTSize / 2;
706 for (
auto i = 0; i < nBins; ++i)
708 int sortIdx = WDL_fft_permute(mFFTSize, i);
709 mSTFTOutput[ch][i] = mSTFTFrames[frameIdx].bins[ch][sortIdx].re;
710 mSTFTOutput[ch][i + nBins] = mSTFTFrames[frameIdx].bins[ch][sortIdx].im;
715 auto nBins = mFFTSize / 2;
716 for (
auto i = 0; i < nBins; ++i)
718 int sortIdx = WDL_fft_permute(mFFTSize, i);
719 auto re = mSTFTFrames[frameIdx].bins[ch][sortIdx].re;
720 auto im = mSTFTFrames[frameIdx].bins[ch][sortIdx].im;
721 mSTFTOutput[ch][i] = std::sqrt(2.0f * (re * re + im * im) / mScalingFactor);
722 mSTFTOutput[ch][i + nBins] = std::atan2(im, re);
730 std::array<std::array<WDL_FFT_COMPLEX, MAX_FFT_SIZE>, MAXNC> bins;
735 EWindowType mWindowType;
736 EOutputType mOutputType;
737 std::array<float, MAX_FFT_SIZE> mWindow;
738 std::vector<STFTFrame> mSTFTFrames;
739 std::array<std::array<float, MAX_FFT_SIZE>, MAXNC> mSTFTOutput;
740 float mScalingFactor = 0.0f;
IBufferSender is a utility class which can be used to defer buffer data for sending to the GUI.
void ProcessBlock(sample **inputs, int nFrames, int ctrlTag=kNoTag, int nChans=MAXNC, int chanOffset=0)
Queue sample buffers into the sender, checking the data is over the required threshold.
This pure virtual interface delegates communication in both directions between a UI editor and someth...
virtual void SendControlMsgFromDelegate(int ctrlTag, int msgTag, int dataSize=0, const void *pData=nullptr)
SendControlMsgFromDelegate (Abbreviation: SCMFD) WARNING: should not be called on the realtime audio ...
IPeakAvgSender is a utility class which can be used to defer peak & avg/RMS data from sample buffers ...
void ProcessBlock(sample **inputs, int nFrames, int ctrlTag=kNoTag, int nChans=MAXNC, int chanOffset=0)
Queue peaks from sample buffers into the sender This can be called on the realtime audio thread.
IPeakSender is a utility class which can be used to defer peak data from sample buffers for sending t...
void ProcessBlock(sample **inputs, int nFrames, int ctrlTag=kNoTag, int nChans=MAXNC, int chanOffset=0)
Queue peaks from sample buffers into the sender This can be called on the realtime audio thread.
A lock-free SPSC queue used to transfer data between threads based on MLQueue.h by Randy Jones based ...
ISender is a utility class which can be used to defer data from the realtime audio processing and sen...
ISenderData< MAXNC, T > GetLastData()
Gets the last data item sent to the UI
virtual void PrepareDataForUI(ISenderData< MAXNC, T > &d)
This is called on the main thread and can be used to transform the data, e.g.
void PushData(const ISenderData< MAXNC, T > &d)
Pushes a data element onto the queue.
void TransmitDataToControlsWithTags(IEditorDelegate &dlg, const std::initializer_list< int > &ctrlTags)
This variation can be used if you need to supply multiple controls with the same ISenderData,...
void TransmitData(IEditorDelegate &dlg)
Pops elements off the queue and sends messages to controls.
ISpectrumSender is designed for sending Spectral Data from the plug-in to the UI.
static double DBToAmp(double dB)
Calculates gain from a given dB value.
ISenderData is used to represent a typed data packet, that may contain values for multiple channels.