AkDelayLineMemory.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // Length of delay line is mapped on 4 frames boundary (i.e. may not be suited for reverberation for example)
  21. // This is not a delay line implementation, but rather just some services for memory managment related
  22. // to specific delay line execution needs as detailed by clients
  23. #ifndef _AKDSP_DELAYLINEMEMORY_
  24. #define _AKDSP_DELAYLINEMEMORY_
  25. #include <AK/SoundEngine/Common/AkTypes.h>
  26. #include <AK/SoundEngine/Common/IAkPluginMemAlloc.h>
  27. #include <AK/Tools/Common/AkPlatformFuncs.h>
  28. namespace AK
  29. {
  30. namespace DSP
  31. {
  32. template < class T_SAMPLETYPE, AkUInt32 T_MAXNUMCHANNELS >
  33. class CAkDelayLineMemory
  34. {
  35. public:
  36. CAkDelayLineMemory( )
  37. : m_uDelayLineLength( 0 )
  38. , m_uOffset( 0 )
  39. , m_uNumChannels( 0 )
  40. {
  41. AkZeroMemSmall( m_pDelay, T_MAXNUMCHANNELS*sizeof(T_SAMPLETYPE *) );
  42. }
  43. AKRESULT Init(
  44. AK::IAkPluginMemAlloc * in_pAllocator,
  45. AkUInt32 in_uDelayLineLength,
  46. AkUInt32 in_uNumChannels )
  47. {
  48. m_uNumChannels = in_uNumChannels;
  49. // Align delay length on 4 frame boundary to simplify DMA and SIMD alignement
  50. m_uDelayLineLength = AK_ALIGN_TO_NEXT_BOUNDARY( in_uDelayLineLength, 4 );
  51. m_uOffset = 0;
  52. if ( m_uDelayLineLength )
  53. {
  54. for ( AkUInt32 i = 0; i < m_uNumChannels; i++ )
  55. {
  56. m_pDelay[i] = (T_SAMPLETYPE*)AK_PLUGIN_ALLOC( in_pAllocator, sizeof(T_SAMPLETYPE) * m_uDelayLineLength );
  57. if ( m_pDelay[i] == NULL )
  58. return AK_InsufficientMemory;
  59. }
  60. }
  61. return AK_Success;
  62. }
  63. void Term( AK::IAkPluginMemAlloc * in_pAllocator )
  64. {
  65. for ( AkUInt32 i = 0; i < m_uNumChannels; i++ )
  66. {
  67. if ( m_pDelay[i] )
  68. {
  69. AK_PLUGIN_FREE( in_pAllocator, m_pDelay[i] );
  70. m_pDelay[i] = NULL;
  71. }
  72. }
  73. m_uDelayLineLength = 0;
  74. }
  75. void Reset( )
  76. {
  77. if ( m_uDelayLineLength )
  78. {
  79. for ( AkUInt32 i = 0; i < m_uNumChannels; i++ )
  80. {
  81. if (m_pDelay[i])
  82. AkZeroMemLarge( (void*) m_pDelay[i], m_uDelayLineLength*sizeof(T_SAMPLETYPE) );
  83. }
  84. }
  85. m_uOffset = 0;
  86. }
  87. AkForceInline AkUInt32 GetCurrentOffset()
  88. {
  89. return m_uOffset;
  90. }
  91. AkForceInline void SetCurrentOffset( AkUInt32 in_uOffset )
  92. {
  93. m_uOffset = in_uOffset;
  94. }
  95. AkForceInline AkUInt32 GetDelayLength()
  96. {
  97. return m_uDelayLineLength;
  98. }
  99. T_SAMPLETYPE * GetCurrentPointer( AkUInt32 in_uOffset, AkUInt32 in_uChannelIndex )
  100. {
  101. return m_pDelay[in_uChannelIndex] + in_uOffset;
  102. }
  103. public:
  104. T_SAMPLETYPE * m_pDelay[T_MAXNUMCHANNELS]; // Delay lines for each channel
  105. AkUInt32 m_uDelayLineLength; // Total delay line length
  106. AkUInt32 m_uOffset; // Current delay line write position
  107. AkUInt32 m_uNumChannels; // Number of delayed channels
  108. };
  109. } // namespace DSP
  110. } // namespace AK
  111. #endif // _AKDSP_DELAYLINEMEMORY_