f@0
|
1 //
|
f@0
|
2 // AccessiblePeakMeter.cpp
|
f@0
|
3 //
|
f@0
|
4 // Author: Fiore Martin
|
f@0
|
5 // Started from IPlugMultiTargets example in WDL-OL, by Oli Larkin - https://github.com/olilarkin/wdl-ol
|
f@0
|
6 //
|
f@0
|
7 // Licensed under the Cockos WDL License, see README.txt
|
f@0
|
8 //
|
f@0
|
9
|
f@0
|
10
|
f@0
|
11 #include "AccessiblePeakMeter.h"
|
f@0
|
12 #include "IPlug_include_in_plug_src.h"
|
f@0
|
13 #include "resource.h"
|
f@0
|
14
|
f@0
|
15
|
f@0
|
16 #include "IControl.h"
|
f@0
|
17 #include "IBitmapMonoText.h"
|
f@0
|
18 #include "AccessiblePeakMeter_controls.h"
|
f@0
|
19
|
f@0
|
20
|
f@0
|
21 inline double midi2Freq(int note) {
|
f@0
|
22 return 440. * pow(2., (note - 69.) / 12.);
|
f@0
|
23 }
|
f@0
|
24
|
f@0
|
25 double toDBMeter(double val, double range)
|
f@0
|
26 {
|
f@0
|
27 double db;
|
f@0
|
28 if (val > 0)
|
f@0
|
29 db = ::AmpToDB(val);
|
f@0
|
30 else
|
f@0
|
31 db = -999;
|
f@0
|
32 return BOUNDED((db + 60) / range,0,1);
|
f@0
|
33 }
|
f@0
|
34
|
f@0
|
35 /* reference points for controls layout, by changing these numbers only
|
f@0
|
36 the widgets can be moved around and all the other bits (top/left/right
|
f@0
|
37 borders, labels etc.) will follow. X and Y refer to the top-left coord */
|
f@0
|
38 enum ELayout {
|
f@0
|
39 lDryX = 20,
|
f@0
|
40 lDryY = 10,
|
f@0
|
41 lWetX = 85,
|
f@0
|
42 lWetY = 10,
|
f@0
|
43
|
f@0
|
44 lFaderLen = 190,
|
f@0
|
45 lPeakMeterX = 180,
|
f@0
|
46 lPeaklMeterY = 30,
|
f@0
|
47
|
f@0
|
48 lSonifTypeX = 20,
|
f@0
|
49 lSonifTypeY = 200,
|
f@0
|
50
|
f@0
|
51 lDecayRateX = 20,
|
f@0
|
52 lDecayRateY = 90
|
f@0
|
53 };
|
f@0
|
54
|
f@0
|
55
|
f@0
|
56 enum EParams
|
f@0
|
57 {
|
f@0
|
58 kDry = 0,
|
f@0
|
59 kWet,
|
f@0
|
60 kThreshold,
|
f@0
|
61 kSonificationType,
|
f@0
|
62 kMeterDecayRate,
|
f@0
|
63 kNumParams
|
f@0
|
64 };
|
f@0
|
65
|
f@0
|
66
|
f@0
|
67 AccessiblePeakMeter::AccessiblePeakMeter(IPlugInstanceInfo instanceInfo)
|
f@0
|
68 : IPLUG_CTOR(kNumParams, NUM_PRESETS, instanceInfo),
|
f@0
|
69 mDry(DRYWET_DEFAULT),
|
f@0
|
70 mWet(DRYWET_DEFAULT),
|
f@0
|
71 mMeterDecayRate(1.0),
|
f@0
|
72 mSampleRate(44100.),
|
f@0
|
73 mThreshold(1.0)
|
f@0
|
74 {
|
f@0
|
75 TRACE;
|
f@0
|
76
|
f@0
|
77 for (int i = 0; i < MAX_CHANNELS; i++) {
|
f@0
|
78 mPrevPeak[i] = 0.0;
|
f@0
|
79 }
|
f@0
|
80
|
f@0
|
81 //arguments are: name, defaultVal, minVal, maxVal, step, label
|
f@0
|
82 GetParam(kDry)->InitDouble("Dry", -6., -61.0, 0., 0.2, "dB");
|
f@0
|
83 GetParam(kDry)->SetDisplayText(-61.0, " -inf");
|
f@0
|
84 GetParam(kWet)->InitDouble("Wet", -6., -61.0, 0., 0.2, "dB");
|
f@0
|
85 GetParam(kWet)->SetDisplayText(-61.0, " -inf");
|
f@0
|
86 GetParam(kThreshold)->InitDouble("Threshold", 0.0, -60.0, 6.2, 0.2, "dB");
|
f@2
|
87 GetParam(kSonificationType)->InitEnum("Sonification Type", SONIFICATION_TYPE_DEFAULT, 2);
|
f@0
|
88 GetParam(kSonificationType)->SetDisplayText(SONIFICATION_TYPE_CONTINUOUS, "Continuous");
|
f@0
|
89 GetParam(kSonificationType)->SetDisplayText(SONIFICATION_TYPE_CLIPPING, "Clipping");
|
f@0
|
90 GetParam(kMeterDecayRate)->InitDouble("Decay", 1.0, 0.05, 1.0, 0.05, "sec.");
|
f@0
|
91
|
f@0
|
92 IGraphics* pGraphics = MakeGraphics(this, GUI_WIDTH, GUI_HEIGHT);
|
f@0
|
93 pGraphics->AttachBackground(BG_ID, BG_FN);
|
f@0
|
94
|
f@0
|
95 /* load bitmaps for fader, knob and switch button */
|
f@0
|
96 IBitmap knob = pGraphics->LoadIBitmap(KNOB_ID, KNOB_FN, NUM_KNOB_FRAMES);
|
f@0
|
97 IBitmap faderBmap = pGraphics->LoadIBitmap(FADER_ID, FADER_FN);
|
f@0
|
98 IBitmap aSwitch = pGraphics->LoadIBitmap(SWITCH_ID, SWITCH_FN,2);
|
f@0
|
99
|
f@0
|
100 /* text has info about the font-size, font-type etc. */
|
f@0
|
101 IText text = IText(14);
|
f@0
|
102
|
f@0
|
103 /* attach sonification type switch to the GUI */
|
f@0
|
104 pGraphics->AttachControl(new ISwitchPopUpControl(this, lSonifTypeX ,lSonifTypeY, kSonificationType, &aSwitch));
|
f@0
|
105 pGraphics->AttachControl(new ITextControl(this, IRECT(lSonifTypeX+10, lSonifTypeY - 20, lSonifTypeX + 110, lSonifTypeY ), &text, "Sonification Type"));
|
f@0
|
106
|
f@0
|
107 /* attach dry and wet knobs to GUI */
|
f@0
|
108 pGraphics->AttachControl(new IKnobMultiControlText(this, IRECT(lDryX, lDryY, lDryX + 52, lDryY + 48 + 19 + 19 ), kDry, &knob, &text, 27)); // 48 for image, 19 for text
|
f@0
|
109 pGraphics->AttachControl(new IKnobMultiControlText(this, IRECT(lWetX, lWetY, lWetX + 52, lWetY + 48 + 19 + 19), kWet, &knob, &text, 27));
|
f@0
|
110
|
f@0
|
111 /* attach decay rate knob to the GUI */
|
f@0
|
112 pGraphics->AttachControl(new IKnobMultiControlText(this, IRECT(lDecayRateX, lDecayRateY, lDecayRateX + 48 , lDecayRateY + 48 + 19 + 19 ), kMeterDecayRate, &knob, &text, 33));
|
f@0
|
113
|
f@0
|
114 /* attach fader display, which shows the fader value, to GUI */
|
f@0
|
115 ITextControl *faderText = new ITextControl(this, IRECT(lPeakMeterX+60, lPeaklMeterY + lFaderLen, lPeakMeterX + faderBmap.W + 95, lPeaklMeterY + lFaderLen + 20), &text);
|
f@0
|
116 pGraphics->AttachControl(faderText);
|
f@0
|
117
|
f@0
|
118 /* attach the fader to GUI */
|
f@0
|
119 pGraphics->AttachControl(new IFaderVertText(this, lPeakMeterX, lPeaklMeterY, lFaderLen, kThreshold, &faderBmap, faderText));
|
f@0
|
120
|
f@0
|
121 pGraphics->AttachControl(new ITextControl(this, IRECT(lPeakMeterX, lPeaklMeterY - 20, lPeakMeterX + 100, lPeaklMeterY), &text, "Peak Level Meter"));
|
f@0
|
122 pGraphics->AttachControl(new ITextControl(this, IRECT(lPeakMeterX-20, lPeaklMeterY + lFaderLen, lPeakMeterX + 75, lPeaklMeterY + lFaderLen + 20), &text, "Threshold: "));
|
f@0
|
123
|
f@0
|
124 /* attach peak meters to GUI.
|
f@0
|
125 Half the bitmap height is added to the peak meters on both top and bottom to prevent the
|
f@0
|
126 triangular fader from going past the peak meters span
|
f@0
|
127 */
|
f@0
|
128 const int halfFaderBmapLen = faderBmap.W / 2;
|
f@0
|
129 mMeterIdx[0] = pGraphics->AttachControl(new IPeakMeterVert(this,
|
f@0
|
130 IRECT(lPeakMeterX + 25, lPeaklMeterY + halfFaderBmapLen, lPeakMeterX + 45, lPeaklMeterY + 170 + halfFaderBmapLen), GetParam(kThreshold)->GetDefaultNormalized()));
|
f@0
|
131 mMeterIdx[1] = pGraphics->AttachControl(new IPeakMeterVert(this,
|
f@0
|
132 IRECT(lPeakMeterX + 50, lPeaklMeterY + halfFaderBmapLen, lPeakMeterX + 70, lPeaklMeterY + lFaderLen - halfFaderBmapLen), GetParam(kThreshold)->GetDefaultNormalized()));
|
f@0
|
133
|
f@0
|
134 AttachGraphics(pGraphics);
|
f@0
|
135
|
f@0
|
136 /* add presets */
|
f@0
|
137 MakePreset("Detect Clipping", DRYWET_DEFAULT, DRYWET_DEFAULT, THRESHOLD_DEFAULT, SONIFICATION_TYPE_CLIPPING, METERDECAY_DEFAULT);
|
f@0
|
138 MakePreset("Sonify Audio", DRYWET_DEFAULT, DRYWET_DEFAULT, THRESHOLD_DEFAULT, SONIFICATION_TYPE_CONTINUOUS, METERDECAY_DEFAULT);
|
f@0
|
139 }
|
f@0
|
140
|
f@0
|
141 AccessiblePeakMeter::~AccessiblePeakMeter() {}
|
f@0
|
142
|
f@0
|
143
|
f@0
|
144 void AccessiblePeakMeter::ProcessDoubleReplacing(double** inputs, double** outputs, int nFrames)
|
f@0
|
145 {
|
f@0
|
146 if(mSonification.type == SONIFICATION_TYPE_CONTINUOUS) {
|
f@0
|
147 addContinuousSonification(inputs, outputs, nFrames);
|
f@0
|
148 } else {
|
f@0
|
149 addClippingSonification(inputs, outputs, nFrames);
|
f@0
|
150 }
|
f@0
|
151 }
|
f@0
|
152
|
f@0
|
153
|
f@0
|
154 void AccessiblePeakMeter::Reset()
|
f@0
|
155 {
|
f@0
|
156 TRACE;
|
f@0
|
157 IMutexLock lock(this);
|
f@0
|
158
|
f@0
|
159 mSampleRate = GetSampleRate();
|
f@0
|
160
|
f@0
|
161 mSonification.reset(mSampleRate);
|
f@0
|
162
|
f@0
|
163 for (int i = 0; i < MAX_CHANNELS; i++) {
|
f@0
|
164 mPrevPeak[i] = 0.0;
|
f@0
|
165 }
|
f@0
|
166
|
f@0
|
167 }
|
f@0
|
168
|
f@0
|
169 void AccessiblePeakMeter::OnParamChange(int paramIdx)
|
f@0
|
170 {
|
f@0
|
171 IMutexLock lock(this);
|
f@0
|
172
|
f@0
|
173 switch (paramIdx)
|
f@0
|
174 {
|
f@0
|
175 case kDry:
|
f@0
|
176 /* if the level goes below 60.5 dB, just bring it to silence */
|
f@0
|
177 if (GetParam(kDry)->Value() < -60.5 ){
|
f@0
|
178 mDry = 0.0;
|
f@0
|
179 }
|
f@0
|
180 else {
|
f@0
|
181 mDry = ::DBToAmp(GetParam(kDry)->Value());
|
f@0
|
182 }
|
f@0
|
183 break;
|
f@0
|
184
|
f@0
|
185 case kWet:
|
f@0
|
186 if (GetParam(kWet)->Value() < -60.5){
|
f@0
|
187 mWet = 0.0;
|
f@0
|
188 }
|
f@0
|
189 else{
|
f@0
|
190 mWet = ::DBToAmp(GetParam(kWet)->Value());
|
f@0
|
191 }
|
f@0
|
192 break;
|
f@0
|
193
|
f@0
|
194 case kThreshold:
|
f@0
|
195 mThreshold = GetParam(kThreshold)->DBToAmp();
|
f@0
|
196 break;
|
f@0
|
197
|
f@0
|
198 case kMeterDecayRate :
|
f@0
|
199 mMeterDecayRate = 1.0 / GetParam(kMeterDecayRate)->Value();
|
f@0
|
200 break;
|
f@0
|
201
|
f@0
|
202 case kSonificationType:
|
f@0
|
203 mSonification.type = GetParam(kSonificationType)->Int();
|
f@0
|
204
|
f@0
|
205 mSonification.reset();
|
f@0
|
206
|
f@0
|
207 for (int i = 0; i < MAX_CHANNELS; i++) {
|
f@0
|
208 mPrevPeak[i] = 0.0;
|
f@0
|
209 mSonification.ugen[i].setFrequency(mSonification.type == SONIFICATION_TYPE_CLIPPING ? 440.0 : 0.0);
|
f@0
|
210 }
|
f@0
|
211 break;
|
f@0
|
212
|
f@0
|
213 default:
|
f@0
|
214 break;
|
f@0
|
215 }
|
f@0
|
216 }
|
f@0
|
217
|
f@0
|
218 void AccessiblePeakMeter::addClippingSonification(double** inputs, double** outputs, int nFrames) {
|
f@0
|
219 // Mutex is already locked for us.
|
f@0
|
220
|
f@0
|
221 for (unsigned int channel = 0; channel < NInChannels(); channel++) {
|
f@0
|
222 double* in = inputs[channel];
|
f@0
|
223 double* out = outputs[channel];
|
f@0
|
224 double peak = 0.0;
|
f@0
|
225
|
f@0
|
226 /* find the max absolute value in the block of samples */
|
f@0
|
227 for (int offset = 0; offset < nFrames; ++offset, ++in, ++out) {
|
f@0
|
228 const double ampl = fabs(*in); // amplitude
|
f@0
|
229 peak = IPMAX(peak, ampl); // find max peak for this block
|
f@0
|
230
|
f@0
|
231 if (ampl > mThreshold) {
|
f@0
|
232 /* find the clipping amount in dB */
|
f@0
|
233 double clippingDiff = fabs(::AmpToDB(ampl) - ::AmpToDB(mThreshold));
|
f@0
|
234
|
f@0
|
235 /* clipDiff will be rounded downward later, but if it's very very
|
f@0
|
236 close to the ceil, then let it be the ceil.
|
f@0
|
237 */
|
f@0
|
238 const double ceilClippingDiff = ceil(clippingDiff);
|
f@0
|
239 if (ceilClippingDiff - clippingDiff < CLIPPING_CEILING_SNAP){
|
f@0
|
240 clippingDiff = ceilClippingDiff;
|
f@0
|
241 }
|
f@0
|
242
|
f@0
|
243 if (clippingDiff > mSonification.clipping.maxDiff[channel]){
|
f@0
|
244 /* bound the difference to 12 semitones to prevent the sonification from going too high */
|
f@0
|
245 mSonification.clipping.maxDiff[channel] = BOUNDED(clippingDiff, 0.0, 12.0);
|
f@0
|
246 }
|
f@0
|
247
|
f@0
|
248 /* sonify the difference between the amplitude and threshold *
|
f@0
|
249 * one db (rounded downward) is one tone, up to one octave (12 semitones) */
|
f@0
|
250 mSonification.ugen[channel].setFrequency(midi2Freq(69 + (int)(mSonification.clipping.maxDiff[channel])));
|
f@0
|
251 mSonification.clipping.envelope[channel].keyOn();
|
f@0
|
252 }
|
f@0
|
253
|
f@0
|
254 /* when attack is done switch immediately to RELEASE (keyOff) *
|
f@0
|
255 * so it goes like: attack->release->silence */
|
f@0
|
256 if (mSonification.clipping.envelope[channel].getState() == stk::ADSR::DECAY) {
|
f@0
|
257 mSonification.clipping.envelope[channel].keyOff();
|
f@0
|
258 }
|
f@0
|
259
|
f@0
|
260 /* add the sonification to the mix */
|
f@0
|
261 if (mSonification.clipping.envelope[channel].getState() == stk::ADSR::ATTACK ||
|
f@0
|
262 mSonification.clipping.envelope[channel].getState() == stk::ADSR::RELEASE) {
|
f@0
|
263 const double env = mSonification.clipping.envelope[channel].tick();
|
f@0
|
264 const double tick = mSonification.ugen[channel].tick() * env;
|
f@0
|
265 *out = mix(*in, tick);
|
f@0
|
266 } else { // no sonification
|
f@0
|
267 mSonification.clipping.maxDiff[channel] = 0.0; // reset max clipping diff
|
f@0
|
268 *out = mix(*in, 0.0); // still honours the user's knobs settings
|
f@0
|
269 }
|
f@0
|
270 }
|
f@0
|
271
|
f@0
|
272 /* now draw the peak meter with the maximum of this block of samples */
|
f@0
|
273
|
f@0
|
274 const double deltaT = nFrames / mSampleRate;
|
f@0
|
275 const double decayAmount = deltaT * mMeterDecayRate;
|
f@0
|
276
|
f@0
|
277 peak = ::toDBMeter(peak, DB_RANGE);
|
f@0
|
278
|
f@0
|
279 /* max between new peak and old peak decay wins */
|
f@0
|
280 peak = IPMAX(peak, mPrevPeak[channel] - decayAmount);
|
f@0
|
281
|
f@0
|
282 /* save the peak for next block of samples */
|
f@0
|
283 mPrevPeak[channel] = peak;
|
f@0
|
284
|
f@0
|
285 /* update the GUI */
|
f@0
|
286 if (GetGUI()) {
|
f@0
|
287 GetGUI()->SetControlFromPlug(mMeterIdx[channel], peak);
|
f@0
|
288 }
|
f@0
|
289 }
|
f@0
|
290
|
f@0
|
291 }
|
f@0
|
292
|
f@0
|
293 void AccessiblePeakMeter::addContinuousSonification(double** inputs, double** outputs, int nFrames) {
|
f@0
|
294 // Mutex is already locked for us.
|
f@0
|
295
|
f@0
|
296 const int nChannels = NInChannels();
|
f@0
|
297
|
f@0
|
298 const double deltaT = nFrames / mSampleRate;
|
f@0
|
299 const double decayAmount = deltaT * mMeterDecayRate;
|
f@0
|
300
|
f@0
|
301 for (int channel = 0; channel < nChannels; channel++){
|
f@0
|
302 double peak = 0.0;
|
f@0
|
303 double *in = inputs[channel];
|
f@0
|
304
|
f@0
|
305 /* find the max absolute value in the block of samples */
|
f@0
|
306 for (int offset = 0; offset < nFrames; ++offset, ++in) {
|
f@0
|
307 peak = IPMAX(peak, fabs(*in));
|
f@0
|
308 }
|
f@0
|
309
|
f@0
|
310 /* pick the max between new audio and peak meter decaying */
|
f@0
|
311 peak = ::toDBMeter(peak, DB_RANGE);
|
f@0
|
312 peak = IPMAX(peak, mPrevPeak[channel] - decayAmount);
|
f@0
|
313
|
f@0
|
314 /* set the sonification frequency according to the last peak value */
|
f@0
|
315 const double sonifFreq = SONIFICATION_RANGE * peak;
|
f@0
|
316 mSonification.ugen[channel].setFrequency(sonifFreq);
|
f@0
|
317
|
f@0
|
318 /* If level goes below audible level just hush the sonification. *
|
f@0
|
319 * this avoids DC offset when sonification frequency gets too low. *
|
f@0
|
320 * Uses an envelope to bring the sonification volume down smoothly
|
f@0
|
321 */
|
f@0
|
322 if (sonifFreq < MIN_SONIFICATION_FREQ){
|
f@0
|
323 /* turn the sonification off, if it's not off already */
|
f@0
|
324 if (mSonification.continous.isOn[channel]){
|
f@0
|
325 mSonification.continous.isOn[channel] = false;
|
f@0
|
326 mSonification.continous.envelope[channel].setTarget(0.0);
|
f@0
|
327 }
|
f@0
|
328 }
|
f@0
|
329 else if (!mSonification.continous.isOn[channel]){
|
f@0
|
330 /* if the sonification frequency goes past MIN_SONIFICATION_FREQ *
|
f@0
|
331 turn it on again unless it's already on */
|
f@0
|
332 mSonification.continous.envelope[channel].setValue(1.0);
|
f@0
|
333 mSonification.continous.isOn[channel] = true;
|
f@0
|
334 }
|
f@0
|
335
|
f@0
|
336 in = inputs[channel];
|
f@0
|
337 double *out = outputs[channel];
|
f@0
|
338 /* add peak meter line continuous sonification to output */
|
f@0
|
339 for (int offset = 0; offset < nFrames; ++offset, ++in, ++out) {
|
f@0
|
340 double tick = mSonification.ugen[channel].tick();
|
f@0
|
341 tick *= mSonification.continous.envelope[channel].tick(); // apply envelope
|
f@0
|
342 /* write the output buffer: mix original audio + sonification */
|
f@0
|
343 *out = mix(*in, tick);
|
f@0
|
344 }
|
f@0
|
345
|
f@0
|
346 /* save the peaks for next block of samples */
|
f@0
|
347 mPrevPeak[channel] = peak;
|
f@0
|
348
|
f@0
|
349 /* update the GUI */
|
f@0
|
350 if (GetGUI()) {
|
f@0
|
351 GetGUI()->SetControlFromPlug(mMeterIdx[channel], peak);
|
f@0
|
352 }
|
f@0
|
353 }
|
f@0
|
354 }
|
f@0
|
355
|
f@0
|
356 //Called by the standalone wrapper if someone clicks about
|
f@0
|
357 bool AccessiblePeakMeter::HostRequestingAboutBox()
|
f@0
|
358 {
|
f@0
|
359 IMutexLock lock(this);
|
f@0
|
360 if(GetGUI())
|
f@0
|
361 {
|
f@0
|
362 // do nothing
|
f@0
|
363 }
|
f@0
|
364 return true;
|
f@0
|
365 }
|
f@0
|
366
|
f@0
|
367
|
f@0
|
368 // -------------- static variables init -------------
|
f@0
|
369
|
f@0
|
370 const double AccessiblePeakMeter::DRYWET_DEFAULT = -6.0;
|
f@2
|
371 const int AccessiblePeakMeter::SONIFICATION_TYPE_DEFAULT = 1;
|
f@0
|
372 const double AccessiblePeakMeter::METERDECAY_DEFAULT = 60.0;
|
f@0
|
373 const double AccessiblePeakMeter::THRESHOLD_DEFAULT = 0.0;
|
f@0
|
374
|
f@0
|
375 const double AccessiblePeakMeter::DB_RANGE = 66.0;
|
f@0
|
376 const double AccessiblePeakMeter::SONIFICATION_RANGE = 2000;
|
f@0
|
377 const int AccessiblePeakMeter::SONIFICATION_TYPE_CLIPPING = 1;
|
f@0
|
378 const int AccessiblePeakMeter::SONIFICATION_TYPE_CONTINUOUS = 0;
|
f@0
|
379 const double AccessiblePeakMeter::BEEP_TIME = 0.2;
|
f@0
|
380 const double AccessiblePeakMeter::MIN_SONIFICATION_FREQ = 20.0;
|
f@0
|
381 const double AccessiblePeakMeter::CLIPPING_CEILING_SNAP = 0.05;
|
f@0
|
382
|
f@0
|
383 const int AccessiblePeakMeter::NUM_KNOB_FRAMES = 60;
|
f@0
|
384 const int AccessiblePeakMeter::NUM_PRESETS = 2;
|
f@0
|
385
|
f@0
|
386
|