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