f@0: // f@0: // AccessiblePeakMeter.cpp f@0: // f@0: // Author: Fiore Martin f@0: // Started from IPlugMultiTargets example in WDL-OL, by Oli Larkin - https://github.com/olilarkin/wdl-ol f@0: // f@0: // Licensed under the Cockos WDL License, see README.txt f@0: // f@0: f@0: f@0: #include "AccessiblePeakMeter.h" f@0: #include "IPlug_include_in_plug_src.h" f@0: #include "resource.h" f@0: f@0: f@0: #include "IControl.h" f@0: #include "IBitmapMonoText.h" f@0: #include "AccessiblePeakMeter_controls.h" f@0: f@0: f@0: inline double midi2Freq(int note) { f@0: return 440. * pow(2., (note - 69.) / 12.); f@0: } f@0: f@0: double toDBMeter(double val, double range) f@0: { f@0: double db; f@0: if (val > 0) f@0: db = ::AmpToDB(val); f@0: else f@0: db = -999; f@0: return BOUNDED((db + 60) / range,0,1); f@0: } f@0: f@0: /* reference points for controls layout, by changing these numbers only f@0: the widgets can be moved around and all the other bits (top/left/right f@0: borders, labels etc.) will follow. X and Y refer to the top-left coord */ f@0: enum ELayout { f@0: lDryX = 20, f@0: lDryY = 10, f@0: lWetX = 85, f@0: lWetY = 10, f@0: f@0: lFaderLen = 190, f@0: lPeakMeterX = 180, f@0: lPeaklMeterY = 30, f@0: f@0: lSonifTypeX = 20, f@0: lSonifTypeY = 200, f@0: f@0: lDecayRateX = 20, f@0: lDecayRateY = 90 f@0: }; f@0: f@0: f@0: enum EParams f@0: { f@0: kDry = 0, f@0: kWet, f@0: kThreshold, f@0: kSonificationType, f@0: kMeterDecayRate, f@0: kNumParams f@0: }; f@0: f@0: f@0: AccessiblePeakMeter::AccessiblePeakMeter(IPlugInstanceInfo instanceInfo) f@0: : IPLUG_CTOR(kNumParams, NUM_PRESETS, instanceInfo), f@0: mDry(DRYWET_DEFAULT), f@0: mWet(DRYWET_DEFAULT), f@0: mMeterDecayRate(1.0), f@0: mSampleRate(44100.), f@0: mThreshold(1.0) f@0: { f@0: TRACE; f@0: f@0: for (int i = 0; i < MAX_CHANNELS; i++) { f@0: mPrevPeak[i] = 0.0; f@0: } f@0: f@0: //arguments are: name, defaultVal, minVal, maxVal, step, label f@0: GetParam(kDry)->InitDouble("Dry", -6., -61.0, 0., 0.2, "dB"); f@0: GetParam(kDry)->SetDisplayText(-61.0, " -inf"); f@0: GetParam(kWet)->InitDouble("Wet", -6., -61.0, 0., 0.2, "dB"); f@0: GetParam(kWet)->SetDisplayText(-61.0, " -inf"); f@0: GetParam(kThreshold)->InitDouble("Threshold", 0.0, -60.0, 6.2, 0.2, "dB"); f@2: GetParam(kSonificationType)->InitEnum("Sonification Type", SONIFICATION_TYPE_DEFAULT, 2); f@0: GetParam(kSonificationType)->SetDisplayText(SONIFICATION_TYPE_CONTINUOUS, "Continuous"); f@0: GetParam(kSonificationType)->SetDisplayText(SONIFICATION_TYPE_CLIPPING, "Clipping"); f@0: GetParam(kMeterDecayRate)->InitDouble("Decay", 1.0, 0.05, 1.0, 0.05, "sec."); f@0: f@0: IGraphics* pGraphics = MakeGraphics(this, GUI_WIDTH, GUI_HEIGHT); f@0: pGraphics->AttachBackground(BG_ID, BG_FN); f@0: f@0: /* load bitmaps for fader, knob and switch button */ f@0: IBitmap knob = pGraphics->LoadIBitmap(KNOB_ID, KNOB_FN, NUM_KNOB_FRAMES); f@0: IBitmap faderBmap = pGraphics->LoadIBitmap(FADER_ID, FADER_FN); f@0: IBitmap aSwitch = pGraphics->LoadIBitmap(SWITCH_ID, SWITCH_FN,2); f@0: f@0: /* text has info about the font-size, font-type etc. */ f@0: IText text = IText(14); f@0: f@0: /* attach sonification type switch to the GUI */ f@0: pGraphics->AttachControl(new ISwitchPopUpControl(this, lSonifTypeX ,lSonifTypeY, kSonificationType, &aSwitch)); f@0: pGraphics->AttachControl(new ITextControl(this, IRECT(lSonifTypeX+10, lSonifTypeY - 20, lSonifTypeX + 110, lSonifTypeY ), &text, "Sonification Type")); f@0: f@0: /* attach dry and wet knobs to GUI */ f@0: 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: pGraphics->AttachControl(new IKnobMultiControlText(this, IRECT(lWetX, lWetY, lWetX + 52, lWetY + 48 + 19 + 19), kWet, &knob, &text, 27)); f@0: f@0: /* attach decay rate knob to the GUI */ f@0: pGraphics->AttachControl(new IKnobMultiControlText(this, IRECT(lDecayRateX, lDecayRateY, lDecayRateX + 48 , lDecayRateY + 48 + 19 + 19 ), kMeterDecayRate, &knob, &text, 33)); f@0: f@0: /* attach fader display, which shows the fader value, to GUI */ f@0: ITextControl *faderText = new ITextControl(this, IRECT(lPeakMeterX+60, lPeaklMeterY + lFaderLen, lPeakMeterX + faderBmap.W + 95, lPeaklMeterY + lFaderLen + 20), &text); f@0: pGraphics->AttachControl(faderText); f@0: f@0: /* attach the fader to GUI */ f@0: pGraphics->AttachControl(new IFaderVertText(this, lPeakMeterX, lPeaklMeterY, lFaderLen, kThreshold, &faderBmap, faderText)); f@0: f@0: pGraphics->AttachControl(new ITextControl(this, IRECT(lPeakMeterX, lPeaklMeterY - 20, lPeakMeterX + 100, lPeaklMeterY), &text, "Peak Level Meter")); f@0: pGraphics->AttachControl(new ITextControl(this, IRECT(lPeakMeterX-20, lPeaklMeterY + lFaderLen, lPeakMeterX + 75, lPeaklMeterY + lFaderLen + 20), &text, "Threshold: ")); f@0: f@0: /* attach peak meters to GUI. f@0: Half the bitmap height is added to the peak meters on both top and bottom to prevent the f@0: triangular fader from going past the peak meters span f@0: */ f@0: const int halfFaderBmapLen = faderBmap.W / 2; f@0: mMeterIdx[0] = pGraphics->AttachControl(new IPeakMeterVert(this, f@0: IRECT(lPeakMeterX + 25, lPeaklMeterY + halfFaderBmapLen, lPeakMeterX + 45, lPeaklMeterY + 170 + halfFaderBmapLen), GetParam(kThreshold)->GetDefaultNormalized())); f@0: mMeterIdx[1] = pGraphics->AttachControl(new IPeakMeterVert(this, f@0: IRECT(lPeakMeterX + 50, lPeaklMeterY + halfFaderBmapLen, lPeakMeterX + 70, lPeaklMeterY + lFaderLen - halfFaderBmapLen), GetParam(kThreshold)->GetDefaultNormalized())); f@0: f@0: AttachGraphics(pGraphics); f@0: f@0: /* add presets */ f@0: MakePreset("Detect Clipping", DRYWET_DEFAULT, DRYWET_DEFAULT, THRESHOLD_DEFAULT, SONIFICATION_TYPE_CLIPPING, METERDECAY_DEFAULT); f@0: MakePreset("Sonify Audio", DRYWET_DEFAULT, DRYWET_DEFAULT, THRESHOLD_DEFAULT, SONIFICATION_TYPE_CONTINUOUS, METERDECAY_DEFAULT); f@0: } f@0: f@0: AccessiblePeakMeter::~AccessiblePeakMeter() {} f@0: f@0: f@0: void AccessiblePeakMeter::ProcessDoubleReplacing(double** inputs, double** outputs, int nFrames) f@0: { f@0: if(mSonification.type == SONIFICATION_TYPE_CONTINUOUS) { f@0: addContinuousSonification(inputs, outputs, nFrames); f@0: } else { f@0: addClippingSonification(inputs, outputs, nFrames); f@0: } f@0: } f@0: f@0: f@0: void AccessiblePeakMeter::Reset() f@0: { f@0: TRACE; f@0: IMutexLock lock(this); f@0: f@0: mSampleRate = GetSampleRate(); f@0: f@0: mSonification.reset(mSampleRate); f@0: f@0: for (int i = 0; i < MAX_CHANNELS; i++) { f@0: mPrevPeak[i] = 0.0; f@0: } f@0: f@0: } f@0: f@0: void AccessiblePeakMeter::OnParamChange(int paramIdx) f@0: { f@0: IMutexLock lock(this); f@0: f@0: switch (paramIdx) f@0: { f@0: case kDry: f@0: /* if the level goes below 60.5 dB, just bring it to silence */ f@0: if (GetParam(kDry)->Value() < -60.5 ){ f@0: mDry = 0.0; f@0: } f@0: else { f@0: mDry = ::DBToAmp(GetParam(kDry)->Value()); f@0: } f@0: break; f@0: f@0: case kWet: f@0: if (GetParam(kWet)->Value() < -60.5){ f@0: mWet = 0.0; f@0: } f@0: else{ f@0: mWet = ::DBToAmp(GetParam(kWet)->Value()); f@0: } f@0: break; f@0: f@0: case kThreshold: f@0: mThreshold = GetParam(kThreshold)->DBToAmp(); f@0: break; f@0: f@0: case kMeterDecayRate : f@0: mMeterDecayRate = 1.0 / GetParam(kMeterDecayRate)->Value(); f@0: break; f@0: f@0: case kSonificationType: f@0: mSonification.type = GetParam(kSonificationType)->Int(); f@0: f@0: mSonification.reset(); f@0: f@0: for (int i = 0; i < MAX_CHANNELS; i++) { f@0: mPrevPeak[i] = 0.0; f@0: mSonification.ugen[i].setFrequency(mSonification.type == SONIFICATION_TYPE_CLIPPING ? 440.0 : 0.0); f@0: } f@0: break; f@0: f@0: default: f@0: break; f@0: } f@0: } f@0: f@0: void AccessiblePeakMeter::addClippingSonification(double** inputs, double** outputs, int nFrames) { f@0: // Mutex is already locked for us. f@0: f@0: for (unsigned int channel = 0; channel < NInChannels(); channel++) { f@0: double* in = inputs[channel]; f@0: double* out = outputs[channel]; f@0: double peak = 0.0; f@0: f@0: /* find the max absolute value in the block of samples */ f@0: for (int offset = 0; offset < nFrames; ++offset, ++in, ++out) { f@0: const double ampl = fabs(*in); // amplitude f@0: peak = IPMAX(peak, ampl); // find max peak for this block f@0: f@0: if (ampl > mThreshold) { f@0: /* find the clipping amount in dB */ f@0: double clippingDiff = fabs(::AmpToDB(ampl) - ::AmpToDB(mThreshold)); f@0: f@0: /* clipDiff will be rounded downward later, but if it's very very f@0: close to the ceil, then let it be the ceil. f@0: */ f@0: const double ceilClippingDiff = ceil(clippingDiff); f@0: if (ceilClippingDiff - clippingDiff < CLIPPING_CEILING_SNAP){ f@0: clippingDiff = ceilClippingDiff; f@0: } f@0: f@0: if (clippingDiff > mSonification.clipping.maxDiff[channel]){ f@0: /* bound the difference to 12 semitones to prevent the sonification from going too high */ f@0: mSonification.clipping.maxDiff[channel] = BOUNDED(clippingDiff, 0.0, 12.0); f@0: } f@0: f@0: /* sonify the difference between the amplitude and threshold * f@0: * one db (rounded downward) is one tone, up to one octave (12 semitones) */ f@0: mSonification.ugen[channel].setFrequency(midi2Freq(69 + (int)(mSonification.clipping.maxDiff[channel]))); f@0: mSonification.clipping.envelope[channel].keyOn(); f@0: } f@0: f@0: /* when attack is done switch immediately to RELEASE (keyOff) * f@0: * so it goes like: attack->release->silence */ f@0: if (mSonification.clipping.envelope[channel].getState() == stk::ADSR::DECAY) { f@0: mSonification.clipping.envelope[channel].keyOff(); f@0: } f@0: f@0: /* add the sonification to the mix */ f@0: if (mSonification.clipping.envelope[channel].getState() == stk::ADSR::ATTACK || f@0: mSonification.clipping.envelope[channel].getState() == stk::ADSR::RELEASE) { f@0: const double env = mSonification.clipping.envelope[channel].tick(); f@0: const double tick = mSonification.ugen[channel].tick() * env; f@0: *out = mix(*in, tick); f@0: } else { // no sonification f@0: mSonification.clipping.maxDiff[channel] = 0.0; // reset max clipping diff f@0: *out = mix(*in, 0.0); // still honours the user's knobs settings f@0: } f@0: } f@0: f@0: /* now draw the peak meter with the maximum of this block of samples */ f@0: f@0: const double deltaT = nFrames / mSampleRate; f@0: const double decayAmount = deltaT * mMeterDecayRate; f@0: f@0: peak = ::toDBMeter(peak, DB_RANGE); f@0: f@0: /* max between new peak and old peak decay wins */ f@0: peak = IPMAX(peak, mPrevPeak[channel] - decayAmount); f@0: f@0: /* save the peak for next block of samples */ f@0: mPrevPeak[channel] = peak; f@0: f@0: /* update the GUI */ f@0: if (GetGUI()) { f@0: GetGUI()->SetControlFromPlug(mMeterIdx[channel], peak); f@0: } f@0: } f@0: f@0: } f@0: f@0: void AccessiblePeakMeter::addContinuousSonification(double** inputs, double** outputs, int nFrames) { f@0: // Mutex is already locked for us. f@0: f@0: const int nChannels = NInChannels(); f@0: f@0: const double deltaT = nFrames / mSampleRate; f@0: const double decayAmount = deltaT * mMeterDecayRate; f@0: f@0: for (int channel = 0; channel < nChannels; channel++){ f@0: double peak = 0.0; f@0: double *in = inputs[channel]; f@0: f@0: /* find the max absolute value in the block of samples */ f@0: for (int offset = 0; offset < nFrames; ++offset, ++in) { f@0: peak = IPMAX(peak, fabs(*in)); f@0: } f@0: f@0: /* pick the max between new audio and peak meter decaying */ f@0: peak = ::toDBMeter(peak, DB_RANGE); f@0: peak = IPMAX(peak, mPrevPeak[channel] - decayAmount); f@0: f@0: /* set the sonification frequency according to the last peak value */ f@0: const double sonifFreq = SONIFICATION_RANGE * peak; f@0: mSonification.ugen[channel].setFrequency(sonifFreq); f@0: f@0: /* If level goes below audible level just hush the sonification. * f@0: * this avoids DC offset when sonification frequency gets too low. * f@0: * Uses an envelope to bring the sonification volume down smoothly f@0: */ f@0: if (sonifFreq < MIN_SONIFICATION_FREQ){ f@0: /* turn the sonification off, if it's not off already */ f@0: if (mSonification.continous.isOn[channel]){ f@0: mSonification.continous.isOn[channel] = false; f@0: mSonification.continous.envelope[channel].setTarget(0.0); f@0: } f@0: } f@0: else if (!mSonification.continous.isOn[channel]){ f@0: /* if the sonification frequency goes past MIN_SONIFICATION_FREQ * f@0: turn it on again unless it's already on */ f@0: mSonification.continous.envelope[channel].setValue(1.0); f@0: mSonification.continous.isOn[channel] = true; f@0: } f@0: f@0: in = inputs[channel]; f@0: double *out = outputs[channel]; f@0: /* add peak meter line continuous sonification to output */ f@0: for (int offset = 0; offset < nFrames; ++offset, ++in, ++out) { f@0: double tick = mSonification.ugen[channel].tick(); f@0: tick *= mSonification.continous.envelope[channel].tick(); // apply envelope f@0: /* write the output buffer: mix original audio + sonification */ f@0: *out = mix(*in, tick); f@0: } f@0: f@0: /* save the peaks for next block of samples */ f@0: mPrevPeak[channel] = peak; f@0: f@0: /* update the GUI */ f@0: if (GetGUI()) { f@0: GetGUI()->SetControlFromPlug(mMeterIdx[channel], peak); f@0: } f@0: } f@0: } f@0: f@0: //Called by the standalone wrapper if someone clicks about f@0: bool AccessiblePeakMeter::HostRequestingAboutBox() f@0: { f@0: IMutexLock lock(this); f@0: if(GetGUI()) f@0: { f@0: // do nothing f@0: } f@0: return true; f@0: } f@0: f@0: f@0: // -------------- static variables init ------------- f@0: f@0: const double AccessiblePeakMeter::DRYWET_DEFAULT = -6.0; f@2: const int AccessiblePeakMeter::SONIFICATION_TYPE_DEFAULT = 1; f@0: const double AccessiblePeakMeter::METERDECAY_DEFAULT = 60.0; f@0: const double AccessiblePeakMeter::THRESHOLD_DEFAULT = 0.0; f@0: f@0: const double AccessiblePeakMeter::DB_RANGE = 66.0; f@0: const double AccessiblePeakMeter::SONIFICATION_RANGE = 2000; f@0: const int AccessiblePeakMeter::SONIFICATION_TYPE_CLIPPING = 1; f@0: const int AccessiblePeakMeter::SONIFICATION_TYPE_CONTINUOUS = 0; f@0: const double AccessiblePeakMeter::BEEP_TIME = 0.2; f@0: const double AccessiblePeakMeter::MIN_SONIFICATION_FREQ = 20.0; f@0: const double AccessiblePeakMeter::CLIPPING_CEILING_SNAP = 0.05; f@0: f@0: const int AccessiblePeakMeter::NUM_KNOB_FRAMES = 60; f@0: const int AccessiblePeakMeter::NUM_PRESETS = 2; f@0: f@0: