<commit_before>// ------------------------------------------------------------------------
// audiofx_compressor.cpp: C++ implementation of <NAME>'s 
//                         compressor code. If you want the original
//			   C-sources, mail me. 
// Copyright (C) 1999-2000 <NAME> (<EMAIL>)
//
// Copyright for the actual algorithm (compressor2.c):
// ***************************************************
/*
 * Copyright (c) 1996, <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * This code (easily) runs realtime on a P5-166 w/EDO, Triton-II on FreeBSD.
 *
 * More info/comments: <EMAIL>
 *
 * This program provides compression of a stereo 16bit audio stream,
 * such as that contained by a 16Bit wav file.  Extreme measures have
 * been taken to make the compression as subtile as possible.  One
 * possible purpose for this code would be to master cassette tapes from
 * CD's for playback in automobiles where dynamic range needs to be
 * restricted.
 *
 * Suitably recoded for an embedded DSP, this would make a killer audio
 * compressor for broadcast or recording.  When writing this code, I
 * ignored the issues of roundoff error or trucation -- Pentiums have
 * really nice FP processors :-).
 */
// ------------------------------------------------------------------------

#include <cmath>
#include <vector>

#include <kvu_message_item.h>

#include "samplebuffer_iterators.h"

#include "audiofx_amplitude.h"

#include "eca-logger.h"

ADVANCED_COMPRESSOR::ADVANCED_COMPRESSOR (double peak_limit, double release_time, double cfrate,
					  double crate) 
  : rlevelsqn(ADVANCED_COMPRESSOR::NFILT),
    rlevelsqe(ADVANCED_COMPRESSOR::NEFILT)
{
  init_values();

  set_parameter(1, peak_limit);
  set_parameter(2, release_time);
  set_parameter(3, cfrate);
  set_parameter(4, crate);

  MESSAGE_ITEM otemp;
  otemp.setprecision(2);
  otemp << "(audiofx_compressor) Advanced compressor enabled;";
  otemp << " peak limit " << peakpercent;
  otemp << " release time " << release_time;
  otemp << " cfrate " << fastgaincompressionratio;
  otemp << " crate " << compressionratio << ".";
  ECA_LOG_MSG(ECA_LOGGER::info, otemp.to_string());
}

void ADVANCED_COMPRESSOR::init_values(void) {
  mingain = 10000;
  maxgain = 0;

  /* These filters should filter at least the lowest audio freq */
  rlevelsq0filter = .001;
  rlevelsq1filter = .010;
  /* These are the attack time for the rms measurement */
  rlevelsq0ffilter = .001;
  rlevelsqefilter = .001;

  /*
   * maximum gain for fast compressor
   */
  maxfastgain = 3;
  /*
   * maximum gain for slow compressor
   */
  maxslowgain = 9;

  /*
   * Level below which gain tracking shuts off
   */
  floorlevel = SAMPLE_SPECS::max_amplitude * 0.06;   // was 2000
  //  floorlevel = 2000;
  
  /*
   * Slow compressor time constants
   */
  rmastergain0filter = .000003;
  
  rpeakgainfilter = .001;
  rpeaklimitdelay = 2500;
  
  rgain = rmastergain0 = 1.0;
  rlevelsq0 = levelsq1 = 0;
  rlevelsq1 = 0;
  compress = 1;
  ndelay = (int)(1.0 / rlevelsq0ffilter);
  //  ECA_LOG_MSG(ECA_LOGGER::user_objects, "(audiofx_compressor) Number of delays : " +
  //	       kvu_numtostr(ndelay) + ".");

  rightdelay.resize(ndelay);
  leftdelay.resize(ndelay);

  //  rlevelsqn = new vector<double> (NFILT + 1);
  //  rlevelsqe = new vector<double> (NEFILT + 1);

  rpeakgain0 = 1.0;
  rpeakgain1 = 1.0;
  rpeaklimitdelay = 0;
  ndelayptr = 0;
  lastrgain = 1.0;

  for(i = 0; i < NFILT;i++)
    rlevelsqn[i] = 0.0;
  for(i = 0; i < NEFILT;i++)
    rlevelsqe[i] = 0.0;
}

ADVANCED_COMPRESSOR::~ADVANCED_COMPRESSOR (void)
{
}

void ADVANCED_COMPRESSOR::set_parameter(int param, CHAIN_OPERATOR::parameter_t value) {

  //  cerr << "Param: " << param << ", value: " << value << ".\n";

  switch (param) {
  case 1: 
    {
      // ---
      // target level for compression
      // ---

      maxlevel = SAMPLE_SPECS::max_amplitude * 0.9; // limiter level (was 32000)
      //      maxlevel = 32000;
      peakpercent = value;
      if (peakpercent == 0) peakpercent = 69;
      targetlevel = maxlevel * peakpercent / 100.0;
      break;
    }

  case 2:
    {
      // --
      // Linear gain filters as opposed to the level measurement filters
      // --
      releasetime = value;
      if (releasetime == 0) releasetime = 0.01;
      rgainfilter = 1.0 / (releasetime * 44100.0);
      //      rgainfilter = 1.0 / (releasetime * samples_per_second());
      break;
    }

  case 3:
    {
      // --
      // compression ratio for fast gain.  This will determine how
      // much the audio is made more dense.  .5 is equiv to 2:1
      // compression.  1.0 is equiv to inf:1 compression.
      // --
      fratio = value;
      if (fratio == 0) fratio = 0.5;
      fastgaincompressionratio = fratio;
      break;      
    }
    
  case 4:
    {<commit_msg>Initialize with valid values to avoid float infs.<commit_after>// ------------------------------------------------------------------------
// audiofx_compressor.cpp: C++ implementation of <NAME>'s 
//                         compressor code. If you want the original
//			   C-sources, mail me. 
// Copyright (C) 1999-2000 <NAME> (<EMAIL>)
//
// Copyright for the actual algorithm (compressor2.c):
// ***************************************************
/*
 * Copyright (c) 1996, <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * This code (easily) runs realtime on a P5-166 w/EDO, Triton-II on FreeBSD.
 *
 * More info/comments: <EMAIL>
 *
 * This program provides compression of a stereo 16bit audio stream,
 * such as that contained by a 16Bit wav file.  Extreme measures have
 * been taken to make the compression as subtile as possible.  One
 * possible purpose for this code would be to master cassette tapes from
 * CD's for playback in automobiles where dynamic range needs to be
 * restricted.
 *
 * Suitably recoded for an embedded DSP, this would make a killer audio
 * compressor for broadcast or recording.  When writing this code, I
 * ignored the issues of roundoff error or trucation -- Pentiums have
 * really nice FP processors :-).
 */
// ------------------------------------------------------------------------

#include <cmath>
#include <vector>

#include <kvu_dbc.h>
#include <kvu_message_item.h>

#include "samplebuffer_iterators.h"

#include "audiofx_amplitude.h"

#include "eca-logger.h"

ADVANCED_COMPRESSOR::ADVANCED_COMPRESSOR (double peak_limit, double release_time, double cfrate,
					  double crate) 
  : rlevelsqn(ADVANCED_COMPRESSOR::NFILT),
    rlevelsqe(ADVANCED_COMPRESSOR::NEFILT)
{
  init_values();

  set_parameter(1, peak_limit);
  set_parameter(2, release_time);
  set_parameter(3, cfrate);
  set_parameter(4, crate);

  MESSAGE_ITEM otemp;
  otemp.setprecision(2);
  otemp << "(audiofx_compressor) Advanced compressor enabled;";
  otemp << " peak limit " << peakpercent;
  otemp << " release time " << release_time;
  otemp << " cfrate " << fastgaincompressionratio;
  otemp << " crate " << compressionratio << ".";
  ECA_LOG_MSG(ECA_LOGGER::info, otemp.to_string());
}

void ADVANCED_COMPRESSOR::init_values(void) {
  mingain = 10000;
  maxgain = 0;

  /* These filters should filter at least the lowest audio freq */
  rlevelsq0filter = .001;
  rlevelsq1filter = .010;
  /* These are the attack time for the rms measurement */
  rlevelsq0ffilter = .001;
  rlevelsqefilter = .001;

  /*
   * maximum gain for fast compressor
   */
  maxfastgain = 3;
  /*
   * maximum gain for slow compressor
   */
  maxslowgain = 9;

  /*
   * Level below which gain tracking shuts off
   */
  floorlevel = SAMPLE_SPECS::max_amplitude * 0.06;   // was 2000
  //  floorlevel = 2000;
  
  /*
   * Slow compressor time constants
   */
  rmastergain0filter = .000003;
  
  rpeakgainfilter = .001;
  rpeaklimitdelay = 2500;
  
  rgain = rmastergain0 = 1.0;
  rlevelsq0 = levelsq1 = 0;
  rlevelsq1 = 0;
  compress = 1;
  ndelay = (int)(1.0 / rlevelsq0ffilter);
  //  ECA_LOG_MSG(ECA_LOGGER::user_objects, "(audiofx_compressor) Number of delays : " +
  //	       kvu_numtostr(ndelay) + ".");

  rightdelay.resize(ndelay);
  leftdelay.resize(ndelay);

  //  rlevelsqn = new vector<double> (NFILT + 1);
  //  rlevelsqe = new vector<double> (NEFILT + 1);

  rpeakgain0 = 1.0;
  rpeakgain1 = 1.0;
  rpeaklimitdelay = 0;
  ndelayptr = 0;
  lastrgain = 1.0;

  for(i = 0; i < NFILT;i++)
    rlevelsqn[i] = 0.0;
  for(i = 0; i < NEFILT;i++)
    rlevelsqe[i] = 0.0;

  /* set defaults to some sane values */
  peakpercent = 100.0f;
  releasetime = 0;
  fratio = 1.0;
  fratio = 1.0;
}

ADVANCED_COMPRESSOR::~ADVANCED_COMPRESSOR (void)
{
}

void ADVANCED_COMPRESSOR::set_parameter(int param, CHAIN_OPERATOR::parameter_t value) {

  //  cerr << "Param: " << param << ", value: " << value << ".\n";

  switch (param) {
  case 1: 
    {
      // ---
      // target level for compression
      // ---

      maxlevel = SAMPLE_SPECS::max_amplitude * 0.9; // limiter level (was 32000)
      //      maxlevel = 32000;
      peakpercent = value;
      if (peakpercent == 0) peakpercent = 69;
      targetlevel = maxlevel * peakpercent / 100.0;
      break;
    }

  case 2:
    {
      // --
      // Linear gain filters as opposed to the level measurement filters
      // --
      DBC_CHECK(samples_per_second() != 0);
      releasetime = value;
      if (releasetime == 0) releasetime = 0.01;
      rgainfilter = 1.0 / (releasetime * samples_per_second());
      break;
    }

  case 3:
    {
      // --
      // compression ratio for fast gain.  This will determine how
      // much the audio is made more dense.  .5 is equiv to 2:1
      // compression.  1.0 is equiv to inf:1 compression.
      // --
      fratio = value;
      if (fratio == 0) fratio = 0.5;
      fastgaincompressionratio = fratio;
      break;      
    }
    
  case 4:
    {
{'fraction_non_alphanumeric': 0.14545961520587417, 'fraction_numerical': 0.028441304953992008, 'mean_word_length': 2.597459043798061, 'pattern_counts': {'":': 0, '<': 88, '<?xml version=': 0, '>': 24, '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': '7229427', 'n_tokens_mistral': 3799, 'n_tokens_neox': 3440, 'n_words': 1027}