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.

113 lines
3.6KB

  1. #ifndef STK_JETTABL_H
  2. #define STK_JETTABL_H
  3. #include "Function.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class JetTable
  7. \brief STK jet table class.
  8. This class implements a flue jet non-linear
  9. function, computed by a polynomial calculation.
  10. Contrary to the name, this is not a "table".
  11. Consult Fletcher and Rossing, Karjalainen,
  12. Cook, and others for more information.
  13. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  14. */
  15. /***************************************************/
  16. class JetTable : public Function
  17. {
  18. public:
  19. //! Take one sample input and map to one sample of output.
  20. StkFloat tick( StkFloat input );
  21. //! Take a channel of the StkFrames object as inputs to the table and replace with corresponding outputs.
  22. /*!
  23. The StkFrames argument reference is returned. The \c channel
  24. argument must be less than the number of channels in the
  25. StkFrames argument (the first channel is specified by 0).
  26. However, range checking is only performed if _STK_DEBUG_ is
  27. defined during compilation, in which case an out-of-range value
  28. will trigger an StkError exception.
  29. */
  30. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  31. //! Take a channel of the \c iFrames object as inputs to the table and write outputs to the \c oFrames object.
  32. /*!
  33. The \c iFrames object reference is returned. Each channel
  34. argument must be less than the number of channels in the
  35. corresponding StkFrames argument (the first channel is specified
  36. by 0). However, range checking is only performed if _STK_DEBUG_
  37. is defined during compilation, in which case an out-of-range value
  38. will trigger an StkError exception.
  39. */
  40. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  41. };
  42. inline StkFloat JetTable :: tick( StkFloat input )
  43. {
  44. // Perform "table lookup" using a polynomial
  45. // calculation (x^3 - x), which approximates
  46. // the jet sigmoid behavior.
  47. lastFrame_[0] = input * (input * input - 1.0);
  48. // Saturate at +/- 1.0.
  49. if ( lastFrame_[0] > 1.0 ) lastFrame_[0] = 1.0;
  50. if ( lastFrame_[0] < -1.0 ) lastFrame_[0] = -1.0;
  51. return lastFrame_[0];
  52. }
  53. inline StkFrames& JetTable :: tick( StkFrames& frames, unsigned int channel )
  54. {
  55. #if defined(_STK_DEBUG_)
  56. if ( channel >= frames.channels() ) {
  57. oStream_ << "JetTable::tick(): channel and StkFrames arguments are incompatible!";
  58. handleError( StkError::FUNCTION_ARGUMENT );
  59. }
  60. #endif
  61. StkFloat *samples = &frames[channel];
  62. unsigned int hop = frames.channels();
  63. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  64. *samples = *samples * (*samples * *samples - 1.0);
  65. if ( *samples > 1.0) *samples = 1.0;
  66. if ( *samples < -1.0) *samples = -1.0;
  67. }
  68. lastFrame_[0] = *(samples-hop);
  69. return frames;
  70. }
  71. inline StkFrames& JetTable :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  72. {
  73. #if defined(_STK_DEBUG_)
  74. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  75. oStream_ << "JetTable::tick(): channel and StkFrames arguments are incompatible!";
  76. handleError( StkError::FUNCTION_ARGUMENT );
  77. }
  78. #endif
  79. StkFloat *iSamples = &iFrames[iChannel];
  80. StkFloat *oSamples = &oFrames[oChannel];
  81. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  82. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  83. *oSamples = *oSamples * (*oSamples * *oSamples - 1.0);
  84. if ( *oSamples > 1.0) *oSamples = 1.0;
  85. if ( *oSamples < -1.0) *oSamples = -1.0;
  86. }
  87. lastFrame_[0] = *(oSamples-oHop);
  88. return iFrames;
  89. }
  90. } // stk namespace
  91. #endif