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