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.

161 lines
5.2KB

  1. #ifndef STK_TWANG_H
  2. #define STK_TWANG_H
  3. #include "Stk.h"
  4. #include "DelayA.h"
  5. #include "DelayL.h"
  6. #include "Fir.h"
  7. namespace stk {
  8. /***************************************************/
  9. /*! \class Twang
  10. \brief STK enhanced plucked string class.
  11. This class implements an enhanced plucked-string
  12. physical model, a la Jaffe-Smith, Smith,
  13. Karjalainen and others. It includes a comb
  14. filter to simulate pluck position. The tick()
  15. function takes an input sample, which is added
  16. to the delayline input. This can be used to
  17. implement commuted synthesis (if the input
  18. samples are derived from the impulse response of
  19. a body filter) and/or feedback (as in an electric
  20. guitar model).
  21. This is a digital waveguide model, making its
  22. use possibly subject to patents held by Stanford
  23. University, Yamaha, and others.
  24. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  25. */
  26. /***************************************************/
  27. class Twang : public Stk
  28. {
  29. public:
  30. //! Class constructor, taking the lowest desired playing frequency.
  31. Twang( StkFloat lowestFrequency = 50.0 );
  32. //! Reset and clear all internal state.
  33. void clear( void );
  34. //! Set the delayline parameters to allow frequencies as low as specified.
  35. void setLowestFrequency( StkFloat frequency );
  36. //! Set the delayline parameters for a particular frequency.
  37. void setFrequency( StkFloat frequency );
  38. //! Set the pluck or "excitation" position along the string (0.0 - 1.0).
  39. void setPluckPosition( StkFloat position );
  40. //! Set the nominal loop gain.
  41. /*!
  42. The actual loop gain is based on the value set with this
  43. function, but scaled slightly according to the frequency. Higher
  44. frequency settings have greater loop gains because of
  45. high-frequency loop-filter roll-off.
  46. */
  47. void setLoopGain( StkFloat loopGain );
  48. //! Set the loop filter coefficients.
  49. /*!
  50. The loop filter can be any arbitrary FIR filter. By default,
  51. the coefficients are set for a first-order lowpass filter with
  52. coefficients b = [0.5 0.5].
  53. */
  54. void setLoopFilter( std::vector<StkFloat> coefficients );
  55. //! Return an StkFrames reference to the last output sample frame.
  56. //const StkFrames& lastFrame( void ) const { return lastFrame_; };
  57. //! Return the last computed output value.
  58. // StkFloat lastOut( void ) { return lastFrame_[0]; };
  59. StkFloat lastOut( void ) { return lastOutput_; };
  60. //! Compute and return one output sample.
  61. StkFloat tick( StkFloat input );
  62. //! Take a channel of the \c iFrames object as inputs to the class and write outputs to the \c oFrames object.
  63. /*!
  64. The \c iFrames object reference is returned. Each channel
  65. argument must be less than the number of channels in the
  66. corresponding StkFrames argument (the first channel is specified
  67. by 0). However, range checking is only performed if _STK_DEBUG_
  68. is defined during compilation, in which case an out-of-range value
  69. will trigger an StkError exception.
  70. */
  71. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  72. //! Take a channel of the \c iFrames object as inputs to the effect and write outputs to the \c oFrames object.
  73. /*!
  74. The \c iFrames object reference is returned. Each channel
  75. argument must be less than the number of channels in the
  76. corresponding StkFrames argument (the first channel is specified
  77. by 0). However, range checking is only performed if _STK_DEBUG_
  78. is defined during compilation, in which case an out-of-range value
  79. will trigger an StkError exception.
  80. */
  81. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  82. protected:
  83. DelayA delayLine_;
  84. DelayL combDelay_;
  85. Fir loopFilter_;
  86. StkFloat lastOutput_;
  87. StkFloat frequency_;
  88. StkFloat loopGain_;
  89. StkFloat pluckPosition_;
  90. };
  91. inline StkFloat Twang :: tick( StkFloat input )
  92. {
  93. lastOutput_ = delayLine_.tick( input + loopFilter_.tick( delayLine_.lastOut() ) );
  94. lastOutput_ -= combDelay_.tick( lastOutput_ ); // comb filtering on output
  95. lastOutput_ *= 0.5;
  96. return lastOutput_;
  97. }
  98. inline StkFrames& Twang :: tick( StkFrames& frames, unsigned int channel )
  99. {
  100. #if defined(_STK_DEBUG_)
  101. if ( channel >= frames.channels() ) {
  102. oStream_ << "Twang::tick(): channel and StkFrames arguments are incompatible!";
  103. handleError( StkError::FUNCTION_ARGUMENT );
  104. }
  105. #endif
  106. StkFloat *samples = &frames[channel];
  107. unsigned int hop = frames.channels();
  108. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  109. *samples = tick( *samples );
  110. return frames;
  111. }
  112. inline StkFrames& Twang :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  113. {
  114. #if defined(_STK_DEBUG_)
  115. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  116. oStream_ << "Twang::tick(): channel and StkFrames arguments are incompatible!";
  117. handleError( StkError::FUNCTION_ARGUMENT );
  118. }
  119. #endif
  120. StkFloat *iSamples = &iFrames[iChannel];
  121. StkFloat *oSamples = &oFrames[oChannel];
  122. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  123. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop )
  124. *oSamples = tick( *iSamples );
  125. return iFrames;
  126. }
  127. } // stk namespace
  128. #endif