Mercurial > hg > apm2s
comparison AccessiblePeakMeter.cpp @ 0:4606bd505630 tip
first import
author | Fiore Martin <f.martin@qmul.ac.uk> |
---|---|
date | Sat, 13 Jun 2015 15:08:10 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4606bd505630 |
---|---|
1 // | |
2 // AccessiblePeakMeter.cpp | |
3 // | |
4 // Author: Fiore Martin | |
5 // Started from IPlugMultiTargets example in WDL-OL, by Oli Larkin - https://github.com/olilarkin/wdl-ol | |
6 // | |
7 // Licensed under the Cockos WDL License, see README.txt | |
8 // | |
9 | |
10 #include "AccessiblePeakMeter.h" | |
11 #include "IPlug_include_in_plug_src.h" | |
12 #include "resource.h" | |
13 | |
14 | |
15 #include "IControl.h" | |
16 #include "IBitmapMonoText.h" | |
17 #include "AccessiblePeakMeter_controls.h" | |
18 | |
19 #include <memory> | |
20 | |
21 inline double midi2Freq(int note) { | |
22 return 440. * pow(2., (note - 69.) / 12.); | |
23 } | |
24 | |
25 double toDBMeter(double val, double range) | |
26 { | |
27 double db; | |
28 if (val > 0) | |
29 db = ::AmpToDB(val); | |
30 else | |
31 db = -999; | |
32 return BOUNDED((db + 60) / range,0,1); | |
33 } | |
34 | |
35 /* reference points for controls layout, by changing these numbers only | |
36 the widgets can be moved around and all the other bits (top/left/right | |
37 borders, labels etc.) will follow. X and Y refer to the top-left coord */ | |
38 enum ELayout { | |
39 lDryX = 20, | |
40 lDryY = 10, | |
41 lWetX = 85, | |
42 lWetY = 10, | |
43 | |
44 lFaderLen = 190, | |
45 lPeakMeterX = 180, | |
46 lPeaklMeterY = 30, | |
47 | |
48 lSonifTypeX = 20, | |
49 lSonifTypeY = 200, | |
50 | |
51 lDecayRateX = 20, | |
52 lDecayRateY = 90 | |
53 }; | |
54 | |
55 enum EParams | |
56 { | |
57 kDry = 0, | |
58 kWet, | |
59 kThreshold, | |
60 kSonificationType, | |
61 kMeterDecayRate, | |
62 kNumParams | |
63 }; | |
64 | |
65 AccessiblePeakMeter::AccessiblePeakMeter(IPlugInstanceInfo instanceInfo) | |
66 : IPLUG_CTOR(kNumParams, NUM_PRESETS, instanceInfo), | |
67 mDry(DRY_DEFAULT), | |
68 mWet(WET_DEFAULT), | |
69 mMeterDecayRate(1.0), | |
70 mThreshold(1.0), | |
71 mSampleRate(44100.), | |
72 mSonificationType(SONIFICATION_TYPE_CLIPPING) | |
73 | |
74 { | |
75 TRACE; | |
76 | |
77 //arguments are: name, defaultVal, minVal, maxVal, step, label | |
78 GetParam(kDry)->InitDouble("Dry", DRY_DEFAULT, -61.0, 0., 0.2, "dB"); | |
79 GetParam(kDry)->SetDisplayText(-61.0, " -inf"); | |
80 GetParam(kWet)->InitDouble("Wet", WET_DEFAULT, -61.0, 0., 0.2, "dB"); | |
81 GetParam(kWet)->SetDisplayText(-61.0, " -inf"); | |
82 GetParam(kThreshold)->InitDouble("Threshold", 0.0, -60.0, 6.2, 0.2, "dB"); | |
83 GetParam(kSonificationType)->InitEnum("Sonification Type", SONIFICATION_TYPE_DEFAULT, 2); | |
84 GetParam(kSonificationType)->SetDisplayText(SONIFICATION_TYPE_CLIPPING, "Clipping"); | |
85 GetParam(kSonificationType)->SetDisplayText(SONIFICATION_TYPE_CONTINUOUS, "Continuous"); | |
86 | |
87 GetParam(kMeterDecayRate)->InitDouble("Decay", 1.0, 0.05, 1.0, 0.05, "sec."); | |
88 | |
89 IGraphics* pGraphics = MakeGraphics(this, GUI_WIDTH, GUI_HEIGHT); | |
90 pGraphics->AttachBackground(BG_ID, BG_FN); | |
91 | |
92 /* load bitmaps for fader, knob and switch button */ | |
93 IBitmap knob = pGraphics->LoadIBitmap(KNOB_ID, KNOB_FN, NUM_KNOB_FRAMES); | |
94 IBitmap faderBmap = pGraphics->LoadIBitmap(FADER_ID, FADER_FN); | |
95 IBitmap aSwitch = pGraphics->LoadIBitmap(SWITCH_ID, SWITCH_FN,2); | |
96 | |
97 //pGraphics->AttachKeyCatcher(new IKeyCatcher(this, IRECT(0, 0, GUI_WIDTH, GUI_HEIGHT))); | |
98 | |
99 /* text has info about the font-size, font-type etc. */ | |
100 IText text = IText(14); | |
101 /* attach sonification type switch to the GUI */ | |
102 pGraphics->AttachControl(new ISwitchPopUpControl(this, lSonifTypeX ,lSonifTypeY, kSonificationType, &aSwitch)); | |
103 pGraphics->AttachControl(new ITextControl(this, IRECT(lSonifTypeX+10, lSonifTypeY - 20, lSonifTypeX + 110, lSonifTypeY ), &text, "Sonification Type")); | |
104 | |
105 /* attach dry and wet knobs to GUI */ | |
106 pGraphics->AttachControl(new IKnobMultiControlText(this, IRECT(lDryX, lDryY, lDryX + 52, lDryY + 48 + 20 + 20), kDry, &knob, &text, 27)); | |
107 pGraphics->AttachControl(new IKnobMultiControlText(this, IRECT(lWetX, lWetY, lWetX + 52, lWetY + 48 + 20 + 20), kWet, &knob, &text, 27)); | |
108 | |
109 /* attach decay rate knob to the GUI */ | |
110 pGraphics->AttachControl(new IKnobMultiControlText(this, IRECT(lDecayRateX, lDecayRateY, lDecayRateX + 48, lDecayRateY + 48 + 20 + 20), kMeterDecayRate, &knob, &text, 33)); | |
111 | |
112 /* attach fader display, which shows the fader value, to GUI */ | |
113 ITextControl *faderText = new ITextControl(this, IRECT(lPeakMeterX+60, lPeaklMeterY + lFaderLen, lPeakMeterX + faderBmap.W + 95, lPeaklMeterY + lFaderLen + 20), &text); | |
114 pGraphics->AttachControl(faderText); | |
115 | |
116 /* attach the fader to GUI */ | |
117 pGraphics->AttachControl(new IFaderVertText(this, lPeakMeterX, lPeaklMeterY, lFaderLen, kThreshold, &faderBmap, faderText)); | |
118 | |
119 pGraphics->AttachControl(new ITextControl(this, IRECT(lPeakMeterX, lPeaklMeterY - 20, lPeakMeterX + 100, lPeaklMeterY), &text, "Peak Level Meter")); | |
120 pGraphics->AttachControl(new ITextControl(this, IRECT(lPeakMeterX-20, lPeaklMeterY + lFaderLen, lPeakMeterX + 75, lPeaklMeterY + lFaderLen + 20), &text, "Threshold: ")); | |
121 | |
122 /* attach peak meters to GUI */ | |
123 /* half the bitmap height is added to the peak meters on both top and bottom to prevent the fader | |
124 - a triangle pointing at half the height of the bitmap - from overflowing the peak meters */ | |
125 int halfFaderBmapLen = faderBmap.W / 2; | |
126 mMeterIdx[0] = pGraphics->AttachControl(new IPeakMeterVert(this, IRECT(lPeakMeterX + 25, lPeaklMeterY + halfFaderBmapLen, lPeakMeterX + 45, lPeaklMeterY + 170 + halfFaderBmapLen), | |
127 GetParam(kThreshold)->GetDefaultNormalized())); | |
128 mMeterIdx[1] = pGraphics->AttachControl(new IPeakMeterVert(this, | |
129 IRECT(lPeakMeterX + 50, lPeaklMeterY + halfFaderBmapLen, lPeakMeterX + 70, lPeaklMeterY + lFaderLen - halfFaderBmapLen), GetParam(kThreshold)->GetDefaultNormalized())); | |
130 | |
131 AttachGraphics(pGraphics); | |
132 | |
133 //kDry, kWet, kThreshold, kSonificationType, kMeterDecayRate, | |
134 MakePreset("Detect Clipping", DRY_DEFAULT, WET_DEFAULT, THRESHOLD_DEFAULT, SONIFICATION_TYPE_CLIPPING, METERDECAY_DEFAULT); | |
135 MakePreset("Sonify Audio", DRY_DEFAULT, WET_DEFAULT, THRESHOLD_DEFAULT, SONIFICATION_TYPE_CONTINUOUS, METERDECAY_DEFAULT); | |
136 | |
137 } | |
138 | |
139 AccessiblePeakMeter::~AccessiblePeakMeter() { } | |
140 | |
141 | |
142 void AccessiblePeakMeter::ProcessDoubleReplacing(double** inputs, double** outputs, int nFrames) | |
143 { | |
144 if(mSonificationType == SONIFICATION_TYPE_CONTINUOUS) { | |
145 addContinuousSonification(inputs, outputs, nFrames); | |
146 } else { | |
147 addClippingSonification(inputs, outputs, nFrames); | |
148 } | |
149 } | |
150 | |
151 | |
152 void AccessiblePeakMeter::Reset() | |
153 { | |
154 TRACE; | |
155 IMutexLock lock(this); | |
156 | |
157 mSampleRate = GetSampleRate(); | |
158 | |
159 for (int i = 0; i < MAX_CHANNELS; i++) { | |
160 mPrevPeak[i] = 0.0; | |
161 } | |
162 | |
163 if (!sDacThread.started){ | |
164 sDacThread.started = true; | |
165 sDacThread.t = std::move(std::thread(DacRoutine)); | |
166 } | |
167 } | |
168 | |
169 void AccessiblePeakMeter::OnParamChange(int paramIdx) | |
170 { | |
171 IMutexLock lock(this); | |
172 | |
173 switch (paramIdx) | |
174 { | |
175 case kDry : | |
176 if (GetParam(kDry)->Value() < -60.5){ | |
177 mDry = 0.0; | |
178 } | |
179 else { | |
180 mDry = ::DBToAmp(GetParam(kDry)->Value()); | |
181 } | |
182 break; | |
183 | |
184 case kWet: | |
185 if (GetParam(kWet)->Value() < -60.5){ | |
186 mWet = 0.0; | |
187 } | |
188 else{ | |
189 mWet = ::DBToAmp(GetParam(kWet)->Value()); | |
190 } | |
191 break; | |
192 | |
193 case kThreshold: | |
194 mThreshold = GetParam(kThreshold)->DBToAmp(); | |
195 break; | |
196 | |
197 case kMeterDecayRate : | |
198 mMeterDecayRate = 1.0 / GetParam(kMeterDecayRate)->Value(); | |
199 break; | |
200 | |
201 case kSonificationType: | |
202 mSonificationType = GetParam(kSonificationType)->Int(); | |
203 | |
204 for (int i = 0; i < MAX_CHANNELS; i++) { | |
205 mPrevPeak[i] = 0.0; | |
206 } | |
207 | |
208 sDacMutex.lock(); | |
209 sDacSynced.sonificationType = mSonificationType; | |
210 sDacMutex.unlock(); | |
211 | |
212 break; | |
213 | |
214 default: | |
215 break; | |
216 } | |
217 } | |
218 | |
219 void AccessiblePeakMeter::addClippingSonification(double** inputs, double** outputs, int nFrames) { | |
220 // Mutex is already locked for us. | |
221 | |
222 double clippingDiff[MAX_CHANNELS] = { 0.0, 0.0 }; | |
223 | |
224 for (unsigned int channel = 0; channel < NInChannels(); channel++) { | |
225 double* in = inputs[channel]; | |
226 double* out = outputs[channel]; | |
227 double peak = 0.0; | |
228 | |
229 /* find the max absolute value in the block of samples */ | |
230 for (int offset = 0; offset < nFrames; ++offset, ++in, ++out) { | |
231 /* find the peak of this block */ | |
232 peak = IPMAX(peak, fabs(*in)); | |
233 /* write the input buffer to the output */ | |
234 *out = mDry * (*in); | |
235 } | |
236 | |
237 if (peak > mThreshold) { | |
238 double difftoThrs = fabs(::AmpToDB(peak) - ::AmpToDB(mThreshold)); | |
239 /* clipDiff will be rounded downward later, but if it's very very | |
240 close to the ceil, then let it be the ceil. | |
241 */ | |
242 const double ceilClippingDiff = ceil(difftoThrs); | |
243 if (ceilClippingDiff - difftoThrs < CLIPPING_CEILING_SNAP){ | |
244 difftoThrs = ceilClippingDiff; | |
245 } | |
246 | |
247 clippingDiff[channel] = BOUNDED(difftoThrs, 0.0, 12.0); | |
248 } | |
249 | |
250 /* now draw the peak meter with the maximum of this block of samples */ | |
251 const double deltaT = nFrames / mSampleRate; | |
252 const double decayAmount = deltaT * mMeterDecayRate; | |
253 | |
254 peak = ::toDBMeter(peak, DB_RANGE); | |
255 | |
256 /* max between new peak and old peak decay wins */ | |
257 peak = IPMAX(peak, mPrevPeak[channel] - decayAmount); | |
258 | |
259 /* save the peaks for next block of samples */ | |
260 mPrevPeak[channel] = peak; | |
261 | |
262 | |
263 /* update the GUI */ | |
264 if (GetGUI()){ | |
265 GetGUI()->SetControlFromPlug(mMeterIdx[channel], peak); | |
266 } | |
267 } | |
268 | |
269 /* pass the data related to this block over to the sonification thread */ | |
270 sDacMutex.lock(); | |
271 sDacSynced.wet = mWet; | |
272 sDacSynced.maxClippingDiff[0] = clippingDiff[0]; | |
273 sDacSynced.maxClippingDiff[1] = clippingDiff[1]; | |
274 sDacMutex.unlock(); | |
275 | |
276 } | |
277 | |
278 void AccessiblePeakMeter::addContinuousSonification(double** inputs, double** outputs, int nFrames) { | |
279 // Mutex is already locked for us. | |
280 | |
281 const int nChannels = NInChannels(); | |
282 | |
283 const double deltaT = nFrames / mSampleRate; | |
284 const double decayAmount = deltaT * mMeterDecayRate; | |
285 double sonifyFreq[MAX_CHANNELS] = {0.0, 0.0}; | |
286 | |
287 for (int channel = 0; channel < nChannels; channel++){ | |
288 | |
289 double peak = 0.0; | |
290 double *in = inputs[channel]; | |
291 double *out = outputs[channel]; | |
292 | |
293 /* find the max absolute value in the block of samples and write output */ | |
294 for (int offset = 0; offset < nFrames; ++offset, ++in, ++out) { | |
295 /* find the peak of this block */ | |
296 peak = IPMAX(peak, fabs(*in)); | |
297 /* write the input buffer to the output */ | |
298 *out = mDry * (*in); | |
299 } | |
300 | |
301 /* pick the max between new audio and peak meter decaying */ | |
302 peak = ::toDBMeter(peak, DB_RANGE); | |
303 peak = IPMAX(peak, mPrevPeak[channel] - decayAmount); | |
304 | |
305 /* set the sonification frequency according to the last peak value */ | |
306 sonifyFreq[channel] = SONIFICATION_RANGE * peak; | |
307 | |
308 /* save the peaks for next block of samples */ | |
309 mPrevPeak[channel] = peak; | |
310 | |
311 /* update the GUI */ | |
312 if (GetGUI()){ | |
313 GetGUI()->SetControlFromPlug(mMeterIdx[channel], peak); | |
314 } | |
315 | |
316 } | |
317 | |
318 /* pass the data related to this block over to the sonification thread */ | |
319 sDacMutex.lock(); | |
320 sDacSynced.sonifFreq[0] = sonifyFreq[0]; | |
321 sDacSynced.sonifFreq[1] = sonifyFreq[1]; | |
322 sDacSynced.wet = mWet; | |
323 sDacMutex.unlock(); | |
324 } | |
325 | |
326 /* Global scope function executed by the thread that plays the sonification to the dac */ | |
327 void DacRoutine(){ | |
328 | |
329 /* the sound card handle */ | |
330 std::unique_ptr<stk::RtWvOut> sDac; | |
331 | |
332 try { | |
333 // Define and open the default realtime output device for two-channels playback | |
334 sDac.reset(new stk::RtWvOut(MAX_CHANNELS, stk::Stk::sampleRate(), 0, DAC_BUFFER_SIZE)); | |
335 } | |
336 catch (stk::StkError &) { | |
337 exit(1); | |
338 } | |
339 | |
340 /* buffer to calculate the blocks of DAC_BUFFER_SIZE samples. | |
341 The content of the buffer is then fed to the sound card | |
342 */ | |
343 stk::StkFrames frames(DAC_BUFFER_SIZE, MAX_CHANNELS); | |
344 | |
345 while (true){ | |
346 | |
347 /* init local variables to be filled with shared variables' content */ | |
348 double freqs[MAX_CHANNELS] = { 0.0, 0.0 }; | |
349 double clippingDiffs[MAX_CHANNELS] = { 0.0, 0.0 }; | |
350 double wet = 0.0; | |
351 bool die = false; | |
352 int sonificationType; | |
353 | |
354 /* read the shared variables all together into local variables */ | |
355 sDacMutex.lock(); | |
356 | |
357 sonificationType = sDacSynced.sonificationType; | |
358 | |
359 for (int i = 0; i < MAX_CHANNELS; i++){ | |
360 freqs[i] = sDacSynced.sonifFreq[i]; | |
361 clippingDiffs[i] = sDacSynced.maxClippingDiff[i]; | |
362 } | |
363 | |
364 wet = sDacSynced.wet; | |
365 | |
366 die = sDacSynced.die; | |
367 | |
368 sDacMutex.unlock(); | |
369 | |
370 /* check if the thread has to stop. Called when the user exits the Daw */ | |
371 if (die){ | |
372 return; | |
373 } | |
374 | |
375 /* reset the ugen if sonification type has changed */ | |
376 if (sonificationType != sPrevSonificationType) { | |
377 | |
378 for (int i = 0; i < MAX_CHANNELS; i++){ | |
379 sSonification.ugen[i].reset(); | |
380 sSonification.ugen[i].setFrequency(sonificationType == SONIFICATION_TYPE_CLIPPING ? 440 : 0); | |
381 } | |
382 | |
383 sPrevSonificationType = sonificationType; | |
384 } | |
385 | |
386 if (sonificationType == SONIFICATION_TYPE_CONTINUOUS) { | |
387 /* write the next block of samples to send to the soundcard */ | |
388 for (int nFrame = 0; nFrame < DAC_BUFFER_SIZE; nFrame++){ | |
389 for (int channel = 0; channel < MAX_CHANNELS; channel++){ | |
390 | |
391 sSonification.ugen[channel].setFrequency(freqs[channel]); | |
392 /* If level goes below audible level just hush the sonification. * | |
393 * this avoids DC offset when sonification frequency gets too low. * | |
394 * Use an envelope to bring the sonification volume down gently */ | |
395 if (freqs[channel] < AccessiblePeakMeter::MIN_SONIFICATION_FREQ){ | |
396 | |
397 if (sSonification.continous.isOn[channel]){ // if it's on and level's low, turn it off | |
398 sSonification.continous.isOn[channel] = false; | |
399 sSonification.continous.envelope[channel].setTarget(0.0); | |
400 } | |
401 | |
402 } | |
403 else if (!sSonification.continous.isOn[channel]){ // if it's off and level's high, turn it on | |
404 | |
405 sSonification.continous.envelope[channel].setValue(1.0); | |
406 sSonification.continous.isOn[channel] = true; | |
407 | |
408 } | |
409 | |
410 double tick = sSonification.ugen[channel].tick(); | |
411 tick *= sSonification.continous.envelope[channel].tick(); // apply envelope | |
412 tick *= wet; // apply wet parameter, controlled by the user | |
413 frames(nFrame, channel) = tick; | |
414 } | |
415 } | |
416 } | |
417 else { // sonificationType = AccessiblePeakMeter::SONIFICATION_TYPE_CLIPPING | |
418 | |
419 for (int channel = 0; channel < MAX_CHANNELS; channel++){ | |
420 | |
421 if (clippingDiffs[channel] > 0.0){ | |
422 if (clippingDiffs[channel] > sSonification.clipping.maxDiff[channel]){ | |
423 sSonification.clipping.maxDiff[channel] = clippingDiffs[channel]; | |
424 } | |
425 | |
426 /* sonify the difference between the amplitude and threshold * | |
427 * one db (rounded downward) is one tone, up to one octave */ | |
428 sSonification.ugen[channel].setFrequency(midi2Freq(69 + (int)(sSonification.clipping.maxDiff[channel]))); | |
429 sSonification.clipping.envelope[channel].keyOn(); | |
430 } | |
431 | |
432 } | |
433 | |
434 for (int nFrame = 0; nFrame < DAC_BUFFER_SIZE; nFrame++){ | |
435 for (int channel = 0; channel < MAX_CHANNELS; channel++){ | |
436 | |
437 /* when attack is done switch immediately to RELEASE (keyOff) * | |
438 * this way the evelope goes like attack->release->silence */ | |
439 if (sSonification.clipping.envelope[channel].getState() == stk::ADSR::DECAY) { | |
440 sSonification.clipping.envelope[channel].keyOff(); | |
441 } | |
442 | |
443 /* write the sonification in the frames object */ | |
444 if (sSonification.clipping.envelope[channel].getState() == stk::ADSR::ATTACK || | |
445 sSonification.clipping.envelope[channel].getState() == stk::ADSR::RELEASE) { | |
446 | |
447 const double env = sSonification.clipping.envelope[channel].tick(); | |
448 double tick = sSonification.ugen[channel].tick() * env; | |
449 tick *= wet; | |
450 frames(nFrame, channel) = tick; | |
451 | |
452 } | |
453 else { // no sonification | |
454 | |
455 sSonification.clipping.maxDiff[channel] = 0.0; // reset max clipping diff | |
456 frames(nFrame, channel) = 0.0; | |
457 | |
458 } | |
459 } | |
460 } | |
461 | |
462 } | |
463 | |
464 /* play this block to the default soundcard */ | |
465 sDac->tick(frames); | |
466 } | |
467 | |
468 } | |
469 | |
470 //Called by the standalone wrapper if someone clicks about | |
471 bool AccessiblePeakMeter::HostRequestingAboutBox() | |
472 { | |
473 IMutexLock lock(this); | |
474 if(GetGUI()) | |
475 { | |
476 // do nothing | |
477 } | |
478 return true; | |
479 } | |
480 | |
481 | |
482 | |
483 const double AccessiblePeakMeter::DRY_DEFAULT = 0.0; | |
484 const double AccessiblePeakMeter::WET_DEFAULT = -6.0; | |
485 const int AccessiblePeakMeter::SONIFICATION_TYPE_DEFAULT = 1; | |
486 const double AccessiblePeakMeter::METERDECAY_DEFAULT = 60.0; | |
487 const double AccessiblePeakMeter::THRESHOLD_DEFAULT = 0.0; | |
488 | |
489 const double AccessiblePeakMeter::DB_RANGE = 66.0; | |
490 const double AccessiblePeakMeter::SONIFICATION_RANGE = 2000; | |
491 const double AccessiblePeakMeter::MIN_SONIFICATION_FREQ = 20.0; | |
492 const double AccessiblePeakMeter::CLIPPING_CEILING_SNAP = 0.05; | |
493 const int AccessiblePeakMeter::NUM_KNOB_FRAMES = 60; | |
494 |