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.

144 lines
4.6KB

  1. #ifndef STK_REEDTABLE_H
  2. #define STK_REEDTABLE_H
  3. #include "Function.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class ReedTable
  7. \brief STK reed table class.
  8. This class implements a simple one breakpoint,
  9. non-linear reed function, as described by
  10. Smith (1986). This function is based on a
  11. memoryless non-linear spring model of the reed
  12. (the reed mass is ignored) which saturates when
  13. the reed collides with the mouthpiece facing.
  14. See McIntyre, Schumacher, & Woodhouse (1983),
  15. Smith (1986), Hirschman, Cook, Scavone, and
  16. others for more information.
  17. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  18. */
  19. /***************************************************/
  20. class ReedTable : public Function
  21. {
  22. public:
  23. //! Default constructor.
  24. ReedTable( void ) : offset_(0.6), slope_(-0.8) {};
  25. //! Set the table offset value.
  26. /*!
  27. The table offset roughly corresponds to the size
  28. of the initial reed tip opening (a greater offset
  29. represents a smaller opening).
  30. */
  31. void setOffset( StkFloat offset ) { offset_ = offset; };
  32. //! Set the table slope value.
  33. /*!
  34. The table slope roughly corresponds to the reed
  35. stiffness (a greater slope represents a harder
  36. reed).
  37. */
  38. void setSlope( StkFloat slope ) { slope_ = slope; };
  39. //! Take one sample input and map to one sample of output.
  40. StkFloat tick( StkFloat input );
  41. //! Take a channel of the StkFrames object as inputs to the table and replace with corresponding outputs.
  42. /*!
  43. The StkFrames argument reference is returned. The \c channel
  44. argument must be less than the number of channels in the
  45. StkFrames argument (the first channel is specified by 0).
  46. However, range checking is only performed if _STK_DEBUG_ is
  47. defined during compilation, in which case an out-of-range value
  48. will trigger an StkError exception.
  49. */
  50. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  51. //! Take a channel of the \c iFrames object as inputs to the table and write outputs to the \c oFrames object.
  52. /*!
  53. The \c iFrames object reference is returned. Each channel
  54. argument must be less than the number of channels in the
  55. corresponding StkFrames argument (the first channel is specified
  56. by 0). However, range checking is only performed if _STK_DEBUG_
  57. is defined during compilation, in which case an out-of-range value
  58. will trigger an StkError exception.
  59. */
  60. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  61. protected:
  62. StkFloat offset_;
  63. StkFloat slope_;
  64. };
  65. inline StkFloat ReedTable :: tick( StkFloat input )
  66. {
  67. // The input is differential pressure across the reed.
  68. lastFrame_[0] = offset_ + (slope_ * input);
  69. // If output is > 1, the reed has slammed shut and the
  70. // reflection function value saturates at 1.0.
  71. if ( lastFrame_[0] > 1.0) lastFrame_[0] = (StkFloat) 1.0;
  72. // This is nearly impossible in a physical system, but
  73. // a reflection function value of -1.0 corresponds to
  74. // an open end (and no discontinuity in bore profile).
  75. if ( lastFrame_[0] < -1.0) lastFrame_[0] = (StkFloat) -1.0;
  76. return lastFrame_[0];
  77. }
  78. inline StkFrames& ReedTable :: tick( StkFrames& frames, unsigned int channel )
  79. {
  80. #if defined(_STK_DEBUG_)
  81. if ( channel >= frames.channels() ) {
  82. oStream_ << "ReedTable::tick(): channel and StkFrames arguments are incompatible!";
  83. handleError( StkError::FUNCTION_ARGUMENT );
  84. }
  85. #endif
  86. StkFloat *samples = &frames[channel];
  87. unsigned int hop = frames.channels();
  88. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  89. *samples = offset_ + (slope_ * *samples);
  90. if ( *samples > 1.0) *samples = 1.0;
  91. if ( *samples < -1.0) *samples = -1.0;
  92. }
  93. lastFrame_[0] = *(samples-hop);
  94. return frames;
  95. }
  96. inline StkFrames& ReedTable :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  97. {
  98. #if defined(_STK_DEBUG_)
  99. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  100. oStream_ << "ReedTable::tick(): channel and StkFrames arguments are incompatible!";
  101. handleError( StkError::FUNCTION_ARGUMENT );
  102. }
  103. #endif
  104. StkFloat *iSamples = &iFrames[iChannel];
  105. StkFloat *oSamples = &oFrames[oChannel];
  106. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  107. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  108. *oSamples = offset_ + (slope_ * *iSamples);
  109. if ( *oSamples > 1.0) *oSamples = 1.0;
  110. if ( *oSamples < -1.0) *oSamples = -1.0;
  111. }
  112. lastFrame_[0] = *(oSamples-oHop);
  113. return iFrames;
  114. }
  115. } // stk namespace
  116. #endif