1 /*
2 * Copyright 2019 Google LLC
3 *
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
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 #ifndef ANDROID_FXLAB_SINGLEFUNCTIONEFFECTS_H
17 #define ANDROID_FXLAB_SINGLEFUNCTIONEFFECTS_H
18
19 #include <list>
20
21 namespace SingleFunctionEffects {
22
23 template<class floating>
_overdrive(floating & x)24 void _overdrive (floating &x) {
25 static constexpr double third = (1.0 / 3.0);
26 auto abs = std::abs(x);
27 if (abs <= third) {
28 x *= 2;
29 } else if (abs <= 2 * third) {
30 x = std::copysign((3 - (2 - 3 * abs) * (2 - 3 * abs)) * third, x);
31 } else {
32 x = std::copysign(1, x);
33 }
34 }
35
36 template <class iter_type>
overdrive(iter_type beg,iter_type end)37 void overdrive(iter_type beg, iter_type end) {
38 for (; beg != end; ++beg){
39 _overdrive(*beg);
40 }
41 }
42
43 template <class floating>
_distortion(floating & x)44 void _distortion (floating &x) {
45 x = std::copysign(-std::expm1(-std::abs(x)), x);
46 }
47
48 template <class iter_type>
distortion(iter_type beg,iter_type end)49 void distortion(iter_type beg, iter_type end) {
50 for (; beg != end; ++beg) {
51 _distortion(*beg);
52 }
53 }
54
55 }
56 #endif //ANDROID_FXLAB_SINGLEFUNCTIONEFFECTS_H
57