AkFXTailHandler.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 _AKFXTAILHANDLER_H_
  21. #define _AKFXTAILHANDLER_H_
  22. #include <AK/SoundEngine/Common/AkTypes.h>
  23. #include <AK/SoundEngine/Common/AkCommonDefs.h>
  24. #include <AK/Tools/Common/AkPlatformFuncs.h>
  25. /// Default value when effect has not enterred tail mode yet.
  26. #define AKFXTAILHANDLER_NOTINTAIL 0xFFFFFFFF
  27. /// Effect tail handling utility class.
  28. /// Handles varying number of tail frames from frame to frame (i.e. based on RTPC parameters).
  29. /// Handles effect revived (quit tail) and reenters etc.
  30. class AkFXTailHandler
  31. {
  32. public:
  33. /// Constructor
  34. inline AkFXTailHandler()
  35. : uTailFramesRemaining( AKFXTAILHANDLER_NOTINTAIL )
  36. , uTotalTailFrames(0) {}
  37. /// Handle FX tail and zero pads AkAudioBuffer if necessary
  38. inline void HandleTail(
  39. AkAudioBuffer * io_pBuffer,
  40. AkUInt32 in_uTotalTailFrames )
  41. {
  42. bool bPreStop = io_pBuffer->eState == AK_NoMoreData;
  43. if ( bPreStop )
  44. {
  45. // Tail not yet finished processing
  46. if ( uTailFramesRemaining > 0
  47. || io_pBuffer->uValidFrames > 0 // <-- there are valid frames, so last (maybe partially filled) buffer.
  48. )
  49. {
  50. // Not previously in tail, compute tail time
  51. if (uTailFramesRemaining == AKFXTAILHANDLER_NOTINTAIL
  52. || io_pBuffer->uValidFrames > 0 // <- ANY valid frames in the buffer should reset the tail.
  53. )
  54. {
  55. uTailFramesRemaining = in_uTotalTailFrames;
  56. uTotalTailFrames = in_uTotalTailFrames;
  57. }
  58. // Tail time changed, augment if necessary but preserve where we are so that effect will
  59. // still finish when constantly changing this based on RTPC parameters
  60. else if ( in_uTotalTailFrames > uTotalTailFrames )
  61. {
  62. AkUInt32 uFramesElapsed = uTotalTailFrames - uTailFramesRemaining;
  63. uTailFramesRemaining = in_uTotalTailFrames - uFramesElapsed;
  64. uTotalTailFrames = in_uTotalTailFrames;
  65. }
  66. // Always full buffers while in tail
  67. AkUInt32 uNumTailFrames = (AkUInt32)(io_pBuffer->MaxFrames()-io_pBuffer->uValidFrames);
  68. uTailFramesRemaining -= AkMin( uTailFramesRemaining, uNumTailFrames );
  69. io_pBuffer->ZeroPadToMaxFrames();
  70. if ( uTailFramesRemaining > 0 )
  71. io_pBuffer->eState = AK_DataReady;
  72. }
  73. }
  74. else
  75. {
  76. // Reset tail mode for next time if exits tail mode (on bus only)
  77. uTailFramesRemaining = AKFXTAILHANDLER_NOTINTAIL;
  78. }
  79. }
  80. inline bool HasTailRemaining() { return uTailFramesRemaining > 0; } // Also true when AKFXTAILHANDLER_NOTINTAIL
  81. protected:
  82. AkUInt32 uTailFramesRemaining; // AKFXTAILHANDLER_NOTINTAIL, otherwise value represents number of frames remaining in tail
  83. AkUInt32 uTotalTailFrames;
  84. };
  85. #endif // _AKFXTAILHANDLER_H_