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 "graph/near_field_effect_node.h" |
18 | |
19 | #include <algorithm> |
20 | #include <cmath> |
21 | |
22 | #include "base/logging.h" |
23 | #include "base/spherical_angle.h" |
24 | |
25 | #include "dsp/distance_attenuation.h" |
26 | #include "dsp/gain.h" |
27 | #include "dsp/stereo_panner.h" |
28 | |
29 | namespace vraudio { |
30 | |
31 | NearFieldEffectNode::NearFieldEffectNode(SourceId source_id, |
32 | const SystemSettings& system_settings) |
33 | : pan_gains_({0.0f, 0.0f}), |
34 | near_field_processor_(system_settings.GetSampleRateHz(), |
35 | system_settings.GetFramesPerBuffer()), |
36 | system_settings_(system_settings), |
37 | output_buffer_(kNumStereoChannels, system_settings.GetFramesPerBuffer()) { |
38 | output_buffer_.set_source_id(source_id); |
39 | } |
40 | |
41 | const AudioBuffer* NearFieldEffectNode::AudioProcess(const NodeInput& input) { |
42 | |
43 | |
44 | const AudioBuffer* input_buffer = input.GetSingleInput(); |
45 | DCHECK(input_buffer); |
46 | DCHECK_EQ(input_buffer->num_channels(), 1U); |
47 | DCHECK_EQ(input_buffer->source_id(), output_buffer_.source_id()); |
48 | |
49 | const auto source_parameters = |
50 | system_settings_.GetSourceParameters(source_id: input_buffer->source_id()); |
51 | if (source_parameters == nullptr) { |
52 | LOG(WARNING) << "Could not find source parameters" ; |
53 | return nullptr; |
54 | } |
55 | |
56 | |
57 | DCHECK_EQ(pan_gains_.size(), kNumStereoChannels); |
58 | const float near_field_gain = source_parameters->near_field_gain; |
59 | if (near_field_gain > 0.0f) { |
60 | const auto& listener_position = system_settings_.GetHeadPosition(); |
61 | const auto& listener_rotation = system_settings_.GetHeadRotation(); |
62 | const auto& source_transform = source_parameters->object_transform; |
63 | // Compute the relative source direction in spherical angles to calculate |
64 | // the left and right panner gains. |
65 | WorldPosition relative_direction; |
66 | GetRelativeDirection(from_position: listener_position, from_rotation: listener_rotation, |
67 | to_position: source_transform.position, relative_direction: &relative_direction); |
68 | const auto source_direction = |
69 | SphericalAngle::FromWorldPosition(world_position: relative_direction); |
70 | CalculateStereoPanGains(source_direction, stereo_gains: &pan_gains_); |
71 | // Combine pan gains with per-source near field gain. |
72 | const float total_near_field_gain = |
73 | ComputeNearFieldEffectGain(listener_position, |
74 | source_position: source_transform.position) * |
75 | near_field_gain / kMaxNearFieldEffectGain; |
76 | for (size_t i = 0; i < pan_gains_.size(); ++i) { |
77 | pan_gains_[i] *= total_near_field_gain; |
78 | } |
79 | } else { |
80 | // Disable near field effect if |near_field_gain| is zero. |
81 | std::fill(first: pan_gains_.begin(), last: pan_gains_.end(), value: 0.0f); |
82 | } |
83 | |
84 | const float left_current_gain = left_panner_.GetGain(); |
85 | const float right_current_gain = right_panner_.GetGain(); |
86 | const float left_target_gain = pan_gains_[0]; |
87 | const float right_target_gain = pan_gains_[1]; |
88 | const bool is_left_zero_gain = |
89 | IsGainNearZero(gain: left_current_gain) && IsGainNearZero(gain: left_target_gain); |
90 | const bool is_right_zero_gain = |
91 | IsGainNearZero(gain: right_current_gain) && IsGainNearZero(gain: right_target_gain); |
92 | |
93 | if (is_left_zero_gain && is_right_zero_gain) { |
94 | // Make sure gain processors are initialized. |
95 | left_panner_.Reset(gain: 0.0f); |
96 | right_panner_.Reset(gain: 0.0f); |
97 | // Both channels go to zero, there is no need for further processing. |
98 | return nullptr; |
99 | } |
100 | |
101 | const auto& input_channel = (*input_buffer)[0]; |
102 | auto* left_output_channel = &output_buffer_[0]; |
103 | auto* right_output_channel = &output_buffer_[1]; |
104 | // Apply bass boost and delay compensation (if necessary) to the input signal |
105 | // and place it temporarily in the right output channel. This way we avoid |
106 | // allocating a temporary buffer. |
107 | near_field_processor_.Process(input: input_channel, output: right_output_channel, |
108 | enable_hrtf: source_parameters->enable_hrtf); |
109 | left_panner_.ApplyGain(target_gain: left_target_gain, input: *right_output_channel, |
110 | output: left_output_channel, /*accumulate_output=*/false); |
111 | right_panner_.ApplyGain(target_gain: right_target_gain, input: *right_output_channel, |
112 | output: right_output_channel, /*accumulate_output=*/false); |
113 | |
114 | return &output_buffer_; |
115 | } |
116 | |
117 | } // namespace vraudio |
118 | |