<commit_before>/* COMPLIMIT - a compressor-limiter, with lookahead

   The parameters marked with '*' can receive dynamic updates from a table
   or a real-time control source.

      p0  = output start time
      p1  = input start time
      p2  = duration
    * p3  = gain applied to input before compression (in dBFS; 0 is no change)
    * p4  = gain applied to output after compression - "makeup gain" (in dBFS)
    * p5  = attack time (seconds)
    * p6  = release time (seconds)
    * p7  = threshold (in dBFS)
    * p8  = compression ratio - e.g. 20 means 20:1 (100 is infinity)
      p9  = look-ahead time (seconds)
      p10 = peak detection window size (power of 2 <= RTCmix output buffer size)
    * p11 = detection type (0: peak, 1: average peak, 2: rms)
            [optional; default is 0)
    * p12 = bypass (1: bypass on, 0: bypass off) [optional; default is 0]
    * p13 = input channel  [optional; default is 0]
    * p14 = percent output to left channel (0 - 1)  [optional; default is 0.5]

   NOTES

   1. For backward compatibility with pre-v4 scores, the optional gen table 1
      scales the makeup gain (p4), after conversion from dB to linear amp.
      When bypass (p12) is on, there is no enveloping at all in this version.

   2. BUG: Sometimes the compressor will stay in sustain for too long.  This
      happens when the source sound has a long decaying envelope during which
      some of the decay is above the threshold.


   <NAME> <johgibso at indiana dot edu>, 4/21/00; rev. for v4, 6/18/05
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>       // DBL_MAX
#include <ugens.h>
#include <Instrument.h>
#include "COMPLIMIT.h"
#include <rt.h>
#include <rtdefs.h>
#include <assert.h>

//#define DEBUG
//#define DEBUG2

#ifdef DEBUG
   #define DPRINT(msg)        printf((msg))
   #define DPRINT1(msg, arg)  printf((msg), (arg))
#else
   #define DPRINT(msg)
   #define DPRINT1(msg, arg)
#endif

// experiment to relieve symptom of bug described in comment above
// (major problem for pnoEb2stop.tm2.aif)
#define LONG_SUSTAIN_FIX
#define MAX_WINS 5        // just a guess

#define DEFAULT_WINDOW_SIZE   128


COMPLIMIT::COMPLIMIT() : Instrument()
{
   in = NULL;
   first_time = true;
   env_state = ENV_INACTIVE;
   env_count = -1;
   sus_count = 0;
   gain = 0.0;
   ingain = -DBL_MAX;
   outgain = -DBL_MAX;
   threshold_dbfs = -DBL_MAX;
   atk_time = -DBL_MAX;
   rel_time = -DBL_MAX;
   target_peak = 0.0;
   next_target_gain = 0.0;
   wins_under_thresh = 0;
   branch = 0;
}

COMPLIMIT::~COMPLIMIT()
{
   delete [] in;
}

int COMPLIMIT::usage()
{
	return die("COMPLIMIT", "Usage: COMPLIMIT(start, inskip, dur, ingain, "
              "outgain, atktime, reltime, threshold, ratio, lookahead, "
              "[windowsize, detection_type, bypass, inchan, pan])");
}

inline int min(int a, int b) { return (a < b) ? a : b; }

DetectType getDetectType(double pval)
{
   int intval = int(pval);
   DetectType type = PEAK_DETECTOR;

   switch (intval) {
      case 0:
         type = PEAK_DETECTOR;
         break;
      case 1:
         type = AVERAGE_DETECTOR;
         break;
      case 2:
         type = RMS_DETECTOR;
         break;
      default:
         die("COMPLIMIT", "Invalid detector type %d\n.", intval);
         break;
   }
   return type;
}

int COMPLIMIT::init(double p[], int n_args)
{
   nargs = n_args;
   if (nargs < 11)
      return usage();
<commit_msg>Fixed missing COMPLIMIT:: on getDetectType<commit_after>/* COMPLIMIT - a compressor-limiter, with lookahead

   The parameters marked with '*' can receive dynamic updates from a table
   or a real-time control source.

      p0  = output start time
      p1  = input start time
      p2  = duration
    * p3  = gain applied to input before compression (in dBFS; 0 is no change)
    * p4  = gain applied to output after compression - "makeup gain" (in dBFS)
    * p5  = attack time (seconds)
    * p6  = release time (seconds)
    * p7  = threshold (in dBFS)
    * p8  = compression ratio - e.g. 20 means 20:1 (100 is infinity)
      p9  = look-ahead time (seconds)
      p10 = peak detection window size (power of 2 <= RTCmix output buffer size)
    * p11 = detection type (0: peak, 1: average peak, 2: rms)
            [optional; default is 0)
    * p12 = bypass (1: bypass on, 0: bypass off) [optional; default is 0]
    * p13 = input channel  [optional; default is 0]
    * p14 = percent output to left channel (0 - 1)  [optional; default is 0.5]

   NOTES

   1. For backward compatibility with pre-v4 scores, the optional gen table 1
      scales the makeup gain (p4), after conversion from dB to linear amp.
      When bypass (p12) is on, there is no enveloping at all in this version.

   2. BUG: Sometimes the compressor will stay in sustain for too long.  This
      happens when the source sound has a long decaying envelope during which
      some of the decay is above the threshold.


   <NAME> <johgibso at indiana dot edu>, 4/21/00; rev. for v4, 6/18/05
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>       // DBL_MAX
#include <ugens.h>
#include <Instrument.h>
#include "COMPLIMIT.h"
#include <rt.h>
#include <rtdefs.h>
#include <assert.h>

//#define DEBUG
//#define DEBUG2

#ifdef DEBUG
   #define DPRINT(msg)        printf((msg))
   #define DPRINT1(msg, arg)  printf((msg), (arg))
#else
   #define DPRINT(msg)
   #define DPRINT1(msg, arg)
#endif

// experiment to relieve symptom of bug described in comment above
// (major problem for pnoEb2stop.tm2.aif)
#define LONG_SUSTAIN_FIX
#define MAX_WINS 5        // just a guess

#define DEFAULT_WINDOW_SIZE   128


COMPLIMIT::COMPLIMIT() : Instrument()
{
   in = NULL;
   first_time = true;
   env_state = ENV_INACTIVE;
   env_count = -1;
   sus_count = 0;
   gain = 0.0;
   ingain = -DBL_MAX;
   outgain = -DBL_MAX;
   threshold_dbfs = -DBL_MAX;
   atk_time = -DBL_MAX;
   rel_time = -DBL_MAX;
   target_peak = 0.0;
   next_target_gain = 0.0;
   wins_under_thresh = 0;
   branch = 0;
}

COMPLIMIT::~COMPLIMIT()
{
   delete [] in;
}

int COMPLIMIT::usage()
{
	return die("COMPLIMIT", "Usage: COMPLIMIT(start, inskip, dur, ingain, "
              "outgain, atktime, reltime, threshold, ratio, lookahead, "
              "[windowsize, detection_type, bypass, inchan, pan])");
}

inline int min(int a, int b) { return (a < b) ? a : b; }

DetectType COMPLIMIT::getDetectType(double pval)
{
   int intval = int(pval);
   DetectType type = PEAK_DETECTOR;

   switch (intval) {
      case 0:
         type = PEAK_DETECTOR;
         break;
      case 1:
         type = AVERAGE_DETECTOR;
         break;
      case 2:
         type = RMS_DETECTOR;
         break;
      default:
         die("COMPLIMIT", "Invalid detector type %d\n.", intval);
         break;
   }
   return type;
}

int COMPLIMIT::init(double p[], int n_args)
{
   nargs = n_args;
   if (nargs < 11)
      return usage();

{'fraction_non_alphanumeric': 0.1087648249927683, 'fraction_numerical': 0.02429852473242696, 'mean_word_length': 2.6998394863563404, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5607070', 'n_tokens_mistral': 2501, 'n_tokens_neox': 2391, 'n_words': 810}