AkFXDurationHandler.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef _AKFXDURATIONHANDLER_H_
  21. #define _AKFXDURATIONHANDLER_H_
  22. #include <AK/SoundEngine/Common/AkTypes.h>
  23. /// Duration handler service for source plug-in.
  24. /// Duration may change between different execution
  25. class AkFXDurationHandler
  26. {
  27. public:
  28. /// Setup duration handler
  29. inline void Setup(
  30. AkReal32 in_fDuration, ///< Duration (in secs)
  31. AkInt16 in_iLoopingCount, ///< Number of loop iterations (0 == infinite)
  32. AkUInt32 in_uSampleRate ///< Sample rate
  33. )
  34. {
  35. m_uSampleRate = in_uSampleRate;
  36. SetDuration( in_fDuration );
  37. SetLooping( in_iLoopingCount );
  38. Reset();
  39. }
  40. /// Reset looping and frame counters and start again.
  41. inline void Reset()
  42. {
  43. m_uFrameCount = 0;
  44. }
  45. /// Change number of loop iterations (0 == infinite).
  46. inline void SetLooping( AkInt16 in_iNumLoops )
  47. {
  48. m_iNumLoops = in_iNumLoops;
  49. }
  50. /// Set current duration per iteration (in secs).
  51. inline void SetDuration( AkReal32 in_fDuration )
  52. {
  53. m_uIterationFrame = (AkUInt32) (in_fDuration*m_uSampleRate);
  54. m_uIterationFrame = (m_uIterationFrame + 3) & ~3; // Align to next 4 frame boundary for SIMD alignment
  55. }
  56. /// Return current total duration (considering looping) in secs.
  57. inline AkReal32 GetDuration() const
  58. {
  59. // Note: Infinite looping will return a duration of 0 secs.
  60. return (AkReal32)(m_uIterationFrame*m_iNumLoops)/m_uSampleRate;
  61. }
  62. /// Set current number of frames to be produced (validFrames)
  63. /// and output state of audio buffer and advance internal state.
  64. inline void ProduceBuffer( AkAudioBuffer * io_pBuffer )
  65. {
  66. io_pBuffer->eState = ProduceBuffer( io_pBuffer->MaxFrames(), io_pBuffer->uValidFrames );
  67. }
  68. /// Set current number of frames to be produced (validFrames)
  69. /// and output state of audio buffer and advance internal state.
  70. inline AKRESULT ProduceBuffer( AkUInt16 in_uMaxFrames, AkUInt16 & out_uValidFrames )
  71. {
  72. // Infinite looping or not reached the end, always producing full capacity
  73. out_uValidFrames = in_uMaxFrames;
  74. AKRESULT eState = AK_DataReady;
  75. if ( m_iNumLoops != 0 )
  76. {
  77. // Finite looping, produce full buffer untill the end.
  78. const AkUInt32 uTotalFrames = m_iNumLoops*m_uIterationFrame;
  79. if ( m_uFrameCount < uTotalFrames )
  80. {
  81. const AkUInt32 uFramesRemaining = uTotalFrames-m_uFrameCount;
  82. if ( uFramesRemaining <= in_uMaxFrames )
  83. {
  84. out_uValidFrames = (AkUInt16)uFramesRemaining;
  85. eState = AK_NoMoreData;
  86. }
  87. }
  88. else
  89. {
  90. out_uValidFrames = 0;
  91. eState = AK_NoMoreData;
  92. }
  93. }
  94. m_uFrameCount += out_uValidFrames;
  95. return eState;
  96. }
  97. protected:
  98. AkUInt32 m_uIterationFrame; // Number of frames in a single loop iteration
  99. AkUInt32 m_uFrameCount; // Number of frames output in the current iteration
  100. AkUInt32 m_uSampleRate; // Sample rate used to convert time to samples
  101. AkInt16 m_iNumLoops; // Number of loop iterations (0 == infinite looping)
  102. };
  103. #endif // _AKFXDURATIONHANDLER_H_