| 1 | /* |
| 2 | Copyright 2018 Google Inc. All Rights Reserved. |
| 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 | http://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 | |
| 17 | #include "dsp/occlusion_calculator.h" |
| 18 | |
| 19 | #include <cmath> |
| 20 | |
| 21 | #include "base/logging.h" |
| 22 | #include "base/misc_math.h" |
| 23 | |
| 24 | namespace vraudio { |
| 25 | |
| 26 | float CalculateDirectivity(float alpha, float order, |
| 27 | const SphericalAngle& spherical_angle) { |
| 28 | // Clamp alpha weighting. |
| 29 | const float alpha_clamped = std::min(a: std::max(a: alpha, b: 0.0f), b: 1.0f); |
| 30 | |
| 31 | // Check for zero-valued alpha (omnidirectional). |
| 32 | if (alpha_clamped < std::numeric_limits<float>::epsilon()) { |
| 33 | return 1.0f; |
| 34 | } else { |
| 35 | const float gain = (1.0f - alpha_clamped) + |
| 36 | alpha_clamped * (std::cos(x: spherical_angle.azimuth()) * |
| 37 | std::cos(x: spherical_angle.elevation())); |
| 38 | |
| 39 | return std::pow(x: std::abs(x: gain), y: std::max(a: order, b: 1.0f)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | float CalculateOcclusionFilterCoefficient(float directivity, |
| 44 | float occlusion_intensity) { |
| 45 | DCHECK_GE(occlusion_intensity, 0.0f); |
| 46 | |
| 47 | const float occlusion_factor = |
| 48 | 1.0f / IntegerPow(base: occlusion_intensity + 1.0f, exp: 4); |
| 49 | return std::max(a: 0.0f, b: 1.0f - directivity * occlusion_factor); |
| 50 | } |
| 51 | |
| 52 | } // namespace vraudio |
| 53 | |