iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
Smoothers.h
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#pragma once
11
12#include "denormal.h"
13#include "IPlugConstants.h"
14
15BEGIN_IPLUG_NAMESPACE
16
17template<typename T, int NC = 1>
19{
20private:
21 double mA, mB;
22 T mOutM1[NC];
23
24public:
25 LogParamSmooth(double timeMs = 5., T initialValue = 0.)
26 {
27 for (auto i = 0; i < NC; i++)
28 {
29 mOutM1[i] = initialValue;
30 }
31
32 SetSmoothTime(timeMs, DEFAULT_SAMPLE_RATE);
33 }
34
35 // only works for NC = 1
36 inline T Process(T input)
37 {
38 mOutM1[0] = (input * mB) + (mOutM1[0] * mA);
39#ifndef OS_IOS
40 denormal_fix(&mOutM1[0]);
41#endif
42 return mOutM1[0];
43 }
44
45 inline void SetValue(T value)
46 {
47 for (auto i = 0; i < NC; i++)
48 {
49 mOutM1[i] = value;
50 }
51 }
52
53 inline void SetValues(T values[NC])
54 {
55 for (auto i = 0; i < NC; i++)
56 {
57 mOutM1[i] = values[i];
58 }
59 }
60
61 void SetSmoothTime(double timeMs, double sampleRate)
62 {
63 static constexpr double TWO_PI = 6.283185307179586476925286766559;
64
65 mA = exp(-TWO_PI / (timeMs * 0.001 * sampleRate));
66 mB = 1.0 - mA;
67 }
68
69 void ProcessBlock(T inputs[NC], T** outputs, int nFrames, int channelOffset = 0)
70 {
71 const T b = mB;
72 const T a = mA;
73
74 for (auto s = 0; s < nFrames; ++s)
75 {
76 for (auto c = 0; c < NC; c++)
77 {
78 T output = (inputs[channelOffset + c] * b) + (mOutM1[c] * a);
79#ifndef OS_IOS
80 denormal_fix(&output);
81#endif
82 mOutM1[c] = output;
83 outputs[channelOffset + c][s] = output;
84 }
85 }
86 }
87
88} WDL_FIXALIGN;
89
90template<typename T>
92{
93public:
94 SmoothedGain(double smoothingTime = 5.0)
95 : mSmoothingTime(smoothingTime)
96 {
97 }
98
99 void ProcessBlock(T** inputs, T** outputs, int nChans, int nFrames, double gainValue)
100 {
101 for (auto s = 0; s < nFrames; ++s)
102 {
103 const T smoothedGain = static_cast<T>(mSmoother.Process(gainValue));
104
105 for (auto c = 0; c < nChans; c++)
106 {
107 outputs[c][s] = inputs[c][s] * smoothedGain;
108 }
109 }
110 }
111
112 void SetSampleRate(double sampleRate)
113 {
114 mSmoother.SetSmoothTime(mSmoothingTime, sampleRate);
115 }
116
117private:
118 const double mSmoothingTime;
120} WDL_FIXALIGN;
121
122END_IPLUG_NAMESPACE
IPlug Constant definitions, Types, magic numbers.