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.

151 lines
4.9KB

  1. #ifndef STK_BOWTABL_H
  2. #define STK_BOWTABL_H
  3. #include "Function.h"
  4. #include <cmath>
  5. namespace stk {
  6. /***************************************************/
  7. /*! \class BowTable
  8. \brief STK bowed string table class.
  9. This class implements a simple bowed string
  10. non-linear function, as described by Smith
  11. (1986). The output is an instantaneous
  12. reflection coefficient value.
  13. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  14. */
  15. /***************************************************/
  16. class BowTable : public Function
  17. {
  18. public:
  19. //! Default constructor.
  20. BowTable( void ) : offset_(0.0), slope_(0.1), minOutput_(0.01), maxOutput_(0.98) {};
  21. //! Set the table offset value.
  22. /*!
  23. The table offset is a bias which controls the
  24. symmetry of the friction. If you want the
  25. friction to vary with direction, use a non-zero
  26. value for the offset. The default value is zero.
  27. */
  28. void setOffset( StkFloat offset ) { offset_ = offset; };
  29. //! Set the table slope value.
  30. /*!
  31. The table slope controls the width of the friction
  32. pulse, which is related to bow force.
  33. */
  34. void setSlope( StkFloat slope ) { slope_ = slope; };
  35. //! Set the minimum table output value (0.0 - 1.0).
  36. void setMinOutput( StkFloat minimum ) { minOutput_ = minimum; };
  37. //! Set the maximum table output value (0.0 - 1.0).
  38. void setMaxOutput( StkFloat maximum ) { maxOutput_ = maximum; };
  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. StkFloat minOutput_;
  65. StkFloat maxOutput_;
  66. };
  67. inline StkFloat BowTable :: tick( StkFloat input )
  68. {
  69. // The input represents differential string vs. bow velocity.
  70. StkFloat sample = input + offset_; // add bias to input
  71. sample *= slope_; // then scale it
  72. lastFrame_[0] = (StkFloat) fabs( (double) sample ) + (StkFloat) 0.75;
  73. lastFrame_[0] = (StkFloat) pow( lastFrame_[0], (StkFloat) -4.0 );
  74. // Set minimum threshold
  75. if ( lastFrame_[0] < minOutput_ ) lastFrame_[0] = minOutput_;
  76. // Set maximum threshold
  77. if ( lastFrame_[0] > maxOutput_ ) lastFrame_[0] = maxOutput_;
  78. return lastFrame_[0];
  79. }
  80. inline StkFrames& BowTable :: tick( StkFrames& frames, unsigned int channel )
  81. {
  82. #if defined(_STK_DEBUG_)
  83. if ( channel >= frames.channels() ) {
  84. oStream_ << "BowTable::tick(): channel and StkFrames arguments are incompatible!";
  85. handleError( StkError::FUNCTION_ARGUMENT );
  86. }
  87. #endif
  88. StkFloat *samples = &frames[channel];
  89. unsigned int hop = frames.channels();
  90. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  91. *samples = *samples + offset_;
  92. *samples *= slope_;
  93. *samples = (StkFloat) fabs( (double) *samples ) + 0.75;
  94. *samples = (StkFloat) pow( *samples, (StkFloat) -4.0 );
  95. if ( *samples > 1.0) *samples = 1.0;
  96. }
  97. lastFrame_[0] = *(samples-hop);
  98. return frames;
  99. }
  100. inline StkFrames& BowTable :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  101. {
  102. #if defined(_STK_DEBUG_)
  103. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  104. oStream_ << "BowTable::tick(): channel and StkFrames arguments are incompatible!";
  105. handleError( StkError::FUNCTION_ARGUMENT );
  106. }
  107. #endif
  108. StkFloat *iSamples = &iFrames[iChannel];
  109. StkFloat *oSamples = &oFrames[oChannel];
  110. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  111. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  112. *oSamples = *iSamples + offset_;
  113. *oSamples *= slope_;
  114. *oSamples = (StkFloat) fabs( (double) *oSamples ) + 0.75;
  115. *oSamples = (StkFloat) pow( *oSamples, (StkFloat) -4.0 );
  116. if ( *oSamples > 1.0) *oSamples = 1.0;
  117. }
  118. lastFrame_[0] = *(oSamples-oHop);
  119. return iFrames;
  120. }
  121. } // stk namespace
  122. #endif