| 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 | #ifndef RESONANCE_AUDIO_AMBISONICS_HOA_ROTATOR_H_ |
| 18 | #define RESONANCE_AUDIO_AMBISONICS_HOA_ROTATOR_H_ |
| 19 | |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "Eigen/Dense" |
| 23 | #include "base/audio_buffer.h" |
| 24 | #include "base/misc_math.h" |
| 25 | |
| 26 | namespace vraudio { |
| 27 | |
| 28 | // Rotator for higher order ambisonic sound fields. It supports ACN channel |
| 29 | // ordering and SN3D normalization (AmbiX). |
| 30 | class HoaRotator { |
| 31 | public: |
| 32 | // Constructs a sound field rotator of an arbitrary ambisonic order. |
| 33 | // |
| 34 | // @param ambisonic_order Order of ambisonic sound field. |
| 35 | explicit HoaRotator(int ambisonic_order); |
| 36 | |
| 37 | // Performs a smooth inplace rotation of a sound field buffer from |
| 38 | // |current_rotation_| to |target_rotation|. |
| 39 | // |
| 40 | // @param target_rotation Target rotation to be applied to the input buffer. |
| 41 | // @param input Higher order sound field input buffer to be rotated. |
| 42 | // @param output Pointer to output buffer. |
| 43 | // @return True if rotation has been applied. |
| 44 | bool Process(const WorldRotation& target_rotation, const AudioBuffer& input, |
| 45 | AudioBuffer* output); |
| 46 | |
| 47 | private: |
| 48 | // Updates the rotation matrix with using supplied WorldRotation. |
| 49 | // |
| 50 | // @param rotation World rotation. |
| 51 | void UpdateRotationMatrix(const WorldRotation& rotation); |
| 52 | |
| 53 | // Order of the ambisonic sound field handled by the rotator. |
| 54 | const int ambisonic_order_; |
| 55 | |
| 56 | // Current rotation which is used in the interpolation process in order to |
| 57 | // compute new rotation matrix. Initialized with an identity rotation. |
| 58 | WorldRotation current_rotation_; |
| 59 | |
| 60 | // Spherical harmonics rotation sub-matrices for each order. |
| 61 | std::vector<Eigen::MatrixXf> rotation_matrices_; |
| 62 | |
| 63 | // Final spherical harmonics rotation matrix. |
| 64 | Eigen::MatrixXf rotation_matrix_; |
| 65 | }; |
| 66 | |
| 67 | } // namespace vraudio |
| 68 | |
| 69 | #endif // RESONANCE_AUDIO_AMBISONICS_HOA_ROTATOR_H_ |
| 70 | |