AkValueRamp.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // AkValueRamp.h
  21. /// \file
  22. /// Linear interpolation services for plug-in parameters.
  23. #ifndef _AK_VALUERAMP_H_
  24. #define _AK_VALUERAMP_H_
  25. #include <AK/SoundEngine/Common/AkTypes.h>
  26. #include <AK/Tools/Common/AkAssert.h>
  27. #include <math.h>
  28. namespace AK
  29. {
  30. /// Platform-independent parameter interpolation service for software plug-ins.
  31. /// \aknote
  32. /// Algorithm performs linear interpolation.
  33. /// \endaknote
  34. /// \sa
  35. /// - \ref shared_parameter_interface
  36. class CAkValueRamp
  37. {
  38. public:
  39. /// Constructor method.
  40. CAkValueRamp() :
  41. m_fStepIncrement( 0.f ), // Step increment sign
  42. m_fInc( 0.f ), // Signed increment
  43. m_fTarget( 0.f ), // Target gain for ramping
  44. m_fCurrent( 0.f ), // Current interpolated value
  45. m_uRampCount( 0 ), // Position in interpolation ramp
  46. m_uRampLength( 0 ) // Total duration of interpolation ramp
  47. {
  48. }
  49. /// Destructor method.
  50. ~CAkValueRamp()
  51. {
  52. }
  53. /// Initial parameter interpolation ramp setup.
  54. inline void RampSetup(
  55. AkReal32 fStepIncrement, ///< Increment to add to the parameter at every Tick() call
  56. AkReal32 fInitVal ///< Initial ramp value
  57. )
  58. {
  59. AKASSERT( fStepIncrement > 0.f );
  60. m_fStepIncrement = fStepIncrement;
  61. m_fCurrent = fInitVal;
  62. SetTarget( fInitVal );
  63. }
  64. /// Set the ramp's target value.
  65. AkForceInline void SetTarget(
  66. AkReal32 fTarget ///< Target ramp value
  67. )
  68. {
  69. m_fTarget = fTarget;
  70. m_uRampCount = 0;
  71. AkReal32 fDiff = m_fTarget - m_fCurrent;
  72. m_uRampLength = static_cast<AkUInt32>( fabs(fDiff) / m_fStepIncrement );
  73. m_fInc = fDiff > 0 ? m_fStepIncrement : -m_fStepIncrement;
  74. }
  75. /// Process a single interpolation frame.
  76. /// \return The current interpolated value
  77. AkForceInline AkReal32 Tick()
  78. {
  79. if ( m_uRampCount >= m_uRampLength )
  80. m_fCurrent = m_fTarget;
  81. else
  82. {
  83. ++m_uRampCount;
  84. m_fCurrent += m_fInc;
  85. }
  86. return m_fCurrent;
  87. }
  88. /// Retrieve the current interpolated value.
  89. /// \return The current interpolated value
  90. AkReal32 GetCurrent() { return m_fCurrent; }
  91. /// Set the current interpolated value.
  92. void SetCurrent(AkReal32 in_fCurrent) { m_fCurrent = in_fCurrent; }
  93. /// Retrieve the current interpolation frame count.
  94. /// \return The current interpolation frame count
  95. AkUInt32 GetRampCount() { return m_uRampCount; }
  96. /// Set the current interpolation frame count.
  97. void SetRampCount(AkUInt32 in_uRampCount) { m_uRampCount = in_uRampCount; }
  98. /// The ramp is no longer necessary; set to target
  99. void StopRamp()
  100. {
  101. m_fCurrent = m_fTarget;
  102. m_uRampCount = m_uRampLength;
  103. }
  104. private:
  105. AkReal32 m_fStepIncrement; // Step increment size
  106. AkReal32 m_fInc; // Signed increment
  107. AkReal32 m_fTarget; // Target for interpolation ramp
  108. AkReal32 m_fCurrent; // Current interpolated value
  109. AkUInt32 m_uRampCount; // Position in interpolation ramp
  110. AkUInt32 m_uRampLength; // Total duration of interpolation ramp
  111. };
  112. }
  113. #endif //_AK_VALUERAMP_H_