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: #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: #include 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: 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: AccessiblePeakMeter::AccessiblePeakMeter(IPlugInstanceInfo instanceInfo) f@0: : IPLUG_CTOR(kNumParams, NUM_PRESETS, instanceInfo), f@0: mDry(DRY_DEFAULT), f@0: mWet(WET_DEFAULT), f@0: mMeterDecayRate(1.0), f@0: mThreshold(1.0), f@0: mSampleRate(44100.), f@0: mSonificationType(SONIFICATION_TYPE_CLIPPING) f@0: f@0: { f@0: TRACE; f@0: f@0: //arguments are: name, defaultVal, minVal, maxVal, step, label f@0: GetParam(kDry)->InitDouble("Dry", DRY_DEFAULT, -61.0, 0., 0.2, "dB"); f@0: GetParam(kDry)->SetDisplayText(-61.0, " -inf"); f@0: GetParam(kWet)->InitDouble("Wet", WET_DEFAULT, -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@0: GetParam(kSonificationType)->InitEnum("Sonification Type", SONIFICATION_TYPE_DEFAULT, 2); f@0: GetParam(kSonificationType)->SetDisplayText(SONIFICATION_TYPE_CLIPPING, "Clipping"); f@0: GetParam(kSonificationType)->SetDisplayText(SONIFICATION_TYPE_CONTINUOUS, "Continuous"); f@0: 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: //pGraphics->AttachKeyCatcher(new IKeyCatcher(this, IRECT(0, 0, GUI_WIDTH, GUI_HEIGHT))); f@0: f@0: /* text has info about the font-size, font-type etc. */ f@0: IText text = IText(14); 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 + 20 + 20), kDry, &knob, &text, 27)); f@0: pGraphics->AttachControl(new IKnobMultiControlText(this, IRECT(lWetX, lWetY, lWetX + 52, lWetY + 48 + 20 + 20), 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 + 20 + 20), 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 fader f@0: - a triangle pointing at half the height of the bitmap - from overflowing the peak meters */ f@0: int halfFaderBmapLen = faderBmap.W / 2; f@0: mMeterIdx[0] = pGraphics->AttachControl(new IPeakMeterVert(this, IRECT(lPeakMeterX + 25, lPeaklMeterY + halfFaderBmapLen, lPeakMeterX + 45, lPeaklMeterY + 170 + halfFaderBmapLen), f@0: 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: //kDry, kWet, kThreshold, kSonificationType, kMeterDecayRate, f@0: MakePreset("Detect Clipping", DRY_DEFAULT, WET_DEFAULT, THRESHOLD_DEFAULT, SONIFICATION_TYPE_CLIPPING, METERDECAY_DEFAULT); f@0: MakePreset("Sonify Audio", DRY_DEFAULT, WET_DEFAULT, THRESHOLD_DEFAULT, SONIFICATION_TYPE_CONTINUOUS, METERDECAY_DEFAULT); f@0: 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(mSonificationType == 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: for (int i = 0; i < MAX_CHANNELS; i++) { f@0: mPrevPeak[i] = 0.0; f@0: } f@0: f@0: if (!sDacThread.started){ f@0: sDacThread.started = true; f@0: sDacThread.t = std::move(std::thread(DacRoutine)); 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 (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: mSonificationType = GetParam(kSonificationType)->Int(); f@0: f@0: for (int i = 0; i < MAX_CHANNELS; i++) { f@0: mPrevPeak[i] = 0.0; f@0: } f@0: f@0: sDacMutex.lock(); f@0: sDacSynced.sonificationType = mSonificationType; f@0: sDacMutex.unlock(); 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: double clippingDiff[MAX_CHANNELS] = { 0.0, 0.0 }; 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: /* find the peak of this block */ f@0: peak = IPMAX(peak, fabs(*in)); f@0: /* write the input buffer to the output */ f@0: *out = mDry * (*in); f@0: } f@0: f@0: if (peak > mThreshold) { f@0: double difftoThrs = fabs(::AmpToDB(peak) - ::AmpToDB(mThreshold)); 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(difftoThrs); f@0: if (ceilClippingDiff - difftoThrs < CLIPPING_CEILING_SNAP){ f@0: difftoThrs = ceilClippingDiff; f@0: } f@0: f@0: clippingDiff[channel] = BOUNDED(difftoThrs, 0.0, 12.0); f@0: } f@0: f@0: /* now draw the peak meter with the maximum of this block of samples */ 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 peaks for next block of samples */ f@0: mPrevPeak[channel] = peak; f@0: 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: /* pass the data related to this block over to the sonification thread */ f@0: sDacMutex.lock(); f@0: sDacSynced.wet = mWet; f@0: sDacSynced.maxClippingDiff[0] = clippingDiff[0]; f@0: sDacSynced.maxClippingDiff[1] = clippingDiff[1]; f@0: sDacMutex.unlock(); 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: double sonifyFreq[MAX_CHANNELS] = {0.0, 0.0}; f@0: f@0: for (int channel = 0; channel < nChannels; channel++){ f@0: f@0: double peak = 0.0; f@0: double *in = inputs[channel]; f@0: double *out = outputs[channel]; f@0: f@0: /* find the max absolute value in the block of samples and write output */ f@0: for (int offset = 0; offset < nFrames; ++offset, ++in, ++out) { f@0: /* find the peak of this block */ f@0: peak = IPMAX(peak, fabs(*in)); f@0: /* write the input buffer to the output */ f@0: *out = mDry * (*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: sonifyFreq[channel] = SONIFICATION_RANGE * peak; 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: /* pass the data related to this block over to the sonification thread */ f@0: sDacMutex.lock(); f@0: sDacSynced.sonifFreq[0] = sonifyFreq[0]; f@0: sDacSynced.sonifFreq[1] = sonifyFreq[1]; f@0: sDacSynced.wet = mWet; f@0: sDacMutex.unlock(); f@0: } f@0: f@0: /* Global scope function executed by the thread that plays the sonification to the dac */ f@0: void DacRoutine(){ f@0: f@0: /* the sound card handle */ f@0: std::unique_ptr sDac; f@0: f@0: try { f@0: // Define and open the default realtime output device for two-channels playback f@0: sDac.reset(new stk::RtWvOut(MAX_CHANNELS, stk::Stk::sampleRate(), 0, DAC_BUFFER_SIZE)); f@0: } f@0: catch (stk::StkError &) { f@0: exit(1); f@0: } f@0: f@0: /* buffer to calculate the blocks of DAC_BUFFER_SIZE samples. f@0: The content of the buffer is then fed to the sound card f@0: */ f@0: stk::StkFrames frames(DAC_BUFFER_SIZE, MAX_CHANNELS); f@0: f@0: while (true){ f@0: f@0: /* init local variables to be filled with shared variables' content */ f@0: double freqs[MAX_CHANNELS] = { 0.0, 0.0 }; f@0: double clippingDiffs[MAX_CHANNELS] = { 0.0, 0.0 }; f@0: double wet = 0.0; f@0: bool die = false; f@0: int sonificationType; f@0: f@0: /* read the shared variables all together into local variables */ f@0: sDacMutex.lock(); f@0: f@0: sonificationType = sDacSynced.sonificationType; f@0: f@0: for (int i = 0; i < MAX_CHANNELS; i++){ f@0: freqs[i] = sDacSynced.sonifFreq[i]; f@0: clippingDiffs[i] = sDacSynced.maxClippingDiff[i]; f@0: } f@0: f@0: wet = sDacSynced.wet; f@0: f@0: die = sDacSynced.die; f@0: f@0: sDacMutex.unlock(); f@0: f@0: /* check if the thread has to stop. Called when the user exits the Daw */ f@0: if (die){ f@0: return; f@0: } f@0: f@0: /* reset the ugen if sonification type has changed */ f@0: if (sonificationType != sPrevSonificationType) { f@0: f@0: for (int i = 0; i < MAX_CHANNELS; i++){ f@0: sSonification.ugen[i].reset(); f@0: sSonification.ugen[i].setFrequency(sonificationType == SONIFICATION_TYPE_CLIPPING ? 440 : 0); f@0: } f@0: f@0: sPrevSonificationType = sonificationType; f@0: } f@0: f@0: if (sonificationType == SONIFICATION_TYPE_CONTINUOUS) { f@0: /* write the next block of samples to send to the soundcard */ f@0: for (int nFrame = 0; nFrame < DAC_BUFFER_SIZE; nFrame++){ f@0: for (int channel = 0; channel < MAX_CHANNELS; channel++){ f@0: f@0: sSonification.ugen[channel].setFrequency(freqs[channel]); 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: * Use an envelope to bring the sonification volume down gently */ f@0: if (freqs[channel] < AccessiblePeakMeter::MIN_SONIFICATION_FREQ){ f@0: f@0: if (sSonification.continous.isOn[channel]){ // if it's on and level's low, turn it off f@0: sSonification.continous.isOn[channel] = false; f@0: sSonification.continous.envelope[channel].setTarget(0.0); f@0: } f@0: f@0: } f@0: else if (!sSonification.continous.isOn[channel]){ // if it's off and level's high, turn it on f@0: f@0: sSonification.continous.envelope[channel].setValue(1.0); f@0: sSonification.continous.isOn[channel] = true; f@0: f@0: } f@0: f@0: double tick = sSonification.ugen[channel].tick(); f@0: tick *= sSonification.continous.envelope[channel].tick(); // apply envelope f@0: tick *= wet; // apply wet parameter, controlled by the user f@0: frames(nFrame, channel) = tick; f@0: } f@0: } f@0: } f@0: else { // sonificationType = AccessiblePeakMeter::SONIFICATION_TYPE_CLIPPING f@0: f@0: for (int channel = 0; channel < MAX_CHANNELS; channel++){ f@0: f@0: if (clippingDiffs[channel] > 0.0){ f@0: if (clippingDiffs[channel] > sSonification.clipping.maxDiff[channel]){ f@0: sSonification.clipping.maxDiff[channel] = clippingDiffs[channel]; 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 */ f@0: sSonification.ugen[channel].setFrequency(midi2Freq(69 + (int)(sSonification.clipping.maxDiff[channel]))); f@0: sSonification.clipping.envelope[channel].keyOn(); f@0: } f@0: f@0: } f@0: f@0: for (int nFrame = 0; nFrame < DAC_BUFFER_SIZE; nFrame++){ f@0: for (int channel = 0; channel < MAX_CHANNELS; channel++){ f@0: f@0: /* when attack is done switch immediately to RELEASE (keyOff) * f@0: * this way the evelope goes like attack->release->silence */ f@0: if (sSonification.clipping.envelope[channel].getState() == stk::ADSR::DECAY) { f@0: sSonification.clipping.envelope[channel].keyOff(); f@0: } f@0: f@0: /* write the sonification in the frames object */ f@0: if (sSonification.clipping.envelope[channel].getState() == stk::ADSR::ATTACK || f@0: sSonification.clipping.envelope[channel].getState() == stk::ADSR::RELEASE) { f@0: f@0: const double env = sSonification.clipping.envelope[channel].tick(); f@0: double tick = sSonification.ugen[channel].tick() * env; f@0: tick *= wet; f@0: frames(nFrame, channel) = tick; f@0: f@0: } f@0: else { // no sonification f@0: f@0: sSonification.clipping.maxDiff[channel] = 0.0; // reset max clipping diff f@0: frames(nFrame, channel) = 0.0; f@0: f@0: } f@0: } f@0: } f@0: f@0: } f@0: f@0: /* play this block to the default soundcard */ f@0: sDac->tick(frames); 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: f@0: const double AccessiblePeakMeter::DRY_DEFAULT = 0.0; f@0: const double AccessiblePeakMeter::WET_DEFAULT = -6.0; f@0: 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 double AccessiblePeakMeter::MIN_SONIFICATION_FREQ = 20.0; f@0: const double AccessiblePeakMeter::CLIPPING_CEILING_SNAP = 0.05; f@0: const int AccessiblePeakMeter::NUM_KNOB_FRAMES = 60; f@0: