package com.cherryaudio.limiterdemo;

// This class hard limits a signal to a maximum amplitude of >0 to 5.0,
// and optionally compensates for the volume loss by boosting the output signal.

class SignalLimiter
{
   public SignalLimiter()
   {
      maxValue = 5.0;
      ampBoost = 1.0;
   }

   // From >0 to 5.0
   public boolean SetMaxSignal(double max)
   {
      if (max > 0 && max < 5.0)
      {
         maxValue = max;
         ampBoost = 5.0 / maxValue;
      }
      
      return false;
   }

   public void SetVolumeBoost(boolean boost)
   {
      bVolumeBoost = boost;
   }

   public double ProcessSample(double input)
   {
      if (input > maxValue)
         input = maxValue;
      else if (input < -maxValue)
         input = -maxValue;

      if (bVolumeBoost)
         return (input * ampBoost);
      else
         return input;
   }

   private double maxValue = 5.0;
   private double ampBoost = 1.0;
   private boolean bVolumeBoost = false;
}