AkReverbEstimation.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. /// \file
  21. /// Reverb parameter estimation.
  22. #pragma once
  23. #include <AK/SoundEngine/Common/AkTypes.h>
  24. #include <AK/SpatialAudio/Common/AkSpatialAudio.h>
  25. #include <AK/SoundEngine/Common/AkVirtualAcoustics.h>
  26. namespace AK {
  27. namespace SpatialAudio {
  28. /// Audiokinetic reverb estimation namespace
  29. namespace ReverbEstimation
  30. {
  31. ////////////////////////////////////////////////////////////////////////
  32. /// @name Reverb estimation.
  33. /// These functions can be used to estimate the reverb parameters of a physical environment, using its volume and surface area
  34. //@{
  35. /// This is used to estimate the line of best fit through the absorption values of an Acoustic Texture.
  36. /// This value is what's known as the HFDamping.
  37. /// return Gradient of line of best fit through y = mx + c.
  38. float CalculateSlope(const AkAcousticTexture& texture)
  39. {
  40. // constants for the mean and standard deviation of the acoustic texture indices 0,1,2,3
  41. const float meanX = 1.5f;
  42. const float sdX = 1.11803399;
  43. const int N = 4;
  44. float meanY = (texture.fAbsorptionLow + texture.fAbsorptionMidLow + texture.fAbsorptionMidHigh + texture.fAbsorptionHigh) / (float)N;
  45. float absorptions[4] = { texture.fAbsorptionLow, texture.fAbsorptionMidLow, texture.fAbsorptionMidHigh, texture.fAbsorptionHigh };
  46. float sdY = 0.0f;
  47. float meanDiffDotProd = 0.0f;
  48. for (int i = 0; i < N; ++i)
  49. {
  50. const float yMeanDiff = absorptions[i] - meanY;
  51. const float xMeanDiff = (float)i - meanX;
  52. meanDiffDotProd += yMeanDiff * xMeanDiff;
  53. sdY += yMeanDiff * yMeanDiff;
  54. }
  55. if (sdY == 0.0f)
  56. return 0.0f;
  57. sdY = sqrtf(sdY / (float)N);
  58. const float correlationCoeff = (1.0f / (N - 1.0f)) * (meanDiffDotProd / (sdY * sdX));
  59. return correlationCoeff * (sdY / sdX);
  60. }
  61. /// Calculate average absorption values using each of the textures in in_textures, weighted by their corresponding surface area.
  62. void GetAverageAbsorptionValues(AkAcousticTexture* in_textures, float* in_surfaceAreas, int in_numTextures, AkAcousticTexture& out_average)
  63. {
  64. float surfaceArea = 0.0f;
  65. float totalSurfaceArea = 0.0f;
  66. out_average.fAbsorptionLow = 0.0f;
  67. out_average.fAbsorptionMidLow = 0.0f;
  68. out_average.fAbsorptionMidHigh = 0.0f;
  69. out_average.fAbsorptionHigh = 0.0f;
  70. for (int textureIndex = 0; textureIndex < in_numTextures; ++textureIndex)
  71. {
  72. AkAcousticTexture& texture = in_textures[textureIndex];
  73. surfaceArea = in_surfaceAreas[textureIndex];
  74. out_average.fAbsorptionLow += texture.fAbsorptionLow * surfaceArea;
  75. out_average.fAbsorptionMidLow += texture.fAbsorptionMidLow * surfaceArea;
  76. out_average.fAbsorptionMidHigh += texture.fAbsorptionMidHigh * surfaceArea;
  77. out_average.fAbsorptionHigh += texture.fAbsorptionHigh * surfaceArea;
  78. totalSurfaceArea += surfaceArea;
  79. }
  80. if (totalSurfaceArea > 0.0f)
  81. {
  82. out_average.fAbsorptionLow = out_average.fAbsorptionLow / totalSurfaceArea;
  83. out_average.fAbsorptionMidLow = out_average.fAbsorptionMidLow / totalSurfaceArea;
  84. out_average.fAbsorptionMidHigh = out_average.fAbsorptionMidHigh / totalSurfaceArea;
  85. out_average.fAbsorptionHigh = out_average.fAbsorptionHigh / totalSurfaceArea;
  86. }
  87. }
  88. /// Estimate the time taken (in seconds) for the sound reverberation in a physical environment to decay by 60 dB.
  89. /// This is estimated using the Sabine equation. The T60 decay time can be used to drive the decay parameter of a reverb effect.
  90. AK_EXTERNAPIFUNC(AKRESULT, EstimateT60Decay)(
  91. AkReal32 in_volumeCubicMeters, ///< The volume (in cubic meters) of the physical environment. 0 volume or negative volume will give a decay estimate of 0.
  92. AkReal32 in_surfaceAreaSquaredMeters, ///< The surface area (in squared meters) of the physical environment. Must be >= AK_SA_MIN_ENVIRONMENT_SURFACE_AREA
  93. AkReal32 in_environmentAverageAbsorption, ///< The average absorption amount of the surfaces in the environment. Must be between AK_SA_MIN_ENVIRONMENT_ABSORPTION and 1.
  94. AkReal32& out_decayEstimate ///< Returns the time taken (in seconds) for the reverberation to decay bu 60 dB.
  95. )
  96. {
  97. if (in_volumeCubicMeters <= 0.0f)
  98. {
  99. out_decayEstimate = 0.0f;
  100. return AKRESULT::AK_Success;
  101. }
  102. if (in_surfaceAreaSquaredMeters < AK_SA_MIN_ENVIRONMENT_SURFACE_AREA)
  103. {
  104. AKASSERT(false && "AK::SpatialAudio::ReverbEstimation::EstimateT60Decay: Invalid surface area. in_SurfaceAreaSquaredMeters Must be >= AK_SA_MIN_ENVIRONMENT_SURFACE_AREA");
  105. return AKRESULT::AK_Fail;
  106. }
  107. if (in_environmentAverageAbsorption < AK_SA_MIN_ENVIRONMENT_ABSORPTION || in_environmentAverageAbsorption > 1.0f)
  108. {
  109. AKASSERT(false && "AK::SpatialAudio::ReverbEstimation::EstimateT60Decay: Invalid absorption value. in_EnvironmentAverageAbsorption Must be between AK_SA_MIN_ENVIRONMENT_ABSORPTION and 1");
  110. return AKRESULT::AK_Fail;
  111. }
  112. // The Sabine equation.
  113. out_decayEstimate = (0.161f * in_volumeCubicMeters) / (in_surfaceAreaSquaredMeters * in_environmentAverageAbsorption);
  114. return AKRESULT::AK_Success;
  115. }
  116. /// Estimate the time taken (in milliseconds) for the first reflection to reach the listener.
  117. /// This assumes the emitter and listener are both positioned in the centre of the environment.
  118. AK_EXTERNAPIFUNC(AKRESULT, EstimateTimeToFirstReflection)(
  119. AkVector in_environmentExtentMeters, ///< Defines the dimensions of the environment (in meters) relative to the center; all components must be positive numbers.
  120. AkReal32& out_timeToFirstReflectionMs, ///< Returns the time taken (in milliseconds) for the first reflection to reach the listener.
  121. AkReal32 in_speedOfSound = 343.0f ///< Defaults to 343.0 - the speed of sound in dry air. Must be > 0.
  122. )
  123. {
  124. if (in_speedOfSound <= 0.0f)
  125. {
  126. AKASSERT(false && "AK::SpatialAudio::ReverbEstimation::EstimateTimeToFirstReflection: Invalid speed of sound. in_speedOfSound must be greater than 0.");
  127. return AKRESULT::AK_Fail;
  128. }
  129. if (in_environmentExtentMeters.X < 0.0f || in_environmentExtentMeters.Y < 0.0f || in_environmentExtentMeters.Z < 0.0f)
  130. {
  131. AKASSERT(false && "AK::SpatialAudio::ReverbEstimation::EstimateTimeToFirstReflection: Invalid extent. All components must be positive numbers.");
  132. return AKRESULT::AK_Fail;
  133. }
  134. const float minDimension = AkMin(AkMin(in_environmentExtentMeters.X, in_environmentExtentMeters.Y), in_environmentExtentMeters.Z);
  135. out_timeToFirstReflectionMs = (minDimension / in_speedOfSound) * 1000.0f;
  136. return AKRESULT::AK_Success;
  137. }
  138. /// Estimate the high frequency damping from a collection of AkAcousticTextures and corresponding surface areas.
  139. /// The high frequency damping is a measure of how much high frequencies are dampened compared to low frequencies.
  140. /// The value is comprised between -1 and 1. A value > 0 indicates more high frequency damping than low frequency damping. < 0 indicates more low frequency damping than high frequency damping. 0 indicates uniform damping.
  141. /// The average absorption values are calculated using each of the textures in the collection, weighted by their corresponding surface area.
  142. /// The HFDamping is then calculated as the line-of-best-fit through the average absorption values.
  143. AK_EXTERNAPIFUNC(AkReal32, EstimateHFDamping)(
  144. AkAcousticTexture* in_textures, ///< A collection of AkAcousticTexture structs from which to calculate the average high frequency damping.
  145. float* in_surfaceAreas, ///< Surface area values for each of the textures in in_textures.
  146. int in_numTextures ///< The number of textures in in_textures (and the number of surface area values in in_surfaceAreas).
  147. )
  148. {
  149. if (in_textures == nullptr || in_surfaceAreas == nullptr || in_numTextures == 0)
  150. {
  151. return 0.0f;
  152. }
  153. AkAcousticTexture averageAbsorptionValues;
  154. GetAverageAbsorptionValues(in_textures, in_surfaceAreas, in_numTextures, averageAbsorptionValues);
  155. return CalculateSlope(averageAbsorptionValues) * 2.f; // Multiply by 2 so that the Hf Damping value is between -1 and 1.
  156. }
  157. //@}
  158. }
  159. }
  160. }