Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.5KB

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