| 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 "dsp/stereo_panner.h" |
| 18 | |
| 19 | #include <cmath> |
| 20 | |
| 21 | #include "base/constants_and_types.h" |
| 22 | |
| 23 | namespace vraudio { |
| 24 | |
| 25 | const float kStereoLeftRadians = kRadiansFromDegrees * kStereoLeftDegrees; |
| 26 | const float kStereoRightRadians = kRadiansFromDegrees * kStereoRightDegrees; |
| 27 | |
| 28 | void CalculateStereoPanGains(const SphericalAngle& source_direction, |
| 29 | std::vector<float>* stereo_gains) { |
| 30 | // Note this applies the same panning law as was applied by the ambisonic |
| 31 | // equivalent panner to ensure consistency. |
| 32 | DCHECK(stereo_gains); |
| 33 | stereo_gains->resize(new_size: kNumStereoChannels); |
| 34 | |
| 35 | const float cos_direction_elevation = std::cos(x: source_direction.elevation()); |
| 36 | |
| 37 | (*stereo_gains)[0] = |
| 38 | 0.5f * (1.0f + std::cos(x: kStereoLeftRadians - source_direction.azimuth()) * |
| 39 | cos_direction_elevation); |
| 40 | (*stereo_gains)[1] = |
| 41 | 0.5f * |
| 42 | (1.0f + std::cos(x: kStereoRightRadians - source_direction.azimuth()) * |
| 43 | cos_direction_elevation); |
| 44 | } |
| 45 | |
| 46 | } // namespace vraudio |
| 47 |
