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/foa_rotator_node.h" |
18 | |
19 | #include "base/logging.h" |
20 | |
21 | |
22 | namespace vraudio { |
23 | |
24 | FoaRotatorNode::FoaRotatorNode(SourceId source_id, |
25 | const SystemSettings& system_settings) |
26 | : system_settings_(system_settings), |
27 | output_buffer_(kNumFirstOrderAmbisonicChannels, |
28 | system_settings.GetFramesPerBuffer()) { |
29 | output_buffer_.Clear(); |
30 | output_buffer_.set_source_id(source_id); |
31 | } |
32 | |
33 | const AudioBuffer* FoaRotatorNode::AudioProcess(const NodeInput& input) { |
34 | |
35 | |
36 | // Get the soundfield input buffer. |
37 | const AudioBuffer* input_buffer = input.GetSingleInput(); |
38 | DCHECK(input_buffer); |
39 | DCHECK_GT(input_buffer->num_frames(), 0U); |
40 | DCHECK_EQ(input_buffer->num_channels(), 4U); |
41 | DCHECK_EQ(input_buffer->source_id(), output_buffer_.source_id()); |
42 | |
43 | // Rotate soundfield buffer by the inverse head orientation. |
44 | const auto source_parameters = |
45 | system_settings_.GetSourceParameters(source_id: input_buffer->source_id()); |
46 | if (source_parameters == nullptr) { |
47 | LOG(WARNING) << "Could not find source parameters" ; |
48 | return nullptr; |
49 | } |
50 | |
51 | const WorldRotation& source_rotation = |
52 | source_parameters->object_transform.rotation; |
53 | const WorldRotation inverse_head_rotation = |
54 | system_settings_.GetHeadRotation().conjugate(); |
55 | const WorldRotation rotation = inverse_head_rotation * source_rotation; |
56 | const bool rotation_applied = |
57 | foa_rotator_.Process(target_rotation: rotation, input: *input_buffer, output: &output_buffer_); |
58 | |
59 | if (!rotation_applied) { |
60 | return input_buffer; |
61 | } |
62 | |
63 | // Copy buffer parameters. |
64 | return &output_buffer_; |
65 | } |
66 | |
67 | } // namespace vraudio |
68 | |