/**********************************************************************

  Audacity: A Digital Audio Editor

  Compressor.cpp

  Dominic Mazzoni
  Martyn Shaw
  Steve Jolly

*******************************************************************//**

\class EffectLegacyCompressor
\brief An Effect derived from EffectTwoPassSimpleMono

 - Martyn Shaw made it inherit from EffectTwoPassSimpleMono 10/2005.
 - Steve Jolly made it inherit from EffectSimpleMono.
 - GUI added and implementation improved by Dominic Mazzoni, 5/11/2003.

*//****************************************************************//**

\class CompressorPanel
\brief Panel used within the EffectLegacyCompressor for EffectLegacyCompressor.

*//*******************************************************************/
#include "LegacyCompressor.h"
#include "EffectEditor.h"
#include "LoadEffects.h"

#include <math.h>

#include <wx/brush.h>
#include <wx/checkbox.h>
#include <wx/dcclient.h>
#include <wx/slider.h>
#include <wx/stattext.h>

#include "AColor.h"
#include "Prefs.h"
#include "ShuttleGui.h"
#include "Theme.h"
#include "float_cast.h"
#include "../widgets/LinearUpdater.h"
#include "../widgets/Ruler.h"
#include "../widgets/LinearDBFormat.h"
#include "../widgets/LinearUpdater.h"

#include "WaveTrack.h"
#include "AllThemeResources.h"

enum
{
   ID_Threshold = 10000,
   ID_NoiseFloor,
   ID_Ratio,
   ID_Attack,
   ID_Decay
};

const EffectParameterMethods& EffectLegacyCompressor::Parameters() const
{
   static CapturedParameters<
      EffectLegacyCompressor, Threshold, NoiseFloor,
      Ratio,       // positive number > 1.0
      AttackTime,  // seconds
      ReleaseTime, // seconds
      Normalize, UsePeak>
      parameters;
   return parameters;
}

//----------------------------------------------------------------------------
// EffectLegacyCompressor
//----------------------------------------------------------------------------

const ComponentInterfaceSymbol EffectLegacyCompressor::Symbol { XO(
   "Legacy Compressor") };

namespace
{
BuiltinEffectsModule::Registration<EffectLegacyCompressor> reg;
}

BEGIN_EVENT_TABLE(EffectLegacyCompressor, wxEvtHandler)
EVT_SLIDER(wxID_ANY, EffectLegacyCompressor::OnSlider)
END_EVENT_TABLE()

EffectLegacyCompressor::EffectLegacyCompressor()
{
   Parameters().Reset(*this);

   mThreshold = 0.25;
   mNoiseFloor = 0.01;
   mCompression = 0.5;
   mFollowLen = 0;

   SetLinearEffectFlag(false);
}

EffectLegacyCompressor::~EffectLegacyCompressor()
{
}

// ComponentInterface implementation

ComponentInterfaceSymbol EffectLegacyCompressor::GetSymbol() const
{
   return Symbol;
}

TranslatableString EffectLegacyCompressor::GetDescription() const
{
   return XO("Compresses the dynamic range of audio");
}

ManualPageID EffectLegacyCompressor::ManualPage() const
{
   return L"Compressor";
}

// EffectDefinitionInterface implementation

EffectType EffectLegacyCompressor::GetType() const
{
   return EffectTypeProcess;
}

// Effect Implementation

namespace {

TranslatableString ThresholdFormat( int value )
   /* i18n-hint: usually leave this as is as dB doesn't get translated*/
{ return XO("%3d dB").Format(value); }

TranslatableString AttackTimeFormat( double value )
{ return XO("%.2f secs").Format( value ); }

TranslatableString DecayTimeFormat( double value )
{ return XO("%.1f secs").Format( value ); }

TranslatableString RatioTextFormat( int sliderValue, double value )
{
   auto format = (sliderValue % 10 == 0)
      /* i18n-hint: Unless your language has a different convention for ratios,
       * like 8:1, leave as is.*/
      ? XO("%.0f:1")
      /* i18n-hint: Unless your language has a different convention for ratios,
       * like 8:1, leave as is.*/
      : XO("%.1f:1");
   return format.Format( value );
}

TranslatableString RatioLabelFormat( int sliderValue, double value )
{
   auto format = (sliderValue % 10 == 0)
      ? XO("Ratio %.0f to 1")
      : XO("Ratio %.1f to 1");
   return format.Format( value );
}

}

std::unique_ptr<EffectEditor> EffectLegacyCompressor::PopulateOrExchange(
   ShuttleGui& S, EffectInstance&, EffectSettingsAccess&, const EffectOutputs*)
{
   mUIParent = S.GetParent();
   S.SetBorder(5);

   S.StartHorizontalLay(wxEXPAND, true);
   {
      S.SetBorder(10);
      mPanel = safenew EffectLegacyCompressorPanel(
         S.GetParent(), wxID_ANY, mThresholdDB, mNoiseFloorDB, mRatio);
      S.Prop(true)
         .Position(wxEXPAND | wxALL)
         .MinSize( { 400, 200 } )
         .AddWindow(mPanel);
      S.SetBorder(5);
   }
   S.EndHorizontalLay();

   S.StartStatic( {} );
   {
      S.StartMultiColumn(3, wxEXPAND);
      {
         S.SetStretchyCol(1);
         mThresholdLabel = S.AddVariableText(XO("&Threshold:"), true,
            wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
         mThresholdSlider = S.Id(ID_Threshold)
            .Name(XO("Threshold"))
            .Style(wxSL_HORIZONTAL)
            .AddSlider( {},
               Threshold.def * Threshold.scale,
               Threshold.max * Threshold.scale,
               Threshold.min * Threshold.scale);
         mThresholdText = S.AddVariableText(ThresholdFormat(999), true,
            wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);

         mNoiseFloorLabel = S.AddVariableText(XO("&Noise Floor:"), true,
            wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
         mNoiseFloorSlider = S.Id(ID_NoiseFloor)
            .Name(XO("Noise Floor"))
            .Style(wxSL_HORIZONTAL)
            .AddSlider( {},
               NoiseFloor.def * NoiseFloor.scale,
               NoiseFloor.max * NoiseFloor.scale,
               NoiseFloor.min * NoiseFloor.scale);
         mNoiseFloorText = S.AddVariableText(ThresholdFormat(999),
            true, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);

         mRatioLabel = S.AddVariableText(XO("&Ratio:"), true,
            wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
         mRatioSlider = S.Id(ID_Ratio)
            .Name(XO("Ratio"))
            .Style(wxSL_HORIZONTAL)
            .AddSlider( {},
               Ratio.def * Ratio.scale,
               Ratio.max * Ratio.scale,
               Ratio.min * Ratio.scale);
         mRatioSlider->SetPageSize(5);
         mRatioText = S.AddVariableText(RatioTextFormat( 1, 99.9 ), true,
            wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);

         /* i18n-hint: Particularly in percussion, sounds can be regarded as having
          * an 'attack' phase where the sound builds up and a 'decay' where the
          * sound dies away.  So this means 'onset duration'.  */
         mAttackLabel = S.AddVariableText(XO("&Attack Time:"), true,
            wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
         mAttackSlider = S.Id(ID_Attack)
         /* i18n-hint: Particularly in percussion, sounds can be regarded as having
          * an 'attack' phase where the sound builds up and a 'decay' where the
          * sound dies away.  So this means 'onset duration'.  */
            .Name(XO("Attack Time"))
            .Style(wxSL_HORIZONTAL)
            .AddSlider( {},
               AttackTime.def * AttackTime.scale,
               AttackTime.max * AttackTime.scale,
               AttackTime.min * AttackTime.scale);
         mAttackText = S.AddVariableText(
            AttackTimeFormat(9.99),
            true, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);

         /* i18n-hint: Particularly in percussion, sounds can be regarded as having
          * an 'attack' phase where the sound builds up and a 'decay' or 'release' where the
          * sound dies away.  */
         mDecayLabel = S.AddVariableText(XO("R&elease Time:"), true,
            wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
         mDecaySlider = S.Id(ID_Decay)
         /* i18n-hint: Particularly in percussion, sounds can be regarded as having
          * an 'attack' phase where the sound builds up and a 'decay' or 'release' where the
          * sound dies away.  */
            .Name(XO("Release Time"))
            .Style(wxSL_HORIZONTAL)
            .AddSlider( {},
               ReleaseTime.def * ReleaseTime.scale,
               ReleaseTime.max * ReleaseTime.scale,
               ReleaseTime.min * ReleaseTime.scale);

         mDecayText = S.AddVariableText(
            DecayTimeFormat(99.9),
            true, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL);
      }
      S.EndMultiColumn();
   }
   S.EndStatic();

   S.StartHorizontalLay(wxCENTER, false);
   {
      /* i18n-hint: Make-up, i.e. correct for any reduction, rather than fabricate it.*/
      mGainCheckBox = S.AddCheckBox(XXO("Ma&ke-up gain for 0 dB after compressing"),
            Normalize.def);
      /* i18n-hint: "Compress" here means reduce variations of sound volume,
       NOT related to file-size compression; Peaks means extremes in volume */
      mPeakCheckBox = S.AddCheckBox(XXO("C&ompress based on Peaks"),
            UsePeak.def);
   }
   S.EndHorizontalLay();
   return nullptr;
}

bool EffectLegacyCompressor::TransferDataToWindow(const EffectSettings&)
{
   mThresholdSlider->SetValue(lrint(mThresholdDB));
   mNoiseFloorSlider->SetValue(lrint(mNoiseFloorDB * NoiseFloor.scale));
   mRatioSlider->SetValue(lrint(mRatio * Ratio.scale));
   mAttackSlider->SetValue(lrint(mAttackTime * AttackTime.scale));
   mDecaySlider->SetValue(lrint(mDecayTime * ReleaseTime.scale));
   mGainCheckBox->SetValue(mNormalize);
   mPeakCheckBox->SetValue(mUsePeak);

   UpdateUI();

   return true;
}

bool EffectLegacyCompressor::TransferDataFromWindow(EffectSettings&)
{
   if (!mUIParent->Validate())
   {
      return false;
   }
   return DoTransferDataFromWindow();
}

bool EffectLegacyCompressor::DoTransferDataFromWindow()
{
   // To do:  eliminate this by using control validators instead
   mThresholdDB = (double) mThresholdSlider->GetValue();
   mNoiseFloorDB = (double) mNoiseFloorSlider->GetValue() / NoiseFloor.scale;
   mRatio = (double) mRatioSlider->GetValue() / Ratio.scale;
   mAttackTime = (double) mAttackSlider->GetValue() / 100.0; //AttackTime.scale;
   mDecayTime = (double) mDecaySlider->GetValue() / ReleaseTime.scale;
   mNormalize = mGainCheckBox->GetValue();
   mUsePeak = mPeakCheckBox->GetValue();

   return true;
}

// EffectTwoPassSimpleMono implementation

bool EffectLegacyCompressor::NewTrackPass1()
{
   mThreshold = DB_TO_LINEAR(mThresholdDB);
   mNoiseFloor = DB_TO_LINEAR(mNoiseFloorDB);
   mNoiseCounter = 100;

   mAttackInverseFactor = exp(log(mThreshold) / (mCurRate * mAttackTime + 0.5));
   mAttackFactor = 1.0 / mAttackInverseFactor;
   mDecayFactor = exp(log(mThreshold) / (mCurRate * mDecayTime + 0.5));

   if(mRatio > 1)
      mCompression = 1.0-1.0/mRatio;
   else
      mCompression = 0.0;

   mLastLevel = mThreshold;

   mCircleSize = 100;
   mCircle.reinit( mCircleSize, true );
   mCirclePos = 0;
   mRMSSum = 0.0;

   return true;
}

bool EffectLegacyCompressor::InitPass1()
{
   mMax=0.0;
   if (!mNormalize)
      DisableSecondPass();

   // Find the maximum block length required for any track
   size_t maxlen = inputTracks()->Selected<const WaveTrack>().max(
      &WaveTrack::GetMaxBlockSize
   );
   mFollow1.reset();
   mFollow2.reset();
   // Allocate buffers for the envelope
   if(maxlen > 0) {
      mFollow1.reinit(maxlen);
      mFollow2.reinit(maxlen);
   }
   mFollowLen = maxlen;

   return true;
}

bool EffectLegacyCompressor::InitPass2()
{
   // Actually, this should not even be called, because we call
   // DisableSecondPass() before, if mNormalize is false.
   return mNormalize;
}

// Process the input with 2 buffers available at a time
// buffer1 will be written upon return
// buffer2 will be passed as buffer1 on the next call
bool EffectLegacyCompressor::TwoBufferProcessPass1(
   float* buffer1, size_t len1, float* buffer2, size_t len2)
{
   // If buffers are bigger than allocated, then abort
   // (this should never happen, but if it does, we don't want to crash)
   if((len1 > mFollowLen) || (len2 > mFollowLen))
      return false;

   // This makes sure that the initial value is well-chosen
   // buffer1 == NULL on the first and only the first call
   if (buffer1 == NULL) {
      // Initialize the mLastLevel to the peak level in the first buffer
      // This avoids problems with large spike events near the beginning of the track
      mLastLevel = mThreshold;
      for(size_t i=0; i<len2; i++) {
         if(mLastLevel < fabs(buffer2[i]))
            mLastLevel = fabs(buffer2[i]);
      }
   }

   // buffer2 is NULL on the last and only the last call
   if(buffer2 != NULL) {
      Follow(buffer2, mFollow2.get(), len2, mFollow1.get(), len1);
   }

   if(buffer1 != NULL) {
      for (size_t i = 0; i < len1; i++) {
         buffer1[i] = DoCompression(buffer1[i], mFollow1[i]);
      }
   }


#if 0
   // Copy the envelope over the track data (for debug purposes)
   memcpy(buffer1, mFollow1, len1*sizeof(float));
#endif

   // Rotate the buffer pointers
   mFollow1.swap(mFollow2);

   return true;
}

bool EffectLegacyCompressor::ProcessPass2(float* buffer, size_t len)
{
   if (mMax != 0)
   {
      for (size_t i = 0; i < len; i++)
         buffer[i] /= mMax;
   }

   return true;
}

void EffectLegacyCompressor::FreshenCircle()
{
   // Recompute the RMS sum periodically to prevent accumulation of rounding errors
   // during long waveforms
   mRMSSum = 0;
   for(size_t i=0; i<mCircleSize; i++)
      mRMSSum += mCircle[i];
}

float EffectLegacyCompressor::AvgCircle(float value)
{
   float level;

   // Calculate current level from root-mean-squared of
   // circular buffer ("RMS")
   mRMSSum -= mCircle[mCirclePos];
   mCircle[mCirclePos] = value*value;
   mRMSSum += mCircle[mCirclePos];
   level = sqrt(mRMSSum/mCircleSize);
   mCirclePos = (mCirclePos+1)%mCircleSize;

   return level;
}

void EffectLegacyCompressor::Follow(
   float* buffer, float* env, size_t len, float* previous, size_t previous_len)
{
   /*

   "Follow"ing algorithm by Roger B. Dannenberg, taken from
   Nyquist.  His description follows.  -DMM

   Description: this is a sophisticated envelope follower.
    The input is an envelope, e.g. something produced with
    the AVG function. The purpose of this function is to
    generate a smooth envelope that is generally not less
    than the input signal. In other words, we want to "ride"
    the peaks of the signal with a smooth function. The
    algorithm is as follows: keep a current output value
    (called the "value"). The value is allowed to increase
    by at most rise_factor and decrease by at most fall_factor.
    Therefore, the next value should be between
    value * rise_factor and value * fall_factor. If the input
    is in this range, then the next value is simply the input.
    If the input is less than value * fall_factor, then the
    next value is just value * fall_factor, which will be greater
    than the input signal. If the input is greater than value *
    rise_factor, then we compute a rising envelope that meets
    the input value by working bacwards in time, changing the
    previous values to input / rise_factor, input / rise_factor^2,
    input / rise_factor^3, etc. until this NEW envelope intersects
    the previously computed values. There is only a limited buffer
    in which we can work backwards, so if the NEW envelope does not
    intersect the old one, then make yet another pass, this time
    from the oldest buffered value forward, increasing on each
    sample by rise_factor to produce a maximal envelope. This will
    still be less than the input.

    The value has a lower limit of floor to make sure value has a
    reasonable positive value from which to begin an attack.
   */
   double level,last;

   if(!mUsePeak) {
      // Update RMS sum directly from the circle buffer
      // to avoid accumulation of rounding errors
      FreshenCircle();
   }
   // First apply a peak detect with the requested decay rate
   last = mLastLevel;
   for(size_t i=0; i<len; i++) {
      if(mUsePeak)
         level = fabs(buffer[i]);
      else // use RMS
         level = AvgCircle(buffer[i]);
      // Don't increase gain when signal is continuously below the noise floor
      if(level < mNoiseFloor) {
         mNoiseCounter++;
      } else {
         mNoiseCounter = 0;
      }
      if(mNoiseCounter < 100) {
         last *= mDecayFactor;
         if(last < mThreshold)
            last = mThreshold;
         if(level > last)
            last = level;
      }
      env[i] = last;
   }
   mLastLevel = last;

   // Next do the same process in reverse direction to get the requested attack rate
   last = mLastLevel;
   for(size_t i = len; i--;) {
      last *= mAttackInverseFactor;
      if(last < mThreshold)
         last = mThreshold;
      if(env[i] < last)
         env[i] = last;
      else
         last = env[i];
   }

   if((previous != NULL) && (previous_len > 0)) {
      // If the previous envelope was passed, propagate the rise back until we intersect
      for(size_t i = previous_len; i--;) {
         last *= mAttackInverseFactor;
         if(last < mThreshold)
            last = mThreshold;
         if(previous[i] < last)
            previous[i] = last;
         else // Intersected the previous envelope buffer, so we are finished
            return;
      }
      // If we can't back up far enough, project the starting level forward
      // until we intersect the desired envelope
      last = previous[0];
      for(size_t i=1; i<previous_len; i++) {
         last *= mAttackFactor;
         if(previous[i] > last)
            previous[i] = last;
         else // Intersected the desired envelope, so we are finished
            return;
      }
      // If we still didn't intersect, then continue ramp up into current buffer
      for(size_t i=0; i<len; i++) {
         last *= mAttackFactor;
         if(buffer[i] > last)
            buffer[i] = last;
         else // Finally got an intersect
            return;
      }
      // If we still didn't intersect, then reset mLastLevel
      mLastLevel = last;
   }
}

float EffectLegacyCompressor::DoCompression(float value, double env)
{
   float out;
   if(mUsePeak) {
      // Peak values map 1.0 to 1.0 - 'upward' compression
      out = value * pow(1.0/env, mCompression);
   } else {
      // With RMS-based compression don't change values below mThreshold - 'downward' compression
      out = value * pow(mThreshold/env, mCompression);
   }

   // Retain the maximum value for use in the normalization pass
   if(mMax < fabs(out))
      mMax = fabs(out);

   return out;
}

void EffectLegacyCompressor::OnSlider(wxCommandEvent& WXUNUSED(evt))
{
   DoTransferDataFromWindow();
   UpdateUI();
}

void EffectLegacyCompressor::UpdateUI()
{
   mThresholdLabel->SetName(wxString::Format(_("Threshold %d dB"), (int) mThresholdDB));
   mThresholdText->SetLabel(ThresholdFormat((int) mThresholdDB).Translation());
   mThresholdText->SetName(mThresholdText->GetLabel()); // fix for bug 577 (NVDA/Narrator screen readers do not read static text in dialogs)

   mNoiseFloorLabel->SetName(wxString::Format(_("Noise Floor %d dB"), (int) mNoiseFloorDB));
   mNoiseFloorText->SetLabel(ThresholdFormat((int) mNoiseFloorDB).Translation());
   mNoiseFloorText->SetName(mNoiseFloorText->GetLabel()); // fix for bug 577 (NVDA/Narrator screen readers do not read static text in dialogs)

   mRatioLabel->SetName(
      RatioLabelFormat(mRatioSlider->GetValue(), mRatio).Translation());
   mRatioText->SetLabel(
      RatioTextFormat(mRatioSlider->GetValue(), mRatio).Translation());
   mRatioText->SetName(mRatioText->GetLabel()); // fix for bug 577 (NVDA/Narrator screen readers do not read static text in dialogs)

   mAttackLabel->SetName(wxString::Format(_("Attack Time %.2f secs"), mAttackTime));
   mAttackText->SetLabel(AttackTimeFormat(mAttackTime).Translation());
   mAttackText->SetName(mAttackText->GetLabel()); // fix for bug 577 (NVDA/Narrator screen readers do not read static text in dialogs)

   mDecayLabel->SetName(wxString::Format(_("Release Time %.1f secs"), mDecayTime));
   mDecayText->SetLabel(DecayTimeFormat(mDecayTime).Translation());
   mDecayText->SetName(mDecayText->GetLabel()); // fix for bug 577 (NVDA/Narrator screen readers do not read static text in dialogs)

   mPanel->Refresh(false);

   return;
}

//----------------------------------------------------------------------------
// EffectLegacyCompressorPanel
//----------------------------------------------------------------------------

BEGIN_EVENT_TABLE(EffectLegacyCompressorPanel, wxPanelWrapper)
EVT_PAINT(EffectLegacyCompressorPanel::OnPaint)
EVT_SIZE(EffectLegacyCompressorPanel::OnSize)
   END_EVENT_TABLE()

   EffectLegacyCompressorPanel::EffectLegacyCompressorPanel(
      wxWindow* parent, wxWindowID winid, double& threshold, double& noiseFloor,
      double& ratio)
       : wxPanelWrapper(parent, winid)
       , threshold(threshold)
       , noiseFloor(noiseFloor)
       , ratio(ratio)
   {
}

void EffectLegacyCompressorPanel::OnPaint(wxPaintEvent& WXUNUSED(evt))
{
   wxPaintDC dc(this);

   int width, height;
   GetSize(&width, &height);

   double rangeDB = 60;

   // Ruler
   int w = 0;
   int h = 0;

   Ruler vRuler{ LinearUpdater::Instance(), LinearDBFormat::Instance() };
   vRuler.SetBounds(0, 0, width, height);
   vRuler.SetOrientation(wxVERTICAL);
   vRuler.SetRange(0, -rangeDB);
   vRuler.SetUnits(XO("dB"));
   vRuler.GetMaxSize(&w, NULL);

   Ruler hRuler{ LinearUpdater::Instance(), LinearDBFormat::Instance() };
   hRuler.SetBounds(0, 0, width, height);
   hRuler.SetOrientation(wxHORIZONTAL);
   hRuler.SetRange(-rangeDB, 0);
   hRuler.SetUnits(XO("dB"));
   hRuler.SetFlip(true);
   hRuler.GetMaxSize(NULL, &h);

   vRuler.SetBounds(0, 0, w, height - h);
   hRuler.SetBounds(w, height - h, width, height);

   vRuler.SetTickColour( theTheme.Colour( clrGraphLabels ));
   hRuler.SetTickColour( theTheme.Colour( clrGraphLabels ));

#if defined(__WXMSW__)
   dc.Clear();
#endif

   wxRect border;
   border.x = w;
   border.y = 0;
   border.width = width - w;
   border.height = height - h + 1;

   dc.SetBrush(*wxWHITE_BRUSH);
   dc.SetPen(*wxBLACK_PEN);
   dc.DrawRectangle(border);

   wxRect envRect = border;
   envRect.Deflate( 2, 2 );

   int kneeX = lrint((rangeDB+threshold)*envRect.width/rangeDB);
   int kneeY = lrint((rangeDB+threshold/ratio)*envRect.height/rangeDB);

   int finalY = envRect.height;
   int startY = lrint((threshold*(1.0/ratio-1.0))*envRect.height/rangeDB);

   // Yellow line for threshold
/*   dc.SetPen(wxPen(wxColour(220, 220, 0), 1, wxSOLID));
   AColor::Line(dc,
                envRect.x,
                envRect.y + envRect.height - kneeY,
                envRect.x + envRect.width - 1,
                envRect.y + envRect.height - kneeY);*/

   // Was: Nice dark red line for the compression diagram
//   dc.SetPen(wxPen(wxColour(180, 40, 40), 3, wxSOLID));

   // Nice blue line for compressor, same color as used in the waveform envelope.
   dc.SetPen( AColor::WideEnvelopePen) ;

   AColor::Line(dc,
                envRect.x,
                envRect.y + envRect.height - startY,
                envRect.x + kneeX - 1,
                envRect.y + envRect.height - kneeY);

   AColor::Line(dc,
                envRect.x + kneeX,
                envRect.y + envRect.height - kneeY,
                envRect.x + envRect.width - 1,
                envRect.y + envRect.height - finalY);

   // Paint border again
   dc.SetBrush(*wxTRANSPARENT_BRUSH);
   dc.SetPen(*wxBLACK_PEN);
   dc.DrawRectangle(border);

   vRuler.Draw(dc);
   hRuler.Draw(dc);
}

void EffectLegacyCompressorPanel::OnSize(wxSizeEvent& WXUNUSED(evt))
{
   Refresh(false);
}
