You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.1KB

  1. #ifndef STK_CUBIC_H
  2. #define STK_CUBIC_H
  3. #include "Function.h"
  4. #include <cmath>
  5. namespace stk {
  6. /***************************************************/
  7. /*! \class Cubic
  8. \brief STK cubic non-linearity class.
  9. This class implements the cubic non-linearity
  10. that was used in SynthBuilder.
  11. The formula implemented is:
  12. \code
  13. output = gain * (a1 * input + a2 * input^2 + a3 * input^3)
  14. \endcode
  15. followed by a limiter for values outside +-threshold.
  16. Ported to STK by Nick Porcaro, 2007. Updated for inclusion
  17. in STK distribution by Gary Scavone, 2011.
  18. */
  19. /***************************************************/
  20. class Cubic : public Function
  21. {
  22. public:
  23. //! Default constructor.
  24. Cubic( void ) : a1_(0.5), a2_(0.5), a3_(0.5), gain_(1.0), threshold_(1.0) {};
  25. //! Set the a1 coefficient value.
  26. void setA1( StkFloat a1 ) { a1_ = a1; };
  27. //! Set the a2 coefficient value.
  28. void setA2( StkFloat a2 ) { a2_ = a2; };
  29. //! Set the a3 coefficient value.
  30. void setA3( StkFloat a3 ) { a3_ = a3; };
  31. //! Set the gain value.
  32. void setGain( StkFloat gain ) { gain_ = gain; };
  33. //! Set the threshold value.
  34. void setThreshold( StkFloat threshold ) { threshold_ = threshold; };
  35. //! Input one sample to the function and return one output.
  36. StkFloat tick( StkFloat input );
  37. //! Take a channel of the StkFrames object as inputs to the function and replace with corresponding outputs.
  38. /*!
  39. The StkFrames argument reference is returned. The \c channel
  40. argument must be less than the number of channels in the
  41. StkFrames argument (the first channel is specified by 0).
  42. However, range checking is only performed if _STK_DEBUG_ is
  43. defined during compilation, in which case an out-of-range value
  44. will trigger an StkError exception.
  45. */
  46. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  47. //! Take a channel of the \c iFrames object as inputs to the function and write outputs to the \c oFrames object.
  48. /*!
  49. The \c iFrames object reference is returned. Each channel
  50. argument must be less than the number of channels in the
  51. corresponding StkFrames argument (the first channel is specified
  52. by 0). However, range checking is only performed if _STK_DEBUG_
  53. is defined during compilation, in which case an out-of-range value
  54. will trigger an StkError exception.
  55. */
  56. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  57. protected:
  58. StkFloat a1_;
  59. StkFloat a2_;
  60. StkFloat a3_;
  61. StkFloat gain_;
  62. StkFloat threshold_;
  63. };
  64. inline StkFloat Cubic :: tick( StkFloat input )
  65. {
  66. StkFloat inSquared = input * input;
  67. StkFloat inCubed = inSquared * input;
  68. lastFrame_[0] = gain_ * (a1_ * input + a2_ * inSquared + a3_ * inCubed);
  69. // Apply threshold if we are out of range.
  70. if ( fabs( lastFrame_[0] ) > threshold_ ) {
  71. lastFrame_[0] = ( lastFrame_[0] < 0 ? -threshold_ : threshold_ );
  72. }
  73. return lastFrame_[0];
  74. }
  75. inline StkFrames& Cubic :: tick( StkFrames& frames, unsigned int channel )
  76. {
  77. #if defined(_STK_DEBUG_)
  78. if ( channel >= frames.channels() ) {
  79. oStream_ << "Cubic::tick(): channel and StkFrames arguments are incompatible!";
  80. handleError( StkError::FUNCTION_ARGUMENT );
  81. }
  82. #endif
  83. StkFloat *samples = &frames[channel];
  84. unsigned int hop = frames.channels();
  85. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  86. *samples = tick( *samples );
  87. lastFrame_[0] = *(samples-hop);
  88. return frames;
  89. }
  90. inline StkFrames& Cubic :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  91. {
  92. #if defined(_STK_DEBUG_)
  93. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  94. oStream_ << "Cubic::tick(): channel and StkFrames arguments are incompatible!";
  95. handleError( StkError::FUNCTION_ARGUMENT );
  96. }
  97. #endif
  98. StkFloat *iSamples = &iFrames[iChannel];
  99. StkFloat *oSamples = &oFrames[oChannel];
  100. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  101. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop )
  102. *oSamples = tick( *iSamples );
  103. lastFrame_[0] = *(oSamples-oHop);
  104. return iFrames;
  105. }
  106. } // stk namespace
  107. #endif