zynaddsubfx

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

commit 5b1a0d20ab4a2ecbec5b980d8a462d45a50a591e
parent 93160d7d07b9e33d040dd54b38ce4884f3969a48
Author: Hans Petter Selasky <hps@selasky.org>
Date:   Wed,  2 Jun 2021 16:21:31 +0200

Convert compressor macros to static inline functions to ease readability.

No functional change intended.

Signed-off-by: Hans Petter Selasky <hps@selasky.org>

Diffstat:
Msrc/Nio/Compressor.h | 83+++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 41 insertions(+), 42 deletions(-)

diff --git a/src/Nio/Compressor.h b/src/Nio/Compressor.h @@ -2,7 +2,7 @@ ZynAddSubFX - a software synthesizer Compressor.h - simple audio compressor macros - Copyright (C) 2016 Hans Petter Selasky + Copyright (C) 2016-2021 Hans Petter Selasky This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -13,47 +13,46 @@ #ifndef _COMPRESSOR_H_ #define _COMPRESSOR_H_ -#define floatIsValid(x) ({ \ - const float __r = (x) * 0.0f; \ - __r == 0.0f; \ -}) +static inline bool floatIsValid(float x) +{ + const float r = x * 0.0f; + return (r == 0.0f); +} -#define stereoCompressor(div,pv,l,r) do { \ - /* \ - * Don't max the output range to avoid \ - * overflowing sample rate conversion and \ - * equalizer filters in the DSP's output \ - * path. Keep one 10th, 1dB, reserved. \ - */ \ - const float __limit = 1.0f - (1.0f / 10.0f); \ - float __peak; \ - \ - /* sanity checks */ \ - __peak = (pv); \ - if (!floatIsValid(__peak)) \ - __peak = 0.0f; \ - if (!floatIsValid(l)) \ - (l) = 0.0f; \ - if (!floatIsValid(r)) \ - (r) = 0.0f; \ - /* compute maximum */ \ - if ((l) < -__peak) \ - __peak = -(l); \ - else if ((l) > __peak) \ - __peak = (l); \ - if ((r) < -__peak) \ - __peak = -(r); \ - else if ((r) > __peak) \ - __peak = (r); \ - /* compressor */ \ - if (__peak > __limit) { \ - (l) /= __peak; \ - (r) /= __peak; \ - (l) *= __limit; \ - (r) *= __limit; \ - __peak -= __peak / (div); \ - } \ - (pv) = __peak; \ -} while (0) +static inline void stereoCompressor(const int div, float &peak, float &l, float &r) +{ + /* + * Don't max the output range to avoid + * overflowing sample rate conversion and + * equalizer filters in the DSP's output + * path. Keep one 10th, 1dB, reserved. + */ + constexpr float limit = 1.0f - (1.0f / 10.0f); + + /* sanity checks */ + if (!floatIsValid(peak)) + peak = 0.0f; + if (!floatIsValid(l)) + l = 0.0f; + if (!floatIsValid(r)) + r = 0.0f; + /* compute maximum */ + if (l < -peak) + peak = -l; + else if (l > peak) + peak = l; + if (r < -peak) + peak = -r; + else if (r > peak) + peak = r; + /* compressor */ + if (peak > limit) { + l /= peak; + r /= peak; + l *= limit; + r *= limit; + peak -= peak / div; + } +} #endif /* _COMPRESSOR_H_ */