Audio plugin host https://kx.studio/carla
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.

159 lines
5.1KB

  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-2011.
  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. //! Class destructor.
  33. ~Twang( void );
  34. //! Reset and clear all internal state.
  35. void clear( void );
  36. //! Set the delayline parameters to allow frequencies as low as specified.
  37. void setLowestFrequency( StkFloat frequency );
  38. //! Set the delayline parameters for a particular frequency.
  39. void setFrequency( StkFloat frequency );
  40. //! Set the pluck or "excitation" position along the string (0.0 - 1.0).
  41. void setPluckPosition( StkFloat position );
  42. //! Set the nominal loop gain.
  43. /*!
  44. The actual loop gain is based on the value set with this
  45. function, but scaled slightly according to the frequency. Higher
  46. frequency settings have greater loop gains because of
  47. high-frequency loop-filter roll-off.
  48. */
  49. void setLoopGain( StkFloat loopGain );
  50. //! Set the loop filter coefficients.
  51. /*!
  52. The loop filter can be any arbitrary FIR filter. By default,
  53. the coefficients are set for a first-order lowpass filter with
  54. coefficients b = [0.5 0.5].
  55. */
  56. void setLoopFilter( std::vector<StkFloat> coefficients );
  57. //! Return an StkFrames reference to the last output sample frame.
  58. const StkFrames& lastFrame( void ) const { return lastFrame_; };
  59. //! Compute and return one output sample.
  60. StkFloat tick( StkFloat input );
  61. //! Take a channel of the \c iFrames object as inputs to the class and write outputs to the \c oFrames object.
  62. /*!
  63. The \c iFrames object reference is returned. Each channel
  64. argument must be less than the number of channels in the
  65. corresponding StkFrames argument (the first channel is specified
  66. by 0). However, range checking is only performed if _STK_DEBUG_
  67. is defined during compilation, in which case an out-of-range value
  68. will trigger an StkError exception.
  69. */
  70. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  71. //! Take a channel of the \c iFrames object as inputs to the effect and write outputs to the \c oFrames object.
  72. /*!
  73. The \c iFrames object reference is returned. Each channel
  74. argument must be less than the number of channels in the
  75. corresponding StkFrames argument (the first channel is specified
  76. by 0). However, range checking is only performed if _STK_DEBUG_
  77. is defined during compilation, in which case an out-of-range value
  78. will trigger an StkError exception.
  79. */
  80. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  81. protected:
  82. DelayA delayLine_;
  83. DelayL combDelay_;
  84. Fir loopFilter_;
  85. StkFrames lastFrame_;
  86. StkFloat frequency_;
  87. StkFloat loopGain_;
  88. StkFloat pluckPosition_;
  89. };
  90. inline StkFloat Twang :: tick( StkFloat input )
  91. {
  92. lastFrame_[0] = delayLine_.tick( input + loopFilter_.tick( delayLine_.lastOut() ) );
  93. lastFrame_[0] -= combDelay_.tick( lastFrame_[0] ); // comb filtering on output
  94. return lastFrame_[0] * 0.5;
  95. }
  96. inline StkFrames& Twang :: tick( StkFrames& frames, unsigned int channel )
  97. {
  98. #if defined(_STK_DEBUG_)
  99. if ( channel >= frames.channels() ) {
  100. oStream_ << "Twang::tick(): channel and StkFrames arguments are incompatible!";
  101. handleError( StkError::FUNCTION_ARGUMENT );
  102. }
  103. #endif
  104. StkFloat *samples = &frames[channel];
  105. unsigned int hop = frames.channels();
  106. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
  107. *samples = tick( *samples );
  108. return frames;
  109. }
  110. inline StkFrames& Twang :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  111. {
  112. #if defined(_STK_DEBUG_)
  113. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  114. oStream_ << "Twang::tick(): channel and StkFrames arguments are incompatible!";
  115. handleError( StkError::FUNCTION_ARGUMENT );
  116. }
  117. #endif
  118. StkFloat *iSamples = &iFrames[iChannel];
  119. StkFloat *oSamples = &oFrames[oChannel];
  120. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  121. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop )
  122. *oSamples = tick( *iSamples );
  123. return iFrames;
  124. }
  125. } // stk namespace
  126. #endif