AkDelayLineMemory.h 4.6 KB

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