AkSpeakerVolumes.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. // AkSpeakerVolumes.h
  21. /// \file
  22. /// Multi-channel volume definitions and services.
  23. /// Always associated with an AkChannelConfig. In the case of standard configurations, the volume items ordering
  24. /// match the bit ordering in the channel mask, except for the LFE which is skipped and placed at the end of the
  25. /// volume array.
  26. /// Refer to \ref goingfurther_speakermatrixcallback for an example of how to manipulate speaker volume vectors/matrices.
  27. #ifndef _AK_SPEAKER_VOLUMES_H_
  28. #define _AK_SPEAKER_VOLUMES_H_
  29. #include <AK/SoundEngine/Common/AkTypes.h>
  30. #include <AK/SoundEngine/Platforms/Generic/AkSpeakerVolumes.h>
  31. #include <AK/Tools/Common/AkPlatformFuncs.h>
  32. namespace AK
  33. {
  34. /// Multi-channel volume definitions and services.
  35. namespace SpeakerVolumes
  36. {
  37. typedef AkReal32 * VectorPtr; ///< Volume vector. Access each element with the standard bracket [] operator.
  38. typedef AkReal32 * MatrixPtr; ///< Volume matrix. Access each input channel vector with AK::SpeakerVolumes::Matrix::GetChannel().
  39. typedef const AkReal32 * ConstVectorPtr; ///< Constant volume vector. Access each element with the standard bracket [] operator.
  40. typedef const AkReal32 * ConstMatrixPtr; ///< Constant volume matrix. Access each input channel vector with AK::SpeakerVolumes::Matrix::GetChannel().
  41. /// Volume vector services.
  42. namespace Vector
  43. {
  44. /// Copy volumes.
  45. AkForceInline void Copy( VectorPtr in_pVolumesDst, ConstVectorPtr in_pVolumesSrc, AkUInt32 in_uNumChannels )
  46. {
  47. AKASSERT( ( in_pVolumesDst && in_pVolumesSrc ) || in_uNumChannels == 0 );
  48. if ( in_uNumChannels )
  49. memcpy( in_pVolumesDst, in_pVolumesSrc, in_uNumChannels * sizeof( AkReal32 ) );
  50. }
  51. /// Copy volumes with gain.
  52. AkForceInline void Copy( VectorPtr in_pVolumesDst, ConstVectorPtr in_pVolumesSrc, AkUInt32 in_uNumChannels, AkReal32 in_fGain )
  53. {
  54. AKASSERT( ( in_pVolumesDst && in_pVolumesSrc ) || in_uNumChannels == 0 );
  55. for ( AkUInt32 uChan = 0; uChan < in_uNumChannels; uChan++ )
  56. {
  57. in_pVolumesDst[uChan] = in_pVolumesSrc[uChan] * in_fGain;
  58. }
  59. }
  60. /// Clear volumes.
  61. AkForceInline void Zero( VectorPtr in_pVolumes, AkUInt32 in_uNumChannels )
  62. {
  63. AKASSERT( in_pVolumes || in_uNumChannels == 0 );
  64. if ( in_uNumChannels )
  65. memset( in_pVolumes, 0, in_uNumChannels * sizeof( AkReal32 ) );
  66. }
  67. /// Accumulate two volume vectors.
  68. AkForceInline void Add( VectorPtr in_pVolumesDst, ConstVectorPtr in_pVolumesSrc, AkUInt32 in_uNumChannels )
  69. {
  70. AKASSERT( ( in_pVolumesDst && in_pVolumesSrc ) || in_uNumChannels == 0 );
  71. for ( AkUInt32 uChan = 0; uChan < in_uNumChannels; uChan++ )
  72. {
  73. in_pVolumesDst[uChan] += in_pVolumesSrc[uChan];
  74. }
  75. }
  76. /// Compute the sum of all components of a volume vector.
  77. AkForceInline AkReal32 L1Norm(ConstVectorPtr io_pVolumes, AkUInt32 in_uNumChannels)
  78. {
  79. AkReal32 total = 0;
  80. AKASSERT((io_pVolumes) || in_uNumChannels == 0);
  81. for (AkUInt32 uChan = 0; uChan < in_uNumChannels; uChan++)
  82. {
  83. total += io_pVolumes[uChan];
  84. }
  85. return total;
  86. }
  87. /// Multiply volume vector with a scalar.
  88. AkForceInline void Mul( VectorPtr in_pVolumesDst, const AkReal32 in_fVol, AkUInt32 in_uNumChannels )
  89. {
  90. AKASSERT( in_pVolumesDst || in_uNumChannels == 0 );
  91. for ( AkUInt32 uChan = 0; uChan < in_uNumChannels; uChan++ )
  92. {
  93. in_pVolumesDst[uChan] *= in_fVol;
  94. }
  95. }
  96. /// Multiply two volume vectors.
  97. AkForceInline void Mul( VectorPtr in_pVolumesDst, ConstVectorPtr in_pVolumesSrc, AkUInt32 in_uNumChannels )
  98. {
  99. AKASSERT( ( in_pVolumesDst && in_pVolumesSrc ) || in_uNumChannels == 0 );
  100. for ( AkUInt32 uChan = 0; uChan < in_uNumChannels; uChan++ )
  101. {
  102. in_pVolumesDst[uChan] *= in_pVolumesSrc[uChan];
  103. }
  104. }
  105. /// Get max for all elements of two volume vectors, independently.
  106. AkForceInline void Max( AkReal32 * in_pVolumesDst, const AkReal32 * in_pVolumesSrc, AkUInt32 in_uNumChannels )
  107. {
  108. AKASSERT( ( in_pVolumesDst && in_pVolumesSrc ) || in_uNumChannels == 0 );
  109. for ( AkUInt32 uChan = 0; uChan < in_uNumChannels; uChan++ )
  110. {
  111. in_pVolumesDst[uChan] = AkMax( in_pVolumesDst[uChan], in_pVolumesSrc[uChan] );
  112. }
  113. }
  114. /// Get min for all elements of two volume vectors, independently.
  115. AkForceInline void Min( AkReal32 * in_pVolumesDst, const AkReal32 * in_pVolumesSrc, AkUInt32 in_uNumChannels )
  116. {
  117. AKASSERT( ( in_pVolumesDst && in_pVolumesSrc ) || in_uNumChannels == 0 );
  118. for ( AkUInt32 uChan = 0; uChan < in_uNumChannels; uChan++ )
  119. {
  120. in_pVolumesDst[uChan] = AkMin( in_pVolumesDst[uChan], in_pVolumesSrc[uChan] );
  121. }
  122. }
  123. }
  124. /// Volume matrix (multi-in/multi-out channel configurations) services.
  125. namespace Matrix
  126. {
  127. /// Compute size (in bytes) required for given channel configurations.
  128. AkForceInline AkUInt32 GetRequiredSize( AkUInt32 in_uNumChannelsIn, AkUInt32 in_uNumChannelsOut )
  129. {
  130. return in_uNumChannelsIn * Vector::GetRequiredSize( in_uNumChannelsOut );
  131. }
  132. /// Compute size (in number of elements) required for given channel configurations.
  133. AkForceInline AkUInt32 GetNumElements( AkUInt32 in_uNumChannelsIn, AkUInt32 in_uNumChannelsOut )
  134. {
  135. return in_uNumChannelsIn * Vector::GetNumElements( in_uNumChannelsOut );
  136. }
  137. /// Get pointer to volume distribution for input channel in_uIdxChannelIn.
  138. AkForceInline VectorPtr GetChannel( MatrixPtr in_pVolumeMx, AkUInt32 in_uIdxChannelIn, AkUInt32 in_uNumChannelsOut )
  139. {
  140. AKASSERT( in_pVolumeMx );
  141. return in_pVolumeMx + in_uIdxChannelIn * Vector::GetNumElements( in_uNumChannelsOut );
  142. }
  143. /// Get pointer to volume distribution for input channel in_uIdxChannelIn.
  144. AkForceInline ConstVectorPtr GetChannel( ConstMatrixPtr in_pVolumeMx, AkUInt32 in_uIdxChannelIn, AkUInt32 in_uNumChannelsOut )
  145. {
  146. AKASSERT( in_pVolumeMx );
  147. return in_pVolumeMx + in_uIdxChannelIn * Vector::GetNumElements( in_uNumChannelsOut );
  148. }
  149. /// Copy matrix.
  150. AkForceInline void Copy( MatrixPtr in_pVolumesDst, ConstMatrixPtr in_pVolumesSrc, AkUInt32 in_uNumChannelsIn, AkUInt32 in_uNumChannelsOut )
  151. {
  152. AkUInt32 uNumElements = Matrix::GetNumElements( in_uNumChannelsIn, in_uNumChannelsOut );
  153. AKASSERT( ( in_pVolumesDst && in_pVolumesSrc ) || uNumElements == 0 );
  154. if ( uNumElements )
  155. memcpy( in_pVolumesDst, in_pVolumesSrc, uNumElements * sizeof( AkReal32 ) );
  156. }
  157. /// Copy matrix with gain.
  158. AkForceInline void Copy( MatrixPtr in_pVolumesDst, ConstMatrixPtr in_pVolumesSrc, AkUInt32 in_uNumChannelsIn, AkUInt32 in_uNumChannelsOut, AkReal32 in_fGain )
  159. {
  160. AkUInt32 uNumElements = Matrix::GetNumElements( in_uNumChannelsIn, in_uNumChannelsOut );
  161. AKASSERT( ( in_pVolumesDst && in_pVolumesSrc ) || uNumElements == 0 );
  162. for ( AkUInt32 uChan = 0; uChan < uNumElements; uChan++ )
  163. {
  164. in_pVolumesDst[uChan] = in_pVolumesSrc[uChan] * in_fGain;
  165. }
  166. }
  167. /// Clear matrix.
  168. AkForceInline void Zero( MatrixPtr in_pVolumes, AkUInt32 in_uNumChannelsIn, AkUInt32 in_uNumChannelsOut )
  169. {
  170. AkUInt32 uNumElements = Matrix::GetNumElements( in_uNumChannelsIn, in_uNumChannelsOut );
  171. AKASSERT( in_pVolumes || uNumElements == 0 );
  172. if ( uNumElements )
  173. memset( in_pVolumes, 0, uNumElements * sizeof( AkReal32 ) );
  174. }
  175. /// Multiply a matrix with a scalar.
  176. AkForceInline void Mul( MatrixPtr in_pVolumesDst, const AkReal32 in_fVol, AkUInt32 in_uNumChannelsIn, AkUInt32 in_uNumChannelsOut )
  177. {
  178. AkUInt32 uNumElements = Matrix::GetNumElements( in_uNumChannelsIn, in_uNumChannelsOut );
  179. AKASSERT( in_pVolumesDst || uNumElements == 0 );
  180. for ( AkUInt32 uChan = 0; uChan < uNumElements; uChan++ )
  181. {
  182. in_pVolumesDst[uChan] *= in_fVol;
  183. }
  184. }
  185. /// Add all elements of two volume matrices, independently.
  186. AkForceInline void Add(MatrixPtr in_pVolumesDst, ConstMatrixPtr in_pVolumesSrc, AkUInt32 in_uNumChannelsIn, AkUInt32 in_uNumChannelsOut)
  187. {
  188. AkUInt32 uNumElements = Matrix::GetNumElements(in_uNumChannelsIn, in_uNumChannelsOut);
  189. AKASSERT((in_pVolumesDst && in_pVolumesSrc) || uNumElements == 0);
  190. for (AkUInt32 uChan = 0; uChan < uNumElements; uChan++)
  191. {
  192. in_pVolumesDst[uChan] += in_pVolumesSrc[uChan];
  193. }
  194. }
  195. /// Pointwise Multiply-Add of all elements of two volume matrices.
  196. AkForceInline void MAdd(MatrixPtr in_pVolumesDst, ConstMatrixPtr in_pVolumesSrc, AkUInt32 in_uNumChannelsIn, AkUInt32 in_uNumChannelsOut, AkReal32 in_fGain)
  197. {
  198. AkUInt32 uNumElements = Matrix::GetNumElements(in_uNumChannelsIn, in_uNumChannelsOut);
  199. AKASSERT((in_pVolumesDst && in_pVolumesSrc) || uNumElements == 0);
  200. for (AkUInt32 uChan = 0; uChan < uNumElements; uChan++)
  201. {
  202. in_pVolumesDst[uChan] += in_pVolumesSrc[uChan] * in_fGain;
  203. }
  204. }
  205. /// Get absolute max for all elements of two volume matrices, independently.
  206. AkForceInline void AbsMax(MatrixPtr in_pVolumesDst, ConstMatrixPtr in_pVolumesSrc, AkUInt32 in_uNumChannelsIn, AkUInt32 in_uNumChannelsOut)
  207. {
  208. AkUInt32 uNumElements = Matrix::GetNumElements( in_uNumChannelsIn, in_uNumChannelsOut );
  209. AKASSERT( ( in_pVolumesDst && in_pVolumesSrc ) || uNumElements == 0 );
  210. for ( AkUInt32 uChan = 0; uChan < uNumElements; uChan++ )
  211. {
  212. in_pVolumesDst[uChan] = ((in_pVolumesDst[uChan] * in_pVolumesDst[uChan]) > (in_pVolumesSrc[uChan] * in_pVolumesSrc[uChan])) ? in_pVolumesDst[uChan] : in_pVolumesSrc[uChan];
  213. }
  214. }
  215. /// Get max for all elements of two volume matrices, independently.
  216. AkForceInline void Max(MatrixPtr in_pVolumesDst, ConstMatrixPtr in_pVolumesSrc, AkUInt32 in_uNumChannelsIn, AkUInt32 in_uNumChannelsOut)
  217. {
  218. AkUInt32 uNumElements = Matrix::GetNumElements(in_uNumChannelsIn, in_uNumChannelsOut);
  219. AKASSERT((in_pVolumesDst && in_pVolumesSrc) || uNumElements == 0);
  220. for (AkUInt32 uChan = 0; uChan < uNumElements; uChan++)
  221. {
  222. in_pVolumesDst[uChan] = (in_pVolumesDst[uChan] > in_pVolumesSrc[uChan]) ? in_pVolumesDst[uChan] : in_pVolumesSrc[uChan];
  223. }
  224. }
  225. }
  226. }
  227. }
  228. #endif //_AK_SPEAKER_VOLUMES_H_