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 | #ifndef RESONANCE_AUDIO_DSP_DISTANCE_ATTENUATION_H_ |
18 | #define RESONANCE_AUDIO_DSP_DISTANCE_ATTENUATION_H_ |
19 | |
20 | #include "base/misc_math.h" |
21 | #include "base/source_parameters.h" |
22 | |
23 | namespace vraudio { |
24 | |
25 | // Returns the distance attenuation for |source_position| with respect to |
26 | // |listener_position|. The amplitude will decrease by approximately 6 dB every |
27 | // time the distance is doubled, i.e., the sound pressure "p" (amplitude) |
28 | // approximately falls inversely proportional to the distance "1/r". |
29 | // |
30 | // @param listener_position World position of the listener. |
31 | // @param source_position World position of the source. |
32 | // @param min_distance The minimum distance at which distance attenuation is |
33 | // applied. |
34 | // @param max_distance The maximum distance at which the direct sound has a gain |
35 | // of 0.0. |
36 | // @return Attenuation (gain) value in range [0.0f, 1.0f]. |
37 | float ComputeLogarithmicDistanceAttenuation( |
38 | const WorldPosition& listener_position, |
39 | const WorldPosition& source_position, float min_distance, |
40 | float max_distance); |
41 | |
42 | // Returns the distance attenuation for |source_position| with respect to |
43 | // |listener_position|. The amplitude will decrease linearly between |
44 | // |min_distance| and |max_distance| from 1.0 to 0.0. |
45 | // |
46 | // @param listener_position World position of the listener. |
47 | // @param source_position World position of the source. |
48 | // @param min_distance The minimum distance at which distance attenuation is |
49 | // applied. |
50 | // @param max_distance The maximum distance at which the direct sound has a gain |
51 | // of 0.0. |
52 | // @return Attenuation (gain) value in range [0.0f, 1.0f]. |
53 | float ComputeLinearDistanceAttenuation(const WorldPosition& listener_position, |
54 | const WorldPosition& source_position, |
55 | float min_distance, float max_distance); |
56 | |
57 | // Calculates the gain to be applied to the near field compensating stereo mix. |
58 | // This function will return 0.0f for all sources further away than one meter |
59 | // and will return a value between 0.0 and 9.0 for sources as they approach |
60 | // the listener's head location. |
61 | // |
62 | // @param listener_position World position of the listener. |
63 | // @param source_position World position of the source. |
64 | // @return Gain value in range [0.0f, 9.0f]. |
65 | float ComputeNearFieldEffectGain(const WorldPosition& listener_position, |
66 | const WorldPosition& source_position); |
67 | |
68 | // Calculates and updates gain attenuations of the given source |parameters|. |
69 | // |
70 | // @param master_gain Global gain adjustment in amplitude. |
71 | // @param reflections_gain Reflections gain in amplitude. |
72 | // @param reverb_gain Reverb gain in amplitude. |
73 | // @param listener_position World position of the listener. |
74 | // @param parameters Source parameters to apply the gain attenuations into. |
75 | void UpdateAttenuationParameters(float master_gain, float reflections_gain, |
76 | float reverb_gain, |
77 | const WorldPosition& listener_position, |
78 | SourceParameters* parameters); |
79 | |
80 | } // namespace vraudio |
81 | |
82 | #endif // RESONANCE_AUDIO_DSP_DISTANCE_ATTENUATION_H_ |
83 | |