zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

Compressor.h (1406B)


      1 /*
      2    ZynAddSubFX - a software synthesizer
      3 
      4    Compressor.h - simple audio compressor macros
      5    Copyright (C) 2016-2021 Hans Petter Selasky
      6 
      7   This program is free software; you can redistribute it and/or
      8   modify it under the terms of the GNU General Public License
      9   as published by the Free Software Foundation; either version 2
     10   of the License, or (at your option) any later version.
     11 */
     12 
     13 #ifndef _COMPRESSOR_H_
     14 #define	_COMPRESSOR_H_
     15 
     16 static inline bool floatIsValid(float x)
     17 {
     18     const float r = x * 0.0f;
     19     return (r == 0.0f);
     20 }
     21 
     22 static inline void stereoCompressor(const int div, float &peak, float &l, float &r)
     23 {
     24     /*
     25      * Don't max the output range to avoid
     26      * overflowing sample rate conversion and
     27      * equalizer filters in the DSP's output
     28      * path. Keep one 10th, 1dB, reserved.
     29      */
     30     constexpr float limit = 1.0f - (1.0f / 10.0f);
     31 
     32     /* sanity checks */
     33     if (!floatIsValid(peak))
     34         peak = 0.0f;
     35     if (!floatIsValid(l))
     36         l = 0.0f;
     37     if (!floatIsValid(r))
     38         r = 0.0f;
     39     /* compute maximum */
     40     if (l < -peak)
     41         peak = -l;
     42     else if (l > peak)
     43         peak = l;
     44     if (r < -peak)
     45         peak = -r;
     46     else if (r > peak)
     47         peak = r;
     48     /* compressor */
     49     if (peak > limit) {
     50         l /= peak;
     51         r /= peak;
     52         l *= limit;
     53         r *= limit;
     54         peak -= peak / div;
     55     }
     56 }
     57 
     58 #endif			/* _COMPRESSOR_H_ */