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/ambisonic_mixing_encoder_node.h" |
18 | |
19 | #include "ambisonics/utils.h" |
20 | #include "base/constants_and_types.h" |
21 | #include "base/logging.h" |
22 | |
23 | |
24 | namespace vraudio { |
25 | |
26 | AmbisonicMixingEncoderNode::AmbisonicMixingEncoderNode( |
27 | const SystemSettings& system_settings, |
28 | const AmbisonicLookupTable& lookup_table, int ambisonic_order) |
29 | : system_settings_(system_settings), |
30 | lookup_table_(lookup_table), |
31 | ambisonic_order_(ambisonic_order), |
32 | gain_mixer_(GetNumPeriphonicComponents(ambisonic_order: ambisonic_order_), |
33 | system_settings_.GetFramesPerBuffer()), |
34 | coefficients_(GetNumPeriphonicComponents(ambisonic_order: ambisonic_order_)) {} |
35 | |
36 | const AudioBuffer* AmbisonicMixingEncoderNode::AudioProcess( |
37 | const NodeInput& input) { |
38 | |
39 | |
40 | const WorldPosition& listener_position = system_settings_.GetHeadPosition(); |
41 | const WorldRotation& listener_rotation = system_settings_.GetHeadRotation(); |
42 | |
43 | gain_mixer_.Reset(); |
44 | for (auto& input_buffer : input.GetInputBuffers()) { |
45 | const int source_id = input_buffer->source_id(); |
46 | const auto source_parameters = |
47 | system_settings_.GetSourceParameters(source_id); |
48 | DCHECK_NE(source_id, kInvalidSourceId); |
49 | DCHECK_EQ(input_buffer->num_channels(), 1U); |
50 | |
51 | // Compute the relative source direction in spherical angles. |
52 | const ObjectTransform& source_transform = |
53 | source_parameters->object_transform; |
54 | WorldPosition relative_direction; |
55 | GetRelativeDirection(from_position: listener_position, from_rotation: listener_rotation, |
56 | to_position: source_transform.position, relative_direction: &relative_direction); |
57 | const SphericalAngle source_direction = |
58 | SphericalAngle::FromWorldPosition(world_position: relative_direction); |
59 | |
60 | lookup_table_.GetEncodingCoeffs(ambisonic_order: ambisonic_order_, source_direction, |
61 | source_spread_deg: source_parameters->spread_deg, |
62 | encoding_coeffs: &coefficients_); |
63 | |
64 | gain_mixer_.AddInputChannel(input: (*input_buffer)[0], source_id, gains: coefficients_); |
65 | } |
66 | return gain_mixer_.GetOutput(); |
67 | } |
68 | |
69 | } // namespace vraudio |
70 | |