| 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_AMBISONIC_LOOKUP_TABLE_H_ |
| 18 | #define RESONANCE_AUDIO_AMBISONICS_AMBISONIC_LOOKUP_TABLE_H_ |
| 19 | |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "base/spherical_angle.h" |
| 23 | |
| 24 | namespace vraudio { |
| 25 | |
| 26 | // Represents a lookup table for encoding of Ambisonic periphonic sound fields. |
| 27 | // Supports arbitrary Ambisonic order and uses AmbiX convention (ACN channel |
| 28 | // sequencing, SN3D normalization). |
| 29 | class AmbisonicLookupTable { |
| 30 | public: |
| 31 | // Creates Ambisonic (AmbiX) encoder lookup table given the |
| 32 | // |max_ambisonic_order| used by the client. AmbiX convention uses ACN channel |
| 33 | // sequencing and SN3D normalization. |
| 34 | explicit AmbisonicLookupTable(int max_ambisonic_order); |
| 35 | |
| 36 | // Gets spherical harmonic encoding coefficients for a given order and |
| 37 | // writes them to |encoding_coeffs|. |
| 38 | // |
| 39 | // @param ambisonic_order Ambisonic order of the encoded sound source. |
| 40 | // @param source_direction Direction of a sound source in spherical |
| 41 | // coordinates. |
| 42 | // @param source_spread_deg Encoded sound source angular spread in degrees. |
| 43 | // @param encoding_coeffs Pointer to a vector of Ambisonic encoding |
| 44 | // coefficients. |
| 45 | void GetEncodingCoeffs(int ambisonic_order, |
| 46 | const SphericalAngle& source_direction, |
| 47 | float source_spread_deg, |
| 48 | std::vector<float>* encoding_coeffs) const; |
| 49 | |
| 50 | private: |
| 51 | // Computes a lookup table of encoding coefficients for one quadrant of the |
| 52 | // sphere. |
| 53 | void ComputeEncoderTable(); |
| 54 | |
| 55 | // Computes a table of spherical harmonics symmetry information for all |
| 56 | // cartesian axes. Value of 1 indicates that the current spherical harmonic is |
| 57 | // symmetric with respect to the current axis. Value of -1 indicates that the |
| 58 | // current spherical harmonic is anti-symmetric with respect to the current |
| 59 | // axis. |
| 60 | void ComputeSymmetriesTable(); |
| 61 | |
| 62 | // Returns the unnormalized spherical harmonic coefficient: |
| 63 | // Y_degree^order(azimuth, elevation). |
| 64 | // |
| 65 | // @param alp_value Associated Legendre polynomial for the given degree and |
| 66 | // order evaluated at sin elevation angle: P_degree^order(sin(elevation)). |
| 67 | // @param order Order of the Associated Legendre polynomial. |
| 68 | // @param azimuth_rad Azimuth angle in radians. |
| 69 | // @return Unnormalized spherical harmonic coefficient. |
| 70 | float UnnormalizedSphericalHarmonic(float alp_value, int order, |
| 71 | float azimuth_rad); |
| 72 | |
| 73 | // Ambisonic order. |
| 74 | const int max_ambisonic_order_; |
| 75 | |
| 76 | // Maximum number of coefficients to be stored in the lookup table equal to |
| 77 | // the number of Ambisonic channels for |max_ambisonic_order_| - 1. This is |
| 78 | // because we do not need to store the coefficient for the first spherical |
| 79 | // harmonic coefficient as it is always 1. |
| 80 | const size_t max_num_coeffs_in_table_; |
| 81 | |
| 82 | // Lookup table for storing Ambisonic encoding coefficients for one quadrant |
| 83 | // of the sphere. |
| 84 | std::vector<float> encoder_table_; |
| 85 | |
| 86 | // Lookup table for storing information about spherical harmonic symmetries. |
| 87 | std::vector<float> symmetries_table_; |
| 88 | }; |
| 89 | |
| 90 | } // namespace vraudio |
| 91 | |
| 92 | #endif // RESONANCE_AUDIO_AMBISONICS_AMBISONIC_LOOKUP_TABLE_H_ |
| 93 | |