2 * Copyright (C) 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <audio_utils/limiter.h>
20 #undef USE_ATAN_APPROXIMATION
22 #ifdef USE_ATAN_APPROXIMATION
23 static inline float atan_approximation(float in)
25 static const float kPi_2 = 1.5625; // pi/2 ~= 2.0 * atan_approximation(1.0)
27 return in / (1.0 + 0.28 * in * in);
29 const float in_inverse = 1.0 / in;
30 return kPi_2 - in_inverse / (1.0 + 0.28 * in_inverse * in_inverse);
35 float limiter(float in)
37 static const float crossover = M_SQRT1_2;
38 float in_abs = fabsf(in);
39 if (in_abs <= crossover) {
43 #ifdef USE_ATAN_APPROXIMATION
45 static const float factor = 4.3125;
46 static const float ratio = 1.0 / factor;
47 out = crossover + atan_approximation((in_abs - crossover)*factor)*ratio;
53 static const float A = 0.3431457505;
54 static const float B = -1.798989873;
55 static const float C = 3.029437252;
56 static const float D = -0.6568542495;
57 // Cubic solution Ax^3 + Bx^2 + Cx + D
58 if (in_abs < M_SQRT2) {
59 // TODO Use Estrin's method P3
60 out = ((A*in_abs + B)*in_abs + C)*in_abs + D;